1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 typedef struct cookie_state {
41 ldap_pvt_thread_mutex_t cs_mutex;
44 struct berval *cs_vals;
48 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
49 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
50 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
52 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
53 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
55 #define RETRYNUM_FOREVER (-1) /* retry forever */
56 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
57 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
58 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
60 typedef struct syncinfo_s {
61 struct syncinfo_s *si_next;
67 slap_bindconf si_bindconf;
68 struct berval si_base;
69 struct berval si_logbase;
70 struct berval si_filterstr;
71 struct berval si_logfilterstr;
75 AttributeName *si_anlist;
76 AttributeName *si_exanlist;
81 int si_schemachecking;
82 int si_type; /* the active type */
83 int si_ctype; /* the configured type */
85 time_t *si_retryinterval;
86 int *si_retrynum_init;
88 struct sync_cookie si_syncCookie;
89 cookie_state *si_cookieState;
95 int si_refreshPresent;
99 Avlnode *si_presentlist;
101 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
102 ldap_pvt_thread_mutex_t si_mutex;
105 static int syncuuid_cmp( const void *, const void * );
106 static void avl_ber_bvfree( void * );
107 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
108 static int syncrepl_message_to_op(
109 syncinfo_t *, Operation *, LDAPMessage * );
110 static int syncrepl_message_to_entry(
111 syncinfo_t *, Operation *, LDAPMessage *,
112 Modifications **, Entry **, int );
113 static int syncrepl_entry(
114 syncinfo_t *, Operation*, Entry*,
115 Modifications**,int, struct berval* );
116 static int syncrepl_updateCookie(
117 syncinfo_t *, Operation *, struct berval *,
118 struct sync_cookie * );
119 static struct berval * slap_uuidstr_from_normalized(
120 struct berval *, struct berval *, void * );
122 /* callback functions */
123 static int dn_callback( Operation *, SlapReply * );
124 static int nonpresent_callback( Operation *, SlapReply * );
125 static int null_callback( Operation *, SlapReply * );
127 static AttributeDescription *sync_descs[4];
130 init_syncrepl(syncinfo_t *si)
133 char **attrs, **exattrs;
135 if ( !sync_descs[0] ) {
136 sync_descs[0] = slap_schema.si_ad_objectClass;
137 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
138 sync_descs[2] = slap_schema.si_ad_entryCSN;
139 sync_descs[3] = NULL;
142 if ( si->si_allattrs && si->si_allopattrs )
145 attrs = anlist2attrs( si->si_anlist );
148 if ( si->si_allattrs ) {
151 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
152 for ( j = i; attrs[j] != NULL; j++ ) {
155 attrs[j] = attrs[j+1];
161 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
162 attrs[i] = ch_strdup("*");
165 } else if ( si->si_allopattrs ) {
168 if ( is_at_operational( at_find( attrs[i] ) ) ) {
169 for ( j = i; attrs[j] != NULL; j++ ) {
172 attrs[j] = attrs[j+1];
178 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
179 attrs[i] = ch_strdup("+");
183 for ( i = 0; sync_descs[i] != NULL; i++ ) {
186 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
187 for ( k = j; attrs[k] != NULL; k++ ) {
190 attrs[k] = attrs[k+1];
198 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
200 if ( si->si_allopattrs ) {
201 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
203 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
207 if ( si->si_allopattrs ) {
208 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
210 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
211 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
219 if ( si->si_allattrs == si->si_allopattrs ) {
220 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
221 attrs[i++] = ch_strdup( "*" );
222 attrs[i++] = ch_strdup( "+" );
223 } else if ( si->si_allattrs && !si->si_allopattrs ) {
224 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
225 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
226 attrs[i++] = ch_strdup( "*" );
227 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
228 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
230 } else if ( !si->si_allattrs && si->si_allopattrs ) {
231 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
232 attrs[i++] = ch_strdup( "+" );
233 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
238 si->si_attrs = attrs;
240 exattrs = anlist2attrs( si->si_exanlist );
243 for ( n = 0; exattrs[n] != NULL; n++ ) ;
245 for ( i = 0; sync_descs[i] != NULL; i++ ) {
247 while ( exattrs[j] != NULL ) {
248 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
249 ch_free( exattrs[j] );
250 for ( k = j; exattrs[k] != NULL; k++ ) {
251 exattrs[k] = exattrs[k+1];
259 for ( i = 0; exattrs[i] != NULL; i++ ) {
260 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
262 if ( ( oc = si->si_anlist[j].an_oc ) ) {
264 while ( oc->soc_required[k] ) {
265 if ( !strcmp( exattrs[i],
266 oc->soc_required[k]->sat_cname.bv_val ) ) {
267 ch_free( exattrs[i] );
268 for ( l = i; exattrs[l]; l++ ) {
269 exattrs[l] = exattrs[l+1];
279 for ( i = 0; exattrs[i] != NULL; i++ ) ;
282 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
285 si->si_exattrs = exattrs;
288 typedef struct logschema {
290 struct berval ls_req;
291 struct berval ls_mod;
292 struct berval ls_newRdn;
293 struct berval ls_delRdn;
294 struct berval ls_newSup;
297 static logschema changelog_sc = {
299 BER_BVC("changeType"),
302 BER_BVC("deleteOldRDN"),
303 BER_BVC("newSuperior")
306 static logschema accesslog_sc = {
310 BER_BVC("reqNewRDN"),
311 BER_BVC("reqDeleteOldRDN"),
312 BER_BVC("reqNewSuperior")
320 BerElementBuffer berbuf;
321 BerElement *ber = (BerElement *)&berbuf;
322 LDAPControl c[2], *ctrls[3];
323 struct timeval timeout;
328 char **attrs, *lattrs[8];
333 /* setup LDAP SYNC control */
334 ber_init2( ber, NULL, LBER_USE_DER );
335 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
337 /* If we're using a log but we have no state, then fallback to
338 * normal mode for a full refresh.
340 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
341 si->si_logstate = SYNCLOG_FALLBACK;
344 /* Use the log parameters if we're in log mode */
345 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
347 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
351 lattrs[0] = ls->ls_dn.bv_val;
352 lattrs[1] = ls->ls_req.bv_val;
353 lattrs[2] = ls->ls_mod.bv_val;
354 lattrs[3] = ls->ls_newRdn.bv_val;
355 lattrs[4] = ls->ls_delRdn.bv_val;
356 lattrs[5] = ls->ls_newSup.bv_val;
357 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
361 base = si->si_logbase.bv_val;
362 filter = si->si_logfilterstr.bv_val;
365 scope = LDAP_SCOPE_SUBTREE;
368 base = si->si_base.bv_val;
369 filter = si->si_filterstr.bv_val;
370 attrs = si->si_attrs;
371 attrsonly = si->si_attrsonly;
372 scope = si->si_scope;
374 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
375 si->si_type = LDAP_SYNC_REFRESH_ONLY;
377 si->si_type = si->si_ctype;
380 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
382 ber_printf( ber, "{eOb}",
383 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
385 ber_printf( ber, "{eb}",
386 abs(si->si_type), rhint );
389 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == LBER_ERROR ) {
394 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
395 c[0].ldctl_iscritical = si->si_type < 0;
398 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
399 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
400 c[1].ldctl_value = si->si_bindconf.sb_authzId;
401 c[1].ldctl_iscritical = 1;
408 timeout.tv_sec = si->si_tlimit;
411 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
412 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
413 si->si_slimit, &msgid );
424 int cmdline_cookie_found = 0;
426 struct sync_cookie *sc = NULL;
431 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
432 if ( rc != LDAP_SUCCESS ) {
435 op->o_protocol = LDAP_VERSION3;
437 /* Set SSF to strongest of TLS, SASL SSFs */
440 op->o_transport_ssf = 0;
442 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
443 == LDAP_SUCCESS && ssl != NULL )
445 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
447 #endif /* HAVE_TLS */
448 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
449 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
450 ? op->o_sasl_ssf : op->o_tls_ssf;
452 /* We've just started up, or the remote server hasn't sent us
453 * any meaningful state.
455 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
458 si->si_syncCookie.rid = si->si_rid;
459 si->si_syncCookie.sid = SLAP_SINGLE_SHADOW( si->si_be ) ? -1 :
462 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
463 if ( si->si_rid == sc->rid ) {
464 cmdline_cookie_found = 1;
469 if ( cmdline_cookie_found ) {
470 /* cookie is supplied in the command line */
472 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
474 /* ctxcsn wasn't parsed yet, do it now */
475 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
476 slap_sync_cookie_free( &si->si_syncCookie, 0 );
477 slap_dup_sync_cookie( &si->si_syncCookie, sc );
478 slap_sync_cookie_free( sc, 1 );
480 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
481 if ( !si->si_cookieState->cs_num ) {
482 /* get contextCSN shadow replica from database */
483 BerVarray csn = NULL;
484 void *ctx = op->o_tmpmemctx;
486 op->o_req_ndn = op->o_bd->be_nsuffix[0];
487 op->o_req_dn = op->o_req_ndn;
489 /* try to read stored contextCSN */
490 op->o_tmpmemctx = NULL;
491 backend_attribute( op, NULL, &op->o_req_ndn,
492 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
493 op->o_tmpmemctx = ctx;
495 si->si_cookieState->cs_vals = csn;
496 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
497 si->si_cookieState->cs_num = i;
498 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i, NULL );
501 if ( si->si_cookieState->cs_num ) {
502 ber_bvarray_free( si->si_syncCookie.ctxcsn );
503 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
504 si->si_cookieState->cs_vals, NULL )) {
508 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
509 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
511 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
512 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
514 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
517 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
518 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
519 si->si_syncCookie.sid );
525 int i, j, changed = 0;
527 /* Look for contextCSN from syncprov overlay. If
528 * there's no overlay, this will be a no-op. That means
529 * this is a pure consumer, so local changes will not be
530 * allowed, and all changes will already be reflected in
533 a.a_desc = slap_schema.si_ad_contextCSN;
535 e.e_name = si->si_wbe->be_suffix[0];
536 e.e_nname = si->si_wbe->be_nsuffix[0];
538 rs.sr_flags = REP_ENTRY_MODIFIABLE;
539 at[0].an_name = a.a_desc->ad_cname;
540 at[0].an_desc = a.a_desc;
541 BER_BVZERO( &at[1].an_name );
543 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
544 rc = backend_operational( op, &rs );
545 if ( rc == LDAP_SUCCESS && a.a_vals ) {
547 for (i=0; !BER_BVISNULL( &a.a_vals[i] ); i++) ;
549 /* check for differences */
550 if ( num != si->si_cookieState->cs_num ) {
553 for ( i=0; i<num; i++ ) {
554 if ( ber_bvcmp( &a.a_vals[i],
555 &si->si_cookieState->cs_vals[i] )) {
562 ber_bvarray_free( si->si_cookieState->cs_vals );
563 ch_free( si->si_cookieState->cs_sids );
564 si->si_cookieState->cs_num = num;
565 si->si_cookieState->cs_vals = a.a_vals;
566 si->si_cookieState->cs_sids = slap_parse_csn_sids( a.a_vals,
568 si->si_cookieState->cs_age++;
570 ber_bvarray_free( a.a_vals );
574 /* See if the cookieState has changed due to anything outside
575 * this particular consumer. That includes other consumers in
576 * the same context, or local changes detected above.
578 if ( si->si_cookieState->cs_num > 1 && si->si_cookieAge !=
579 si->si_cookieState->cs_age ) {
581 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
582 /* bogus, just dup everything */
583 if ( si->si_syncCookie.sids[i] == -1 ) {
584 ber_bvarray_free( si->si_syncCookie.ctxcsn );
585 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
586 si->si_cookieState->cs_vals, NULL );
590 for (j=0; j<si->si_cookieState->cs_num; j++) {
591 if ( si->si_syncCookie.sids[i] !=
592 si->si_cookieState->cs_sids[j] )
594 if ( bvmatch( &si->si_syncCookie.ctxcsn[i],
595 &si->si_cookieState->cs_vals[j] ))
597 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
598 &si->si_cookieState->cs_vals[j] );
604 ch_free( si->si_syncCookie.octet_str.bv_val );
605 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
606 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid,
607 SLAP_SINGLE_SHADOW( si->si_be ) ? -1 : slap_serverID );
610 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
613 rc = ldap_sync_search( si, op->o_tmpmemctx );
615 if( rc != LDAP_SUCCESS ) {
616 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
617 "ldap_search_ext: %s (%d)\n",
618 si->si_ridtxt, ldap_err2string( rc ), rc );
624 ldap_unbind_ext( si->si_ld, NULL, NULL );
633 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
640 if ( sc1->numcsns < sc2->numcsns ) {
641 *which = sc1->numcsns;
645 for (i=0; !BER_BVISNULL( &sc1->ctxcsn[i] ); i++) {
646 for (j=0; !BER_BVISNULL( &sc2->ctxcsn[j] ); j++) {
647 if ( sc1->sids[i] != sc2->sids[j] )
649 value_match( &match, slap_schema.si_ad_entryCSN,
650 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
651 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
652 &sc1->ctxcsn[i], &sc2->ctxcsn[i], &text );
668 LDAPControl **rctrls = NULL;
671 BerElementBuffer berbuf;
672 BerElement *ber = (BerElement *)&berbuf;
674 LDAPMessage *res = NULL;
675 LDAPMessage *msg = NULL;
678 struct berval *retdata = NULL;
683 struct berval syncUUID = BER_BVNULL;
684 struct sync_cookie syncCookie = { NULL };
685 struct sync_cookie syncCookie_req = { NULL };
686 struct berval cookie = BER_BVNULL;
692 Modifications *modlist = NULL;
696 struct timeval *tout_p = NULL;
697 struct timeval tout = { 0, 0 };
699 int refreshDeletes = 0;
701 BerVarray syncUUIDs = NULL;
704 if ( slapd_shutdown ) {
709 ber_init2( ber, NULL, LBER_USE_DER );
710 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
712 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
714 psub = &si->si_be->be_nsuffix[0];
716 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
718 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
724 while ( ( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
725 tout_p, &res ) ) > 0 )
727 if ( slapd_shutdown ) {
731 for( msg = ldap_first_message( si->si_ld, res );
733 msg = ldap_next_message( si->si_ld, msg ) )
735 if ( slapd_shutdown ) {
739 switch( ldap_msgtype( msg ) ) {
740 case LDAP_RES_SEARCH_ENTRY:
741 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
742 /* we can't work without the control */
745 /* NOTE: make sure we use the right one;
746 * a better approach would be to run thru
747 * the whole list and take care of all */
748 rctrlp = ldap_find_control( LDAP_CONTROL_SYNC_STATE, rctrls );
750 if ( rctrlp == NULL ) {
751 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
752 "got search entry without "
753 "Sync State control\n", si->si_ridtxt, 0, 0 );
757 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
758 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
759 /* FIXME: what if syncUUID is NULL or empty?
760 * (happens with back-sql...) */
761 if ( BER_BVISEMPTY( &syncUUID ) ) {
762 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
763 "got empty syncUUID\n", si->si_ridtxt, 0, 0 );
764 ldap_controls_free( rctrls );
768 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
769 ber_scanf( ber, /*"{"*/ "m}", &cookie );
770 if ( !BER_BVISNULL( &cookie ) ) {
771 ch_free( syncCookie.octet_str.bv_val );
772 ber_dupbv( &syncCookie.octet_str, &cookie );
774 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
776 slap_parse_sync_cookie( &syncCookie, NULL );
780 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
782 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
785 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
787 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
788 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
790 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
791 syncstate, &syncUUID ) ) == LDAP_SUCCESS &&
794 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
797 ldap_controls_free( rctrls );
799 slap_mods_free( modlist, 1 );
805 case LDAP_RES_SEARCH_REFERENCE:
806 Debug( LDAP_DEBUG_ANY,
807 "do_syncrep2: %s reference received error\n",
808 si->si_ridtxt, 0, 0 );
811 case LDAP_RES_SEARCH_RESULT:
812 Debug( LDAP_DEBUG_SYNC,
813 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
814 si->si_ridtxt, 0, 0 );
815 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
817 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
818 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
819 /* map old result code to registered code */
820 err = LDAP_SYNC_REFRESH_REQUIRED;
823 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
824 if ( si->si_logstate == SYNCLOG_LOGGING ) {
825 si->si_logstate = SYNCLOG_FALLBACK;
832 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
834 ber_scanf( ber, "{" /*"}"*/);
835 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
836 ber_scanf( ber, "m", &cookie );
837 if ( !BER_BVISNULL( &cookie ) ) {
838 ch_free( syncCookie.octet_str.bv_val );
839 ber_dupbv( &syncCookie.octet_str, &cookie);
841 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
843 slap_parse_sync_cookie( &syncCookie, NULL );
846 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
848 ber_scanf( ber, "b", &refreshDeletes );
850 ber_scanf( ber, /*"{"*/ "}" );
852 if ( !syncCookie.ctxcsn ) {
854 } else if ( !syncCookie_req.ctxcsn ) {
858 match = compare_csns( &syncCookie_req, &syncCookie, &m );
861 ldap_controls_free( rctrls );
863 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
864 /* FIXME : different error behaviors according to
865 * 1) err code : LDAP_BUSY ...
866 * 2) on err policy : stop service, stop sync, retry
868 if ( refreshDeletes == 0 && match < 0 &&
869 err == LDAP_SUCCESS )
871 syncrepl_del_nonpresent( op, si, NULL,
872 &syncCookie.ctxcsn[m] );
874 avl_free( si->si_presentlist, avl_ber_bvfree );
875 si->si_presentlist = NULL;
878 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
880 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
882 if ( err == LDAP_SUCCESS
883 && si->si_logstate == SYNCLOG_FALLBACK ) {
884 si->si_logstate = SYNCLOG_LOGGING;
885 rc = LDAP_SYNC_REFRESH_REQUIRED;
892 case LDAP_RES_INTERMEDIATE:
893 rc = ldap_parse_intermediate( si->si_ld, msg,
894 &retoid, &retdata, NULL, 0 );
895 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
896 ber_init2( ber, retdata, LBER_USE_DER );
898 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
900 case LDAP_TAG_SYNC_NEW_COOKIE:
901 Debug( LDAP_DEBUG_SYNC,
902 "do_syncrep2: %s %s - %s\n",
904 "LDAP_RES_INTERMEDIATE",
906 ber_scanf( ber, "tm", &tag, &cookie );
908 case LDAP_TAG_SYNC_REFRESH_DELETE:
909 case LDAP_TAG_SYNC_REFRESH_PRESENT:
910 Debug( LDAP_DEBUG_SYNC,
911 "do_syncrep2: %s %s - %s\n",
913 "LDAP_RES_INTERMEDIATE",
914 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
915 "REFRESH_PRESENT" : "REFRESH_DELETE" );
916 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
917 si->si_refreshDelete = 1;
919 si->si_refreshPresent = 1;
921 ber_scanf( ber, "t{" /*"}"*/, &tag );
922 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
924 ber_scanf( ber, "m", &cookie );
925 if ( !BER_BVISNULL( &cookie ) ) {
926 ch_free( syncCookie.octet_str.bv_val );
927 ber_dupbv( &syncCookie.octet_str, &cookie );
929 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
931 slap_parse_sync_cookie( &syncCookie, NULL );
934 if ( ber_peek_tag( ber, &len ) ==
935 LDAP_TAG_REFRESHDONE )
937 ber_scanf( ber, "b", &refreshDone );
939 ber_scanf( ber, /*"{"*/ "}" );
941 case LDAP_TAG_SYNC_ID_SET:
942 Debug( LDAP_DEBUG_SYNC,
943 "do_syncrep2: %s %s - %s\n",
945 "LDAP_RES_INTERMEDIATE",
947 ber_scanf( ber, "t{" /*"}"*/, &tag );
948 if ( ber_peek_tag( ber, &len ) ==
949 LDAP_TAG_SYNC_COOKIE )
951 ber_scanf( ber, "m", &cookie );
952 if ( !BER_BVISNULL( &cookie ) ) {
953 ch_free( syncCookie.octet_str.bv_val );
954 ber_dupbv( &syncCookie.octet_str, &cookie );
956 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
958 slap_parse_sync_cookie( &syncCookie, NULL );
959 compare_csns( &syncCookie_req, &syncCookie, &m );
962 if ( ber_peek_tag( ber, &len ) ==
963 LDAP_TAG_REFRESHDELETES )
965 ber_scanf( ber, "b", &refreshDeletes );
967 ber_scanf( ber, "[W]", &syncUUIDs );
968 ber_scanf( ber, /*"{"*/ "}" );
969 if ( refreshDeletes ) {
970 syncrepl_del_nonpresent( op, si, syncUUIDs,
971 &syncCookie.ctxcsn[m] );
972 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
975 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
976 struct berval *syncuuid_bv;
977 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
978 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
979 if ( avl_insert( &si->si_presentlist,
980 (caddr_t) syncuuid_bv,
981 syncuuid_cmp, avl_dup_error ) )
983 ber_bvfree( syncuuid_bv );
986 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
988 slap_sync_cookie_free( &syncCookie, 0 );
991 Debug( LDAP_DEBUG_ANY,
992 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
993 si->si_ridtxt, (long) si_tag, 0 );
994 ldap_memfree( retoid );
995 ber_bvfree( retdata );
999 if ( !syncCookie.ctxcsn ) {
1001 } else if ( !syncCookie_req.ctxcsn ) {
1005 match = compare_csns( &syncCookie_req, &syncCookie, &m );
1009 if ( si->si_refreshPresent == 1 ) {
1010 syncrepl_del_nonpresent( op, si, NULL,
1011 &syncCookie.ctxcsn[m] );
1014 if ( syncCookie.ctxcsn )
1016 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
1020 ldap_memfree( retoid );
1021 ber_bvfree( retdata );
1025 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1026 "unknown intermediate response (%d)\n",
1027 si->si_ridtxt, rc, 0 );
1028 ldap_memfree( retoid );
1029 ber_bvfree( retdata );
1035 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
1036 "unknown message\n", si->si_ridtxt, 0, 0 );
1040 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
1041 slap_sync_cookie_free( &syncCookie_req, 0 );
1042 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
1043 slap_sync_cookie_free( &syncCookie, 0 );
1046 ldap_msgfree( res );
1053 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1054 errstr = ldap_err2string( rc );
1056 Debug( LDAP_DEBUG_ANY,
1057 "do_syncrep2: %s %s\n", si->si_ridtxt, errstr, 0 );
1061 slap_sync_cookie_free( &syncCookie, 0 );
1062 slap_sync_cookie_free( &syncCookie_req, 0 );
1064 if ( res ) ldap_msgfree( res );
1066 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1067 if ( si->si_conn_setup ) {
1069 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1070 connection_client_stop( s );
1071 si->si_conn_setup = 0;
1073 ldap_unbind_ext( si->si_ld, NULL, NULL );
1085 struct re_s* rtask = arg;
1086 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1087 Connection conn = {0};
1088 OperationBuffer opbuf;
1090 int rc = LDAP_SUCCESS;
1093 int i, defer = 1, fail = 0;
1096 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1101 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1103 switch( abs( si->si_type ) ) {
1104 case LDAP_SYNC_REFRESH_ONLY:
1105 case LDAP_SYNC_REFRESH_AND_PERSIST:
1108 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1112 if ( slapd_shutdown ) {
1114 if ( si->si_conn_setup ) {
1115 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1116 connection_client_stop( s );
1117 si->si_conn_setup = 0;
1119 ldap_unbind_ext( si->si_ld, NULL, NULL );
1122 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1126 op = (Operation *) &opbuf;
1127 connection_fake_init( &conn, op, ctx );
1129 /* use global malloc for now */
1130 op->o_tmpmemctx = NULL;
1131 op->o_tmpmfuncs = &ch_mfuncs;
1133 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1134 op->o_bd = be = si->si_be;
1135 op->o_dn = op->o_bd->be_rootdn;
1136 op->o_ndn = op->o_bd->be_rootndn;
1137 if ( !si->si_schemachecking )
1138 op->o_no_schema_check = 1;
1140 /* If we're glued, send writes through the glue parent */
1141 if ( !si->si_wbe ) {
1142 if ( SLAP_GLUE_SUBORDINATE( be )) {
1143 si->si_wbe = select_backend( &be->be_nsuffix[0], 0, 1 );
1149 /* Establish session, do search */
1151 si->si_refreshDelete = 0;
1152 si->si_refreshPresent = 0;
1153 rc = do_syncrep1( op, si );
1157 /* Process results */
1158 if ( rc == LDAP_SUCCESS ) {
1159 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1161 rc = do_syncrep2( op, si );
1162 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1163 rc = ldap_sync_search( si, op->o_tmpmemctx );
1167 /* We got deleted while running on cn=config */
1168 if ( !si->si_ctype ) {
1169 if ( si->si_conn_setup )
1174 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1175 /* If we succeeded, enable the connection for further listening.
1176 * If we failed, tear down the connection and reschedule.
1178 if ( rc == LDAP_SUCCESS ) {
1179 if ( si->si_conn_setup ) {
1180 connection_client_enable( s );
1182 rc = connection_client_setup( s, do_syncrepl, arg );
1184 si->si_conn_setup = 1;
1186 } else if ( si->si_conn_setup ) {
1190 if ( rc == -2 ) rc = 0;
1194 /* At this point, we have 4 cases:
1195 * 1) for any hard failure, give up and remove this task
1196 * 2) for ServerDown, reschedule this task to run
1197 * 3) for Refresh and Success, reschedule to run
1198 * 4) for Persist and Success, reschedule to defer
1200 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1202 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1203 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1207 connection_client_stop( s );
1210 if ( rc == LDAP_SUCCESS ) {
1211 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1214 rtask->interval.tv_sec = si->si_interval;
1215 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1216 if ( si->si_retrynum ) {
1217 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1218 si->si_retrynum[i] = si->si_retrynum_init[i];
1220 si->si_retrynum[i] = RETRYNUM_TAIL;
1223 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1224 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1229 || !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1230 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1231 fail = RETRYNUM_TAIL;
1232 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1233 if ( si->si_retrynum[i] > 0 )
1234 si->si_retrynum[i]--;
1235 fail = si->si_retrynum[i];
1236 rtask->interval.tv_sec = si->si_retryinterval[i];
1237 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1238 slap_wake_listener();
1242 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1243 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1246 if ( fail == RETRYNUM_TAIL ) {
1247 Debug( LDAP_DEBUG_ANY,
1248 "do_syncrepl: rid %03d quitting\n",
1250 } else if ( fail > 0 ) {
1251 Debug( LDAP_DEBUG_ANY,
1252 "do_syncrepl: rid %03d retrying (%d retries left)\n",
1253 si->si_rid, fail, 0 );
1255 Debug( LDAP_DEBUG_ANY,
1256 "do_syncrepl: rid %03d retrying\n",
1261 /* Do final delete cleanup */
1262 if ( !si->si_ctype ) {
1263 cookie_state *cs = NULL;
1266 cs = be->be_syncinfo->si_cookieState;
1267 for ( sip = &be->be_syncinfo; *sip != si; sip = &(*sip)->si_next );
1269 syncinfo_free( si, 0 );
1270 if ( !be->be_syncinfo ) {
1271 SLAP_DBFLAGS( be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
1273 ch_free( cs->cs_sids );
1274 ber_bvarray_free( cs->cs_vals );
1275 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
1283 static slap_verbmasks modops[] = {
1284 { BER_BVC("add"), LDAP_REQ_ADD },
1285 { BER_BVC("delete"), LDAP_REQ_DELETE },
1286 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1287 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1291 static Modifications *
1292 syncrepl_accesslog_mods(
1299 AttributeDescription *ad;
1300 struct berval bv, bv2;
1302 Modifications *mod = NULL, *modlist = NULL, **modtail;
1307 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1311 colon = ber_bvchr( &bv, ':' );
1317 bv.bv_len = colon - bv.bv_val;
1318 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1323 /* Ignore dynamically generated attrs */
1324 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1328 /* Ignore excluded attrs */
1329 if ( ldap_charray_inlist( si->si_exattrs,
1330 ad->ad_type->sat_cname.bv_val ) )
1336 case '+': op = LDAP_MOD_ADD; break;
1337 case '-': op = LDAP_MOD_DELETE; break;
1338 case '=': op = LDAP_MOD_REPLACE; break;
1339 case '#': op = LDAP_MOD_INCREMENT; break;
1343 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1344 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1347 mod->sml_next = NULL;
1349 mod->sml_type = ad->ad_cname;
1350 mod->sml_values = NULL;
1351 mod->sml_nvalues = NULL;
1354 modtail = &mod->sml_next;
1356 if ( colon[2] == ' ' ) {
1357 bv.bv_val = colon + 3;
1358 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1359 ber_dupbv( &bv2, &bv );
1360 ber_bvarray_add( &mod->sml_values, &bv2 );
1366 static Modifications *
1367 syncrepl_changelog_mods(
1372 return NULL; /* FIXME */
1376 syncrepl_message_to_op(
1382 BerElement *ber = NULL;
1383 Modifications *modlist = NULL;
1385 SlapReply rs = { REP_RESULT };
1386 slap_callback cb = { NULL, null_callback, NULL, NULL };
1389 char txtbuf[SLAP_TEXT_BUFLEN];
1390 size_t textlen = sizeof txtbuf;
1392 struct berval bdn, dn = BER_BVNULL, ndn;
1393 struct berval bv, *bvals = NULL;
1394 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1395 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1396 psup = BER_BVNULL, nsup = BER_BVNULL;
1397 int rc, deleteOldRdn = 0, freeReqDn = 0;
1399 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1400 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1401 "Message type should be entry (%d)",
1402 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1406 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1411 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1413 if ( rc != LDAP_SUCCESS ) {
1414 Debug( LDAP_DEBUG_ANY,
1415 "syncrepl_message_to_op: %s dn get failed (%d)",
1416 si->si_ridtxt, rc, 0 );
1420 op->o_tag = LBER_DEFAULT;
1421 op->o_bd = si->si_wbe;
1423 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1425 if ( bv.bv_val == NULL )
1428 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1430 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1431 ber_dupbv( &op->o_req_dn, &dn );
1432 ber_dupbv( &op->o_req_ndn, &ndn );
1433 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1434 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1436 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1437 int i = verb_to_mask( bvals[0].bv_val, modops );
1439 Debug( LDAP_DEBUG_ANY,
1440 "syncrepl_message_to_op: %s unknown op %s",
1441 si->si_ridtxt, bvals[0].bv_val, 0 );
1446 op->o_tag = modops[i].mask;
1447 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1448 /* Parse attribute into modlist */
1449 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1450 modlist = syncrepl_accesslog_mods( si, bvals );
1452 modlist = syncrepl_changelog_mods( si, bvals );
1454 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1456 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1457 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1460 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1462 } else if ( !ber_bvstrcasecmp( &bv,
1463 &slap_schema.si_ad_entryCSN->ad_cname ) )
1465 slap_queue_csn( op, bvals );
1470 /* If we didn't get a mod type or a target DN, bail out */
1471 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1476 op->o_callback = &cb;
1477 slap_op_time( &op->o_time, &op->o_tincr );
1479 switch( op->o_tag ) {
1481 case LDAP_REQ_MODIFY:
1482 /* If we didn't get required data, bail */
1483 if ( !modlist ) goto done;
1485 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1487 if ( rc != LDAP_SUCCESS ) {
1488 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1489 "mods check (%s)\n",
1490 si->si_ridtxt, text, 0 );
1494 if ( op->o_tag == LDAP_REQ_ADD ) {
1495 Entry *e = entry_alloc();
1497 op->ora_e->e_name = op->o_req_dn;
1498 op->ora_e->e_nname = op->o_req_ndn;
1500 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1501 if( rc != LDAP_SUCCESS ) {
1502 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1503 "mods2entry (%s)\n",
1504 si->si_ridtxt, text, 0 );
1506 rc = op->o_bd->be_add( op, &rs );
1507 Debug( LDAP_DEBUG_SYNC,
1508 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1509 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1511 if ( e == op->ora_e )
1512 be_entry_release_w( op, op->ora_e );
1514 op->orm_modlist = modlist;
1515 op->o_bd = si->si_wbe;
1516 rc = op->o_bd->be_modify( op, &rs );
1517 modlist = op->orm_modlist;
1518 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1519 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1520 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1521 op->o_bd = si->si_be;
1524 case LDAP_REQ_MODRDN:
1525 if ( BER_BVISNULL( &rdn ) ) goto done;
1527 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1530 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1533 if ( !BER_BVISNULL( &sup ) ) {
1534 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1537 op->orr_newSup = &psup;
1538 op->orr_nnewSup = ⊅
1540 op->orr_newSup = NULL;
1541 op->orr_nnewSup = NULL;
1543 op->orr_newrdn = prdn;
1544 op->orr_nnewrdn = nrdn;
1545 op->orr_deleteoldrdn = deleteOldRdn;
1546 op->orr_modlist = NULL;
1547 if ( slap_modrdn2mods( op, &rs ) ) {
1551 /* Append modlist for operational attrs */
1555 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1557 m->sml_next = modlist;
1560 rc = op->o_bd->be_modrdn( op, &rs );
1561 slap_mods_free( op->orr_modlist, 1 );
1562 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1563 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1564 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1566 case LDAP_REQ_DELETE:
1567 rc = op->o_bd->be_delete( op, &rs );
1568 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1569 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1570 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1574 slap_graduate_commit_csn( op );
1575 op->o_bd = si->si_be;
1576 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1577 BER_BVZERO( &op->o_csn );
1579 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1581 if ( !BER_BVISNULL( &rdn ) ) {
1582 if ( !BER_BVISNULL( &nsup ) ) {
1583 ch_free( nsup.bv_val );
1585 if ( !BER_BVISNULL( &psup ) ) {
1586 ch_free( psup.bv_val );
1588 if ( !BER_BVISNULL( &nrdn ) ) {
1589 ch_free( nrdn.bv_val );
1591 if ( !BER_BVISNULL( &prdn ) ) {
1592 ch_free( prdn.bv_val );
1596 ch_free( op->o_req_ndn.bv_val );
1597 ch_free( op->o_req_dn.bv_val );
1604 syncrepl_message_to_entry(
1608 Modifications **modlist,
1614 BerElement *ber = NULL;
1617 Modifications **modtail = modlist;
1620 char txtbuf[SLAP_TEXT_BUFLEN];
1621 size_t textlen = sizeof txtbuf;
1623 struct berval bdn = BER_BVNULL, dn, ndn;
1628 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1629 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1630 "Message type should be entry (%d)",
1631 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1635 op->o_tag = LDAP_REQ_ADD;
1637 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1638 if ( rc != LDAP_SUCCESS ) {
1639 Debug( LDAP_DEBUG_ANY,
1640 "syncrepl_message_to_entry: %s dn get failed (%d)",
1641 si->si_ridtxt, rc, 0 );
1645 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1646 /* NOTE: this could be done even before decoding the DN,
1647 * although encoding errors wouldn't be detected */
1652 if ( entry == NULL ) {
1656 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
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 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1665 e->e_name = op->o_req_dn;
1666 e->e_nname = op->o_req_ndn;
1668 while ( ber_remaining( ber ) ) {
1669 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1670 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1675 /* Drop all updates to the contextCSN of the context entry
1678 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1679 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1680 ber_bvarray_free( tmp.sml_values );
1684 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1686 mod->sml_op = LDAP_MOD_REPLACE;
1688 mod->sml_next = NULL;
1689 mod->sml_desc = NULL;
1690 mod->sml_type = tmp.sml_type;
1691 mod->sml_values = tmp.sml_values;
1692 mod->sml_nvalues = NULL;
1695 modtail = &mod->sml_next;
1698 if ( *modlist == NULL ) {
1699 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1700 si->si_ridtxt, 0, 0 );
1705 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1707 if ( rc != LDAP_SUCCESS ) {
1708 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1709 si->si_ridtxt, text, 0 );
1713 /* Strip out dynamically generated attrs */
1714 for ( modtail = modlist; *modtail ; ) {
1716 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1717 *modtail = mod->sml_next;
1718 slap_mod_free( &mod->sml_mod, 0 );
1721 modtail = &mod->sml_next;
1725 /* Strip out attrs in exattrs list */
1726 for ( modtail = modlist; *modtail ; ) {
1728 if ( ldap_charray_inlist( si->si_exattrs,
1729 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1731 *modtail = mod->sml_next;
1732 slap_mod_free( &mod->sml_mod, 0 );
1735 modtail = &mod->sml_next;
1739 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1740 if( rc != LDAP_SUCCESS ) {
1741 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1742 si->si_ridtxt, text, 0 );
1747 if ( rc != LDAP_SUCCESS ) {
1759 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1761 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1762 * entry if a previous refresh was interrupted before sending us a new
1763 * context state. We try to compare the new entry to the existing entry
1764 * and ignore the new entry if they are the same.
1766 * Also, we may get an update where the entryDN has changed, due to
1767 * a ModDn on the provider. We detect this as well, so we can issue
1768 * the corresponding operation locally.
1770 * In the case of a modify, we get a list of all the attributes
1771 * in the original entry. Rather than deleting the entry and re-adding it,
1772 * we issue a Modify request that deletes all the attributes and adds all
1773 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1776 * We otherwise distinguish ModDN from Modify; in the case of
1777 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
1778 * operational attributes from the entry, and do a regular ModDN.
1780 typedef struct dninfo {
1784 int renamed; /* Was an existing entry renamed? */
1785 int delOldRDN; /* Was old RDN deleted? */
1786 Modifications **modlist; /* the modlist we received */
1787 Modifications *mods; /* the modlist we compared */
1795 Modifications** modlist,
1797 struct berval* syncUUID )
1799 Backend *be = op->o_bd;
1800 slap_callback cb = { NULL, NULL, NULL, NULL };
1801 struct berval *syncuuid_bv = NULL;
1802 struct berval syncUUID_strrep = BER_BVNULL;
1803 struct berval uuid_bv = BER_BVNULL;
1805 SlapReply rs_search = {REP_RESULT};
1806 SlapReply rs_delete = {REP_RESULT};
1807 SlapReply rs_add = {REP_RESULT};
1808 SlapReply rs_modify = {REP_RESULT};
1810 #ifdef LDAP_COMP_MATCH
1811 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1813 AttributeAssertion ava = { NULL, BER_BVNULL };
1815 int rc = LDAP_SUCCESS;
1817 struct berval pdn = BER_BVNULL;
1821 switch( syncstate ) {
1822 case LDAP_SYNC_PRESENT:
1823 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1825 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1828 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1830 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1832 case LDAP_SYNC_DELETE:
1833 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1835 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1837 case LDAP_SYNC_MODIFY:
1838 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1840 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1843 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: %s %s\n",
1845 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1848 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
1849 if ( !si->si_refreshPresent ) {
1850 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1851 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1852 syncuuid_cmp, avl_dup_error ) )
1854 ber_bvfree( syncuuid_bv );
1860 if ( syncstate == LDAP_SYNC_PRESENT ) {
1862 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1863 if ( entry == NULL ) {
1868 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1869 if ( syncstate != LDAP_SYNC_DELETE ) {
1870 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1873 /* add if missing */
1874 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1875 &syncUUID_strrep, syncUUID );
1877 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1878 /* replace only if necessary */
1879 if ( a->a_nvals != a->a_vals ) {
1880 ber_memfree( a->a_nvals[0].bv_val );
1881 ber_dupbv( &a->a_nvals[0], syncUUID );
1883 ber_memfree( a->a_vals[0].bv_val );
1884 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1888 f.f_choice = LDAP_FILTER_EQUALITY;
1890 ava.aa_desc = slap_schema.si_ad_entryUUID;
1891 ava.aa_value = *syncUUID;
1893 if ( syncuuid_bv ) {
1894 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
1895 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
1897 op->ors_filter = &f;
1899 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1900 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1901 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1902 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1903 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1904 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1905 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1906 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1908 op->o_tag = LDAP_REQ_SEARCH;
1909 op->ors_scope = LDAP_SCOPE_SUBTREE;
1910 op->ors_deref = LDAP_DEREF_NEVER;
1912 /* get the entry for this UUID */
1913 op->o_req_dn = si->si_base;
1914 op->o_req_ndn = si->si_base;
1916 op->o_time = slap_get_time();
1917 op->ors_tlimit = SLAP_NO_LIMIT;
1920 op->ors_attrs = slap_anlist_all_attributes;
1921 op->ors_attrsonly = 0;
1923 /* set callback function */
1924 op->o_callback = &cb;
1925 cb.sc_response = dn_callback;
1926 cb.sc_private = &dni;
1927 dni.new_entry = entry;
1928 dni.modlist = modlist;
1930 if ( limits_check( op, &rs_search ) == 0 ) {
1931 rc = be->be_search( op, &rs_search );
1932 Debug( LDAP_DEBUG_SYNC,
1933 "syncrepl_entry: %s be_search (%d)\n",
1934 si->si_ridtxt, rc, 0 );
1937 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1938 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1941 cb.sc_response = null_callback;
1944 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1945 Debug( LDAP_DEBUG_SYNC,
1946 "syncrepl_entry: %s %s\n",
1947 si->si_ridtxt, entry->e_name.bv_val, 0 );
1949 Debug( LDAP_DEBUG_SYNC,
1950 "syncrepl_entry: %s %s\n",
1951 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1954 slap_op_time( &op->o_time, &op->o_tincr );
1955 switch ( syncstate ) {
1957 case LDAP_SYNC_MODIFY:
1959 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1961 /* FIXME: op->o_csn is assumed to be
1962 * on the thread's slab; this needs
1963 * to be cleared ASAP.
1964 * What happens if already present?
1966 assert( BER_BVISNULL( &op->o_csn ) );
1967 op->o_csn = a->a_vals[0];
1971 if ( BER_BVISNULL( &dni.dn ) ) {
1973 op->o_req_dn = entry->e_name;
1974 op->o_req_ndn = entry->e_nname;
1975 op->o_tag = LDAP_REQ_ADD;
1977 op->o_bd = si->si_wbe;
1979 rc = op->o_bd->be_add( op, &rs_add );
1980 Debug( LDAP_DEBUG_SYNC,
1981 "syncrepl_entry: %s be_add (%d)\n",
1982 si->si_ridtxt, rc, 0 );
1983 switch ( rs_add.sr_err ) {
1985 if ( op->ora_e == entry ) {
1986 be_entry_release_w( op, entry );
1992 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1993 * only if the suffix entry is not present */
1994 case LDAP_NO_SUCH_OBJECT:
1995 rc = syncrepl_add_glue( op, entry );
1999 /* if an entry was added via syncrepl_add_glue(),
2000 * it likely has no entryUUID, so the previous
2001 * be_search() doesn't find it. In this case,
2002 * give syncrepl a chance to modify it. Also
2003 * allow for entries that were recreated with the
2004 * same DN but a different entryUUID.
2006 case LDAP_ALREADY_EXISTS:
2008 Operation op2 = *op;
2009 SlapReply rs2 = { 0 };
2010 slap_callback cb2 = { 0 };
2013 op2.o_tag = LDAP_REQ_SEARCH;
2014 op2.o_req_dn = entry->e_name;
2015 op2.o_req_ndn = entry->e_nname;
2016 op2.ors_scope = LDAP_SCOPE_BASE;
2017 op2.ors_deref = LDAP_DEREF_NEVER;
2018 op2.ors_attrs = slap_anlist_all_attributes;
2019 op2.ors_attrsonly = 0;
2020 op2.ors_limit = NULL;
2022 op2.ors_tlimit = SLAP_NO_LIMIT;
2024 f.f_choice = LDAP_FILTER_PRESENT;
2025 f.f_desc = slap_schema.si_ad_objectClass;
2026 op2.ors_filter = &f;
2027 op2.ors_filterstr = generic_filterstr;
2029 op2.o_callback = &cb2;
2030 cb2.sc_response = dn_callback;
2031 cb2.sc_private = &dni;
2033 rc = be->be_search( &op2, &rs2 );
2034 if ( rc ) goto done;
2037 slap_op_time( &op->o_time, &op->o_tincr );
2043 Debug( LDAP_DEBUG_ANY,
2044 "syncrepl_entry: %s be_add failed (%d)\n",
2045 si->si_ridtxt, rs_add.sr_err, 0 );
2052 op->o_req_dn = dni.dn;
2053 op->o_req_ndn = dni.ndn;
2054 if ( dni.renamed ) {
2055 struct berval noldp, newp, nnewp;
2057 op->o_tag = LDAP_REQ_MODRDN;
2058 dnRdn( &entry->e_name, &op->orr_newrdn );
2059 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
2061 dnParent( &dni.ndn, &noldp );
2062 dnParent( &entry->e_nname, &nnewp );
2063 if ( !dn_match( &noldp, &nnewp ) ) {
2064 dnParent( &entry->e_name, &newp );
2065 op->orr_newSup = &newp;
2066 op->orr_nnewSup = &nnewp;
2068 op->orr_newSup = NULL;
2069 op->orr_nnewSup = NULL;
2071 op->orr_deleteoldrdn = dni.delOldRDN;
2072 op->orr_modlist = NULL;
2073 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
2077 /* RDNs must be NUL-terminated for back-ldap */
2078 noldp = op->orr_newrdn;
2079 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
2080 noldp = op->orr_nnewrdn;
2081 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
2083 /* Setup opattrs too */
2085 static AttributeDescription *nullattr = NULL;
2086 static AttributeDescription **const opattrs[] = {
2087 &slap_schema.si_ad_entryCSN,
2088 &slap_schema.si_ad_modifiersName,
2089 &slap_schema.si_ad_modifyTimestamp,
2092 AttributeDescription *opattr;
2093 Modifications *mod, **modtail, **ml;
2096 for ( mod = op->orr_modlist;
2098 mod = mod->sml_next )
2100 modtail = &mod->sml_next;
2102 /* pull mod off incoming modlist, append to orr_modlist */
2103 for ( i = 0; (opattr = *opattrs[i]) != NULL; i++ ) {
2104 for ( ml = modlist; *ml; ml = &(*ml)->sml_next )
2106 if ( (*ml)->sml_desc == opattr ) {
2108 *ml = mod->sml_next;
2109 mod->sml_next = NULL;
2111 modtail = &mod->sml_next;
2117 op->o_bd = si->si_wbe;
2118 rc = op->o_bd->be_modrdn( op, &rs_modify );
2119 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
2120 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
2122 slap_mods_free( op->orr_modlist, 1 );
2123 Debug( LDAP_DEBUG_SYNC,
2124 "syncrepl_entry: %s be_modrdn (%d)\n",
2125 si->si_ridtxt, rc, 0 );
2130 op->o_tag = LDAP_REQ_MODIFY;
2131 op->orm_modlist = dni.mods;
2132 op->orm_no_opattrs = 1;
2133 op->o_bd = si->si_wbe;
2135 rc = op->o_bd->be_modify( op, &rs_modify );
2136 slap_mods_free( op->orm_modlist, 1 );
2137 Debug( LDAP_DEBUG_SYNC,
2138 "syncrepl_entry: %s be_modify (%d)\n",
2139 si->si_ridtxt, rc, 0 );
2140 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2141 Debug( LDAP_DEBUG_ANY,
2142 "syncrepl_entry: %s be_modify failed (%d)\n",
2143 si->si_ridtxt, rs_modify.sr_err, 0 );
2147 Debug( LDAP_DEBUG_SYNC,
2148 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2149 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2152 case LDAP_SYNC_DELETE :
2153 if ( !BER_BVISNULL( &dni.dn ) ) {
2154 op->o_req_dn = dni.dn;
2155 op->o_req_ndn = dni.ndn;
2156 op->o_tag = LDAP_REQ_DELETE;
2157 op->o_bd = si->si_wbe;
2158 rc = op->o_bd->be_delete( op, &rs_delete );
2159 Debug( LDAP_DEBUG_SYNC,
2160 "syncrepl_entry: %s be_delete (%d)\n",
2161 si->si_ridtxt, rc, 0 );
2163 while ( rs_delete.sr_err == LDAP_SUCCESS
2164 && op->o_delete_glue_parent ) {
2165 op->o_delete_glue_parent = 0;
2166 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2167 slap_callback cb = { NULL };
2168 cb.sc_response = slap_null_cb;
2169 dnParent( &op->o_req_ndn, &pdn );
2171 op->o_req_ndn = pdn;
2172 op->o_callback = &cb;
2173 op->o_bd->be_delete( op, &rs_delete );
2183 Debug( LDAP_DEBUG_ANY,
2184 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2189 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2190 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2191 BER_BVZERO( &syncUUID_strrep );
2193 if ( !BER_BVISNULL( &dni.ndn ) ) {
2194 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2196 if ( !BER_BVISNULL( &dni.dn ) ) {
2197 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2200 entry_free( entry );
2201 BER_BVZERO( &op->o_csn );
2205 static struct berval gcbva[] = {
2211 #define NP_DELETE_ONE 2
2214 syncrepl_del_nonpresent(
2218 struct berval *cookiecsn )
2220 Backend* be = op->o_bd;
2221 slap_callback cb = { NULL };
2222 SlapReply rs_search = {REP_RESULT};
2223 SlapReply rs_delete = {REP_RESULT};
2224 SlapReply rs_modify = {REP_RESULT};
2225 struct nonpresent_entry *np_list, *np_prev;
2227 AttributeName an[2];
2229 struct berval pdn = BER_BVNULL;
2232 op->o_req_dn = si->si_base;
2233 op->o_req_ndn = si->si_base;
2235 cb.sc_response = nonpresent_callback;
2238 op->o_callback = &cb;
2239 op->o_tag = LDAP_REQ_SEARCH;
2240 op->ors_scope = si->si_scope;
2241 op->ors_deref = LDAP_DEREF_NEVER;
2242 op->o_time = slap_get_time();
2243 op->ors_tlimit = SLAP_NO_LIMIT;
2248 #ifdef LDAP_COMP_MATCH
2249 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2251 AttributeAssertion eq = { NULL, BER_BVNULL };
2255 op->ors_attrsonly = 1;
2256 op->ors_attrs = slap_anlist_no_attrs;
2257 op->ors_limit = NULL;
2258 op->ors_filter = &uf;
2261 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2263 uf.f_choice = LDAP_FILTER_EQUALITY;
2264 si->si_refreshDelete |= NP_DELETE_ONE;
2266 for (i=0; uuids[i].bv_val; i++) {
2268 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2270 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2271 op->o_tmpfree( uf.f_av_value.bv_val, op->o_tmpmemctx );
2272 uf.f_av_value = uuids[i];
2273 rc = be->be_search( op, &rs_search );
2274 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2276 si->si_refreshDelete ^= NP_DELETE_ONE;
2278 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2279 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2280 an[0].an_desc = slap_schema.si_ad_entryUUID;
2282 op->ors_slimit = SLAP_NO_LIMIT;
2283 op->ors_attrsonly = 0;
2284 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2285 op->ors_filterstr = si->si_filterstr;
2286 op->o_nocaching = 1;
2288 if ( limits_check( op, &rs_search ) == 0 ) {
2289 rc = be->be_search( op, &rs_search );
2291 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2294 op->o_nocaching = 0;
2296 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2298 if ( cookiecsn && !BER_BVISNULL( cookiecsn ) ) {
2301 csn = si->si_syncCookie.ctxcsn[0];
2304 op->o_bd = si->si_wbe;
2305 slap_queue_csn( op, &csn );
2307 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2308 while ( np_list != NULL ) {
2309 LDAP_LIST_REMOVE( np_list, npe_link );
2311 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2312 op->o_tag = LDAP_REQ_DELETE;
2313 op->o_callback = &cb;
2314 cb.sc_response = null_callback;
2316 op->o_req_dn = *np_prev->npe_name;
2317 op->o_req_ndn = *np_prev->npe_nname;
2318 rc = op->o_bd->be_delete( op, &rs_delete );
2319 Debug( LDAP_DEBUG_SYNC,
2320 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2321 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2323 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2324 Modifications mod1, mod2;
2325 mod1.sml_op = LDAP_MOD_REPLACE;
2327 mod1.sml_desc = slap_schema.si_ad_objectClass;
2328 mod1.sml_type = mod1.sml_desc->ad_cname;
2329 mod1.sml_values = &gcbva[0];
2330 mod1.sml_nvalues = NULL;
2331 mod1.sml_next = &mod2;
2333 mod2.sml_op = LDAP_MOD_REPLACE;
2335 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2336 mod2.sml_type = mod2.sml_desc->ad_cname;
2337 mod2.sml_values = &gcbva[1];
2338 mod2.sml_nvalues = NULL;
2339 mod2.sml_next = NULL;
2341 op->o_tag = LDAP_REQ_MODIFY;
2342 op->orm_modlist = &mod1;
2344 rc = op->o_bd->be_modify( op, &rs_modify );
2345 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2348 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2349 op->o_delete_glue_parent ) {
2350 op->o_delete_glue_parent = 0;
2351 if ( !be_issuffix( be, &op->o_req_ndn ) ) {
2352 slap_callback cb = { NULL };
2353 cb.sc_response = slap_null_cb;
2354 dnParent( &op->o_req_ndn, &pdn );
2356 op->o_req_ndn = pdn;
2357 op->o_callback = &cb;
2358 /* give it a root privil ? */
2359 op->o_bd->be_delete( op, &rs_delete );
2365 op->o_delete_glue_parent = 0;
2367 ber_bvfree( np_prev->npe_name );
2368 ber_bvfree( np_prev->npe_nname );
2372 slap_graduate_commit_csn( op );
2375 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2376 BER_BVZERO( &op->o_csn );
2387 Backend *be = op->o_bd;
2388 slap_callback cb = { NULL };
2393 struct berval dn = BER_BVNULL;
2394 struct berval ndn = BER_BVNULL;
2396 SlapReply rs_add = {REP_RESULT};
2397 struct berval ptr, nptr;
2400 op->o_tag = LDAP_REQ_ADD;
2401 op->o_callback = &cb;
2402 cb.sc_response = null_callback;
2403 cb.sc_private = NULL;
2408 /* count RDNs in suffix */
2409 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2410 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2412 ptr.bv_len -= comma - ptr.bv_val;
2422 /* Start with BE suffix */
2424 for ( i = 0; i < suffrdns; i++ ) {
2425 comma = ber_bvrchr( &ptr, ',' );
2426 if ( comma != NULL ) {
2427 ptr.bv_len = comma - ptr.bv_val;
2434 if ( !BER_BVISEMPTY( &ptr ) ) {
2435 dn.bv_len -= ptr.bv_len + 1;
2436 dn.bv_val += ptr.bv_len + 1;
2439 /* the normalizedDNs are always the same length, no counting
2443 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2444 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2445 ndn.bv_len = be->be_nsuffix[0].bv_len;
2447 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2453 while ( ndn.bv_val > e->e_nname.bv_val ) {
2454 glue = entry_alloc();
2455 ber_dupbv( &glue->e_name, &dn );
2456 ber_dupbv( &glue->e_nname, &ndn );
2458 a = attr_alloc( slap_schema.si_ad_objectClass );
2460 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2461 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2462 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2463 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2465 a->a_nvals = a->a_vals;
2467 a->a_next = glue->e_attrs;
2470 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2472 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2473 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2474 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2476 a->a_nvals = a->a_vals;
2478 a->a_next = glue->e_attrs;
2481 op->o_req_dn = glue->e_name;
2482 op->o_req_ndn = glue->e_nname;
2484 rc = be->be_add ( op, &rs_add );
2485 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2486 if ( op->ora_e == glue )
2487 be_entry_release_w( op, glue );
2489 /* incl. ALREADY EXIST */
2491 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2497 /* Move to next child */
2498 comma = ber_bvrchr( &ptr, ',' );
2499 if ( comma == NULL ) {
2502 ptr.bv_len = comma - ptr.bv_val;
2504 dn.bv_val = ++comma;
2505 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2507 comma = ber_bvrchr( &nptr, ',' );
2508 assert( comma != NULL );
2509 nptr.bv_len = comma - nptr.bv_val;
2511 ndn.bv_val = ++comma;
2512 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2515 op->o_req_dn = e->e_name;
2516 op->o_req_ndn = e->e_nname;
2518 rc = be->be_add ( op, &rs_add );
2519 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2520 if ( op->ora_e == e )
2521 be_entry_release_w( op, e );
2530 syncrepl_updateCookie(
2534 struct sync_cookie *syncCookie )
2536 Backend *be = op->o_bd;
2537 Modifications mod[2];
2538 struct berval first = BER_BVNULL;
2542 slap_callback cb = { NULL };
2543 SlapReply rs_modify = {REP_RESULT};
2545 mod[0].sml_op = LDAP_MOD_DELETE;
2546 mod[0].sml_desc = slap_schema.si_ad_contextCSN;
2547 mod[0].sml_type = mod[0].sml_desc->ad_cname;
2548 mod[0].sml_values = NULL;
2549 mod[0].sml_nvalues = NULL;
2550 mod[0].sml_next = &mod[1];
2552 mod[1].sml_op = LDAP_MOD_ADD;
2553 mod[1].sml_desc = slap_schema.si_ad_contextCSN;
2554 mod[1].sml_type = mod[0].sml_desc->ad_cname;
2555 mod[1].sml_values = NULL;
2556 mod[1].sml_nvalues = NULL;
2557 mod[1].sml_next = NULL;
2559 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2561 for ( i=0; i<syncCookie->numcsns; i++ ) {
2562 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2563 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
2565 len = syncCookie->ctxcsn[i].bv_len;
2566 if ( len > si->si_cookieState->cs_vals[j].bv_len )
2567 len = si->si_cookieState->cs_vals[j].bv_len;
2568 if ( memcmp( syncCookie->ctxcsn[i].bv_val,
2569 si->si_cookieState->cs_vals[j].bv_val, len ) > 0 ) {
2570 ber_bvarray_add_x( &mod[0].sml_values,
2571 &si->si_cookieState->cs_vals[j], op->o_tmpmemctx );
2572 ber_bvarray_add_x( &mod[1].sml_values,
2573 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2574 if ( BER_BVISNULL( &first ))
2575 first = syncCookie->ctxcsn[i];
2579 /* there was no match for this SID, it's a new CSN */
2580 if ( j == si->si_cookieState->cs_num ) {
2581 ber_bvarray_add_x( &mod[1].sml_values,
2582 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2583 if ( BER_BVISNULL( &first ))
2584 first = syncCookie->ctxcsn[i];
2587 op->o_bd = si->si_wbe;
2588 slap_queue_csn( op, &first );
2590 op->o_tag = LDAP_REQ_MODIFY;
2592 cb.sc_response = null_callback;
2595 op->o_callback = &cb;
2596 op->o_req_dn = op->o_bd->be_suffix[0];
2597 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2599 /* update contextCSN */
2600 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2602 if ( mod[0].sml_values )
2603 op->orm_modlist = mod;
2605 op->orm_modlist = &mod[1];
2607 op->orm_no_opattrs = 1;
2608 rc = op->o_bd->be_modify( op, &rs_modify );
2611 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2612 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2613 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2614 /* If we replaced any old values */
2615 if ( mod[0].sml_values ) {
2616 for ( i=0; !BER_BVISNULL( &mod[0].sml_values[i] ); i++ ) {
2617 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2618 if ( mod[0].sml_values[i].bv_val !=
2619 si->si_cookieState->cs_vals[j].bv_val )
2621 ber_bvreplace( &si->si_cookieState->cs_vals[j],
2622 &mod[1].sml_values[i] );
2627 /* Else we just added */
2628 si->si_cookieState->cs_num += syncCookie->numcsns;
2629 value_add( &si->si_cookieState->cs_vals, syncCookie->ctxcsn );
2630 free( si->si_cookieState->cs_sids );
2631 si->si_cookieState->cs_sids = slap_parse_csn_sids(
2632 si->si_cookieState->cs_vals, si->si_cookieState->cs_num, NULL );
2635 si->si_cookieState->cs_age++;
2636 si->si_cookieAge = si->si_cookieState->cs_age;
2638 Debug( LDAP_DEBUG_ANY,
2639 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
2640 si->si_ridtxt, rs_modify.sr_err, 0 );
2642 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2644 slap_graduate_commit_csn( op );
2646 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2647 BER_BVZERO( &op->o_csn );
2648 if ( mod[1].sml_next ) slap_mods_free( mod[1].sml_next, 1 );
2649 op->o_tmpfree( mod[1].sml_values, op->o_tmpmemctx );
2650 op->o_tmpfree( mod[0].sml_values, op->o_tmpmemctx );
2656 attr_cmp( Operation *op, Attribute *old, Attribute *new,
2657 Modifications ***mret, Modifications ***mcur )
2660 Modifications *mod, **modtail;
2665 int n, o, d, a, *adds, *dels;
2666 /* count old and new */
2667 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
2668 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
2670 adds = op->o_tmpalloc( sizeof(int) * n, op->o_tmpmemctx );
2671 dels = op->o_tmpalloc( sizeof(int) * o, op->o_tmpmemctx );
2677 while ( i < o && j < n ) {
2679 if ( bvmatch( &old->a_vals[i], &new->a_vals[j] ) ) {
2684 for ( k = j + 1; k<n; k++ ) {
2685 if ( bvmatch( &old->a_vals[i], &new->a_vals[k] ) ) {
2689 /* an old value was deleted */
2694 for ( k = i + 1; k < o; k++ ) {
2695 if ( bvmatch( &old->a_vals[k], &new->a_vals[j] ) ) {
2708 /* all old values were deleted, just use the replace op */
2712 /* delete some values */
2713 mod = ch_malloc( sizeof( Modifications ) );
2714 mod->sml_op = LDAP_MOD_DELETE;
2716 mod->sml_desc = old->a_desc;
2717 mod->sml_type = mod->sml_desc->ad_cname;
2718 mod->sml_values = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2719 if ( old->a_vals != old->a_nvals ) {
2720 mod->sml_nvalues = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2722 mod->sml_nvalues = NULL;
2724 for ( i = 0; i < d; i++ ) {
2725 ber_dupbv( &mod->sml_values[i], &old->a_vals[dels[i]] );
2726 if ( mod->sml_nvalues ) {
2727 ber_dupbv( &mod->sml_nvalues[i], &old->a_nvals[dels[i]] );
2730 BER_BVZERO( &mod->sml_values[i] );
2731 if ( mod->sml_nvalues ) {
2732 BER_BVZERO( &mod->sml_nvalues[i] );
2735 modtail = &mod->sml_next;
2738 op->o_tmpfree( dels, op->o_tmpmemctx );
2739 /* some values were added */
2741 mod = ch_malloc( sizeof( Modifications ) );
2742 mod->sml_op = LDAP_MOD_ADD;
2744 mod->sml_desc = old->a_desc;
2745 mod->sml_type = mod->sml_desc->ad_cname;
2746 mod->sml_values = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2747 if ( old->a_vals != old->a_nvals ) {
2748 mod->sml_nvalues = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2750 mod->sml_nvalues = NULL;
2752 for ( i = 0; i < a; i++ ) {
2753 ber_dupbv( &mod->sml_values[i], &new->a_vals[adds[i]] );
2754 if ( mod->sml_nvalues ) {
2755 ber_dupbv( &mod->sml_nvalues[i], &new->a_nvals[adds[i]] );
2758 BER_BVZERO( &mod->sml_values[i] );
2759 if ( mod->sml_nvalues ) {
2760 BER_BVZERO( &mod->sml_nvalues[i] );
2763 modtail = &mod->sml_next;
2766 op->o_tmpfree( adds, op->o_tmpmemctx );
2768 /* new attr, just use the new mod */
2772 /* advance to next element */
2776 **mcur = mod->sml_next;
2778 modtail = &mod->sml_next;
2780 *mcur = &mod->sml_next;
2791 dninfo *dni = op->o_callback->sc_private;
2793 if ( rs->sr_type == REP_SEARCH ) {
2794 if ( !BER_BVISNULL( &dni->dn ) ) {
2795 Debug( LDAP_DEBUG_ANY,
2796 "dn_callback : consistency error - "
2797 "entryUUID is not unique\n", 0, 0, 0 );
2799 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2800 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2801 /* If there is a new entry, see if it differs from the old.
2802 * We compare the non-normalized values so that cosmetic changes
2803 * in the provider are always propagated.
2805 if ( dni->new_entry ) {
2806 Modifications **modtail, **ml;
2807 Attribute *old, *new;
2810 is_ctx = dn_match( &rs->sr_entry->e_nname,
2811 &op->o_bd->be_nsuffix[0] );
2813 /* Did the DN change?
2815 if ( !dn_match( &rs->sr_entry->e_name,
2816 &dni->new_entry->e_name ) )
2818 struct berval oldRDN, oldVal;
2819 AttributeDescription *ad = NULL;
2823 /* See if the oldRDN was deleted */
2824 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
2825 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
2826 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
2828 oldRDN.bv_len -= oldVal.bv_len + 2;
2829 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
2830 a = attr_find( dni->new_entry->e_attrs, ad );
2831 if ( !a || value_find_ex( ad,
2832 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
2833 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2834 SLAP_MR_VALUE_OF_SYNTAX, a->a_nvals,
2835 &oldVal, op->o_tmpmemctx ) != LDAP_SUCCESS )
2839 /* OK, this was just a modDN, we're done */
2840 return LDAP_SUCCESS;
2843 modtail = &dni->mods;
2846 /* Make sure new entry is actually newer than old entry */
2847 old = attr_find( rs->sr_entry->e_attrs,
2848 slap_schema.si_ad_entryCSN );
2849 new = attr_find( dni->new_entry->e_attrs,
2850 slap_schema.si_ad_entryCSN );
2852 int len = old->a_vals[0].bv_len;
2853 if ( len > new->a_vals[0].bv_len )
2854 len = new->a_vals[0].bv_len;
2855 if ( memcmp( old->a_vals[0].bv_val,
2856 new->a_vals[0].bv_val, len ) >= 0 ) {
2857 Debug( LDAP_DEBUG_SYNC,
2858 "dn_callback : new entry is older than ours "
2859 "%s ours %s, new %s\n",
2860 rs->sr_entry->e_name.bv_val,
2861 old->a_vals[0].bv_val,
2862 new->a_vals[0].bv_val );
2863 return LDAP_SUCCESS;
2867 /* We assume that attributes are saved in the same order
2868 * in the remote and local databases. So if we walk through
2869 * the attributeDescriptions one by one they should match in
2870 * lock step. If not, look for an add or delete.
2872 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2875 /* If we've seen this before, use its mod now */
2876 if ( new->a_flags & SLAP_ATTR_IXADD ) {
2877 attr_cmp( op, NULL, new, &modtail, &ml );
2881 /* Skip contextCSN */
2882 if ( is_ctx && old->a_desc ==
2883 slap_schema.si_ad_contextCSN ) {
2888 if ( old->a_desc != new->a_desc ) {
2892 /* If it's just been re-added later,
2893 * remember that we've seen it.
2895 tmp = attr_find( new, old->a_desc );
2897 tmp->a_flags |= SLAP_ATTR_IXADD;
2899 /* If it's a new attribute, pull it in.
2901 tmp = attr_find( old, new->a_desc );
2903 attr_cmp( op, NULL, new, &modtail, &ml );
2907 /* Delete old attr */
2908 mod = ch_malloc( sizeof( Modifications ) );
2909 mod->sml_op = LDAP_MOD_DELETE;
2911 mod->sml_desc = old->a_desc;
2912 mod->sml_type = mod->sml_desc->ad_cname;
2913 mod->sml_values = NULL;
2914 mod->sml_nvalues = NULL;
2916 modtail = &mod->sml_next;
2921 /* kludge - always update modifiersName so that it
2922 * stays co-located with the other mod opattrs. But only
2923 * if we know there are other valid mods.
2925 if ( old->a_desc == slap_schema.si_ad_modifiersName &&
2927 attr_cmp( op, NULL, new, &modtail, &ml );
2929 attr_cmp( op, old, new, &modtail, &ml );
2937 } else if ( rs->sr_type == REP_RESULT ) {
2938 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2939 Debug( LDAP_DEBUG_ANY,
2940 "dn_callback : consistency error - "
2941 "entryUUID is not unique\n", 0, 0, 0 );
2945 return LDAP_SUCCESS;
2949 nonpresent_callback(
2953 syncinfo_t *si = op->o_callback->sc_private;
2956 struct berval* present_uuid = NULL;
2957 struct nonpresent_entry *np_entry;
2959 if ( rs->sr_type == REP_RESULT ) {
2960 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2961 si->si_presentlist = NULL;
2963 } else if ( rs->sr_type == REP_SEARCH ) {
2964 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
2965 char buf[sizeof("rid=000 not")];
2967 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2970 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2974 if ( slap_debug & LDAP_DEBUG_SYNC ) {
2975 sprintf( buf, "%s %s", si->si_ridtxt,
2976 present_uuid ? "got" : "not" );
2979 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
2980 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
2982 if ( a == NULL ) return 0;
2985 if ( present_uuid == NULL ) {
2986 np_entry = (struct nonpresent_entry *)
2987 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
2988 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2989 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2990 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2993 avl_delete( &si->si_presentlist,
2994 &a->a_nvals[0], syncuuid_cmp );
2995 ch_free( present_uuid->bv_val );
2996 ch_free( present_uuid );
2999 return LDAP_SUCCESS;
3007 if ( rs->sr_err != LDAP_SUCCESS &&
3008 rs->sr_err != LDAP_REFERRAL &&
3009 rs->sr_err != LDAP_ALREADY_EXISTS &&
3010 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
3011 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
3013 Debug( LDAP_DEBUG_ANY,
3014 "null_callback : error code 0x%x\n",
3017 return LDAP_SUCCESS;
3020 static struct berval *
3021 slap_uuidstr_from_normalized(
3022 struct berval* uuidstr,
3023 struct berval* normalized,
3027 unsigned char nibble;
3030 if ( normalized == NULL ) return NULL;
3031 if ( normalized->bv_len != 16 ) return NULL;
3036 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
3037 if ( new == NULL ) {
3044 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
3045 if ( new != uuidstr ) {
3046 slap_sl_free( new, ctx );
3051 for ( i = 0; i < 16; i++ ) {
3052 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
3053 new->bv_val[(i<<1)+d] = '-';
3057 nibble = (normalized->bv_val[i] >> 4) & 0xF;
3058 if ( nibble < 10 ) {
3059 new->bv_val[(i<<1)+d] = nibble + '0';
3061 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
3064 nibble = (normalized->bv_val[i]) & 0xF;
3065 if ( nibble < 10 ) {
3066 new->bv_val[(i<<1)+d+1] = nibble + '0';
3068 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
3072 new->bv_val[new->bv_len] = '\0';
3077 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
3079 const struct berval *uuid1 = v_uuid1;
3080 const struct berval *uuid2 = v_uuid2;
3081 int rc = uuid1->bv_len - uuid2->bv_len;
3082 if ( rc ) return rc;
3083 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
3087 avl_ber_bvfree( void *v_bv )
3089 struct berval *bv = (struct berval *)v_bv;
3091 if( v_bv == NULL ) return;
3092 if ( !BER_BVISNULL( bv ) ) {
3093 ch_free( bv->bv_val );
3095 ch_free( (char *) bv );
3099 syncinfo_free( syncinfo_t *sie, int free_all )
3101 syncinfo_t *si_next;
3103 if ( free_all && sie->si_cookieState ) {
3104 ch_free( sie->si_cookieState->cs_sids );
3105 ber_bvarray_free( sie->si_cookieState->cs_vals );
3106 ldap_pvt_thread_mutex_destroy( &sie->si_cookieState->cs_mutex );
3107 ch_free( sie->si_cookieState );
3110 si_next = sie->si_next;
3113 if ( sie->si_conn_setup ) {
3115 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
3116 connection_client_stop( s );
3117 sie->si_conn_setup = 0;
3119 ldap_unbind_ext( sie->si_ld, NULL, NULL );
3122 /* re-fetch it, in case it was already removed */
3123 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
3125 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
3126 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
3127 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
3130 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
3132 bindconf_free( &sie->si_bindconf );
3134 if ( sie->si_filterstr.bv_val ) {
3135 ch_free( sie->si_filterstr.bv_val );
3137 if ( sie->si_logfilterstr.bv_val ) {
3138 ch_free( sie->si_logfilterstr.bv_val );
3140 if ( sie->si_base.bv_val ) {
3141 ch_free( sie->si_base.bv_val );
3143 if ( sie->si_logbase.bv_val ) {
3144 ch_free( sie->si_logbase.bv_val );
3146 if ( sie->si_attrs ) {
3148 while ( sie->si_attrs[i] != NULL ) {
3149 ch_free( sie->si_attrs[i] );
3152 ch_free( sie->si_attrs );
3154 if ( sie->si_exattrs ) {
3156 while ( sie->si_exattrs[i] != NULL ) {
3157 ch_free( sie->si_exattrs[i] );
3160 ch_free( sie->si_exattrs );
3162 if ( sie->si_anlist ) {
3164 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
3165 ch_free( sie->si_anlist[i].an_name.bv_val );
3168 ch_free( sie->si_anlist );
3170 if ( sie->si_exanlist ) {
3172 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
3173 ch_free( sie->si_exanlist[i].an_name.bv_val );
3176 ch_free( sie->si_exanlist );
3178 if ( sie->si_retryinterval ) {
3179 ch_free( sie->si_retryinterval );
3181 if ( sie->si_retrynum ) {
3182 ch_free( sie->si_retrynum );
3184 if ( sie->si_retrynum_init ) {
3185 ch_free( sie->si_retrynum_init );
3187 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3188 if ( sie->si_presentlist ) {
3189 avl_free( sie->si_presentlist, avl_ber_bvfree );
3191 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3192 struct nonpresent_entry* npe;
3193 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3194 LDAP_LIST_REMOVE( npe, npe_link );
3195 if ( npe->npe_name ) {
3196 if ( npe->npe_name->bv_val ) {
3197 ch_free( npe->npe_name->bv_val );
3199 ch_free( npe->npe_name );
3201 if ( npe->npe_nname ) {
3202 if ( npe->npe_nname->bv_val ) {
3203 ch_free( npe->npe_nname->bv_val );
3205 ch_free( npe->npe_nname );
3211 } while ( free_all && si_next );
3216 /* NOTE: used & documented in slapd.conf(5) */
3218 #define PROVIDERSTR "provider"
3219 #define SCHEMASTR "schemachecking"
3220 #define FILTERSTR "filter"
3221 #define SEARCHBASESTR "searchbase"
3222 #define SCOPESTR "scope"
3223 #define ATTRSONLYSTR "attrsonly"
3224 #define ATTRSSTR "attrs"
3225 #define TYPESTR "type"
3226 #define INTERVALSTR "interval"
3227 #define RETRYSTR "retry"
3228 #define SLIMITSTR "sizelimit"
3229 #define TLIMITSTR "timelimit"
3230 #define SYNCDATASTR "syncdata"
3231 #define LOGBASESTR "logbase"
3232 #define LOGFILTERSTR "logfilter"
3234 /* FIXME: undocumented */
3235 #define EXATTRSSTR "exattrs"
3236 #define MANAGEDSAITSTR "manageDSAit"
3239 #define GOT_ID 0x0001
3240 #define GOT_PROVIDER 0x0002
3241 #define GOT_BASE 0x0004
3244 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
3250 { BER_BVC("base"), LDAP_SCOPE_BASE },
3251 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
3252 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
3253 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
3254 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
3255 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
3256 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
3257 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
3261 static slap_verbmasks datamodes[] = {
3262 { BER_BVC("default"), SYNCDATA_DEFAULT },
3263 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3264 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3269 parse_syncrepl_line(
3277 for ( i = 1; i < c->argc; i++ ) {
3278 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3279 STRLENOF( IDSTR "=" ) ) )
3282 /* '\0' string terminator accounts for '=' */
3283 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3284 if ( lutil_atoi( &tmp, val ) != 0 ) {
3285 snprintf( c->msg, sizeof( c->msg ),
3286 "Error: parse_syncrepl_line: "
3287 "unable to parse syncrepl id \"%s\"", val );
3288 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3291 if ( tmp >= 1000 || tmp < 0 ) {
3292 snprintf( c->msg, sizeof( c->msg ),
3293 "Error: parse_syncrepl_line: "
3294 "syncrepl id %d is out of range [0..999]", tmp );
3295 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3299 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3301 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3302 STRLENOF( PROVIDERSTR "=" ) ) )
3304 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3305 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3306 gots |= GOT_PROVIDER;
3307 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3308 STRLENOF( SCHEMASTR "=" ) ) )
3310 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3311 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3312 si->si_schemachecking = 1;
3313 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3314 si->si_schemachecking = 0;
3316 si->si_schemachecking = 1;
3318 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
3319 STRLENOF( FILTERSTR "=" ) ) )
3321 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
3322 if ( si->si_filterstr.bv_val )
3323 ch_free( si->si_filterstr.bv_val );
3324 ber_str2bv( val, 0, 1, &si->si_filterstr );
3325 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
3326 STRLENOF( LOGFILTERSTR "=" ) ) )
3328 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
3329 if ( si->si_logfilterstr.bv_val )
3330 ch_free( si->si_logfilterstr.bv_val );
3331 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
3332 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
3333 STRLENOF( SEARCHBASESTR "=" ) ) )
3338 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
3339 if ( si->si_base.bv_val ) {
3340 ch_free( si->si_base.bv_val );
3342 ber_str2bv( val, 0, 0, &bv );
3343 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
3344 if ( rc != LDAP_SUCCESS ) {
3345 snprintf( c->msg, sizeof( c->msg ),
3346 "Invalid base DN \"%s\": %d (%s)",
3347 val, rc, ldap_err2string( rc ) );
3348 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3352 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
3353 STRLENOF( LOGBASESTR "=" ) ) )
3358 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
3359 if ( si->si_logbase.bv_val ) {
3360 ch_free( si->si_logbase.bv_val );
3362 ber_str2bv( val, 0, 0, &bv );
3363 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
3364 if ( rc != LDAP_SUCCESS ) {
3365 snprintf( c->msg, sizeof( c->msg ),
3366 "Invalid logbase DN \"%s\": %d (%s)",
3367 val, rc, ldap_err2string( rc ) );
3368 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3371 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
3372 STRLENOF( SCOPESTR "=" ) ) )
3375 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
3376 for ( j = 0; !BER_BVISNULL(&scopes[j].key); j++ ) {
3377 if (!strcasecmp( val, scopes[j].key.bv_val ) ) {
3378 si->si_scope = scopes[j].val;
3382 if ( BER_BVISNULL(&scopes[j].key) ) {
3383 snprintf( c->msg, sizeof( c->msg ),
3384 "Error: parse_syncrepl_line: "
3385 "unknown scope \"%s\"", val);
3386 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3389 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
3390 STRLENOF( ATTRSONLYSTR ) ) )
3392 si->si_attrsonly = 1;
3393 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
3394 STRLENOF( ATTRSSTR "=" ) ) )
3396 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
3397 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3399 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3400 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
3401 if ( si->si_anlist == NULL ) {
3402 ch_free( attr_fname );
3405 si->si_anfile = attr_fname;
3407 char *str, *s, *next;
3408 char delimstr[] = " ,\t";
3409 str = ch_strdup( val );
3410 for ( s = ldap_pvt_strtok( str, delimstr, &next );
3412 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
3414 if ( strlen(s) == 1 && *s == '*' ) {
3415 si->si_allattrs = 1;
3416 val[ s - str ] = delimstr[0];
3418 if ( strlen(s) == 1 && *s == '+' ) {
3419 si->si_allopattrs = 1;
3420 val [ s - str ] = delimstr[0];
3424 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
3425 if ( si->si_anlist == NULL ) {
3429 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
3430 STRLENOF( EXATTRSSTR "=" ) ) )
3432 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
3433 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3435 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3436 si->si_exanlist = file2anlist(
3437 si->si_exanlist, attr_fname, " ,\t" );
3438 if ( si->si_exanlist == NULL ) {
3439 ch_free( attr_fname );
3442 ch_free( attr_fname );
3444 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3445 if ( si->si_exanlist == NULL ) {
3449 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3450 STRLENOF( TYPESTR "=" ) ) )
3452 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3453 if ( !strncasecmp( val, "refreshOnly",
3454 STRLENOF("refreshOnly") ) )
3456 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3457 } else if ( !strncasecmp( val, "refreshAndPersist",
3458 STRLENOF("refreshAndPersist") ) )
3460 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3461 si->si_interval = 60;
3463 snprintf( c->msg, sizeof( c->msg ),
3464 "Error: parse_syncrepl_line: "
3465 "unknown sync type \"%s\"", val);
3466 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3469 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3470 STRLENOF( INTERVALSTR "=" ) ) )
3472 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3473 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3474 si->si_interval = 0;
3475 } else if ( strchr( val, ':' ) != NULL ) {
3476 char *next, *ptr = val;
3477 unsigned dd, hh, mm, ss;
3479 /* NOTE: the test for ptr[ 0 ] == '-'
3480 * should go before the call to strtoul() */
3481 dd = strtoul( ptr, &next, 10 );
3482 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3483 snprintf( c->msg, sizeof( c->msg ),
3484 "Error: parse_syncrepl_line: "
3485 "invalid interval \"%s\", unable to parse days", val );
3486 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3490 hh = strtoul( ptr, &next, 10 );
3491 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3492 snprintf( c->msg, sizeof( c->msg ),
3493 "Error: parse_syncrepl_line: "
3494 "invalid interval \"%s\", unable to parse hours", val );
3495 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3499 mm = strtoul( ptr, &next, 10 );
3500 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3501 snprintf( c->msg, sizeof( c->msg ),
3502 "Error: parse_syncrepl_line: "
3503 "invalid interval \"%s\", unable to parse minutes", val );
3504 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3508 ss = strtoul( ptr, &next, 10 );
3509 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3510 snprintf( c->msg, sizeof( c->msg ),
3511 "Error: parse_syncrepl_line: "
3512 "invalid interval \"%s\", unable to parse seconds", val );
3513 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3516 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3520 if ( lutil_parse_time( val, &t ) != 0 ) {
3521 snprintf( c->msg, sizeof( c->msg ),
3522 "Error: parse_syncrepl_line: "
3523 "invalid interval \"%s\"", val );
3524 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3527 si->si_interval = (time_t)t;
3529 if ( si->si_interval < 0 ) {
3530 snprintf( c->msg, sizeof( c->msg ),
3531 "Error: parse_syncrepl_line: "
3532 "invalid interval \"%ld\"",
3533 (long) si->si_interval);
3534 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3537 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3538 STRLENOF( RETRYSTR "=" ) ) )
3543 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3544 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3545 retry_list[0] = NULL;
3547 slap_str2clist( &retry_list, val, " ,\t" );
3549 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3552 snprintf( c->msg, sizeof( c->msg ),
3553 "Error: incomplete syncrepl retry list" );
3554 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3555 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3556 ch_free( retry_list[k] );
3558 ch_free( retry_list );
3561 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3562 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3563 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3564 for ( j = 0; j < n; j++ ) {
3566 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3567 snprintf( c->msg, sizeof( c->msg ),
3568 "Error: invalid retry interval \"%s\" (#%d)",
3569 retry_list[j*2], j );
3570 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3571 /* do some cleanup */
3574 si->si_retryinterval[j] = (time_t)t;
3575 if ( *retry_list[j*2+1] == '+' ) {
3576 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3577 si->si_retrynum[j] = RETRYNUM_FOREVER;
3581 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3582 || si->si_retrynum_init[j] <= 0 )
3584 snprintf( c->msg, sizeof( c->msg ),
3585 "Error: invalid initial retry number \"%s\" (#%d)",
3586 retry_list[j*2+1], j );
3587 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3588 /* do some cleanup */
3591 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3592 || si->si_retrynum[j] <= 0 )
3594 snprintf( c->msg, sizeof( c->msg ),
3595 "Error: invalid retry number \"%s\" (#%d)",
3596 retry_list[j*2+1], j );
3597 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3598 /* do some cleanup */
3603 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3604 si->si_retrynum[j] = RETRYNUM_TAIL;
3605 si->si_retryinterval[j] = 0;
3607 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3608 ch_free( retry_list[k] );
3610 ch_free( retry_list );
3611 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3612 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3614 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3615 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3616 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3618 snprintf( c->msg, sizeof( c->msg ),
3619 "invalid manageDSAit value \"%s\".\n",
3621 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3624 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3625 STRLENOF( SLIMITSTR "=") ) )
3627 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3628 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3631 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3632 snprintf( c->msg, sizeof( c->msg ),
3633 "invalid size limit value \"%s\".\n",
3635 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3638 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3639 STRLENOF( TLIMITSTR "=" ) ) )
3641 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3642 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3645 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3646 snprintf( c->msg, sizeof( c->msg ),
3647 "invalid time limit value \"%s\".\n",
3649 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3652 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3653 STRLENOF( SYNCDATASTR "=" ) ) )
3655 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3656 si->si_syncdata = verb_to_mask( val, datamodes );
3657 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3658 snprintf( c->msg, sizeof( c->msg ),
3659 "Error: parse_syncrepl_line: "
3660 "unable to parse \"%s\"\n", c->argv[ i ] );
3661 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3666 if ( gots != GOT_ALL ) {
3667 snprintf( c->msg, sizeof( c->msg ),
3668 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3669 gots & GOT_ID ? "" : " "IDSTR,
3670 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3671 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3672 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3686 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3687 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3688 "operations required for syncrepl", c->be->be_type );
3689 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3692 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
3693 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3694 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3697 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3700 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3704 si->si_bindconf.sb_tls = SB_TLS_OFF;
3705 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3706 si->si_schemachecking = 0;
3707 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3708 &si->si_filterstr );
3709 si->si_base.bv_val = NULL;
3710 si->si_scope = LDAP_SCOPE_SUBTREE;
3711 si->si_attrsonly = 0;
3712 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3713 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3714 si->si_attrs = NULL;
3715 si->si_allattrs = 0;
3716 si->si_allopattrs = 0;
3717 si->si_exattrs = NULL;
3718 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3719 si->si_interval = 86400;
3720 si->si_retryinterval = NULL;
3721 si->si_retrynum_init = NULL;
3722 si->si_retrynum = NULL;
3723 si->si_manageDSAit = 0;
3726 si->si_conn_setup = 0;
3728 si->si_presentlist = NULL;
3729 LDAP_LIST_INIT( &si->si_nonpresentlist );
3730 ldap_pvt_thread_mutex_init( &si->si_mutex );
3732 rc = parse_syncrepl_line( c, si );
3735 /* Must be LDAPv3 because we need controls */
3736 switch ( si->si_bindconf.sb_version ) {
3738 /* not explicitly set */
3739 si->si_bindconf.sb_version = LDAP_VERSION3;
3742 /* explicitly set */
3745 Debug( LDAP_DEBUG_ANY,
3746 "version %d incompatible with syncrepl\n",
3747 si->si_bindconf.sb_version, 0, 0 );
3748 syncinfo_free( si, 0 );
3753 if ( slapMode & SLAP_SERVER_MODE ) {
3754 Listener **l = slapd_get_listeners();
3757 /* check if URL points to current server. If so, ignore
3758 * this configuration. We require an exact match. Just
3759 * in case they really want to do this, they can vary
3760 * the case of the URL to allow it.
3762 if ( l && !SLAP_DBHIDDEN( c->be ) ) {
3764 for ( i=0; l[i]; i++ ) {
3765 if ( bvmatch( &l[i]->sl_url, &si->si_bindconf.sb_uri ) ) {
3773 init_syncrepl( si );
3774 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
3775 si->si_interval, do_syncrepl, si, "do_syncrepl",
3778 rc = config_sync_shadow( c ) ? -1 : 0;
3786 /* Use main slapd defaults */
3787 bindconf_tls_defaults( &si->si_bindconf );
3790 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3791 syncinfo_free( si, 0 );
3794 Debug( LDAP_DEBUG_CONFIG,
3795 "Config: ** successfully added syncrepl \"%s\"\n",
3796 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3797 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3798 if ( !si->si_schemachecking ) {
3799 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3801 if ( c->be->be_syncinfo ) {
3802 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
3804 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
3805 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
3807 si->si_next = c->be->be_syncinfo;
3808 c->be->be_syncinfo = si;
3814 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3816 struct berval bc, uri;
3817 char buf[BUFSIZ*2], *ptr;
3820 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3824 /* temporarily inhibit bindconf from printing URI */
3825 uri = si->si_bindconf.sb_uri;
3826 BER_BVZERO( &si->si_bindconf.sb_uri );
3827 si->si_bindconf.sb_version = 0;
3828 bindconf_unparse( &si->si_bindconf, &bc );
3829 si->si_bindconf.sb_uri = uri;
3830 si->si_bindconf.sb_version = LDAP_VERSION3;
3833 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03d " PROVIDERSTR "=%s",
3834 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3835 if ( ptr - buf >= sizeof( buf ) ) return;
3836 if ( !BER_BVISNULL( &bc ) ) {
3837 if ( WHATSLEFT <= bc.bv_len ) {
3841 ptr = lutil_strcopy( ptr, bc.bv_val );
3844 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
3845 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3846 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3847 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3850 if ( !BER_BVISNULL( &si->si_base ) ) {
3851 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3852 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3853 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3856 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
3857 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3858 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3859 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3862 if ( !BER_BVISNULL( &si->si_logbase ) ) {
3863 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3864 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3865 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3868 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3869 if ( si->si_scope == scopes[i].val ) {
3870 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3871 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3872 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3876 if ( si->si_attrsonly ) {
3877 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3878 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3880 if ( si->si_anfile ) {
3881 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3882 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3883 ptr = lutil_strcopy( ptr, si->si_anfile );
3885 } else if ( si->si_allattrs || si->si_allopattrs ||
3886 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
3890 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3891 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3893 /* FIXME: add check for overflow */
3894 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3895 if ( si->si_allattrs ) {
3896 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3897 if ( old != ptr ) *ptr++ = ',';
3900 if ( si->si_allopattrs ) {
3901 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3902 if ( old != ptr ) *ptr++ = ',';
3907 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3908 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3909 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3910 /* FIXME: add check for overflow */
3911 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3913 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3914 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3915 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3917 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3918 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3919 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3920 "refreshAndPersist" : "refreshOnly" );
3922 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3925 dd = si->si_interval;
3932 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3933 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3934 if ( ptr - buf >= sizeof( buf ) ) return;
3935 } else if ( si->si_retryinterval ) {
3937 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3938 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3939 for (i=0; si->si_retryinterval[i]; i++) {
3940 if ( space ) *ptr++ = ' ';
3942 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3943 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3946 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3948 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3952 if ( si->si_slimit ) {
3953 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3954 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3955 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3958 if ( si->si_tlimit ) {
3959 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3960 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3961 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3964 if ( si->si_syncdata ) {
3965 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3966 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3967 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3968 ptr = lutil_strcopy( ptr, bc.bv_val );
3971 bc.bv_len = ptr - buf;
3973 ber_dupbv( bv, &bc );
3977 syncrepl_config( ConfigArgs *c )
3979 if (c->op == SLAP_CONFIG_EMIT) {
3980 if ( c->be->be_syncinfo ) {
3984 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
3985 syncrepl_unparse( si, &bv );
3986 ber_bvarray_add( &c->rvalue_vals, &bv );
3991 } else if ( c->op == LDAP_MOD_DELETE ) {
3992 cookie_state *cs = NULL;
3993 if ( c->be->be_syncinfo ) {
3994 syncinfo_t *si, **sip;
3997 cs = c->be->be_syncinfo->si_cookieState;
3998 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
4000 if ( c->valx == -1 || i == c->valx ) {
4002 /* If the task is currently active, we have to leave
4003 * it running. It will exit on its own. This will only
4004 * happen when running on the cn=config DB.
4007 ldap_pvt_runqueue_isrunning( &slapd_rq, si->si_re ) ) {
4010 syncinfo_free( si, 0 );
4019 if ( !c->be->be_syncinfo ) {
4020 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
4022 ber_bvarray_free( cs->cs_vals );
4023 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
4029 if ( SLAP_SLURP_SHADOW( c->be ) ) {
4030 Debug(LDAP_DEBUG_ANY, "%s: "
4031 "syncrepl: database already shadowed.\n",
4035 return add_syncrepl( c );