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-2009 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003-2008 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 typedef struct cookie_state {
41 ldap_pvt_thread_mutex_t cs_mutex;
45 struct berval *cs_vals;
49 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
50 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
51 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
53 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
54 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
56 #define RETRYNUM_FOREVER (-1) /* retry forever */
57 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
58 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
59 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
61 typedef struct syncinfo_s {
62 struct syncinfo_s *si_next;
67 char si_ridtxt[ STRLENOF("rid=999") + 1 ];
68 slap_bindconf si_bindconf;
69 struct berval si_base;
70 struct berval si_logbase;
71 struct berval si_filterstr;
72 struct berval si_logfilterstr;
73 struct berval si_contextdn;
77 AttributeName *si_anlist;
78 AttributeName *si_exanlist;
83 int si_schemachecking;
84 int si_type; /* the active type */
85 int si_ctype; /* the configured type */
87 time_t *si_retryinterval;
88 int *si_retrynum_init;
90 struct sync_cookie si_syncCookie;
91 cookie_state *si_cookieState;
97 int si_refreshPresent;
103 Avlnode *si_presentlist;
106 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
107 ldap_pvt_thread_mutex_t si_mutex;
110 static int syncuuid_cmp( const void *, const void * );
111 static int avl_presentlist_insert( syncinfo_t* si, struct berval *syncUUID );
112 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct sync_cookie *, int );
113 static int syncrepl_message_to_op(
114 syncinfo_t *, Operation *, LDAPMessage * );
115 static int syncrepl_message_to_entry(
116 syncinfo_t *, Operation *, LDAPMessage *,
117 Modifications **, Entry **, int );
118 static int syncrepl_entry(
119 syncinfo_t *, Operation*, Entry*,
120 Modifications**,int, struct berval*,
121 struct berval *cookieCSN );
122 static int syncrepl_updateCookie(
123 syncinfo_t *, Operation *,
124 struct sync_cookie * );
125 static struct berval * slap_uuidstr_from_normalized(
126 struct berval *, struct berval *, void * );
128 /* callback functions */
129 static int dn_callback( Operation *, SlapReply * );
130 static int nonpresent_callback( Operation *, SlapReply * );
131 static int null_callback( Operation *, SlapReply * );
133 static AttributeDescription *sync_descs[4];
136 syncrepl_state2str( int state )
139 case LDAP_SYNC_PRESENT:
145 case LDAP_SYNC_MODIFY:
148 case LDAP_SYNC_DELETE:
156 init_syncrepl(syncinfo_t *si)
159 char **attrs, **exattrs;
161 if ( !sync_descs[0] ) {
162 sync_descs[0] = slap_schema.si_ad_objectClass;
163 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
164 sync_descs[2] = slap_schema.si_ad_entryCSN;
165 sync_descs[3] = NULL;
168 if ( si->si_allattrs && si->si_allopattrs )
171 attrs = anlist2attrs( si->si_anlist );
174 if ( si->si_allattrs ) {
177 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
178 for ( j = i; attrs[j] != NULL; j++ ) {
181 attrs[j] = attrs[j+1];
187 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
188 attrs[i] = ch_strdup("*");
191 } else if ( si->si_allopattrs ) {
194 if ( is_at_operational( at_find( attrs[i] ) ) ) {
195 for ( j = i; attrs[j] != NULL; j++ ) {
198 attrs[j] = attrs[j+1];
204 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
205 attrs[i] = ch_strdup("+");
209 for ( i = 0; sync_descs[i] != NULL; i++ ) {
212 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
213 for ( k = j; attrs[k] != NULL; k++ ) {
216 attrs[k] = attrs[k+1];
224 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
226 if ( si->si_allopattrs ) {
227 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
229 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
233 if ( si->si_allopattrs ) {
234 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
236 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
237 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
245 if ( si->si_allattrs == si->si_allopattrs ) {
246 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
247 attrs[i++] = ch_strdup( "*" );
248 attrs[i++] = ch_strdup( "+" );
249 } else if ( si->si_allattrs && !si->si_allopattrs ) {
250 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
251 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
252 attrs[i++] = ch_strdup( "*" );
253 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
254 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
256 } else if ( !si->si_allattrs && si->si_allopattrs ) {
257 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
258 attrs[i++] = ch_strdup( "+" );
259 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
264 si->si_attrs = attrs;
266 exattrs = anlist2attrs( si->si_exanlist );
269 for ( n = 0; exattrs[n] != NULL; n++ ) ;
271 for ( i = 0; sync_descs[i] != NULL; i++ ) {
273 while ( exattrs[j] != NULL ) {
274 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
275 ch_free( exattrs[j] );
276 for ( k = j; exattrs[k] != NULL; k++ ) {
277 exattrs[k] = exattrs[k+1];
285 for ( i = 0; exattrs[i] != NULL; i++ ) {
286 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
288 if ( ( oc = si->si_anlist[j].an_oc ) ) {
290 while ( oc->soc_required[k] ) {
291 if ( !strcmp( exattrs[i],
292 oc->soc_required[k]->sat_cname.bv_val ) ) {
293 ch_free( exattrs[i] );
294 for ( l = i; exattrs[l]; l++ ) {
295 exattrs[l] = exattrs[l+1];
305 for ( i = 0; exattrs[i] != NULL; i++ ) ;
308 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
311 si->si_exattrs = exattrs;
314 typedef struct logschema {
316 struct berval ls_req;
317 struct berval ls_mod;
318 struct berval ls_newRdn;
319 struct berval ls_delRdn;
320 struct berval ls_newSup;
323 static logschema changelog_sc = {
325 BER_BVC("changeType"),
328 BER_BVC("deleteOldRDN"),
329 BER_BVC("newSuperior")
332 static logschema accesslog_sc = {
336 BER_BVC("reqNewRDN"),
337 BER_BVC("reqDeleteOldRDN"),
338 BER_BVC("reqNewSuperior")
346 BerElementBuffer berbuf;
347 BerElement *ber = (BerElement *)&berbuf;
348 LDAPControl c[3], *ctrls[4];
352 char **attrs, *lattrs[8];
357 /* setup LDAP SYNC control */
358 ber_init2( ber, NULL, LBER_USE_DER );
359 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
361 /* If we're using a log but we have no state, then fallback to
362 * normal mode for a full refresh.
364 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
365 si->si_logstate = SYNCLOG_FALLBACK;
368 /* Use the log parameters if we're in log mode */
369 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
371 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
375 lattrs[0] = ls->ls_dn.bv_val;
376 lattrs[1] = ls->ls_req.bv_val;
377 lattrs[2] = ls->ls_mod.bv_val;
378 lattrs[3] = ls->ls_newRdn.bv_val;
379 lattrs[4] = ls->ls_delRdn.bv_val;
380 lattrs[5] = ls->ls_newSup.bv_val;
381 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
385 base = si->si_logbase.bv_val;
386 filter = si->si_logfilterstr.bv_val;
389 scope = LDAP_SCOPE_SUBTREE;
392 base = si->si_base.bv_val;
393 filter = si->si_filterstr.bv_val;
394 attrs = si->si_attrs;
395 attrsonly = si->si_attrsonly;
396 scope = si->si_scope;
398 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
399 si->si_type = LDAP_SYNC_REFRESH_ONLY;
401 si->si_type = si->si_ctype;
404 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
406 ber_printf( ber, "{eOb}",
407 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
409 ber_printf( ber, "{eb}",
410 abs(si->si_type), rhint );
413 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == -1 ) {
418 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
419 c[0].ldctl_iscritical = si->si_type < 0;
422 c[1].ldctl_oid = LDAP_CONTROL_MANAGEDSAIT;
423 BER_BVZERO( &c[1].ldctl_value );
424 c[1].ldctl_iscritical = 1;
427 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
428 c[2].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
429 c[2].ldctl_value = si->si_bindconf.sb_authzId;
430 c[2].ldctl_iscritical = 1;
437 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
438 ctrls, NULL, NULL, si->si_slimit, &si->si_msgid );
452 int i, j, changed = 0;
454 /* Look for contextCSN from syncprov overlay. If
455 * there's no overlay, this will be a no-op. That means
456 * this is a pure consumer, so local changes will not be
457 * allowed, and all changes will already be reflected in
460 a.a_desc = slap_schema.si_ad_contextCSN;
462 e.e_name = si->si_contextdn;
463 e.e_nname = si->si_contextdn;
464 at[0].an_name = a.a_desc->ad_cname;
465 at[0].an_desc = a.a_desc;
466 BER_BVZERO( &at[1].an_name );
468 rs.sr_flags = REP_ENTRY_MODIFIABLE;
470 op->o_req_dn = e.e_name;
471 op->o_req_ndn = e.e_nname;
473 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
474 i = backend_operational( op, &rs );
475 if ( i == LDAP_SUCCESS && a.a_nvals ) {
476 int num = a.a_numvals;
477 /* check for differences */
478 if ( num != si->si_cookieState->cs_num ) {
481 for ( i=0; i<num; i++ ) {
482 if ( ber_bvcmp( &a.a_nvals[i],
483 &si->si_cookieState->cs_vals[i] )) {
490 ber_bvarray_free( si->si_cookieState->cs_vals );
491 ch_free( si->si_cookieState->cs_sids );
492 si->si_cookieState->cs_num = num;
493 si->si_cookieState->cs_vals = a.a_nvals;
494 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_nvals,
496 si->si_cookieState->cs_age++;
498 ber_bvarray_free( a.a_nvals );
500 ber_bvarray_free( a.a_vals );
502 /* See if the cookieState has changed due to anything outside
503 * this particular consumer. That includes other consumers in
504 * the same context, or local changes detected above.
506 if ( si->si_cookieState->cs_num > 0 && si->si_cookieAge !=
507 si->si_cookieState->cs_age ) {
508 if ( !si->si_syncCookie.numcsns ) {
509 ber_bvarray_free( si->si_syncCookie.ctxcsn );
510 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
511 si->si_cookieState->cs_vals, NULL );
514 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
515 /* bogus, just dup everything */
516 if ( si->si_syncCookie.sids[i] == -1 ) {
517 ber_bvarray_free( si->si_syncCookie.ctxcsn );
518 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
519 si->si_cookieState->cs_vals, NULL );
523 for (j=0; j<si->si_cookieState->cs_num; j++) {
524 if ( si->si_syncCookie.sids[i] !=
525 si->si_cookieState->cs_sids[j] )
527 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
528 &si->si_cookieState->cs_vals[j] ))
530 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
531 &si->si_cookieState->cs_vals[j] );
539 si->si_cookieAge = si->si_cookieState->cs_age;
540 ch_free( si->si_syncCookie.octet_str.bv_val );
541 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
542 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
543 si->si_syncCookie.sid );
545 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
555 int cmdline_cookie_found = 0;
557 struct sync_cookie *sc = NULL;
562 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
563 if ( rc != LDAP_SUCCESS ) {
566 op->o_protocol = LDAP_VERSION3;
568 /* Set SSF to strongest of TLS, SASL SSFs */
571 op->o_transport_ssf = 0;
573 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
574 == LDAP_SUCCESS && ssl != NULL )
576 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
578 #endif /* HAVE_TLS */
580 ber_len_t ssf; /* ITS#5403, 3864 LDAP_OPT_X_SASL_SSF probably ought
581 to use sasl_ssf_t but currently uses ber_len_t */
582 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &ssf )
584 op->o_sasl_ssf = ssf;
586 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
587 ? op->o_sasl_ssf : op->o_tls_ssf;
589 ldap_set_option( si->si_ld, LDAP_OPT_TIMELIMIT, &si->si_tlimit );
591 rc = LDAP_DEREF_NEVER; /* actually could allow DEREF_FINDING */
592 ldap_set_option( si->si_ld, LDAP_OPT_DEREF, &rc );
594 ldap_set_option( si->si_ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF );
596 si->si_syncCookie.rid = si->si_rid;
598 /* whenever there are multiple data sources possible, advertise sid */
599 si->si_syncCookie.sid = ( SLAP_MULTIMASTER( si->si_be ) || si->si_be != si->si_wbe ) ?
602 /* We've just started up, or the remote server hasn't sent us
603 * any meaningful state.
605 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
608 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
609 if ( si->si_rid == sc->rid ) {
610 cmdline_cookie_found = 1;
615 if ( cmdline_cookie_found ) {
616 /* cookie is supplied in the command line */
618 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
620 /* ctxcsn wasn't parsed yet, do it now */
621 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
622 slap_sync_cookie_free( &si->si_syncCookie, 0 );
623 slap_dup_sync_cookie( &si->si_syncCookie, sc );
624 slap_sync_cookie_free( sc, 1 );
626 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
627 if ( !si->si_cookieState->cs_num ) {
628 /* get contextCSN shadow replica from database */
629 BerVarray csn = NULL;
630 void *ctx = op->o_tmpmemctx;
632 op->o_req_ndn = si->si_contextdn;
633 op->o_req_dn = op->o_req_ndn;
635 /* try to read stored contextCSN */
636 op->o_tmpmemctx = NULL;
637 backend_attribute( op, NULL, &op->o_req_ndn,
638 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
639 op->o_tmpmemctx = ctx;
641 si->si_cookieState->cs_vals = csn;
642 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
643 si->si_cookieState->cs_num = i;
644 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
647 if ( si->si_cookieState->cs_num ) {
648 ber_bvarray_free( si->si_syncCookie.ctxcsn );
649 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
650 si->si_cookieState->cs_vals, NULL )) {
652 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
655 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
656 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
658 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
659 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
661 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
664 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
665 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
666 si->si_syncCookie.sid );
668 /* ITS#6367: recreate the cookie so it has our SID, not our peer's */
669 ch_free( si->si_syncCookie.octet_str.bv_val );
670 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
671 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
672 si->si_syncCookie.sid );
673 /* Look for contextCSN from syncprov overlay. */
674 check_syncprov( op, si );
677 si->si_refreshDone = 0;
679 rc = ldap_sync_search( si, op->o_tmpmemctx );
681 if( rc != LDAP_SUCCESS ) {
682 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
683 "ldap_search_ext: %s (%d)\n",
684 si->si_ridtxt, ldap_err2string( rc ), rc );
690 ldap_unbind_ext( si->si_ld, NULL, NULL );
699 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
706 if ( sc1->numcsns < sc2->numcsns ) {
707 *which = sc1->numcsns;
711 for (j=0; j<sc2->numcsns; j++) {
712 for (i=0; i<sc1->numcsns; i++) {
713 if ( sc1->sids[i] != sc2->sids[j] )
715 value_match( &match, slap_schema.si_ad_entryCSN,
716 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
717 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
718 &sc1->ctxcsn[i], &sc2->ctxcsn[j], &text );
725 if ( i == sc1->numcsns ) {
726 /* sc2 has a sid sc1 lacks */
734 #define SYNC_PAUSED -3
741 LDAPControl **rctrls = NULL;
743 BerElementBuffer berbuf;
744 BerElement *ber = (BerElement *)&berbuf;
746 LDAPMessage *msg = NULL;
749 struct berval *retdata = NULL;
754 struct berval syncUUID = BER_BVNULL;
755 struct sync_cookie syncCookie = { NULL };
756 struct sync_cookie syncCookie_req = { NULL };
757 struct berval cookie = BER_BVNULL;
763 Modifications *modlist = NULL;
767 struct timeval *tout_p = NULL;
768 struct timeval tout = { 0, 0 };
770 int refreshDeletes = 0;
771 BerVarray syncUUIDs = NULL;
774 if ( slapd_shutdown ) {
779 ber_init2( ber, NULL, LBER_USE_DER );
780 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
782 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
784 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
786 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
792 while ( ( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
793 tout_p, &msg ) ) > 0 )
795 LDAPControl *rctrlp = NULL;
797 if ( slapd_shutdown ) {
801 switch( ldap_msgtype( msg ) ) {
802 case LDAP_RES_SEARCH_ENTRY:
803 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
804 /* we can't work without the control */
807 /* NOTE: make sure we use the right one;
808 * a better approach would be to run thru
809 * the whole list and take care of all */
810 /* NOTE: since we issue the search request,
811 * we should know what controls to expect,
812 * and there should be none apart from the
813 * sync-related control */
814 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_STATE, rctrls, &next );
815 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_STATE, next, NULL ) )
817 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
818 "got search entry with multiple "
819 "Sync State control\n", si->si_ridtxt, 0, 0 );
820 ldap_controls_free( rctrls );
825 if ( rctrlp == NULL ) {
826 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
827 "got search entry without "
828 "Sync State control\n", si->si_ridtxt, 0, 0 );
832 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
833 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
834 /* FIXME: what if syncUUID is NULL or empty?
835 * (happens with back-sql...) */
836 if ( BER_BVISEMPTY( &syncUUID ) ) {
837 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
838 "got empty syncUUID with LDAP_SYNC_%s\n",
840 syncrepl_state2str( syncstate ), 0 );
841 ldap_controls_free( rctrls );
845 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
846 ber_scanf( ber, /*"{"*/ "m}", &cookie );
848 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
849 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
851 if ( !BER_BVISNULL( &cookie ) ) {
852 ch_free( syncCookie.octet_str.bv_val );
853 ber_dupbv( &syncCookie.octet_str, &cookie );
855 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
857 slap_parse_sync_cookie( &syncCookie, NULL );
858 if ( syncCookie.ctxcsn ) {
859 int i, sid = slap_parse_csn_sid( syncCookie.ctxcsn );
860 for ( i =0; i<si->si_cookieState->cs_num; i++ ) {
861 if ( si->si_cookieState->cs_sids[i] == sid &&
862 ber_bvcmp( syncCookie.ctxcsn, &si->si_cookieState->cs_vals[i] ) <= 0 ) {
863 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: %s CSN too old, ignoring %s\n",
864 si->si_ridtxt, syncCookie.ctxcsn->bv_val, 0 );
865 ldap_controls_free( rctrls );
871 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
875 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
877 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
880 rc = syncrepl_updateCookie( si, op, &syncCookie );
881 } else switch ( rc ) {
882 case LDAP_ALREADY_EXISTS:
883 case LDAP_NO_SUCH_OBJECT:
884 case LDAP_NO_SUCH_ATTRIBUTE:
885 case LDAP_TYPE_OR_VALUE_EXISTS:
886 rc = LDAP_SYNC_REFRESH_REQUIRED;
887 si->si_logstate = SYNCLOG_FALLBACK;
888 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
893 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
894 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
896 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
897 syncstate, &syncUUID, syncCookie.ctxcsn ) ) == LDAP_SUCCESS &&
900 rc = syncrepl_updateCookie( si, op, &syncCookie );
903 ldap_controls_free( rctrls );
905 slap_mods_free( modlist, 1 );
911 case LDAP_RES_SEARCH_REFERENCE:
912 Debug( LDAP_DEBUG_ANY,
913 "do_syncrep2: %s reference received error\n",
914 si->si_ridtxt, 0, 0 );
917 case LDAP_RES_SEARCH_RESULT:
918 Debug( LDAP_DEBUG_SYNC,
919 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
920 si->si_ridtxt, 0, 0 );
921 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
923 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
924 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
925 /* map old result code to registered code */
926 err = LDAP_SYNC_REFRESH_REQUIRED;
929 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
930 if ( si->si_logstate == SYNCLOG_LOGGING ) {
931 si->si_logstate = SYNCLOG_FALLBACK;
937 Debug( LDAP_DEBUG_ANY,
938 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT (%d) %s\n",
939 si->si_ridtxt, err, ldap_err2string( err ) );
943 /* NOTE: make sure we use the right one;
944 * a better approach would be to run thru
945 * the whole list and take care of all */
946 /* NOTE: since we issue the search request,
947 * we should know what controls to expect,
948 * and there should be none apart from the
949 * sync-related control */
950 rctrlp = ldap_control_find( LDAP_CONTROL_SYNC_DONE, rctrls, &next );
951 if ( next && ldap_control_find( LDAP_CONTROL_SYNC_DONE, next, NULL ) )
953 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
954 "got search result with multiple "
955 "Sync State control\n", si->si_ridtxt, 0, 0 );
956 ldap_controls_free( rctrls );
962 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
964 ber_scanf( ber, "{" /*"}"*/);
965 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
966 ber_scanf( ber, "m", &cookie );
968 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
969 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
971 if ( !BER_BVISNULL( &cookie ) ) {
972 ch_free( syncCookie.octet_str.bv_val );
973 ber_dupbv( &syncCookie.octet_str, &cookie);
975 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
977 slap_parse_sync_cookie( &syncCookie, NULL );
978 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
981 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
983 ber_scanf( ber, "b", &refreshDeletes );
985 ber_scanf( ber, /*"{"*/ "}" );
987 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
988 slap_sync_cookie_free( &syncCookie_req, 0 );
989 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
991 if ( !syncCookie.ctxcsn ) {
993 } else if ( !syncCookie_req.ctxcsn ) {
997 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1000 ldap_controls_free( rctrls );
1002 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
1003 /* FIXME : different error behaviors according to
1004 * 1) err code : LDAP_BUSY ...
1005 * 2) on err policy : stop service, stop sync, retry
1007 if ( refreshDeletes == 0 && match < 0 &&
1008 err == LDAP_SUCCESS &&
1009 syncCookie_req.numcsns == syncCookie.numcsns )
1011 syncrepl_del_nonpresent( op, si, NULL,
1014 avl_free( si->si_presentlist, ch_free );
1015 si->si_presentlist = NULL;
1018 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
1020 rc = syncrepl_updateCookie( si, op, &syncCookie );
1022 if ( err == LDAP_SUCCESS
1023 && si->si_logstate == SYNCLOG_FALLBACK ) {
1024 si->si_logstate = SYNCLOG_LOGGING;
1025 rc = LDAP_SYNC_REFRESH_REQUIRED;
1032 case LDAP_RES_INTERMEDIATE:
1033 rc = ldap_parse_intermediate( si->si_ld, msg,
1034 &retoid, &retdata, NULL, 0 );
1035 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
1036 ber_init2( ber, retdata, LBER_USE_DER );
1038 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
1040 case LDAP_TAG_SYNC_NEW_COOKIE:
1041 Debug( LDAP_DEBUG_SYNC,
1042 "do_syncrep2: %s %s - %s\n",
1044 "LDAP_RES_INTERMEDIATE",
1046 ber_scanf( ber, "tm", &tag, &cookie );
1047 Debug( LDAP_DEBUG_SYNC,
1048 "do_syncrep2: %s NEW_COOKIE: %s\n",
1051 if ( !BER_BVISNULL( &cookie ) ) {
1052 ch_free( syncCookie.octet_str.bv_val );
1053 ber_dupbv( &syncCookie.octet_str, &cookie );
1055 if (!BER_BVISNULL( &syncCookie.octet_str ) ) {
1056 slap_parse_sync_cookie( &syncCookie, NULL );
1057 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1060 case LDAP_TAG_SYNC_REFRESH_DELETE:
1061 case LDAP_TAG_SYNC_REFRESH_PRESENT:
1062 Debug( LDAP_DEBUG_SYNC,
1063 "do_syncrep2: %s %s - %s\n",
1065 "LDAP_RES_INTERMEDIATE",
1066 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
1067 "REFRESH_PRESENT" : "REFRESH_DELETE" );
1068 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
1069 si->si_refreshDelete = 1;
1071 si->si_refreshPresent = 1;
1073 ber_scanf( ber, "t{" /*"}"*/, &tag );
1074 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
1076 ber_scanf( ber, "m", &cookie );
1078 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1079 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1081 if ( !BER_BVISNULL( &cookie ) ) {
1082 ch_free( syncCookie.octet_str.bv_val );
1083 ber_dupbv( &syncCookie.octet_str, &cookie );
1085 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1087 slap_parse_sync_cookie( &syncCookie, NULL );
1088 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1091 /* Defaults to TRUE */
1092 if ( ber_peek_tag( ber, &len ) ==
1093 LDAP_TAG_REFRESHDONE )
1095 ber_scanf( ber, "b", &si->si_refreshDone );
1098 si->si_refreshDone = 1;
1100 ber_scanf( ber, /*"{"*/ "}" );
1102 case LDAP_TAG_SYNC_ID_SET:
1103 Debug( LDAP_DEBUG_SYNC,
1104 "do_syncrep2: %s %s - %s\n",
1106 "LDAP_RES_INTERMEDIATE",
1108 ber_scanf( ber, "t{" /*"}"*/, &tag );
1109 if ( ber_peek_tag( ber, &len ) ==
1110 LDAP_TAG_SYNC_COOKIE )
1112 ber_scanf( ber, "m", &cookie );
1114 Debug( LDAP_DEBUG_SYNC, "do_syncrep2: cookie=%s\n",
1115 BER_BVISNULL( &cookie ) ? "" : cookie.bv_val, 0, 0 );
1117 if ( !BER_BVISNULL( &cookie ) ) {
1118 ch_free( syncCookie.octet_str.bv_val );
1119 ber_dupbv( &syncCookie.octet_str, &cookie );
1121 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
1123 slap_parse_sync_cookie( &syncCookie, NULL );
1124 op->o_controls[slap_cids.sc_LDAPsync] = &syncCookie;
1125 compare_csns( &syncCookie_req, &syncCookie, &m );
1128 if ( ber_peek_tag( ber, &len ) ==
1129 LDAP_TAG_REFRESHDELETES )
1131 ber_scanf( ber, "b", &refreshDeletes );
1133 ber_scanf( ber, "[W]", &syncUUIDs );
1134 ber_scanf( ber, /*"{"*/ "}" );
1135 if ( refreshDeletes ) {
1136 syncrepl_del_nonpresent( op, si, syncUUIDs,
1138 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
1141 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
1142 (void)avl_presentlist_insert( si, &syncUUIDs[i] );
1143 slap_sl_free( syncUUIDs[i].bv_val, op->o_tmpmemctx );
1145 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
1147 slap_sync_cookie_free( &syncCookie, 0 );
1150 Debug( LDAP_DEBUG_ANY,
1151 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
1152 si->si_ridtxt, (long) si_tag, 0 );
1153 ldap_memfree( retoid );
1154 ber_bvfree( retdata );
1158 if ( SLAP_MULTIMASTER( op->o_bd ) && check_syncprov( op, si )) {
1159 slap_sync_cookie_free( &syncCookie_req, 0 );
1160 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
1162 if ( !syncCookie.ctxcsn ) {
1164 } else if ( !syncCookie_req.ctxcsn ) {
1168 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1172 if ( si->si_refreshPresent == 1 &&
1173 si_tag != LDAP_TAG_SYNC_NEW_COOKIE &&
1174 syncCookie_req.numcsns == syncCookie.numcsns ) {
1175 syncrepl_del_nonpresent( op, si, NULL,
1179 if ( syncCookie.ctxcsn )
1181 rc = syncrepl_updateCookie( si, op, &syncCookie);
1185 ldap_memfree( retoid );
1186 ber_bvfree( retdata );
1190 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1191 "unknown intermediate response (%d)\n",
1192 si->si_ridtxt, rc, 0 );
1193 ldap_memfree( retoid );
1194 ber_bvfree( retdata );
1200 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1201 "unknown message (0x%02lx)\n",
1203 (unsigned long)ldap_msgtype( msg ), 0 );
1207 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1208 slap_sync_cookie_free( &syncCookie_req, 0 );
1209 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1210 slap_sync_cookie_free( &syncCookie, 0 );
1212 ldap_msgfree( msg );
1214 if ( ldap_pvt_thread_pool_pausing( &connection_pool )) {
1215 slap_sync_cookie_free( &syncCookie, 0 );
1216 slap_sync_cookie_free( &syncCookie_req, 0 );
1222 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1227 if ( err != LDAP_SUCCESS ) {
1228 Debug( LDAP_DEBUG_ANY,
1229 "do_syncrep2: %s (%d) %s\n",
1230 si->si_ridtxt, err, ldap_err2string( err ) );
1233 slap_sync_cookie_free( &syncCookie, 0 );
1234 slap_sync_cookie_free( &syncCookie_req, 0 );
1236 if ( msg ) ldap_msgfree( msg );
1238 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1239 if ( si->si_conn ) {
1240 connection_client_stop( si->si_conn );
1243 ldap_unbind_ext( si->si_ld, NULL, NULL );
1255 struct re_s* rtask = arg;
1256 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1257 Connection conn = {0};
1258 OperationBuffer opbuf;
1260 int rc = LDAP_SUCCESS;
1263 int i, defer = 1, fail = 0, freeinfo = 0;
1268 if ( slapd_shutdown )
1271 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1273 /* Don't get stuck here while a pause is initiated */
1274 while ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
1275 if ( slapd_shutdown )
1277 if ( !ldap_pvt_thread_pool_pausecheck( &connection_pool ))
1278 ldap_pvt_thread_yield();
1281 if ( si->si_ctype < 1 ) {
1285 switch( abs( si->si_type ) ) {
1286 case LDAP_SYNC_REFRESH_ONLY:
1287 case LDAP_SYNC_REFRESH_AND_PERSIST:
1290 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1294 if ( slapd_shutdown ) {
1296 if ( si->si_conn ) {
1297 connection_client_stop( si->si_conn );
1300 ldap_unbind_ext( si->si_ld, NULL, NULL );
1303 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1307 connection_fake_init( &conn, &opbuf, ctx );
1310 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1313 /* Coordinate contextCSN updates with any syncprov overlays
1314 * in use. This may be complicated by the use of the glue
1317 * Typically there is a single syncprov mastering the entire
1318 * glued tree. In that case, our contextCSN updates should
1319 * go to the master DB. But if there is no syncprov on the
1320 * master DB, then nothing special is needed here.
1322 * Alternatively, there may be individual syncprov overlays
1323 * on each glued branch. In that case, each syncprov only
1324 * knows about changes within its own branch. And so our
1325 * contextCSN updates should only go to the local DB.
1327 if ( !si->si_wbe ) {
1328 if ( SLAP_GLUE_SUBORDINATE( be ) && !overlay_is_inst( be, "syncprov" )) {
1329 BackendDB * top_be = select_backend( &be->be_nsuffix[0], 1 );
1330 if ( overlay_is_inst( top_be, "syncprov" ))
1331 si->si_wbe = top_be;
1337 if ( SLAP_SYNC_SUBENTRY( si->si_wbe )) {
1338 build_new_dn( &si->si_contextdn, &si->si_wbe->be_nsuffix[0],
1339 (struct berval *)&slap_ldapsync_cn_bv, NULL );
1341 si->si_contextdn = si->si_wbe->be_nsuffix[0];
1344 if ( !si->si_schemachecking )
1345 op->o_no_schema_check = 1;
1347 /* Establish session, do search */
1349 si->si_refreshDelete = 0;
1350 si->si_refreshPresent = 0;
1352 /* use main DB when retrieving contextCSN */
1353 op->o_bd = si->si_wbe;
1354 op->o_dn = op->o_bd->be_rootdn;
1355 op->o_ndn = op->o_bd->be_rootndn;
1356 rc = do_syncrep1( op, si );
1360 /* Process results */
1361 if ( rc == LDAP_SUCCESS ) {
1362 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1364 /* use current DB */
1366 op->o_dn = op->o_bd->be_rootdn;
1367 op->o_ndn = op->o_bd->be_rootndn;
1368 rc = do_syncrep2( op, si );
1369 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1370 rc = ldap_sync_search( si, op->o_tmpmemctx );
1375 /* We got deleted while running on cn=config */
1376 if ( si->si_ctype < 1 ) {
1377 if ( si->si_ctype == -1 ) {
1386 if ( rc != SYNC_PAUSED ) {
1387 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1388 /* If we succeeded, enable the connection for further listening.
1389 * If we failed, tear down the connection and reschedule.
1391 if ( rc == LDAP_SUCCESS ) {
1392 if ( si->si_conn ) {
1393 connection_client_enable( si->si_conn );
1395 si->si_conn = connection_client_setup( s, do_syncrepl, arg );
1397 } else if ( si->si_conn ) {
1401 if ( rc == -2 ) rc = 0;
1406 /* At this point, we have 5 cases:
1407 * 1) for any hard failure, give up and remove this task
1408 * 2) for ServerDown, reschedule this task to run later
1409 * 3) for threadpool pause, reschedule to run immediately
1410 * 4) for Refresh and Success, reschedule to run
1411 * 5) for Persist and Success, reschedule to defer
1413 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1415 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1416 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1420 connection_client_stop( si->si_conn );
1424 if ( rc == SYNC_PAUSED ) {
1425 rtask->interval.tv_sec = 0;
1426 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1427 rtask->interval.tv_sec = si->si_interval;
1429 } else if ( rc == LDAP_SUCCESS ) {
1430 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1433 rtask->interval.tv_sec = si->si_interval;
1434 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1435 if ( si->si_retrynum ) {
1436 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1437 si->si_retrynum[i] = si->si_retrynum_init[i];
1439 si->si_retrynum[i] = RETRYNUM_TAIL;
1442 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1443 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1447 if ( si->si_ctype < 1
1448 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1450 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1453 fail = RETRYNUM_TAIL;
1454 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1455 if ( si->si_retrynum[i] > 0 )
1456 si->si_retrynum[i]--;
1457 fail = si->si_retrynum[i];
1458 rtask->interval.tv_sec = si->si_retryinterval[i];
1459 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1460 slap_wake_listener();
1464 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1465 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1468 if ( fail == RETRYNUM_TAIL ) {
1469 Debug( LDAP_DEBUG_ANY,
1470 "do_syncrepl: %s rc %d quitting\n",
1471 si->si_ridtxt, rc, 0 );
1472 } else if ( fail > 0 ) {
1473 Debug( LDAP_DEBUG_ANY,
1474 "do_syncrepl: %s rc %d retrying (%d retries left)\n",
1475 si->si_ridtxt, rc, fail );
1477 Debug( LDAP_DEBUG_ANY,
1478 "do_syncrepl: %s rc %d retrying\n",
1479 si->si_ridtxt, rc, 0 );
1483 /* Do final delete cleanup */
1485 syncinfo_free( si, 0 );
1490 static slap_verbmasks modops[] = {
1491 { BER_BVC("add"), LDAP_REQ_ADD },
1492 { BER_BVC("delete"), LDAP_REQ_DELETE },
1493 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1494 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1498 static Modifications *
1499 syncrepl_accesslog_mods(
1506 AttributeDescription *ad;
1507 struct berval bv, bv2;
1509 Modifications *mod = NULL, *modlist = NULL, **modtail;
1514 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1518 colon = ber_bvchr( &bv, ':' );
1524 bv.bv_len = colon - bv.bv_val;
1525 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1530 /* Ignore dynamically generated attrs */
1531 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1535 /* Ignore excluded attrs */
1536 if ( ldap_charray_inlist( si->si_exattrs,
1537 ad->ad_type->sat_cname.bv_val ) )
1543 case '+': op = LDAP_MOD_ADD; break;
1544 case '-': op = LDAP_MOD_DELETE; break;
1545 case '=': op = LDAP_MOD_REPLACE; break;
1546 case '#': op = LDAP_MOD_INCREMENT; break;
1550 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1551 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1554 mod->sml_next = NULL;
1556 mod->sml_type = ad->ad_cname;
1557 mod->sml_values = NULL;
1558 mod->sml_nvalues = NULL;
1559 mod->sml_numvals = 0;
1562 modtail = &mod->sml_next;
1564 if ( colon[2] == ' ' ) {
1565 bv.bv_val = colon + 3;
1566 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1567 ber_dupbv( &bv2, &bv );
1568 ber_bvarray_add( &mod->sml_values, &bv2 );
1575 static Modifications *
1576 syncrepl_changelog_mods(
1581 return NULL; /* FIXME */
1585 syncrepl_message_to_op(
1591 BerElement *ber = NULL;
1592 Modifications *modlist = NULL;
1594 SlapReply rs = { REP_RESULT };
1595 slap_callback cb = { NULL, null_callback, NULL, NULL };
1598 char txtbuf[SLAP_TEXT_BUFLEN];
1599 size_t textlen = sizeof txtbuf;
1601 struct berval bdn, dn = BER_BVNULL, ndn;
1602 struct berval bv, *bvals = NULL;
1603 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1604 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1605 psup = BER_BVNULL, nsup = BER_BVNULL;
1606 int rc, deleteOldRdn = 0, freeReqDn = 0;
1607 int do_graduate = 0;
1609 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1610 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1611 "Message type should be entry (%d)",
1612 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1616 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1621 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1623 if ( rc != LDAP_SUCCESS ) {
1624 Debug( LDAP_DEBUG_ANY,
1625 "syncrepl_message_to_op: %s dn get failed (%d)",
1626 si->si_ridtxt, rc, 0 );
1630 op->o_tag = LBER_DEFAULT;
1631 op->o_bd = si->si_wbe;
1633 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
1634 Debug( LDAP_DEBUG_ANY,
1635 "syncrepl_message_to_op: %s got empty dn",
1636 si->si_ridtxt, 0, 0 );
1640 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1642 if ( bv.bv_val == NULL )
1645 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1647 rc = dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1648 if ( rc != LDAP_SUCCESS ) {
1649 Debug( LDAP_DEBUG_ANY,
1650 "syncrepl_message_to_op: %s "
1651 "dn \"%s\" normalization failed (%d)",
1652 si->si_ridtxt, bdn.bv_val, rc );
1657 ber_dupbv( &op->o_req_dn, &dn );
1658 ber_dupbv( &op->o_req_ndn, &ndn );
1659 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1660 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1662 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1663 int i = verb_to_mask( bvals[0].bv_val, modops );
1665 Debug( LDAP_DEBUG_ANY,
1666 "syncrepl_message_to_op: %s unknown op %s",
1667 si->si_ridtxt, bvals[0].bv_val, 0 );
1672 op->o_tag = modops[i].mask;
1673 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1674 /* Parse attribute into modlist */
1675 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1676 modlist = syncrepl_accesslog_mods( si, bvals );
1678 modlist = syncrepl_changelog_mods( si, bvals );
1680 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1682 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1683 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1686 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1688 } else if ( !ber_bvstrcasecmp( &bv,
1689 &slap_schema.si_ad_entryCSN->ad_cname ) )
1691 slap_queue_csn( op, bvals );
1697 /* If we didn't get a mod type or a target DN, bail out */
1698 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1703 op->o_callback = &cb;
1704 slap_op_time( &op->o_time, &op->o_tincr );
1706 switch( op->o_tag ) {
1708 case LDAP_REQ_MODIFY:
1709 /* If we didn't get required data, bail */
1710 if ( !modlist ) goto done;
1712 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1714 if ( rc != LDAP_SUCCESS ) {
1715 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1716 "mods check (%s)\n",
1717 si->si_ridtxt, text, 0 );
1721 if ( op->o_tag == LDAP_REQ_ADD ) {
1722 Entry *e = entry_alloc();
1724 op->ora_e->e_name = op->o_req_dn;
1725 op->ora_e->e_nname = op->o_req_ndn;
1727 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1728 if( rc != LDAP_SUCCESS ) {
1729 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1730 "mods2entry (%s)\n",
1731 si->si_ridtxt, text, 0 );
1733 rc = op->o_bd->be_add( op, &rs );
1734 Debug( LDAP_DEBUG_SYNC,
1735 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1736 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1739 if ( e == op->ora_e )
1740 be_entry_release_w( op, op->ora_e );
1742 op->orm_modlist = modlist;
1743 op->o_bd = si->si_wbe;
1744 rc = op->o_bd->be_modify( op, &rs );
1745 modlist = op->orm_modlist;
1746 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1747 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1748 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1749 op->o_bd = si->si_be;
1753 case LDAP_REQ_MODRDN:
1754 if ( BER_BVISNULL( &rdn ) ) goto done;
1756 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1759 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1762 if ( !BER_BVISNULL( &sup ) ) {
1763 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1766 op->orr_newSup = &psup;
1767 op->orr_nnewSup = ⊅
1769 op->orr_newSup = NULL;
1770 op->orr_nnewSup = NULL;
1772 op->orr_newrdn = prdn;
1773 op->orr_nnewrdn = nrdn;
1774 op->orr_deleteoldrdn = deleteOldRdn;
1775 op->orr_modlist = NULL;
1776 if ( slap_modrdn2mods( op, &rs ) ) {
1780 /* Append modlist for operational attrs */
1784 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1786 m->sml_next = modlist;
1789 rc = op->o_bd->be_modrdn( op, &rs );
1790 slap_mods_free( op->orr_modlist, 1 );
1791 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1792 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1793 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1796 case LDAP_REQ_DELETE:
1797 rc = op->o_bd->be_delete( op, &rs );
1798 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1799 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1800 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1806 slap_graduate_commit_csn( op );
1807 op->o_bd = si->si_be;
1808 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1809 BER_BVZERO( &op->o_csn );
1811 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1813 if ( !BER_BVISNULL( &rdn ) ) {
1814 if ( !BER_BVISNULL( &nsup ) ) {
1815 ch_free( nsup.bv_val );
1817 if ( !BER_BVISNULL( &psup ) ) {
1818 ch_free( psup.bv_val );
1820 if ( !BER_BVISNULL( &nrdn ) ) {
1821 ch_free( nrdn.bv_val );
1823 if ( !BER_BVISNULL( &prdn ) ) {
1824 ch_free( prdn.bv_val );
1828 ch_free( op->o_req_ndn.bv_val );
1829 ch_free( op->o_req_dn.bv_val );
1836 syncrepl_message_to_entry(
1840 Modifications **modlist,
1846 BerElement *ber = NULL;
1849 Modifications **modtail = modlist;
1852 char txtbuf[SLAP_TEXT_BUFLEN];
1853 size_t textlen = sizeof txtbuf;
1855 struct berval bdn = BER_BVNULL, dn, ndn;
1860 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1861 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1862 "Message type should be entry (%d)",
1863 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1867 op->o_tag = LDAP_REQ_ADD;
1869 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1870 if ( rc != LDAP_SUCCESS ) {
1871 Debug( LDAP_DEBUG_ANY,
1872 "syncrepl_message_to_entry: %s dn get failed (%d)",
1873 si->si_ridtxt, rc, 0 );
1877 if ( BER_BVISEMPTY( &bdn ) && !BER_BVISEMPTY( &op->o_bd->be_nsuffix[0] ) ) {
1878 Debug( LDAP_DEBUG_ANY,
1879 "syncrepl_message_to_entry: %s got empty dn",
1880 si->si_ridtxt, 0, 0 );
1884 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1885 /* NOTE: this could be done even before decoding the DN,
1886 * although encoding errors wouldn't be detected */
1891 if ( entry == NULL ) {
1895 rc = dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1896 if ( rc != LDAP_SUCCESS ) {
1897 /* One of the things that could happen is that the schema
1898 * is not lined-up; this could result in unknown attributes.
1899 * A value non conformant to the syntax should be unlikely,
1900 * except when replicating between different versions
1901 * of the software, or when syntax validation bugs are fixed
1903 Debug( LDAP_DEBUG_ANY,
1904 "syncrepl_message_to_entry: "
1905 "%s dn \"%s\" normalization failed (%d)",
1906 si->si_ridtxt, bdn.bv_val, rc );
1910 ber_dupbv( &op->o_req_dn, &dn );
1911 ber_dupbv( &op->o_req_ndn, &ndn );
1912 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1913 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1915 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1918 e->e_name = op->o_req_dn;
1919 e->e_nname = op->o_req_ndn;
1921 while ( ber_remaining( ber ) ) {
1922 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1923 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1928 /* Drop all updates to the contextCSN of the context entry
1931 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1932 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1933 ber_bvarray_free( tmp.sml_values );
1937 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1939 mod->sml_op = LDAP_MOD_REPLACE;
1941 mod->sml_next = NULL;
1942 mod->sml_desc = NULL;
1943 mod->sml_type = tmp.sml_type;
1944 mod->sml_values = tmp.sml_values;
1945 mod->sml_nvalues = NULL;
1946 mod->sml_numvals = 0; /* slap_mods_check will set this */
1949 modtail = &mod->sml_next;
1952 if ( *modlist == NULL ) {
1953 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1954 si->si_ridtxt, 0, 0 );
1959 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1961 if ( rc != LDAP_SUCCESS ) {
1962 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1963 si->si_ridtxt, text, 0 );
1967 /* Strip out dynamically generated attrs */
1968 for ( modtail = modlist; *modtail ; ) {
1970 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1971 *modtail = mod->sml_next;
1972 slap_mod_free( &mod->sml_mod, 0 );
1975 modtail = &mod->sml_next;
1979 /* Strip out attrs in exattrs list */
1980 for ( modtail = modlist; *modtail ; ) {
1982 if ( ldap_charray_inlist( si->si_exattrs,
1983 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1985 *modtail = mod->sml_next;
1986 slap_mod_free( &mod->sml_mod, 0 );
1989 modtail = &mod->sml_next;
1993 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1994 if( rc != LDAP_SUCCESS ) {
1995 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1996 si->si_ridtxt, text, 0 );
2001 if ( rc != LDAP_SUCCESS ) {
2013 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
2015 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
2016 * entry if a previous refresh was interrupted before sending us a new
2017 * context state. We try to compare the new entry to the existing entry
2018 * and ignore the new entry if they are the same.
2020 * Also, we may get an update where the entryDN has changed, due to
2021 * a ModDn on the provider. We detect this as well, so we can issue
2022 * the corresponding operation locally.
2024 * In the case of a modify, we get a list of all the attributes
2025 * in the original entry. Rather than deleting the entry and re-adding it,
2026 * we issue a Modify request that deletes all the attributes and adds all
2027 * the new ones. This avoids the issue of trying to delete/add a non-leaf
2030 * We otherwise distinguish ModDN from Modify; in the case of
2031 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
2032 * operational attributes from the entry, and do a regular ModDN.
2034 typedef struct dninfo {
2038 struct berval nnewSup;
2039 int renamed; /* Was an existing entry renamed? */
2040 int delOldRDN; /* Was old RDN deleted? */
2041 Modifications **modlist; /* the modlist we received */
2042 Modifications *mods; /* the modlist we compared */
2043 Attribute *oldNattr; /* old naming attr */
2044 AttributeDescription *oldDesc; /* for renames */
2045 AttributeDescription *newDesc; /* for renames */
2048 /* return 1 if inserted, 0 otherwise */
2050 avl_presentlist_insert(
2052 struct berval *syncUUID )
2054 struct berval *syncuuid_bv = ch_malloc( sizeof( struct berval ) + syncUUID->bv_len + 1 );
2056 syncuuid_bv->bv_len = syncUUID->bv_len;
2057 syncuuid_bv->bv_val = (char *)&syncuuid_bv[1];
2058 AC_MEMCPY( syncuuid_bv->bv_val, syncUUID->bv_val, syncUUID->bv_len );
2059 syncuuid_bv->bv_val[ syncuuid_bv->bv_len ] = '\0';
2061 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
2062 syncuuid_cmp, avl_dup_error ) )
2064 ch_free( syncuuid_bv );
2076 Modifications** modlist,
2078 struct berval* syncUUID,
2079 struct berval* syncCSN )
2081 Backend *be = op->o_bd;
2082 slap_callback cb = { NULL, NULL, NULL, NULL };
2083 int syncuuid_inserted = 0;
2084 struct berval syncUUID_strrep = BER_BVNULL;
2086 SlapReply rs_search = {REP_RESULT};
2087 SlapReply rs_delete = {REP_RESULT};
2088 SlapReply rs_add = {REP_RESULT};
2089 SlapReply rs_modify = {REP_RESULT};
2091 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
2092 int rc = LDAP_SUCCESS;
2094 struct berval pdn = BER_BVNULL;
2099 Debug( LDAP_DEBUG_SYNC,
2100 "syncrepl_entry: %s LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_%s)\n",
2101 si->si_ridtxt, syncrepl_state2str( syncstate ), 0 );
2103 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
2104 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
2105 syncuuid_inserted = avl_presentlist_insert( si, syncUUID );
2109 if ( syncstate == LDAP_SYNC_PRESENT ) {
2111 } else if ( syncstate != LDAP_SYNC_DELETE ) {
2112 if ( entry == NULL ) {
2117 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
2118 if ( syncstate != LDAP_SYNC_DELETE ) {
2119 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
2122 /* add if missing */
2123 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
2124 &syncUUID_strrep, syncUUID );
2126 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
2127 /* replace only if necessary */
2128 if ( a->a_nvals != a->a_vals ) {
2129 ber_memfree( a->a_nvals[0].bv_val );
2130 ber_dupbv( &a->a_nvals[0], syncUUID );
2132 ber_memfree( a->a_vals[0].bv_val );
2133 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
2137 f.f_choice = LDAP_FILTER_EQUALITY;
2139 ava.aa_desc = slap_schema.si_ad_entryUUID;
2140 ava.aa_value = *syncUUID;
2142 if ( syncuuid_inserted ) {
2143 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
2144 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
2146 op->ors_filter = &f;
2148 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
2149 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
2150 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
2151 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
2152 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
2153 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
2154 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
2155 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
2157 op->o_tag = LDAP_REQ_SEARCH;
2158 op->ors_scope = LDAP_SCOPE_SUBTREE;
2159 op->ors_deref = LDAP_DEREF_NEVER;
2161 /* get the entry for this UUID */
2162 op->o_req_dn = si->si_base;
2163 op->o_req_ndn = si->si_base;
2165 op->o_time = slap_get_time();
2166 op->ors_tlimit = SLAP_NO_LIMIT;
2168 op->ors_limit = NULL;
2170 op->ors_attrs = slap_anlist_all_attributes;
2171 op->ors_attrsonly = 0;
2173 /* set callback function */
2174 op->o_callback = &cb;
2175 cb.sc_response = dn_callback;
2176 cb.sc_private = &dni;
2177 dni.new_entry = entry;
2178 dni.modlist = modlist;
2180 rc = be->be_search( op, &rs_search );
2181 Debug( LDAP_DEBUG_SYNC,
2182 "syncrepl_entry: %s be_search (%d)\n",
2183 si->si_ridtxt, rc, 0 );
2185 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
2186 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2189 cb.sc_response = null_callback;
2192 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
2193 Debug( LDAP_DEBUG_SYNC,
2194 "syncrepl_entry: %s %s\n",
2195 si->si_ridtxt, entry->e_name.bv_val, 0 );
2197 Debug( LDAP_DEBUG_SYNC,
2198 "syncrepl_entry: %s %s\n",
2199 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
2202 assert( BER_BVISNULL( &op->o_csn ) );
2204 slap_queue_csn( op, syncCSN );
2207 slap_op_time( &op->o_time, &op->o_tincr );
2208 switch ( syncstate ) {
2210 case LDAP_SYNC_MODIFY:
2211 if ( BER_BVISNULL( &op->o_csn ))
2214 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
2216 /* FIXME: op->o_csn is assumed to be
2217 * on the thread's slab; this needs
2218 * to be cleared ASAP.
2219 * What happens if already present?
2221 assert( BER_BVISNULL( &op->o_csn ) );
2222 op->o_csn = a->a_vals[0];
2227 if ( BER_BVISNULL( &dni.dn ) ) {
2229 op->o_req_dn = entry->e_name;
2230 op->o_req_ndn = entry->e_nname;
2231 op->o_tag = LDAP_REQ_ADD;
2233 op->o_bd = si->si_wbe;
2235 rc = op->o_bd->be_add( op, &rs_add );
2236 Debug( LDAP_DEBUG_SYNC,
2237 "syncrepl_entry: %s be_add %s (%d)\n",
2238 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2239 switch ( rs_add.sr_err ) {
2241 if ( op->ora_e == entry ) {
2242 be_entry_release_w( op, entry );
2248 /* we assume that LDAP_NO_SUCH_OBJECT is returned
2249 * only if the suffix entry is not present */
2250 case LDAP_NO_SUCH_OBJECT:
2251 rc = syncrepl_add_glue( op, entry );
2255 /* if an entry was added via syncrepl_add_glue(),
2256 * it likely has no entryUUID, so the previous
2257 * be_search() doesn't find it. In this case,
2258 * give syncrepl a chance to modify it. Also
2259 * allow for entries that were recreated with the
2260 * same DN but a different entryUUID.
2262 case LDAP_ALREADY_EXISTS:
2264 Operation op2 = *op;
2265 SlapReply rs2 = { 0 };
2266 slap_callback cb2 = { 0 };
2269 op2.o_tag = LDAP_REQ_SEARCH;
2270 op2.o_req_dn = entry->e_name;
2271 op2.o_req_ndn = entry->e_nname;
2272 op2.ors_scope = LDAP_SCOPE_BASE;
2273 op2.ors_deref = LDAP_DEREF_NEVER;
2274 op2.ors_attrs = slap_anlist_all_attributes;
2275 op2.ors_attrsonly = 0;
2276 op2.ors_limit = NULL;
2278 op2.ors_tlimit = SLAP_NO_LIMIT;
2280 f.f_choice = LDAP_FILTER_PRESENT;
2281 f.f_desc = slap_schema.si_ad_objectClass;
2282 op2.ors_filter = &f;
2283 op2.ors_filterstr = generic_filterstr;
2285 op2.o_callback = &cb2;
2286 cb2.sc_response = dn_callback;
2287 cb2.sc_private = &dni;
2289 rc = be->be_search( &op2, &rs2 );
2290 if ( rc ) goto done;
2293 slap_op_time( &op->o_time, &op->o_tincr );
2299 Debug( LDAP_DEBUG_ANY,
2300 "syncrepl_entry: %s be_add %s failed (%d)\n",
2301 si->si_ridtxt, op->o_req_dn.bv_val, rs_add.sr_err );
2309 op->o_req_dn = dni.dn;
2310 op->o_req_ndn = dni.ndn;
2311 if ( dni.renamed ) {
2312 struct berval noldp, newp;
2313 Modifications *mod, **modtail, **ml, *m2;
2314 int i, got_replace = 0, just_rename = 0;
2316 op->o_tag = LDAP_REQ_MODRDN;
2317 dnRdn( &entry->e_name, &op->orr_newrdn );
2318 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2320 if ( !BER_BVISNULL( &dni.nnewSup )) {
2321 dnParent( &entry->e_name, &newp );
2322 op->orr_newSup = &newp;
2323 op->orr_nnewSup = &dni.nnewSup;
2325 op->orr_newSup = NULL;
2326 op->orr_nnewSup = NULL;
2328 op->orr_deleteoldrdn = dni.delOldRDN;
2329 op->orr_modlist = NULL;
2330 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2334 /* Drop the RDN-related mods from this op, because their
2335 * equivalents were just setup by slap_modrdn2mods.
2337 * If delOldRDN is TRUE then we should see a delete modop
2338 * for oldDesc. We might see a replace instead.
2339 * delete with no values: therefore newDesc != oldDesc.
2340 * if oldNattr had only one value, then Drop this op.
2341 * delete with 1 value: can only be the oldRDN value. Drop op.
2342 * delete with N values: Drop oldRDN value, keep remainder.
2343 * replace with 1 value: if oldNattr had only one value and
2344 * newDesc == oldDesc, Drop this op.
2345 * Any other cases must be left intact.
2347 * We should also see an add modop for newDesc. (But not if
2348 * we got a replace modop due to delOldRDN.) If it has
2349 * multiple values, we'll have to drop the new RDN value.
2351 modtail = &op->orr_modlist;
2352 if ( dni.delOldRDN ) {
2353 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2354 if ( (*ml)->sml_desc == dni.oldDesc ) {
2356 if ( mod->sml_op == LDAP_MOD_REPLACE &&
2357 dni.oldDesc != dni.newDesc ) {
2358 /* This Replace is due to other Mods.
2363 if ( mod->sml_numvals <= 1 &&
2364 dni.oldNattr->a_numvals == 1 &&
2365 ( mod->sml_op == LDAP_MOD_DELETE ||
2366 mod->sml_op == LDAP_MOD_REPLACE )) {
2367 if ( mod->sml_op == LDAP_MOD_REPLACE )
2370 *ml = mod->sml_next;
2371 mod->sml_next = NULL;
2372 slap_mods_free( mod, 1 );
2375 if ( mod->sml_op != LDAP_MOD_DELETE || mod->sml_numvals == 0 )
2377 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2378 if ( m2->sml_desc == dni.oldDesc &&
2379 m2->sml_op == LDAP_MOD_DELETE ) break;
2381 for ( i=0; i<mod->sml_numvals; i++ ) {
2382 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2384 ch_free( mod->sml_values[i].bv_val );
2385 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2386 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2387 if ( mod->sml_nvalues ) {
2388 ch_free( mod->sml_nvalues[i].bv_val );
2389 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2390 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2395 if ( !mod->sml_numvals ) {
2397 *ml = mod->sml_next;
2398 mod->sml_next = NULL;
2399 slap_mods_free( mod, 1 );
2405 if ( !got_replace ) {
2406 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next ) {
2407 if ( (*ml)->sml_desc == dni.newDesc ) {
2409 if ( mod->sml_op != LDAP_MOD_ADD )
2411 if ( mod->sml_numvals == 1 ) {
2413 *ml = mod->sml_next;
2414 mod->sml_next = NULL;
2415 slap_mods_free( mod, 1 );
2418 for ( m2 = op->orr_modlist; m2; m2=m2->sml_next ) {
2419 if ( m2->sml_desc == dni.oldDesc &&
2420 m2->sml_op == SLAP_MOD_SOFTADD ) break;
2422 for ( i=0; i<mod->sml_numvals; i++ ) {
2423 if ( bvmatch( &mod->sml_values[i], &m2->sml_values[0] )) {
2425 ch_free( mod->sml_values[i].bv_val );
2426 mod->sml_values[i] = mod->sml_values[mod->sml_numvals];
2427 BER_BVZERO( &mod->sml_values[mod->sml_numvals] );
2428 if ( mod->sml_nvalues ) {
2429 ch_free( mod->sml_nvalues[i].bv_val );
2430 mod->sml_nvalues[i] = mod->sml_nvalues[mod->sml_numvals];
2431 BER_BVZERO( &mod->sml_nvalues[mod->sml_numvals] );
2441 /* RDNs must be NUL-terminated for back-ldap */
2442 noldp = op->orr_newrdn;
2443 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2444 noldp = op->orr_nnewrdn;
2445 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2447 /* Setup opattrs too */
2449 static AttributeDescription *nullattr = NULL;
2450 static AttributeDescription **const opattrs[] = {
2451 &slap_schema.si_ad_entryCSN,
2452 &slap_schema.si_ad_modifiersName,
2453 &slap_schema.si_ad_modifyTimestamp,
2456 AttributeDescription *opattr;
2460 /* pull mod off incoming modlist */
2461 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2462 for ( ml = &dni.mods; *ml; ml = &(*ml)->sml_next )
2464 if ( (*ml)->sml_desc == opattr ) {
2466 *ml = mod->sml_next;
2467 mod->sml_next = NULL;
2469 modtail = &mod->sml_next;
2474 /* If there are still Modifications left, put the opattrs
2475 * back, and let be_modify run. Otherwise, append the opattrs
2476 * to the orr_modlist.
2480 /* don't set a CSN for the rename op */
2482 slap_graduate_commit_csn( op );
2484 mod = op->orr_modlist;
2487 for ( ; mod->sml_next; mod=mod->sml_next );
2490 op->o_bd = si->si_wbe;
2491 rc = op->o_bd->be_modrdn( op, &rs_modify );
2492 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2493 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2495 slap_mods_free( op->orr_modlist, 1 );
2496 Debug( LDAP_DEBUG_SYNC,
2497 "syncrepl_entry: %s be_modrdn %s (%d)\n",
2498 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2500 /* Renamed entries may still have other mods so just fallthru */
2501 op->o_req_dn = entry->e_name;
2502 op->o_req_ndn = entry->e_nname;
2503 /* Use CSN on the modify */
2507 slap_queue_csn( op, syncCSN );
2510 op->o_tag = LDAP_REQ_MODIFY;
2511 op->orm_modlist = dni.mods;
2512 op->orm_no_opattrs = 1;
2513 op->o_bd = si->si_wbe;
2515 rc = op->o_bd->be_modify( op, &rs_modify );
2516 slap_mods_free( op->orm_modlist, 1 );
2517 op->orm_no_opattrs = 0;
2518 Debug( LDAP_DEBUG_SYNC,
2519 "syncrepl_entry: %s be_modify %s (%d)\n",
2520 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2521 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2522 Debug( LDAP_DEBUG_ANY,
2523 "syncrepl_entry: %s be_modify failed (%d)\n",
2524 si->si_ridtxt, rs_modify.sr_err, 0 );
2528 } else if ( !dni.renamed ) {
2529 Debug( LDAP_DEBUG_SYNC,
2530 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2531 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2533 slap_graduate_commit_csn( op );
2538 case LDAP_SYNC_DELETE :
2539 if ( !BER_BVISNULL( &dni.dn ) ) {
2540 op->o_req_dn = dni.dn;
2541 op->o_req_ndn = dni.ndn;
2542 op->o_tag = LDAP_REQ_DELETE;
2543 op->o_bd = si->si_wbe;
2544 rc = op->o_bd->be_delete( op, &rs_delete );
2545 Debug( LDAP_DEBUG_SYNC,
2546 "syncrepl_entry: %s be_delete %s (%d)\n",
2547 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2549 while ( rs_delete.sr_err == LDAP_SUCCESS
2550 && op->o_delete_glue_parent ) {
2551 op->o_delete_glue_parent = 0;
2552 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2553 slap_callback cb = { NULL };
2554 cb.sc_response = slap_null_cb;
2555 dnParent( &op->o_req_ndn, &pdn );
2557 op->o_req_ndn = pdn;
2558 op->o_callback = &cb;
2559 op->o_bd->be_delete( op, &rs_delete );
2570 Debug( LDAP_DEBUG_ANY,
2571 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2576 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2577 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2578 BER_BVZERO( &syncUUID_strrep );
2580 if ( !BER_BVISNULL( &dni.ndn ) ) {
2581 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2583 if ( !BER_BVISNULL( &dni.dn ) ) {
2584 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2587 entry_free( entry );
2590 slap_graduate_commit_csn( op );
2592 if ( !BER_BVISNULL( &op->o_csn ) && freecsn ) {
2593 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2595 BER_BVZERO( &op->o_csn );
2599 static struct berval gcbva[] = {
2605 #define NP_DELETE_ONE 2
2608 syncrepl_del_nonpresent(
2612 struct sync_cookie *sc,
2615 Backend* be = op->o_bd;
2616 slap_callback cb = { NULL };
2617 SlapReply rs_search = {REP_RESULT};
2618 SlapReply rs_delete = {REP_RESULT};
2619 SlapReply rs_modify = {REP_RESULT};
2620 struct nonpresent_entry *np_list, *np_prev;
2622 AttributeName an[2];
2624 struct berval pdn = BER_BVNULL;
2627 op->o_req_dn = si->si_base;
2628 op->o_req_ndn = si->si_base;
2630 cb.sc_response = nonpresent_callback;
2633 op->o_callback = &cb;
2634 op->o_tag = LDAP_REQ_SEARCH;
2635 op->ors_scope = si->si_scope;
2636 op->ors_deref = LDAP_DEREF_NEVER;
2637 op->o_time = slap_get_time();
2638 op->ors_tlimit = SLAP_NO_LIMIT;
2643 AttributeAssertion eq = ATTRIBUTEASSERTION_INIT;
2646 op->ors_attrsonly = 1;
2647 op->ors_attrs = slap_anlist_no_attrs;
2648 op->ors_limit = NULL;
2649 op->ors_filter = &uf;
2652 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2654 uf.f_choice = LDAP_FILTER_EQUALITY;
2655 si->si_refreshDelete |= NP_DELETE_ONE;
2657 for (i=0; uuids[i].bv_val; i++) {
2659 uf.f_av_value = uuids[i];
2660 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2661 rc = be->be_search( op, &rs_search );
2662 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2664 si->si_refreshDelete ^= NP_DELETE_ONE;
2668 AttributeAssertion mmaa;
2670 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2671 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2672 an[0].an_desc = slap_schema.si_ad_entryUUID;
2674 op->ors_slimit = SLAP_NO_LIMIT;
2675 op->ors_tlimit = SLAP_NO_LIMIT;
2676 op->ors_limit = NULL;
2677 op->ors_attrsonly = 0;
2678 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2679 /* In multimaster, updates can continue to arrive while
2680 * we're searching. Limit the search result to entries
2681 * older than our newest cookie CSN.
2683 if ( SLAP_MULTIMASTER( op->o_bd )) {
2688 f->f_choice = LDAP_FILTER_AND;
2689 f->f_next = op->ors_filter;
2693 f->f_choice = LDAP_FILTER_LE;
2695 f->f_av_desc = slap_schema.si_ad_entryCSN;
2697 BER_BVZERO( &f->f_av_value );
2698 for ( i=0; i<sc->numcsns; i++ ) {
2699 if ( ber_bvcmp( &sc->ctxcsn[i], &f->f_av_value ) > 0 )
2700 f->f_av_value = sc->ctxcsn[i];
2702 of = op->ors_filter;
2703 op->ors_filter = mmf;
2704 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2707 op->ors_filterstr = si->si_filterstr;
2709 op->o_nocaching = 1;
2712 rc = be->be_search( op, &rs_search );
2713 if ( SLAP_MULTIMASTER( op->o_bd )) {
2714 op->ors_filter = of;
2716 if ( op->ors_filter ) filter_free_x( op, op->ors_filter, 1 );
2717 if ( op->ors_filterstr.bv_val != si->si_filterstr.bv_val ) {
2718 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2723 op->o_nocaching = 0;
2725 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2727 if ( sc->ctxcsn && !BER_BVISNULL( &sc->ctxcsn[m] ) ) {
2728 csn = sc->ctxcsn[m];
2730 csn = si->si_syncCookie.ctxcsn[0];
2733 op->o_bd = si->si_wbe;
2734 slap_queue_csn( op, &csn );
2736 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2737 while ( np_list != NULL ) {
2738 LDAP_LIST_REMOVE( np_list, npe_link );
2740 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2741 op->o_tag = LDAP_REQ_DELETE;
2742 op->o_callback = &cb;
2743 cb.sc_response = null_callback;
2745 op->o_req_dn = *np_prev->npe_name;
2746 op->o_req_ndn = *np_prev->npe_nname;
2747 rc = op->o_bd->be_delete( op, &rs_delete );
2748 Debug( LDAP_DEBUG_SYNC,
2749 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2750 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2752 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2753 Modifications mod1, mod2;
2754 mod1.sml_op = LDAP_MOD_REPLACE;
2756 mod1.sml_desc = slap_schema.si_ad_objectClass;
2757 mod1.sml_type = mod1.sml_desc->ad_cname;
2758 mod1.sml_numvals = 2;
2759 mod1.sml_values = &gcbva[0];
2760 mod1.sml_nvalues = NULL;
2761 mod1.sml_next = &mod2;
2763 mod2.sml_op = LDAP_MOD_REPLACE;
2765 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2766 mod2.sml_type = mod2.sml_desc->ad_cname;
2767 mod2.sml_numvals = 1;
2768 mod2.sml_values = &gcbva[1];
2769 mod2.sml_nvalues = NULL;
2770 mod2.sml_next = NULL;
2772 op->o_tag = LDAP_REQ_MODIFY;
2773 op->orm_modlist = &mod1;
2775 rc = op->o_bd->be_modify( op, &rs_modify );
2776 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2779 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2780 op->o_delete_glue_parent ) {
2781 op->o_delete_glue_parent = 0;
2782 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2783 slap_callback cb = { NULL };
2784 cb.sc_response = slap_null_cb;
2785 dnParent( &op->o_req_ndn, &pdn );
2787 op->o_req_ndn = pdn;
2788 op->o_callback = &cb;
2789 /* give it a root privil ? */
2790 op->o_bd->be_delete( op, &rs_delete );
2796 op->o_delete_glue_parent = 0;
2798 ber_bvfree( np_prev->npe_name );
2799 ber_bvfree( np_prev->npe_nname );
2802 if ( slapd_shutdown ) {
2807 slap_graduate_commit_csn( op );
2810 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2811 BER_BVZERO( &op->o_csn );
2822 Backend *be = op->o_bd;
2823 slap_callback cb = { NULL };
2828 struct berval dn = BER_BVNULL;
2829 struct berval ndn = BER_BVNULL;
2831 SlapReply rs_add = {REP_RESULT};
2832 struct berval ptr, nptr;
2835 op->o_tag = LDAP_REQ_ADD;
2836 op->o_callback = &cb;
2837 cb.sc_response = null_callback;
2838 cb.sc_private = NULL;
2843 /* count RDNs in suffix */
2844 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2845 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2847 ptr.bv_len -= comma - ptr.bv_val;
2857 /* Start with BE suffix */
2859 for ( i = 0; i < suffrdns; i++ ) {
2860 comma = ber_bvrchr( &ptr, ',' );
2861 if ( comma != NULL ) {
2862 ptr.bv_len = comma - ptr.bv_val;
2869 if ( !BER_BVISEMPTY( &ptr ) ) {
2870 dn.bv_len -= ptr.bv_len + 1;
2871 dn.bv_val += ptr.bv_len + 1;
2874 /* the normalizedDNs are always the same length, no counting
2878 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2879 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2880 ndn.bv_len = be->be_nsuffix[0].bv_len;
2882 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2888 while ( ndn.bv_val > e->e_nname.bv_val ) {
2889 glue = entry_alloc();
2890 ber_dupbv( &glue->e_name, &dn );
2891 ber_dupbv( &glue->e_nname, &ndn );
2893 a = attr_alloc( slap_schema.si_ad_objectClass );
2896 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2897 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2898 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2899 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2901 a->a_nvals = a->a_vals;
2903 a->a_next = glue->e_attrs;
2906 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2909 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2910 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2911 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2913 a->a_nvals = a->a_vals;
2915 a->a_next = glue->e_attrs;
2918 op->o_req_dn = glue->e_name;
2919 op->o_req_ndn = glue->e_nname;
2921 rc = be->be_add ( op, &rs_add );
2922 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2923 if ( op->ora_e == glue )
2924 be_entry_release_w( op, glue );
2926 /* incl. ALREADY EXIST */
2928 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2934 /* Move to next child */
2935 comma = ber_bvrchr( &ptr, ',' );
2936 if ( comma == NULL ) {
2939 ptr.bv_len = comma - ptr.bv_val;
2941 dn.bv_val = ++comma;
2942 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2944 comma = ber_bvrchr( &nptr, ',' );
2945 assert( comma != NULL );
2946 nptr.bv_len = comma - nptr.bv_val;
2948 ndn.bv_val = ++comma;
2949 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2952 op->o_req_dn = e->e_name;
2953 op->o_req_ndn = e->e_nname;
2955 rc = be->be_add ( op, &rs_add );
2956 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2957 if ( op->ora_e == e )
2958 be_entry_release_w( op, e );
2967 syncrepl_updateCookie(
2970 struct sync_cookie *syncCookie )
2972 Backend *be = op->o_bd;
2974 struct berval first = BER_BVNULL;
2976 Syntax *syn = slap_schema.si_ad_contextCSN->ad_type->sat_syntax;
2982 slap_callback cb = { NULL };
2983 SlapReply rs_modify = {REP_RESULT};
2985 mod.sml_op = LDAP_MOD_REPLACE;
2986 mod.sml_desc = slap_schema.si_ad_contextCSN;
2987 mod.sml_type = mod.sml_desc->ad_cname;
2988 mod.sml_flags = SLAP_MOD_INTERNAL;
2989 mod.sml_nvalues = NULL;
2990 mod.sml_next = NULL;
2992 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2995 for ( i=0; i<syncCookie->numcsns; i++ ) {
2996 assert( !syn->ssyn_validate( syn, syncCookie->ctxcsn+i ));
2998 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
2999 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3003 /* clone the cookieState CSNs so we can Replace the whole thing */
3004 mod.sml_numvals = si->si_cookieState->cs_num;
3005 mod.sml_values = op->o_tmpalloc(( mod.sml_numvals+1 )*sizeof(struct berval), op->o_tmpmemctx );
3006 for ( i=0; i<mod.sml_numvals; i++ )
3007 mod.sml_values[i] = si->si_cookieState->cs_vals[i];
3008 BER_BVZERO( &mod.sml_values[i] );
3010 /* find any CSNs in the syncCookie that are newer than the cookieState */
3011 for ( i=0; i<syncCookie->numcsns; i++ ) {
3012 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
3013 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
3015 len = syncCookie->ctxcsn[i].bv_len;
3016 if ( len > si->si_cookieState->cs_vals[j].bv_len )
3017 len = si->si_cookieState->cs_vals[j].bv_len;
3018 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
3019 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
3020 mod.sml_values[j] = syncCookie->ctxcsn[i];
3021 if ( BER_BVISNULL( &first ) ) {
3022 first = syncCookie->ctxcsn[i];
3024 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3026 first = syncCookie->ctxcsn[i];
3031 /* there was no match for this SID, it's a new CSN */
3032 if ( j == si->si_cookieState->cs_num ) {
3033 mod.sml_values = op->o_tmprealloc( mod.sml_values,
3034 ( mod.sml_numvals+2 )*sizeof(struct berval), op->o_tmpmemctx );
3035 mod.sml_values[mod.sml_numvals++] = syncCookie->ctxcsn[i];
3036 BER_BVZERO( &mod.sml_values[mod.sml_numvals] );
3037 if ( BER_BVISNULL( &first ) ) {
3038 first = syncCookie->ctxcsn[i];
3039 } else if ( memcmp( syncCookie->ctxcsn[i].bv_val, first.bv_val, first.bv_len ) > 0 )
3041 first = syncCookie->ctxcsn[i];
3045 /* Should never happen, ITS#5065 */
3046 if ( BER_BVISNULL( &first )) {
3047 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3048 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3051 op->o_bd = si->si_wbe;
3052 slap_queue_csn( op, &first );
3054 op->o_tag = LDAP_REQ_MODIFY;
3056 cb.sc_response = null_callback;
3059 op->o_callback = &cb;
3060 op->o_req_dn = si->si_contextdn;
3061 op->o_req_ndn = si->si_contextdn;
3063 /* update contextCSN */
3064 op->o_dont_replicate = 1;
3066 op->orm_modlist = &mod;
3067 op->orm_no_opattrs = 1;
3068 rc = op->o_bd->be_modify( op, &rs_modify );
3070 if ( rs_modify.sr_err == LDAP_NO_SUCH_OBJECT &&
3071 SLAP_SYNC_SUBENTRY( op->o_bd )) {
3073 char txtbuf[SLAP_TEXT_BUFLEN];
3074 size_t textlen = sizeof txtbuf;
3075 Entry *e = slap_create_context_csn_entry( op->o_bd, NULL );
3076 rc = slap_mods2entry( &mod, &e, 0, 1, &text, txtbuf, textlen);
3078 rc = op->o_bd->be_add( op, &rs_modify );
3079 if ( e == op->ora_e )
3080 be_entry_release_w( op, op->ora_e );
3083 op->orm_no_opattrs = 0;
3084 op->o_dont_replicate = 0;
3086 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
3087 slap_sync_cookie_free( &si->si_syncCookie, 0 );
3088 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
3089 /* If we replaced any old values */
3090 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3091 if ( mod.sml_values[i].bv_val != si->si_cookieState->cs_vals[i].bv_val )
3092 ber_bvreplace( &si->si_cookieState->cs_vals[i],
3093 &mod.sml_values[i] );
3095 /* Handle any added values */
3096 if ( i < mod.sml_numvals ) {
3097 si->si_cookieState->cs_num = mod.sml_numvals;
3098 value_add( &si->si_cookieState->cs_vals, &mod.sml_values[i] );
3099 free( si->si_cookieState->cs_sids );
3100 si->si_cookieState->cs_sids = slap_parse_csn_sids(
3101 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
3104 si->si_cookieState->cs_age++;
3105 si->si_cookieAge = si->si_cookieState->cs_age;
3107 Debug( LDAP_DEBUG_ANY,
3108 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
3109 si->si_ridtxt, rs_modify.sr_err, 0 );
3111 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
3114 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
3115 BER_BVZERO( &op->o_csn );
3116 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
3117 op->o_tmpfree( mod.sml_values, op->o_tmpmemctx );
3120 for ( i=0; i<si->si_cookieState->cs_num; i++ ) {
3121 assert( !syn->ssyn_validate( syn, si->si_cookieState->cs_vals+i ));
3128 /* Compare the attribute from the old entry to the one in the new
3129 * entry. The Modifications from the new entry will either be left
3130 * in place, or changed to an Add or Delete as needed.
3133 attr_cmp( Operation *op, Attribute *old, Attribute *new,
3134 Modifications ***mret, Modifications ***mcur )
3137 Modifications *mod, **modtail;
3143 struct berval **adds, **dels;
3144 /* count old and new */
3145 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
3146 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
3148 /* there MUST be both old and new values */
3153 adds = op->o_tmpalloc( sizeof(struct berval *) * n, op->o_tmpmemctx );
3154 dels = op->o_tmpalloc( sizeof(struct berval *) * o, op->o_tmpmemctx );
3156 for ( i=0; i<o; i++ ) dels[i] = &old->a_vals[i];
3157 for ( i=0; i<n; i++ ) adds[i] = &new->a_vals[i];
3161 for ( i=0; i<o; i++ ) {
3162 for ( j=0; j<n; j++ ) {
3165 if ( bvmatch( dels[i], adds[j] ) ) {
3175 /* Don't delete/add an objectClass, always use the replace op.
3176 * Modify would fail if provider has replaced entry with a new,
3177 * and the new explicitly includes a superior of a class that was
3178 * only included implicitly in the old entry. Ref ITS#5517.
3180 * Also use replace op if attr has no equality matching rule.
3183 if ( nn && no < o &&
3184 ( old->a_desc == slap_schema.si_ad_objectClass ||
3185 !old->a_desc->ad_type->sat_equality ))
3189 /* all old values were deleted, just use the replace op */
3193 /* delete some values */
3194 mod = ch_malloc( sizeof( Modifications ) );
3195 mod->sml_op = LDAP_MOD_DELETE;
3197 mod->sml_desc = old->a_desc;
3198 mod->sml_type = mod->sml_desc->ad_cname;
3199 mod->sml_numvals = no;
3200 mod->sml_values = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3201 if ( old->a_vals != old->a_nvals ) {
3202 mod->sml_nvalues = ch_malloc( ( no + 1 ) * sizeof(struct berval) );
3204 mod->sml_nvalues = NULL;
3207 for ( i = 0; i < o; i++ ) {
3208 if ( !dels[i] ) continue;
3209 ber_dupbv( &mod->sml_values[j], &old->a_vals[i] );
3210 if ( mod->sml_nvalues ) {
3211 ber_dupbv( &mod->sml_nvalues[j], &old->a_nvals[i] );
3215 BER_BVZERO( &mod->sml_values[j] );
3216 if ( mod->sml_nvalues ) {
3217 BER_BVZERO( &mod->sml_nvalues[j] );
3220 modtail = &mod->sml_next;
3223 op->o_tmpfree( dels, op->o_tmpmemctx );
3224 /* some values were added */
3225 if ( nn && no < o ) {
3226 mod = ch_malloc( sizeof( Modifications ) );
3227 mod->sml_op = LDAP_MOD_ADD;
3229 mod->sml_desc = old->a_desc;
3230 mod->sml_type = mod->sml_desc->ad_cname;
3231 mod->sml_numvals = nn;
3232 mod->sml_values = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3233 if ( old->a_vals != old->a_nvals ) {
3234 mod->sml_nvalues = ch_malloc( ( nn + 1 ) * sizeof(struct berval) );
3236 mod->sml_nvalues = NULL;
3239 for ( i = 0; i < n; i++ ) {
3240 if ( !adds[i] ) continue;
3241 ber_dupbv( &mod->sml_values[j], &new->a_vals[i] );
3242 if ( mod->sml_nvalues ) {
3243 ber_dupbv( &mod->sml_nvalues[j], &new->a_nvals[i] );
3247 BER_BVZERO( &mod->sml_values[j] );
3248 if ( mod->sml_nvalues ) {
3249 BER_BVZERO( &mod->sml_nvalues[j] );
3252 modtail = &mod->sml_next;
3255 op->o_tmpfree( adds, op->o_tmpmemctx );
3257 /* new attr, just use the new mod */
3261 /* advance to next element */
3265 **mcur = mod->sml_next;
3267 modtail = &mod->sml_next;
3269 *mcur = &mod->sml_next;
3275 /* Generate a set of modifications to change the old entry into the
3276 * new one. On input ml is a list of modifications equivalent to
3277 * the new entry. It will be massaged and the result will be stored
3280 void syncrepl_diff_entry( Operation *op, Attribute *old, Attribute *new,
3281 Modifications **mods, Modifications **ml, int is_ctx)
3283 Modifications **modtail = mods;
3285 /* We assume that attributes are saved in the same order
3286 * in the remote and local databases. So if we walk through
3287 * the attributeDescriptions one by one they should match in
3288 * lock step. If not, look for an add or delete.
3290 while ( old && new )
3292 /* If we've seen this before, use its mod now */
3293 if ( new->a_flags & SLAP_ATTR_IXADD ) {
3294 attr_cmp( op, NULL, new, &modtail, &ml );
3298 /* Skip contextCSN */
3299 if ( is_ctx && old->a_desc ==
3300 slap_schema.si_ad_contextCSN ) {
3305 if ( old->a_desc != new->a_desc ) {
3309 /* If it's just been re-added later,
3310 * remember that we've seen it.
3312 tmp = attr_find( new, old->a_desc );
3314 tmp->a_flags |= SLAP_ATTR_IXADD;
3316 /* If it's a new attribute, pull it in.
3318 tmp = attr_find( old, new->a_desc );
3320 attr_cmp( op, NULL, new, &modtail, &ml );
3324 /* Delete old attr */
3325 mod = ch_malloc( sizeof( Modifications ) );
3326 mod->sml_op = LDAP_MOD_DELETE;
3328 mod->sml_desc = old->a_desc;
3329 mod->sml_type = mod->sml_desc->ad_cname;
3330 mod->sml_numvals = 0;
3331 mod->sml_values = NULL;
3332 mod->sml_nvalues = NULL;
3334 modtail = &mod->sml_next;
3339 /* kludge - always update modifiersName so that it
3340 * stays co-located with the other mod opattrs. But only
3341 * if we know there are other valid mods.
3343 if ( *mods && ( old->a_desc == slap_schema.si_ad_modifiersName ||
3344 old->a_desc == slap_schema.si_ad_modifyTimestamp ))
3345 attr_cmp( op, NULL, new, &modtail, &ml );
3347 attr_cmp( op, old, new, &modtail, &ml );
3360 dninfo *dni = op->o_callback->sc_private;
3362 if ( rs->sr_type == REP_SEARCH ) {
3363 if ( !BER_BVISNULL( &dni->dn ) ) {
3364 Debug( LDAP_DEBUG_ANY,
3365 "dn_callback : consistency error - "
3366 "entryUUID is not unique\n", 0, 0, 0 );
3368 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
3369 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
3370 /* If there is a new entry, see if it differs from the old.
3371 * We compare the non-normalized values so that cosmetic changes
3372 * in the provider are always propagated.
3374 if ( dni->new_entry ) {
3375 Modifications **modtail, **ml;
3376 Attribute *old, *new;
3377 struct berval old_rdn, new_rdn;
3378 struct berval old_p, new_p;
3379 int is_ctx, new_sup = 0;
3381 /* If old entry is not a glue entry, make sure new entry
3382 * is actually newer than old entry
3384 if ( !is_entry_glue( rs->sr_entry )) {
3385 old = attr_find( rs->sr_entry->e_attrs,
3386 slap_schema.si_ad_entryCSN );
3387 new = attr_find( dni->new_entry->e_attrs,
3388 slap_schema.si_ad_entryCSN );
3391 ber_len_t len = old->a_vals[0].bv_len;
3392 if ( len > new->a_vals[0].bv_len )
3393 len = new->a_vals[0].bv_len;
3394 rc = memcmp( old->a_vals[0].bv_val,
3395 new->a_vals[0].bv_val, len );
3397 Debug( LDAP_DEBUG_SYNC,
3398 "dn_callback : new entry is older than ours "
3399 "%s ours %s, new %s\n",
3400 rs->sr_entry->e_name.bv_val,
3401 old->a_vals[0].bv_val,
3402 new->a_vals[0].bv_val );
3403 return LDAP_SUCCESS;
3404 } else if ( rc == 0 ) {
3405 Debug( LDAP_DEBUG_SYNC,
3406 "dn_callback : entries have identical CSN "
3408 rs->sr_entry->e_name.bv_val,
3409 old->a_vals[0].bv_val, 0 );
3410 return LDAP_SUCCESS;
3415 is_ctx = dn_match( &rs->sr_entry->e_nname,
3416 &op->o_bd->be_nsuffix[0] );
3418 /* Did the DN change?
3419 * case changes in the parent are ignored,
3420 * we only want to know if the RDN was
3423 dnRdn( &rs->sr_entry->e_name, &old_rdn );
3424 dnRdn( &dni->new_entry->e_name, &new_rdn );
3425 dnParent( &rs->sr_entry->e_nname, &old_p );
3426 dnParent( &dni->new_entry->e_nname, &new_p );
3428 new_sup = !dn_match( &old_p, &new_p );
3429 if ( !dn_match( &old_rdn, &new_rdn ) || new_sup )
3431 struct berval oldRDN, oldVal;
3432 AttributeDescription *ad = NULL;
3438 dni->nnewSup = new_p;
3440 /* See if the oldRDN was deleted */
3441 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
3442 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
3443 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
3445 oldRDN.bv_len -= oldVal.bv_len + 1;
3446 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3448 for ( oldpos=0, a=rs->sr_entry->e_attrs;
3449 a && a->a_desc != ad; oldpos++, a=a->a_next );
3451 for ( newpos=0, a=dni->new_entry->e_attrs;
3452 a && a->a_desc != ad; newpos++, a=a->a_next );
3453 if ( !a || oldpos != newpos || attr_valfind( a,
3454 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
3455 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
3456 SLAP_MR_VALUE_OF_SYNTAX,
3457 &oldVal, NULL, op->o_tmpmemctx ) != LDAP_SUCCESS )
3461 /* Get the newRDN's desc */
3462 dnRdn( &dni->new_entry->e_nname, &oldRDN );
3463 oldVal.bv_val = strchr(oldRDN.bv_val, '=');
3464 oldRDN.bv_len = oldVal.bv_val - oldRDN.bv_val;
3466 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
3469 /* A ModDN has happened, but in Refresh mode other
3470 * changes may have occurred before we picked it up.
3471 * So fallthru to regular Modify processing.
3475 syncrepl_diff_entry( op, rs->sr_entry->e_attrs,
3476 dni->new_entry->e_attrs, &dni->mods, dni->modlist,
3480 } else if ( rs->sr_type == REP_RESULT ) {
3481 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
3482 Debug( LDAP_DEBUG_ANY,
3483 "dn_callback : consistency error - "
3484 "entryUUID is not unique\n", 0, 0, 0 );
3488 return LDAP_SUCCESS;
3492 nonpresent_callback(
3496 syncinfo_t *si = op->o_callback->sc_private;
3499 struct berval* present_uuid = NULL;
3500 struct nonpresent_entry *np_entry;
3502 if ( rs->sr_type == REP_RESULT ) {
3503 count = avl_free( si->si_presentlist, ch_free );
3504 si->si_presentlist = NULL;
3506 } else if ( rs->sr_type == REP_SEARCH ) {
3507 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
3508 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
3511 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
3515 if ( LogTest( LDAP_DEBUG_SYNC ) ) {
3516 char buf[sizeof("rid=999 non")];
3518 snprintf( buf, sizeof(buf), "%s %s", si->si_ridtxt,
3519 present_uuid ? "" : "non" );
3521 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %spresent UUID %s, dn %s\n",
3522 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
3525 if ( a == NULL ) return 0;
3528 if ( present_uuid == NULL ) {
3529 np_entry = (struct nonpresent_entry *)
3530 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
3531 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
3532 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
3533 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
3536 avl_delete( &si->si_presentlist,
3537 &a->a_nvals[0], syncuuid_cmp );
3538 ch_free( present_uuid );
3541 return LDAP_SUCCESS;
3549 if ( rs->sr_err != LDAP_SUCCESS &&
3550 rs->sr_err != LDAP_REFERRAL &&
3551 rs->sr_err != LDAP_ALREADY_EXISTS &&
3552 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3553 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3555 Debug( LDAP_DEBUG_ANY,
3556 "null_callback : error code 0x%x\n",
3559 return LDAP_SUCCESS;
3562 static struct berval *
3563 slap_uuidstr_from_normalized(
3564 struct berval* uuidstr,
3565 struct berval* normalized,
3570 unsigned char nibble;
3573 if ( normalized == NULL ) return NULL;
3574 if ( normalized->bv_len != 16 ) return NULL;
3579 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3580 if ( new == NULL ) {
3587 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3588 if ( new != uuidstr ) {
3589 slap_sl_free( new, ctx );
3594 for ( i = 0; i < 16; i++ ) {
3595 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3596 new->bv_val[(i<<1)+d] = '-';
3600 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3601 if ( nibble < 10 ) {
3602 new->bv_val[(i<<1)+d] = nibble + '0';
3604 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3607 nibble = (normalized->bv_val[i]) & 0xF;
3608 if ( nibble < 10 ) {
3609 new->bv_val[(i<<1)+d+1] = nibble + '0';
3611 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3615 new->bv_val[new->bv_len] = '\0';
3622 if ( normalized == NULL ) return NULL;
3623 if ( normalized->bv_len != 16 ) return NULL;
3629 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3630 if ( new == NULL ) {
3637 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3642 rc = lutil_uuidstr_from_normalized( normalized->bv_val,
3643 normalized->bv_len, new->bv_val, new->bv_len + 1 );
3647 if ( new != NULL ) {
3648 if ( new->bv_val != NULL ) {
3649 slap_sl_free( new->bv_val, ctx );
3652 if ( new != uuidstr ) {
3653 slap_sl_free( new, ctx );
3666 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3668 const struct berval *uuid1 = v_uuid1;
3669 const struct berval *uuid2 = v_uuid2;
3670 int rc = uuid1->bv_len - uuid2->bv_len;
3671 if ( rc ) return rc;
3672 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3676 syncinfo_free( syncinfo_t *sie, int free_all )
3678 syncinfo_t *si_next;
3680 Debug( LDAP_DEBUG_TRACE, "syncinfo_free: %s\n",
3681 sie->si_ridtxt, 0, 0 );
3684 si_next = sie->si_next;
3687 if ( sie->si_conn ) {
3688 connection_client_stop( sie->si_conn );
3689 sie->si_conn = NULL;
3691 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3695 struct re_s *re = sie->si_re;
3698 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
3699 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3700 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3701 ldap_pvt_runqueue_remove( &slapd_rq, re );
3702 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
3705 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3707 bindconf_free( &sie->si_bindconf );
3709 if ( sie->si_filterstr.bv_val ) {
3710 ch_free( sie->si_filterstr.bv_val );
3712 if ( sie->si_logfilterstr.bv_val ) {
3713 ch_free( sie->si_logfilterstr.bv_val );
3715 if ( sie->si_base.bv_val ) {
3716 ch_free( sie->si_base.bv_val );
3718 if ( sie->si_logbase.bv_val ) {
3719 ch_free( sie->si_logbase.bv_val );
3721 if ( sie->si_be && SLAP_SYNC_SUBENTRY( sie->si_be )) {
3722 ch_free( sie->si_contextdn.bv_val );
3724 if ( sie->si_attrs ) {
3726 while ( sie->si_attrs[i] != NULL ) {
3727 ch_free( sie->si_attrs[i] );
3730 ch_free( sie->si_attrs );
3732 if ( sie->si_exattrs ) {
3734 while ( sie->si_exattrs[i] != NULL ) {
3735 ch_free( sie->si_exattrs[i] );
3738 ch_free( sie->si_exattrs );
3740 if ( sie->si_anlist ) {
3742 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3743 ch_free( sie->si_anlist[i].an_name.bv_val );
3746 ch_free( sie->si_anlist );
3748 if ( sie->si_exanlist ) {
3750 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3751 ch_free( sie->si_exanlist[i].an_name.bv_val );
3754 ch_free( sie->si_exanlist );
3756 if ( sie->si_retryinterval ) {
3757 ch_free( sie->si_retryinterval );
3759 if ( sie->si_retrynum ) {
3760 ch_free( sie->si_retrynum );
3762 if ( sie->si_retrynum_init ) {
3763 ch_free( sie->si_retrynum_init );
3765 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3766 if ( sie->si_presentlist ) {
3767 avl_free( sie->si_presentlist, ch_free );
3769 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3770 struct nonpresent_entry* npe;
3771 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3772 LDAP_LIST_REMOVE( npe, npe_link );
3773 if ( npe->npe_name ) {
3774 if ( npe->npe_name->bv_val ) {
3775 ch_free( npe->npe_name->bv_val );
3777 ch_free( npe->npe_name );
3779 if ( npe->npe_nname ) {
3780 if ( npe->npe_nname->bv_val ) {
3781 ch_free( npe->npe_nname->bv_val );
3783 ch_free( npe->npe_nname );
3787 if ( sie->si_cookieState ) {
3788 sie->si_cookieState->cs_ref--;
3789 if ( !sie->si_cookieState->cs_ref ) {
3790 ch_free( sie->si_cookieState->cs_sids );
3791 ber_bvarray_free( sie->si_cookieState->cs_vals );
3792 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3793 ch_free( sie->si_cookieState );
3798 } while ( free_all && si_next );
3803 /* NOTE: used & documented in slapd.conf(5) */
3805 #define PROVIDERSTR "provider"
3806 #define SCHEMASTR "schemachecking"
3807 #define FILTERSTR "filter"
3808 #define SEARCHBASESTR "searchbase"
3809 #define SCOPESTR "scope"
3810 #define ATTRSONLYSTR "attrsonly"
3811 #define ATTRSSTR "attrs"
3812 #define TYPESTR "type"
3813 #define INTERVALSTR "interval"
3814 #define RETRYSTR "retry"
3815 #define SLIMITSTR "sizelimit"
3816 #define TLIMITSTR "timelimit"
3817 #define SYNCDATASTR "syncdata"
3818 #define LOGBASESTR "logbase"
3819 #define LOGFILTERSTR "logfilter"
3821 /* FIXME: undocumented */
3822 #define EXATTRSSTR "exattrs"
3823 #define MANAGEDSAITSTR "manageDSAit"
3827 GOT_RID = 0x00000001U,
3828 GOT_PROVIDER = 0x00000002U,
3829 GOT_SCHEMACHECKING = 0x00000004U,
3830 GOT_FILTER = 0x00000008U,
3831 GOT_SEARCHBASE = 0x00000010U,
3832 GOT_SCOPE = 0x00000020U,
3833 GOT_ATTRSONLY = 0x00000040U,
3834 GOT_ATTRS = 0x00000080U,
3835 GOT_TYPE = 0x00000100U,
3836 GOT_INTERVAL = 0x00000200U,
3837 GOT_RETRY = 0x00000400U,
3838 GOT_SLIMIT = 0x00000800U,
3839 GOT_TLIMIT = 0x00001000U,
3840 GOT_SYNCDATA = 0x00002000U,
3841 GOT_LOGBASE = 0x00004000U,
3842 GOT_LOGFILTER = 0x00008000U,
3843 GOT_EXATTRS = 0x00010000U,
3844 GOT_MANAGEDSAIT = 0x00020000U,
3845 GOT_BINDCONF = 0x00040000U,
3848 GOT_REQUIRED = (GOT_RID|GOT_PROVIDER|GOT_SEARCHBASE)
3851 static slap_verbmasks datamodes[] = {
3852 { BER_BVC("default"), SYNCDATA_DEFAULT },
3853 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3854 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3859 parse_syncrepl_retry(
3866 int use_default = 0;
3868 char *val = arg + STRLENOF( RETRYSTR "=" );
3869 if ( strcasecmp( val, "undefined" ) == 0 ) {
3874 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3875 retry_list[0] = NULL;
3877 slap_str2clist( &retry_list, val, " ,\t" );
3879 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3882 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3883 "Error: incomplete syncrepl retry list" );
3884 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3885 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3886 ch_free( retry_list[k] );
3888 ch_free( retry_list );
3891 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3892 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3893 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3894 for ( j = 0; j < n; j++ ) {
3896 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3897 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3898 "Error: invalid retry interval \"%s\" (#%d)",
3899 retry_list[j*2], j );
3900 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3901 /* do some cleanup */
3904 si->si_retryinterval[j] = (time_t)t;
3905 if ( *retry_list[j*2+1] == '+' ) {
3906 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3907 si->si_retrynum[j] = RETRYNUM_FOREVER;
3911 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3912 || si->si_retrynum_init[j] <= 0 )
3914 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3915 "Error: invalid initial retry number \"%s\" (#%d)",
3916 retry_list[j*2+1], j );
3917 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3918 /* do some cleanup */
3921 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3922 || si->si_retrynum[j] <= 0 )
3924 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3925 "Error: invalid retry number \"%s\" (#%d)",
3926 retry_list[j*2+1], j );
3927 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3928 /* do some cleanup */
3933 if ( j < 1 || si->si_retrynum_init[j-1] != RETRYNUM_FOREVER ) {
3934 Debug( LDAP_DEBUG_CONFIG,
3935 "%s: syncrepl will eventually stop retrying; the \"retry\" parameter should end with a '+'.\n",
3939 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3940 si->si_retrynum[j] = RETRYNUM_TAIL;
3941 si->si_retryinterval[j] = 0;
3943 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3944 ch_free( retry_list[k] );
3946 ch_free( retry_list );
3947 if ( !use_default ) {
3948 si->si_got |= GOT_RETRY;
3955 parse_syncrepl_line(
3962 for ( i = 1; i < c->argc; i++ ) {
3963 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3964 STRLENOF( IDSTR "=" ) ) )
3967 /* '\0' string terminator accounts for '=' */
3968 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3969 if ( lutil_atoi( &tmp, val ) != 0 ) {
3970 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3971 "Error: parse_syncrepl_line: "
3972 "unable to parse syncrepl id \"%s\"", val );
3973 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3976 if ( tmp > SLAP_SYNC_SID_MAX || tmp < 0 ) {
3977 snprintf( c->cr_msg, sizeof( c->cr_msg ),
3978 "Error: parse_syncrepl_line: "
3979 "syncrepl id %d is out of range [0..4095]", tmp );
3980 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
3984 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3985 si->si_got |= GOT_RID;
3986 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3987 STRLENOF( PROVIDERSTR "=" ) ) )
3989 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3990 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3991 si->si_got |= GOT_PROVIDER;
3992 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3993 STRLENOF( SCHEMASTR "=" ) ) )
3995 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3996 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3997 si->si_schemachecking = 1;
3998 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3999 si->si_schemachecking = 0;
4001 si->si_schemachecking = 1;
4003 si->si_got |= GOT_SCHEMACHECKING;
4004 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
4005 STRLENOF( FILTERSTR "=" ) ) )
4007 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
4008 if ( si->si_filterstr.bv_val )
4009 ch_free( si->si_filterstr.bv_val );
4010 ber_str2bv( val, 0, 1, &si->si_filterstr );
4011 si->si_got |= GOT_FILTER;
4012 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
4013 STRLENOF( LOGFILTERSTR "=" ) ) )
4015 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
4016 if ( si->si_logfilterstr.bv_val )
4017 ch_free( si->si_logfilterstr.bv_val );
4018 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
4019 si->si_got |= GOT_LOGFILTER;
4020 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
4021 STRLENOF( SEARCHBASESTR "=" ) ) )
4026 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
4027 if ( si->si_base.bv_val ) {
4028 ch_free( si->si_base.bv_val );
4030 ber_str2bv( val, 0, 0, &bv );
4031 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
4032 if ( rc != LDAP_SUCCESS ) {
4033 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4034 "Invalid base DN \"%s\": %d (%s)",
4035 val, rc, ldap_err2string( rc ) );
4036 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4039 if ( !be_issubordinate( c->be, &si->si_base ) ) {
4040 ch_free( si->si_base.bv_val );
4041 BER_BVZERO( &si->si_base );
4042 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4043 "Base DN \"%s\" is not within the database naming context",
4045 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4048 si->si_got |= GOT_SEARCHBASE;
4049 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
4050 STRLENOF( LOGBASESTR "=" ) ) )
4055 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
4056 if ( si->si_logbase.bv_val ) {
4057 ch_free( si->si_logbase.bv_val );
4059 ber_str2bv( val, 0, 0, &bv );
4060 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
4061 if ( rc != LDAP_SUCCESS ) {
4062 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4063 "Invalid logbase DN \"%s\": %d (%s)",
4064 val, rc, ldap_err2string( rc ) );
4065 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4068 si->si_got |= GOT_LOGBASE;
4069 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
4070 STRLENOF( SCOPESTR "=" ) ) )
4073 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
4074 j = ldap_pvt_str2scope( val );
4076 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4077 "Error: parse_syncrepl_line: "
4078 "unknown scope \"%s\"", val);
4079 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4083 si->si_got |= GOT_SCOPE;
4084 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
4085 STRLENOF( ATTRSONLYSTR ) ) )
4087 si->si_attrsonly = 1;
4088 si->si_got |= GOT_ATTRSONLY;
4089 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
4090 STRLENOF( ATTRSSTR "=" ) ) )
4092 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
4093 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4095 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4096 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
4097 if ( si->si_anlist == NULL ) {
4098 ch_free( attr_fname );
4101 si->si_anfile = attr_fname;
4103 char *str, *s, *next;
4104 const char *delimstr = " ,\t";
4105 str = ch_strdup( val );
4106 for ( s = ldap_pvt_strtok( str, delimstr, &next );
4108 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
4110 if ( strlen(s) == 1 && *s == '*' ) {
4111 si->si_allattrs = 1;
4112 val[ s - str ] = delimstr[0];
4114 if ( strlen(s) == 1 && *s == '+' ) {
4115 si->si_allopattrs = 1;
4116 val [ s - str ] = delimstr[0];
4120 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
4121 if ( si->si_anlist == NULL ) {
4125 si->si_got |= GOT_ATTRS;
4126 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
4127 STRLENOF( EXATTRSSTR "=" ) ) )
4129 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
4130 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
4132 attr_fname = ch_strdup( val + STRLENOF(":include:") );
4133 si->si_exanlist = file2anlist(
4134 si->si_exanlist, attr_fname, " ,\t" );
4135 if ( si->si_exanlist == NULL ) {
4136 ch_free( attr_fname );
4139 ch_free( attr_fname );
4141 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
4142 if ( si->si_exanlist == NULL ) {
4146 si->si_got |= GOT_EXATTRS;
4147 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
4148 STRLENOF( TYPESTR "=" ) ) )
4150 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
4151 if ( !strncasecmp( val, "refreshOnly",
4152 STRLENOF("refreshOnly") ) )
4154 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4155 } else if ( !strncasecmp( val, "refreshAndPersist",
4156 STRLENOF("refreshAndPersist") ) )
4158 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
4159 si->si_interval = 60;
4161 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4162 "Error: parse_syncrepl_line: "
4163 "unknown sync type \"%s\"", val);
4164 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4167 si->si_got |= GOT_TYPE;
4168 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
4169 STRLENOF( INTERVALSTR "=" ) ) )
4171 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
4172 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
4173 si->si_interval = 0;
4174 } else if ( strchr( val, ':' ) != NULL ) {
4175 char *next, *ptr = val;
4178 dd = strtol( ptr, &next, 10 );
4179 if ( next == ptr || next[0] != ':' || dd < 0 ) {
4180 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4181 "Error: parse_syncrepl_line: "
4182 "invalid interval \"%s\", unable to parse days", val );
4183 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4187 hh = strtol( ptr, &next, 10 );
4188 if ( next == ptr || next[0] != ':' || hh < 0 || hh > 24 ) {
4189 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4190 "Error: parse_syncrepl_line: "
4191 "invalid interval \"%s\", unable to parse hours", val );
4192 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4196 mm = strtol( ptr, &next, 10 );
4197 if ( next == ptr || next[0] != ':' || mm < 0 || mm > 60 ) {
4198 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4199 "Error: parse_syncrepl_line: "
4200 "invalid interval \"%s\", unable to parse minutes", val );
4201 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4205 ss = strtol( ptr, &next, 10 );
4206 if ( next == ptr || next[0] != '\0' || ss < 0 || ss > 60 ) {
4207 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4208 "Error: parse_syncrepl_line: "
4209 "invalid interval \"%s\", unable to parse seconds", val );
4210 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4213 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
4217 if ( lutil_parse_time( val, &t ) != 0 ) {
4218 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4219 "Error: parse_syncrepl_line: "
4220 "invalid interval \"%s\"", val );
4221 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4224 si->si_interval = (time_t)t;
4226 if ( si->si_interval < 0 ) {
4227 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4228 "Error: parse_syncrepl_line: "
4229 "invalid interval \"%ld\"",
4230 (long) si->si_interval);
4231 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4234 si->si_got |= GOT_INTERVAL;
4235 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
4236 STRLENOF( RETRYSTR "=" ) ) )
4238 if ( parse_syncrepl_retry( c, c->argv[ i ], si ) ) {
4241 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
4242 STRLENOF( MANAGEDSAITSTR "=" ) ) )
4244 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
4245 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
4246 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
4248 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4249 "invalid manageDSAit value \"%s\".\n",
4251 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4254 si->si_got |= GOT_MANAGEDSAIT;
4255 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
4256 STRLENOF( SLIMITSTR "=") ) )
4258 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
4259 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4262 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
4263 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4264 "invalid size limit value \"%s\".\n",
4266 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4269 si->si_got |= GOT_SLIMIT;
4270 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
4271 STRLENOF( TLIMITSTR "=" ) ) )
4273 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
4274 if ( strcasecmp( val, "unlimited" ) == 0 ) {
4277 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
4278 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4279 "invalid time limit value \"%s\".\n",
4281 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4284 si->si_got |= GOT_TLIMIT;
4285 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
4286 STRLENOF( SYNCDATASTR "=" ) ) )
4288 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
4289 si->si_syncdata = verb_to_mask( val, datamodes );
4290 si->si_got |= GOT_SYNCDATA;
4291 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
4292 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4293 "Error: parse_syncrepl_line: "
4294 "unable to parse \"%s\"\n", c->argv[ i ] );
4295 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4298 si->si_got |= GOT_BINDCONF;
4301 if ( ( si->si_got & GOT_REQUIRED ) != GOT_REQUIRED ) {
4302 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4303 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
4304 si->si_got & GOT_RID ? "" : " "IDSTR,
4305 si->si_got & GOT_PROVIDER ? "" : " "PROVIDERSTR,
4306 si->si_got & GOT_SEARCHBASE ? "" : " "SEARCHBASESTR );
4307 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->cr_msg, 0 );
4311 if ( !( si->si_got & GOT_RETRY ) ) {
4312 Debug( LDAP_DEBUG_ANY, "syncrepl %s " SEARCHBASESTR "=\"%s\": no retry defined, using default\n",
4313 si->si_ridtxt, c->be->be_suffix ? c->be->be_suffix[ 0 ].bv_val : "(null)", 0 );
4314 if ( si->si_retryinterval == NULL ) {
4315 if ( parse_syncrepl_retry( c, "retry=undefined", si ) ) {
4331 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
4332 snprintf( c->cr_msg, sizeof(c->cr_msg), "database %s does not support "
4333 "operations required for syncrepl", c->be->be_type );
4334 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4337 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
4338 strcpy( c->cr_msg, "rootDN must be defined before syncrepl may be used" );
4339 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->cr_msg, 0 );
4342 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
4345 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
4349 si->si_bindconf.sb_tls = SB_TLS_OFF;
4350 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
4351 si->si_schemachecking = 0;
4352 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
4353 &si->si_filterstr );
4354 si->si_base.bv_val = NULL;
4355 si->si_scope = LDAP_SCOPE_SUBTREE;
4356 si->si_attrsonly = 0;
4357 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4358 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
4359 si->si_attrs = NULL;
4360 si->si_allattrs = 0;
4361 si->si_allopattrs = 0;
4362 si->si_exattrs = NULL;
4363 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
4364 si->si_interval = 86400;
4365 si->si_retryinterval = NULL;
4366 si->si_retrynum_init = NULL;
4367 si->si_retrynum = NULL;
4368 si->si_manageDSAit = 0;
4372 si->si_presentlist = NULL;
4373 LDAP_LIST_INIT( &si->si_nonpresentlist );
4374 ldap_pvt_thread_mutex_init( &si->si_mutex );
4376 rc = parse_syncrepl_line( c, si );
4381 /* Must be LDAPv3 because we need controls */
4382 switch ( si->si_bindconf.sb_version ) {
4384 /* not explicitly set */
4385 si->si_bindconf.sb_version = LDAP_VERSION3;
4388 /* explicitly set */
4391 Debug( LDAP_DEBUG_ANY,
4392 "version %d incompatible with syncrepl\n",
4393 si->si_bindconf.sb_version, 0, 0 );
4394 syncinfo_free( si, 0 );
4398 if ( ldap_url_parse( si->si_bindconf.sb_uri.bv_val, &lud )) {
4399 snprintf( c->cr_msg, sizeof( c->cr_msg ),
4400 "<%s> invalid URL", c->argv[0] );
4401 Debug( LDAP_DEBUG_ANY, "%s: %s %s\n",
4402 c->log, c->cr_msg, si->si_bindconf.sb_uri.bv_val );
4407 if ( slapMode & SLAP_SERVER_MODE ) {
4409 /* check if consumer points to current server and database.
4410 * If so, ignore this configuration.
4412 if ( !SLAP_DBHIDDEN( c->be ) ) {
4414 /* if searchbase doesn't match current DB suffix,
4415 * assume it's different
4417 for ( i=0; !BER_BVISNULL( &c->be->be_nsuffix[i] ); i++ ) {
4418 if ( bvmatch( &si->si_base, &c->be->be_nsuffix[i] )) {
4423 /* if searchbase matches, see if URLs match */
4424 if ( isMe && config_check_my_url( si->si_bindconf.sb_uri.bv_val,
4430 init_syncrepl( si );
4431 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
4432 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
4433 si->si_interval, do_syncrepl, si, "do_syncrepl",
4435 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
4437 rc = config_sync_shadow( c ) ? -1 : 0;
4442 /* mirrormode still needs to see this flag in tool mode */
4443 rc = config_sync_shadow( c ) ? -1 : 0;
4445 ldap_free_urldesc( lud );
4449 /* Use main slapd defaults */
4450 bindconf_tls_defaults( &si->si_bindconf );
4453 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
4454 syncinfo_free( si, 0 );
4457 Debug( LDAP_DEBUG_CONFIG,
4458 "Config: ** successfully added syncrepl \"%s\"\n",
4459 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
4460 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
4461 if ( c->be->be_syncinfo ) {
4464 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
4466 /* add new syncrepl to end of list (same order as when deleting) */
4467 for ( sip = c->be->be_syncinfo; sip->si_next; sip = sip->si_next );
4470 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
4471 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
4473 c->be->be_syncinfo = si;
4475 si->si_cookieState->cs_ref++;
4484 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
4486 struct berval bc, uri, bs;
4487 char buf[BUFSIZ*2], *ptr;
4490 # define WHATSLEFT ((ber_len_t) (&buf[sizeof( buf )] - ptr))
4494 /* temporarily inhibit bindconf from printing URI */
4495 uri = si->si_bindconf.sb_uri;
4496 BER_BVZERO( &si->si_bindconf.sb_uri );
4497 si->si_bindconf.sb_version = 0;
4498 bindconf_unparse( &si->si_bindconf, &bc );
4499 si->si_bindconf.sb_uri = uri;
4500 si->si_bindconf.sb_version = LDAP_VERSION3;
4503 assert( si->si_rid >= 0 && si->si_rid <= SLAP_SYNC_SID_MAX );
4504 len = snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
4505 si->si_rid, si->si_bindconf.sb_uri.bv_val );
4506 if ( len >= sizeof( buf ) ) return;
4508 if ( !BER_BVISNULL( &bc ) ) {
4509 if ( WHATSLEFT <= bc.bv_len ) {
4513 ptr = lutil_strcopy( ptr, bc.bv_val );
4516 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
4517 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
4518 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
4519 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
4522 if ( !BER_BVISNULL( &si->si_base ) ) {
4523 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
4524 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
4525 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
4528 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
4529 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
4530 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
4531 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
4534 if ( !BER_BVISNULL( &si->si_logbase ) ) {
4535 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
4536 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
4537 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
4540 if ( ldap_pvt_scope2bv( si->si_scope, &bs ) == LDAP_SUCCESS ) {
4541 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + bs.bv_len ) return;
4542 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
4543 ptr = lutil_strcopy( ptr, bs.bv_val );
4545 if ( si->si_attrsonly ) {
4546 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4547 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
4549 if ( si->si_anfile ) {
4550 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
4551 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
4552 ptr = lutil_strcopy( ptr, si->si_anfile );
4554 } else if ( si->si_allattrs || si->si_allopattrs ||
4555 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
4559 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
4560 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
4562 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
4563 if ( ptr == NULL ) return;
4564 if ( si->si_allattrs ) {
4565 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
4566 if ( old != ptr ) *ptr++ = ',';
4569 if ( si->si_allopattrs ) {
4570 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
4571 if ( old != ptr ) *ptr++ = ',';
4576 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
4577 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
4578 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
4579 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
4580 if ( ptr == NULL ) return;
4582 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
4583 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
4584 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
4586 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
4587 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
4588 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
4589 "refreshAndPersist" : "refreshOnly" );
4591 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
4594 dd = si->si_interval;
4601 len = snprintf( ptr, WHATSLEFT, " %s=%02d:%02d:%02d:%02d",
4602 INTERVALSTR, dd, hh, mm, ss );
4603 if ( len >= WHATSLEFT ) return;
4607 if ( si->si_got & GOT_RETRY ) {
4608 const char *space = "";
4609 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
4610 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
4611 for (i=0; si->si_retryinterval[i]; i++) {
4612 len = snprintf( ptr, WHATSLEFT, "%s%ld ", space,
4613 (long) si->si_retryinterval[i] );
4615 if ( WHATSLEFT - 1 <= len ) return;
4617 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
4620 len = snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
4621 if ( WHATSLEFT <= len ) return;
4625 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
4628 ptr = lutil_strcopy( ptr, " " RETRYSTR "=undefined" );
4631 if ( si->si_slimit ) {
4632 len = snprintf( ptr, WHATSLEFT, " " SLIMITSTR "=%d", si->si_slimit );
4633 if ( WHATSLEFT <= len ) return;
4637 if ( si->si_tlimit ) {
4638 len = snprintf( ptr, WHATSLEFT, " " TLIMITSTR "=%d", si->si_tlimit );
4639 if ( WHATSLEFT <= len ) return;
4643 if ( si->si_syncdata ) {
4644 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
4645 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
4646 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
4647 ptr = lutil_strcopy( ptr, bc.bv_val );
4650 bc.bv_len = ptr - buf;
4652 ber_dupbv( bv, &bc );
4656 syncrepl_config( ConfigArgs *c )
4658 if (c->op == SLAP_CONFIG_EMIT) {
4659 if ( c->be->be_syncinfo ) {
4663 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
4664 syncrepl_unparse( si, &bv );
4665 ber_bvarray_add( &c->rvalue_vals, &bv );
4670 } else if ( c->op == LDAP_MOD_DELETE ) {
4672 if ( c->be->be_syncinfo ) {
4673 syncinfo_t *si, **sip;
4676 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4678 if ( c->valx == -1 || i == c->valx ) {
4680 /* If the task is currently active, we have to leave
4681 * it running. It will exit on its own. This will only
4682 * happen when running on the cn=config DB.
4685 if ( ldap_pvt_thread_mutex_trylock( &si->si_mutex )) {
4688 if ( si->si_conn ) {
4689 /* If there's a persistent connection, it may
4690 * already have a thread queued. We know it's
4691 * not active, so it must be pending and we
4692 * can simply cancel it now.
4694 ldap_pvt_thread_pool_retract( &connection_pool,
4695 si->si_re->routine, si->si_re );
4697 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
4704 syncinfo_free( si, 0 );
4713 if ( !c->be->be_syncinfo ) {
4714 SLAP_DBFLAGS( c->be ) &= ~SLAP_DBFLAG_SHADOW_MASK;
4718 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4719 Debug(LDAP_DEBUG_ANY, "%s: "
4720 "syncrepl: database already shadowed.\n",
4724 return add_syncrepl( c );