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;
62 struct slap_backend_db *si_be;
66 slap_bindconf si_bindconf;
67 struct berval si_base;
68 struct berval si_logbase;
69 struct berval si_filterstr;
70 struct berval si_logfilterstr;
74 AttributeName *si_anlist;
75 AttributeName *si_exanlist;
80 int si_schemachecking;
81 int si_type; /* the active type */
82 int si_ctype; /* the configured type */
84 time_t *si_retryinterval;
85 int *si_retrynum_init;
87 struct sync_cookie si_syncCookie;
88 cookie_state *si_cookieState;
94 int si_refreshPresent;
98 Avlnode *si_presentlist;
100 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
101 ldap_pvt_thread_mutex_t si_mutex;
104 static int syncuuid_cmp( const void *, const void * );
105 static void avl_ber_bvfree( void * );
106 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
107 static int syncrepl_message_to_op(
108 syncinfo_t *, Operation *, LDAPMessage * );
109 static int syncrepl_message_to_entry(
110 syncinfo_t *, Operation *, LDAPMessage *,
111 Modifications **, Entry **, int );
112 static int syncrepl_entry(
113 syncinfo_t *, Operation*, Entry*,
114 Modifications**,int, struct berval*,
115 struct sync_cookie * );
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( struct slap_op *, struct slap_rep * );
124 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
125 static int null_callback( struct slap_op *, struct slap_rep * );
127 static AttributeDescription *sync_descs[4];
130 init_syncrepl(syncinfo_t *si)
133 char **attrs, **exattrs;
135 if ( !sync_descs[0] ) {
136 sync_descs[0] = slap_schema.si_ad_objectClass;
137 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
138 sync_descs[2] = slap_schema.si_ad_entryCSN;
139 sync_descs[3] = NULL;
142 if ( si->si_allattrs && si->si_allopattrs )
145 attrs = anlist2attrs( si->si_anlist );
148 if ( si->si_allattrs ) {
151 if ( !is_at_operational( at_find( attrs[i] ) ) ) {
152 for ( j = i; attrs[j] != NULL; j++ ) {
155 attrs[j] = attrs[j+1];
161 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
162 attrs[i] = ch_strdup("*");
165 } else if ( si->si_allopattrs ) {
168 if ( is_at_operational( at_find( attrs[i] ) ) ) {
169 for ( j = i; attrs[j] != NULL; j++ ) {
172 attrs[j] = attrs[j+1];
178 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
179 attrs[i] = ch_strdup("+");
183 for ( i = 0; sync_descs[i] != NULL; i++ ) {
186 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
187 for ( k = j; attrs[k] != NULL; k++ ) {
190 attrs[k] = attrs[k+1];
198 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
200 if ( si->si_allopattrs ) {
201 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ) );
203 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ) );
206 if ( attrs == NULL ) {
207 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
211 if ( si->si_allopattrs ) {
212 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
214 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
215 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
223 if ( si->si_allattrs == si->si_allopattrs ) {
224 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
225 attrs[i++] = ch_strdup( "*" );
226 attrs[i++] = ch_strdup( "+" );
227 } else if ( si->si_allattrs && !si->si_allopattrs ) {
228 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
229 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
230 attrs[i++] = ch_strdup( "*" );
231 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
232 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
234 } else if ( !si->si_allattrs && si->si_allopattrs ) {
235 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
236 attrs[i++] = ch_strdup( "+" );
237 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
242 si->si_attrs = attrs;
244 exattrs = anlist2attrs( si->si_exanlist );
247 for ( n = 0; exattrs[n] != NULL; n++ ) ;
249 for ( i = 0; sync_descs[i] != NULL; i++ ) {
251 while ( exattrs[j] != NULL ) {
252 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val ) ) {
253 ch_free( exattrs[j] );
254 for ( k = j; exattrs[k] != NULL; k++ ) {
255 exattrs[k] = exattrs[k+1];
263 for ( i = 0; exattrs[i] != NULL; i++ ) {
264 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
266 if ( ( oc = si->si_anlist[j].an_oc ) ) {
268 while ( oc->soc_required[k] ) {
269 if ( !strcmp( exattrs[i],
270 oc->soc_required[k]->sat_cname.bv_val ) ) {
271 ch_free( exattrs[i] );
272 for ( l = i; exattrs[l]; l++ ) {
273 exattrs[l] = exattrs[l+1];
283 for ( i = 0; exattrs[i] != NULL; i++ ) ;
286 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *) );
289 si->si_exattrs = exattrs;
292 typedef struct logschema {
294 struct berval ls_req;
295 struct berval ls_mod;
296 struct berval ls_newRdn;
297 struct berval ls_delRdn;
298 struct berval ls_newSup;
301 static logschema changelog_sc = {
303 BER_BVC("changeType"),
306 BER_BVC("deleteOldRDN"),
307 BER_BVC("newSuperior")
310 static logschema accesslog_sc = {
314 BER_BVC("reqNewRDN"),
315 BER_BVC("reqDeleteOldRDN"),
316 BER_BVC("reqNewSuperior")
324 BerElementBuffer berbuf;
325 BerElement *ber = (BerElement *)&berbuf;
326 LDAPControl c[2], *ctrls[3];
327 struct timeval timeout;
332 char **attrs, *lattrs[8];
337 /* setup LDAP SYNC control */
338 ber_init2( ber, NULL, LBER_USE_DER );
339 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
341 /* If we're using a log but we have no state, then fallback to
342 * normal mode for a full refresh.
344 if ( si->si_syncdata && !si->si_syncCookie.numcsns ) {
345 si->si_logstate = SYNCLOG_FALLBACK;
348 /* Use the log parameters if we're in log mode */
349 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
351 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
355 lattrs[0] = ls->ls_dn.bv_val;
356 lattrs[1] = ls->ls_req.bv_val;
357 lattrs[2] = ls->ls_mod.bv_val;
358 lattrs[3] = ls->ls_newRdn.bv_val;
359 lattrs[4] = ls->ls_delRdn.bv_val;
360 lattrs[5] = ls->ls_newSup.bv_val;
361 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
365 base = si->si_logbase.bv_val;
366 filter = si->si_logfilterstr.bv_val;
369 scope = LDAP_SCOPE_SUBTREE;
372 base = si->si_base.bv_val;
373 filter = si->si_filterstr.bv_val;
374 attrs = si->si_attrs;
375 attrsonly = si->si_attrsonly;
376 scope = si->si_scope;
378 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
379 si->si_type = LDAP_SYNC_REFRESH_ONLY;
381 si->si_type = si->si_ctype;
384 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
386 ber_printf( ber, "{eOb}",
387 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
389 ber_printf( ber, "{eb}",
390 abs(si->si_type), rhint );
393 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 ) ) == LBER_ERROR ) {
398 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
399 c[0].ldctl_iscritical = si->si_type < 0;
402 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
403 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
404 c[1].ldctl_value = si->si_bindconf.sb_authzId;
405 c[1].ldctl_iscritical = 1;
412 timeout.tv_sec = si->si_tlimit;
415 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
416 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
417 si->si_slimit, &msgid );
428 int cmdline_cookie_found = 0;
430 struct sync_cookie *sc = NULL;
435 rc = slap_client_connect( &si->si_ld, &si->si_bindconf );
436 if ( rc != LDAP_SUCCESS ) {
439 op->o_protocol = LDAP_VERSION3;
441 /* Set SSF to strongest of TLS, SASL SSFs */
444 op->o_transport_ssf = 0;
446 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
447 == LDAP_SUCCESS && ssl != NULL )
449 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
451 #endif /* HAVE_TLS */
452 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
453 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
454 ? op->o_sasl_ssf : op->o_tls_ssf;
457 if ( BER_BVISNULL( &si->si_syncCookie.octet_str ) ) {
460 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
461 if ( !si->si_cookieState->cs_num ) {
462 /* get contextCSN shadow replica from database */
463 BerVarray csn = NULL;
464 void *ctx = op->o_tmpmemctx;
466 op->o_req_ndn = op->o_bd->be_nsuffix[0];
467 op->o_req_dn = op->o_req_ndn;
469 /* try to read stored contextCSN */
470 op->o_tmpmemctx = NULL;
471 backend_attribute( op, NULL, &op->o_req_ndn,
472 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
473 op->o_tmpmemctx = ctx;
475 si->si_cookieState->cs_vals = csn;
476 for (i=0; !BER_BVISNULL( &csn[i] ); i++);
477 si->si_cookieState->cs_num = i;
478 si->si_cookieState->cs_sids = slap_parse_csn_sids( csn, i );
481 if ( si->si_cookieState->cs_num ) {
482 ber_bvarray_free( si->si_syncCookie.ctxcsn );
483 if ( ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
484 si->si_cookieState->cs_vals, NULL )) {
488 si->si_syncCookie.numcsns = si->si_cookieState->cs_num;
489 si->si_syncCookie.sids = ch_malloc( si->si_cookieState->cs_num *
491 for ( i=0; i<si->si_syncCookie.numcsns; i++ )
492 si->si_syncCookie.sids[i] = si->si_cookieState->cs_sids[i];
494 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
496 si->si_syncCookie.rid = si->si_rid;
498 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
499 if ( si->si_rid == sc->rid ) {
500 cmdline_cookie_found = 1;
505 if ( cmdline_cookie_found ) {
506 /* cookie is supplied in the command line */
508 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
510 /* ctxcsn wasn't parsed yet, do it now */
511 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
513 /* if cmdline cookie does not have ctxcsn */
514 /* component, set it to an initial value */
515 slap_init_sync_cookie_ctxcsn( sc );
517 slap_sync_cookie_free( &si->si_syncCookie, 0 );
518 slap_dup_sync_cookie( &si->si_syncCookie, sc );
519 slap_sync_cookie_free( sc, 1 );
522 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
523 si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
525 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
527 if ( si->si_cookieState->cs_num > 1 && si->si_cookieAge !=
528 si->si_cookieState->cs_age ) {
531 for (i=0; !BER_BVISNULL( &si->si_syncCookie.ctxcsn[i] ); i++) {
532 /* bogus, just dup everything */
533 if ( si->si_syncCookie.sids[i] == -1 ) {
534 ber_bvarray_free( si->si_syncCookie.ctxcsn );
535 ber_bvarray_dup_x( &si->si_syncCookie.ctxcsn,
536 si->si_cookieState->cs_vals, NULL );
539 for (j=0; j<si->si_cookieState->cs_num; j++) {
540 if ( si->si_syncCookie.sids[i] !=
541 si->si_cookieState->cs_sids[j] )
543 ber_bvreplace( &si->si_syncCookie.ctxcsn[i],
544 &si->si_cookieState->cs_vals[j] );
549 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
552 rc = ldap_sync_search( si, op->o_tmpmemctx );
554 if( rc != LDAP_SUCCESS ) {
555 Debug( LDAP_DEBUG_ANY, "do_syncrep1: %s "
556 "ldap_search_ext: %s (%d)\n",
557 si->si_ridtxt, ldap_err2string( rc ), rc );
563 ldap_unbind_ext( si->si_ld, NULL, NULL );
572 compare_csns( struct sync_cookie *sc1, struct sync_cookie *sc2, int *which )
579 for (i=0; !BER_BVISNULL( &sc1->ctxcsn[i] ); i++) {
580 for (j=0; !BER_BVISNULL( &sc2->ctxcsn[j] ); j++) {
581 if ( sc1->sids[i] != sc2->sids[j] )
583 value_match( &match, slap_schema.si_ad_entryCSN,
584 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
585 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
586 &sc1->ctxcsn[i], &sc2->ctxcsn[i], &text );
602 LDAPControl **rctrls = NULL;
605 BerElementBuffer berbuf;
606 BerElement *ber = (BerElement *)&berbuf;
608 LDAPMessage *res = NULL;
609 LDAPMessage *msg = NULL;
612 struct berval *retdata = NULL;
617 struct berval syncUUID = BER_BVNULL;
618 struct sync_cookie syncCookie = { NULL };
619 struct sync_cookie syncCookie_req = { NULL };
620 struct berval cookie = BER_BVNULL;
626 Modifications *modlist = NULL;
630 struct timeval *tout_p = NULL;
631 struct timeval tout = { 0, 0 };
633 int refreshDeletes = 0;
635 BerVarray syncUUIDs = NULL;
638 if ( slapd_shutdown ) {
643 ber_init2( ber, NULL, LBER_USE_DER );
644 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
646 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 %s\n", si->si_ridtxt, 0, 0 );
648 psub = &si->si_be->be_nsuffix[0];
650 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
652 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
658 while ( ( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
659 tout_p, &res ) ) > 0 )
661 if ( slapd_shutdown ) {
665 for( msg = ldap_first_message( si->si_ld, res );
667 msg = ldap_next_message( si->si_ld, msg ) )
669 if ( slapd_shutdown ) {
673 switch( ldap_msgtype( msg ) ) {
674 case LDAP_RES_SEARCH_ENTRY:
675 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
676 /* we can't work without the control */
679 /* NOTE: make sure we use the right one;
680 * a better approach would be to run thru
681 * the whole list and take care of all */
682 rctrlp = ldap_find_control( LDAP_CONTROL_SYNC_STATE, rctrls );
684 if ( rctrlp == NULL ) {
685 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
686 "got search entry without "
687 "Sync State control\n", si->si_ridtxt, 0, 0 );
691 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
692 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
693 /* FIXME: what if syncUUID is NULL or empty?
694 * (happens with back-sql...) */
695 if ( BER_BVISEMPTY( &syncUUID ) ) {
696 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
697 "got empty syncUUID\n", si->si_ridtxt, 0, 0 );
698 ldap_controls_free( rctrls );
702 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
703 ber_scanf( ber, /*"{"*/ "m}", &cookie );
704 if ( !BER_BVISNULL( &cookie ) ) {
705 ch_free( syncCookie.octet_str.bv_val );
706 ber_dupbv( &syncCookie.octet_str, &cookie );
708 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
710 slap_parse_sync_cookie( &syncCookie, NULL );
714 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
716 if ( ( rc = syncrepl_message_to_op( si, op, msg ) ) == LDAP_SUCCESS &&
719 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
721 } else if ( ( rc = syncrepl_message_to_entry( si, op, msg,
722 &modlist, &entry, syncstate ) ) == LDAP_SUCCESS )
724 if ( ( rc = syncrepl_entry( si, op, entry, &modlist,
725 syncstate, &syncUUID, &syncCookie_req ) ) == LDAP_SUCCESS &&
728 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
731 ldap_controls_free( rctrls );
733 slap_mods_free( modlist, 1 );
739 case LDAP_RES_SEARCH_REFERENCE:
740 Debug( LDAP_DEBUG_ANY,
741 "do_syncrep2: %s reference received error\n",
742 si->si_ridtxt, 0, 0 );
745 case LDAP_RES_SEARCH_RESULT:
746 Debug( LDAP_DEBUG_SYNC,
747 "do_syncrep2: %s LDAP_RES_SEARCH_RESULT\n",
748 si->si_ridtxt, 0, 0 );
749 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
751 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
752 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
753 /* map old result code to registered code */
754 err = LDAP_SYNC_REFRESH_REQUIRED;
757 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
758 if ( si->si_logstate == SYNCLOG_LOGGING ) {
759 si->si_logstate = SYNCLOG_FALLBACK;
766 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
768 ber_scanf( ber, "{" /*"}"*/);
769 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
770 ber_scanf( ber, "m", &cookie );
771 if ( !BER_BVISNULL( &cookie ) ) {
772 ch_free( syncCookie.octet_str.bv_val );
773 ber_dupbv( &syncCookie.octet_str, &cookie);
775 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
777 slap_parse_sync_cookie( &syncCookie, NULL );
780 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
782 ber_scanf( ber, "b", &refreshDeletes );
784 ber_scanf( ber, /*"{"*/ "}" );
786 if ( !syncCookie_req.ctxcsn ) {
788 } else if ( !syncCookie.ctxcsn ) {
791 match = compare_csns( &syncCookie_req, &syncCookie, &m );
794 ldap_controls_free( rctrls );
796 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
797 /* FIXME : different error behaviors according to
798 * 1) err code : LDAP_BUSY ...
799 * 2) on err policy : stop service, stop sync, retry
801 if ( refreshDeletes == 0 && match < 0 &&
802 err == LDAP_SUCCESS )
804 syncrepl_del_nonpresent( op, si, NULL,
805 &syncCookie.ctxcsn[m] );
807 avl_free( si->si_presentlist, avl_ber_bvfree );
808 si->si_presentlist = NULL;
811 if ( syncCookie.ctxcsn && match < 0 && err == LDAP_SUCCESS )
813 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
815 if ( err == LDAP_SUCCESS
816 && si->si_logstate == SYNCLOG_FALLBACK ) {
817 si->si_logstate = SYNCLOG_LOGGING;
818 rc = LDAP_SYNC_REFRESH_REQUIRED;
825 case LDAP_RES_INTERMEDIATE:
826 rc = ldap_parse_intermediate( si->si_ld, msg,
827 &retoid, &retdata, NULL, 0 );
828 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
829 ber_init2( ber, retdata, LBER_USE_DER );
831 switch ( si_tag = ber_peek_tag( ber, &len ) ) {
833 case LDAP_TAG_SYNC_NEW_COOKIE:
834 Debug( LDAP_DEBUG_SYNC,
835 "do_syncrep2: %s %s - %s\n",
837 "LDAP_RES_INTERMEDIATE",
839 ber_scanf( ber, "tm", &tag, &cookie );
841 case LDAP_TAG_SYNC_REFRESH_DELETE:
842 case LDAP_TAG_SYNC_REFRESH_PRESENT:
843 Debug( LDAP_DEBUG_SYNC,
844 "do_syncrep2: %s %s - %s\n",
846 "LDAP_RES_INTERMEDIATE",
847 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
848 "REFRESH_PRESENT" : "REFRESH_DELETE" );
849 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
850 si->si_refreshDelete = 1;
852 si->si_refreshPresent = 1;
854 ber_scanf( ber, "t{" /*"}"*/, &tag );
855 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
857 ber_scanf( ber, "m", &cookie );
858 if ( !BER_BVISNULL( &cookie ) ) {
859 ch_free( syncCookie.octet_str.bv_val );
860 ber_dupbv( &syncCookie.octet_str, &cookie );
862 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
864 slap_parse_sync_cookie( &syncCookie, NULL );
867 if ( ber_peek_tag( ber, &len ) ==
868 LDAP_TAG_REFRESHDONE )
870 ber_scanf( ber, "b", &refreshDone );
872 ber_scanf( ber, /*"{"*/ "}" );
874 case LDAP_TAG_SYNC_ID_SET:
875 Debug( LDAP_DEBUG_SYNC,
876 "do_syncrep2: %s %s - %s\n",
878 "LDAP_RES_INTERMEDIATE",
880 ber_scanf( ber, "t{" /*"}"*/, &tag );
881 if ( ber_peek_tag( ber, &len ) ==
882 LDAP_TAG_SYNC_COOKIE )
884 ber_scanf( ber, "m", &cookie );
885 if ( !BER_BVISNULL( &cookie ) ) {
886 ch_free( syncCookie.octet_str.bv_val );
887 ber_dupbv( &syncCookie.octet_str, &cookie );
889 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
891 slap_parse_sync_cookie( &syncCookie, NULL );
892 compare_csns( &syncCookie_req, &syncCookie, &m );
895 if ( ber_peek_tag( ber, &len ) ==
896 LDAP_TAG_REFRESHDELETES )
898 ber_scanf( ber, "b", &refreshDeletes );
900 ber_scanf( ber, "[W]", &syncUUIDs );
901 ber_scanf( ber, /*"{"*/ "}" );
902 if ( refreshDeletes ) {
903 syncrepl_del_nonpresent( op, si, syncUUIDs,
904 &syncCookie.ctxcsn[m] );
905 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
908 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
909 struct berval *syncuuid_bv;
910 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
911 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
912 if ( avl_insert( &si->si_presentlist,
913 (caddr_t) syncuuid_bv,
914 syncuuid_cmp, avl_dup_error ) )
916 ber_bvfree( syncuuid_bv );
919 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
921 slap_sync_cookie_free( &syncCookie, 0 );
924 Debug( LDAP_DEBUG_ANY,
925 "do_syncrep2: %s unknown syncinfo tag (%ld)\n",
926 si->si_ridtxt, (long) si_tag, 0 );
927 ldap_memfree( retoid );
928 ber_bvfree( retdata );
932 if ( !syncCookie_req.ctxcsn ) {
934 } else if ( !syncCookie.ctxcsn ) {
937 match = compare_csns( &syncCookie_req, &syncCookie, &m );
941 if ( si->si_refreshPresent == 1 ) {
942 syncrepl_del_nonpresent( op, si, NULL,
943 &syncCookie.ctxcsn[m] );
946 if ( syncCookie.ctxcsn )
948 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
952 ldap_memfree( retoid );
953 ber_bvfree( retdata );
957 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
958 "unknown intermediate response (%d)\n",
959 si->si_ridtxt, rc, 0 );
960 ldap_memfree( retoid );
961 ber_bvfree( retdata );
967 Debug( LDAP_DEBUG_ANY, "do_syncrep2: %s "
968 "unknown message\n", si->si_ridtxt, 0, 0 );
972 if ( !BER_BVISNULL( &syncCookie.octet_str ) ) {
973 slap_sync_cookie_free( &syncCookie_req, 0 );
974 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
975 slap_sync_cookie_free( &syncCookie, 0 );
985 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
986 errstr = ldap_err2string( rc );
988 Debug( LDAP_DEBUG_ANY,
989 "do_syncrep2: %s %s\n", si->si_ridtxt, errstr, 0 );
993 slap_sync_cookie_free( &syncCookie, 0 );
994 slap_sync_cookie_free( &syncCookie_req, 0 );
996 if ( res ) ldap_msgfree( res );
998 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
999 if ( si->si_conn_setup ) {
1001 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1002 connection_client_stop( s );
1003 si->si_conn_setup = 0;
1005 ldap_unbind_ext( si->si_ld, NULL, NULL );
1017 struct re_s* rtask = arg;
1018 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1019 Connection conn = {0};
1020 OperationBuffer opbuf;
1022 int rc = LDAP_SUCCESS;
1028 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl %s\n", si->si_ridtxt, 0, 0 );
1033 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1035 switch( abs( si->si_type ) ) {
1036 case LDAP_SYNC_REFRESH_ONLY:
1037 case LDAP_SYNC_REFRESH_AND_PERSIST:
1040 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1044 if ( slapd_shutdown ) {
1046 if ( si->si_conn_setup ) {
1047 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1048 connection_client_stop( s );
1049 si->si_conn_setup = 0;
1051 ldap_unbind_ext( si->si_ld, NULL, NULL );
1054 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1058 op = (Operation *) &opbuf;
1059 connection_fake_init( &conn, op, ctx );
1061 /* use global malloc for now */
1062 op->o_tmpmemctx = NULL;
1063 op->o_tmpmfuncs = &ch_mfuncs;
1065 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1066 op->o_bd = be = si->si_be;
1067 op->o_dn = op->o_bd->be_rootdn;
1068 op->o_ndn = op->o_bd->be_rootndn;
1069 if ( !si->si_schemachecking )
1070 op->o_no_schema_check = 1;
1072 /* Establish session, do search */
1074 si->si_refreshDelete = 0;
1075 si->si_refreshPresent = 0;
1076 rc = do_syncrep1( op, si );
1080 /* Process results */
1081 if ( rc == LDAP_SUCCESS ) {
1082 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1084 rc = do_syncrep2( op, si );
1085 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1086 rc = ldap_sync_search( si, op->o_tmpmemctx );
1090 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1091 /* If we succeeded, enable the connection for further listening.
1092 * If we failed, tear down the connection and reschedule.
1094 if ( rc == LDAP_SUCCESS ) {
1095 if ( si->si_conn_setup ) {
1096 connection_client_enable( s );
1098 rc = connection_client_setup( s, do_syncrepl, arg );
1100 si->si_conn_setup = 1;
1102 } else if ( si->si_conn_setup ) {
1106 if ( rc == -2 ) rc = 0;
1110 /* At this point, we have 4 cases:
1111 * 1) for any hard failure, give up and remove this task
1112 * 2) for ServerDown, reschedule this task to run
1113 * 3) for Refresh and Success, reschedule to run
1114 * 4) for Persist and Success, reschedule to defer
1116 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1118 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask ) ) {
1119 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1123 connection_client_stop( s );
1126 if ( rc == LDAP_SUCCESS ) {
1127 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1130 rtask->interval.tv_sec = si->si_interval;
1131 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1132 if ( si->si_retrynum ) {
1133 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1134 si->si_retrynum[i] = si->si_retrynum_init[i];
1136 si->si_retrynum[i] = RETRYNUM_TAIL;
1139 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1140 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1144 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1145 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1146 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1147 if ( si->si_retrynum[i] > 0 )
1148 si->si_retrynum[i]--;
1149 rtask->interval.tv_sec = si->si_retryinterval[i];
1150 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1151 slap_wake_listener();
1155 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1156 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1161 static slap_verbmasks modops[] = {
1162 { BER_BVC("add"), LDAP_REQ_ADD },
1163 { BER_BVC("delete"), LDAP_REQ_DELETE },
1164 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1165 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1169 static Modifications *
1170 syncrepl_accesslog_mods(
1177 AttributeDescription *ad;
1178 struct berval bv, bv2;
1180 Modifications *mod = NULL, *modlist = NULL, **modtail;
1185 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1189 colon = ber_bvchr( &bv, ':' );
1195 bv.bv_len = colon - bv.bv_val;
1196 if ( slap_bv2ad( &bv, &ad, &text ) ) {
1201 /* Ignore dynamically generated attrs */
1202 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1206 /* Ignore excluded attrs */
1207 if ( ldap_charray_inlist( si->si_exattrs,
1208 ad->ad_type->sat_cname.bv_val ) )
1214 case '+': op = LDAP_MOD_ADD; break;
1215 case '-': op = LDAP_MOD_DELETE; break;
1216 case '=': op = LDAP_MOD_REPLACE; break;
1217 case '#': op = LDAP_MOD_INCREMENT; break;
1221 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1222 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1225 mod->sml_next = NULL;
1227 mod->sml_type = ad->ad_cname;
1228 mod->sml_values = NULL;
1229 mod->sml_nvalues = NULL;
1232 modtail = &mod->sml_next;
1234 if ( colon[2] == ' ' ) {
1235 bv.bv_val = colon + 3;
1236 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1237 ber_dupbv( &bv2, &bv );
1238 ber_bvarray_add( &mod->sml_values, &bv2 );
1244 static Modifications *
1245 syncrepl_changelog_mods(
1250 return NULL; /* FIXME */
1254 syncrepl_message_to_op(
1260 BerElement *ber = NULL;
1261 Modifications *modlist = NULL;
1263 SlapReply rs = { REP_RESULT };
1264 slap_callback cb = { NULL, null_callback, NULL, NULL };
1267 char txtbuf[SLAP_TEXT_BUFLEN];
1268 size_t textlen = sizeof txtbuf;
1270 struct berval bdn, dn = BER_BVNULL, ndn;
1271 struct berval bv, *bvals = NULL;
1272 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1273 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1274 psup = BER_BVNULL, nsup = BER_BVNULL;
1275 int rc, deleteOldRdn = 0, freeReqDn = 0;
1277 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1278 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1279 "Message type should be entry (%d)",
1280 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1284 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1289 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1291 if ( rc != LDAP_SUCCESS ) {
1292 Debug( LDAP_DEBUG_ANY,
1293 "syncrepl_message_to_op: %s dn get failed (%d)",
1294 si->si_ridtxt, rc, 0 );
1298 op->o_tag = LBER_DEFAULT;
1300 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ) )
1302 if ( bv.bv_val == NULL )
1305 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn ) ) {
1307 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1308 ber_dupbv( &op->o_req_dn, &dn );
1309 ber_dupbv( &op->o_req_ndn, &ndn );
1310 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1311 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1313 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req ) ) {
1314 int i = verb_to_mask( bvals[0].bv_val, modops );
1316 Debug( LDAP_DEBUG_ANY,
1317 "syncrepl_message_to_op: %s unknown op %s",
1318 si->si_ridtxt, bvals[0].bv_val, 0 );
1323 op->o_tag = modops[i].mask;
1324 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod ) ) {
1325 /* Parse attribute into modlist */
1326 if ( si->si_syncdata == SYNCDATA_ACCESSLOG ) {
1327 modlist = syncrepl_accesslog_mods( si, bvals );
1329 modlist = syncrepl_changelog_mods( si, bvals );
1331 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn ) ) {
1333 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn ) ) {
1334 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ) ) {
1337 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup ) ) {
1339 } else if ( !ber_bvstrcasecmp( &bv,
1340 &slap_schema.si_ad_entryCSN->ad_cname ) )
1342 slap_queue_csn( op, bvals );
1347 /* If we didn't get a mod type or a target DN, bail out */
1348 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn ) ) {
1353 op->o_callback = &cb;
1354 slap_op_time( &op->o_time, &op->o_tincr );
1356 switch( op->o_tag ) {
1358 case LDAP_REQ_MODIFY:
1359 /* If we didn't get required data, bail */
1360 if ( !modlist ) goto done;
1362 rc = slap_mods_check( op, modlist, &text, txtbuf, textlen, NULL );
1364 if ( rc != LDAP_SUCCESS ) {
1365 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1366 "mods check (%s)\n",
1367 si->si_ridtxt, text, 0 );
1371 if ( op->o_tag == LDAP_REQ_ADD ) {
1372 Entry *e = entry_alloc();
1374 op->ora_e->e_name = op->o_req_dn;
1375 op->ora_e->e_nname = op->o_req_ndn;
1377 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1378 if( rc != LDAP_SUCCESS ) {
1379 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: %s "
1380 "mods2entry (%s)\n",
1381 si->si_ridtxt, text, 0 );
1383 rc = op->o_bd->be_add( op, &rs );
1384 Debug( LDAP_DEBUG_SYNC,
1385 "syncrepl_message_to_op: %s be_add %s (%d)\n",
1386 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1388 if ( e == op->ora_e )
1389 be_entry_release_w( op, op->ora_e );
1391 op->orm_modlist = modlist;
1392 rc = op->o_bd->be_modify( op, &rs );
1393 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1394 "syncrepl_message_to_op: %s be_modify %s (%d)\n",
1395 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1398 case LDAP_REQ_MODRDN:
1399 if ( BER_BVISNULL( &rdn ) ) goto done;
1401 if ( rdnPretty( NULL, &rdn, &prdn, NULL ) ) {
1404 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ) ) {
1407 if ( !BER_BVISNULL( &sup ) ) {
1408 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ) ) {
1411 op->orr_newSup = &psup;
1412 op->orr_nnewSup = ⊅
1414 op->orr_newSup = NULL;
1415 op->orr_nnewSup = NULL;
1417 op->orr_newrdn = prdn;
1418 op->orr_nnewrdn = nrdn;
1419 op->orr_deleteoldrdn = deleteOldRdn;
1420 op->orr_modlist = NULL;
1421 if ( slap_modrdn2mods( op, &rs ) ) {
1425 /* Append modlist for operational attrs */
1429 for ( m = op->orr_modlist; m->sml_next; m = m->sml_next )
1431 m->sml_next = modlist;
1434 rc = op->o_bd->be_modrdn( op, &rs );
1435 slap_mods_free( op->orr_modlist, 1 );
1436 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1437 "syncrepl_message_to_op: %s be_modrdn %s (%d)\n",
1438 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1440 case LDAP_REQ_DELETE:
1441 rc = op->o_bd->be_delete( op, &rs );
1442 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1443 "syncrepl_message_to_op: %s be_delete %s (%d)\n",
1444 si->si_ridtxt, op->o_req_dn.bv_val, rc );
1448 slap_graduate_commit_csn( op );
1449 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1450 BER_BVZERO( &op->o_csn );
1452 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1454 if ( !BER_BVISNULL( &rdn ) ) {
1455 if ( !BER_BVISNULL( &nsup ) ) {
1456 ch_free( nsup.bv_val );
1458 if ( !BER_BVISNULL( &psup ) ) {
1459 ch_free( psup.bv_val );
1461 if ( !BER_BVISNULL( &nrdn ) ) {
1462 ch_free( nrdn.bv_val );
1464 if ( !BER_BVISNULL( &prdn ) ) {
1465 ch_free( prdn.bv_val );
1469 ch_free( op->o_req_ndn.bv_val );
1470 ch_free( op->o_req_dn.bv_val );
1477 syncrepl_message_to_entry(
1481 Modifications **modlist,
1487 BerElement *ber = NULL;
1490 Modifications **modtail = modlist;
1493 char txtbuf[SLAP_TEXT_BUFLEN];
1494 size_t textlen = sizeof txtbuf;
1496 struct berval bdn = BER_BVNULL, dn, ndn;
1501 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1502 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s "
1503 "Message type should be entry (%d)",
1504 si->si_ridtxt, ldap_msgtype( msg ), 0 );
1508 op->o_tag = LDAP_REQ_ADD;
1510 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1511 if ( rc != LDAP_SUCCESS ) {
1512 Debug( LDAP_DEBUG_ANY,
1513 "syncrepl_message_to_entry: %s dn get failed (%d)",
1514 si->si_ridtxt, rc, 0 );
1518 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1519 /* NOTE: this could be done even before decoding the DN,
1520 * although encoding errors wouldn't be detected */
1525 if ( entry == NULL ) {
1530 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1531 ber_dupbv( &op->o_req_dn, &dn );
1532 ber_dupbv( &op->o_req_ndn, &ndn );
1533 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1534 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1536 is_ctx = dn_match( &op->o_req_ndn, &op->o_bd->be_nsuffix[0] );
1539 e->e_name = op->o_req_dn;
1540 e->e_nname = op->o_req_ndn;
1542 while ( ber_remaining( ber ) ) {
1543 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1544 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1549 /* Drop all updates to the contextCSN of the context entry
1552 if ( is_ctx && !strcasecmp( tmp.sml_type.bv_val,
1553 slap_schema.si_ad_contextCSN->ad_cname.bv_val )) {
1554 ber_bvarray_free( tmp.sml_values );
1558 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1560 mod->sml_op = LDAP_MOD_REPLACE;
1562 mod->sml_next = NULL;
1563 mod->sml_desc = NULL;
1564 mod->sml_type = tmp.sml_type;
1565 mod->sml_values = tmp.sml_values;
1566 mod->sml_nvalues = NULL;
1569 modtail = &mod->sml_next;
1572 if ( *modlist == NULL ) {
1573 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s no attributes\n",
1574 si->si_ridtxt, 0, 0 );
1579 rc = slap_mods_check( op, *modlist, &text, txtbuf, textlen, NULL );
1581 if ( rc != LDAP_SUCCESS ) {
1582 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods check (%s)\n",
1583 si->si_ridtxt, text, 0 );
1587 /* Strip out dynamically generated attrs */
1588 for ( modtail = modlist; *modtail ; ) {
1590 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1591 *modtail = mod->sml_next;
1592 slap_mod_free( &mod->sml_mod, 0 );
1595 modtail = &mod->sml_next;
1599 /* Strip out attrs in exattrs list */
1600 for ( modtail = modlist; *modtail ; ) {
1602 if ( ldap_charray_inlist( si->si_exattrs,
1603 mod->sml_desc->ad_type->sat_cname.bv_val ) )
1605 *modtail = mod->sml_next;
1606 slap_mod_free( &mod->sml_mod, 0 );
1609 modtail = &mod->sml_next;
1613 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1614 if( rc != LDAP_SUCCESS ) {
1615 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: %s mods2entry (%s)\n",
1616 si->si_ridtxt, text, 0 );
1621 if ( rc != LDAP_SUCCESS ) {
1632 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1634 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1635 * entry if a previous refresh was interrupted before sending us a new
1636 * context state. We try to compare the new entry to the existing entry
1637 * and ignore the new entry if they are the same.
1639 * Also, we may get an update where the entryDN has changed, due to
1640 * a ModDn on the provider. We detect this as well, so we can issue
1641 * the corresponding operation locally.
1643 * In the case of a modify, we get a list of all the attributes
1644 * in the original entry. Rather than deleting the entry and re-adding it,
1645 * we issue a Modify request that deletes all the attributes and adds all
1646 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1649 * We otherwise distinguish ModDN from Modify; in the case of
1650 * a ModDN we just use the CSN, modifyTimestamp and modifiersName
1651 * operational attributes from the entry, and do a regular ModDN.
1653 typedef struct dninfo {
1657 int renamed; /* Was an existing entry renamed? */
1658 int delOldRDN; /* Was old RDN deleted? */
1659 Modifications **modlist; /* the modlist we received */
1660 Modifications *mods; /* the modlist we compared */
1668 Modifications** modlist,
1670 struct berval* syncUUID,
1671 struct sync_cookie* syncCookie_req )
1673 Backend *be = op->o_bd;
1674 slap_callback cb = { NULL, NULL, NULL, NULL };
1675 struct berval *syncuuid_bv = NULL;
1676 struct berval syncUUID_strrep = BER_BVNULL;
1677 struct berval uuid_bv = BER_BVNULL;
1679 SlapReply rs_search = {REP_RESULT};
1680 SlapReply rs_delete = {REP_RESULT};
1681 SlapReply rs_add = {REP_RESULT};
1682 SlapReply rs_modify = {REP_RESULT};
1684 #ifdef LDAP_COMP_MATCH
1685 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1687 AttributeAssertion ava = { NULL, BER_BVNULL };
1689 int rc = LDAP_SUCCESS;
1691 struct berval pdn = BER_BVNULL;
1695 switch( syncstate ) {
1696 case LDAP_SYNC_PRESENT:
1697 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1699 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1702 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1704 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1706 case LDAP_SYNC_DELETE:
1707 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1709 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1711 case LDAP_SYNC_MODIFY:
1712 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s %s\n",
1714 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1717 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: %s %s\n",
1719 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1722 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ) ) {
1723 if ( !si->si_refreshPresent ) {
1724 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1725 if ( avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1726 syncuuid_cmp, avl_dup_error ) )
1728 ber_bvfree( syncuuid_bv );
1734 if ( syncstate == LDAP_SYNC_PRESENT ) {
1736 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1737 if ( entry == NULL ) {
1742 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1743 if ( syncstate != LDAP_SYNC_DELETE ) {
1744 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1747 /* add if missing */
1748 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1749 &syncUUID_strrep, syncUUID );
1751 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1752 /* replace only if necessary */
1753 if ( a->a_nvals != a->a_vals ) {
1754 ber_memfree( a->a_nvals[0].bv_val );
1755 ber_dupbv( &a->a_nvals[0], syncUUID );
1757 ber_memfree( a->a_vals[0].bv_val );
1758 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1762 f.f_choice = LDAP_FILTER_EQUALITY;
1764 ava.aa_desc = slap_schema.si_ad_entryUUID;
1765 ava.aa_value = *syncUUID;
1767 if ( syncuuid_bv ) {
1768 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: %s inserted UUID %s\n",
1769 si->si_ridtxt, syncUUID_strrep.bv_val, 0 );
1771 op->ors_filter = &f;
1773 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1774 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1775 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1776 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1777 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1778 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1779 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1780 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1782 op->o_tag = LDAP_REQ_SEARCH;
1783 op->ors_scope = LDAP_SCOPE_SUBTREE;
1784 op->ors_deref = LDAP_DEREF_NEVER;
1786 /* get the entry for this UUID */
1787 op->o_req_dn = si->si_base;
1788 op->o_req_ndn = si->si_base;
1790 op->o_time = slap_get_time();
1791 op->ors_tlimit = SLAP_NO_LIMIT;
1794 op->ors_attrs = slap_anlist_all_attributes;
1795 op->ors_attrsonly = 0;
1797 /* set callback function */
1798 op->o_callback = &cb;
1799 cb.sc_response = dn_callback;
1800 cb.sc_private = &dni;
1801 dni.new_entry = entry;
1802 dni.modlist = modlist;
1804 if ( limits_check( op, &rs_search ) == 0 ) {
1805 rc = be->be_search( op, &rs_search );
1806 Debug( LDAP_DEBUG_SYNC,
1807 "syncrepl_entry: %s be_search (%d)\n",
1808 si->si_ridtxt, rc, 0 );
1811 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1812 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1815 cb.sc_response = null_callback;
1818 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1819 Debug( LDAP_DEBUG_SYNC,
1820 "syncrepl_entry: %s %s\n",
1821 si->si_ridtxt, entry->e_name.bv_val, 0 );
1823 Debug( LDAP_DEBUG_SYNC,
1824 "syncrepl_entry: %s %s\n",
1825 si->si_ridtxt, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1828 slap_op_time( &op->o_time, &op->o_tincr );
1829 switch ( syncstate ) {
1831 case LDAP_SYNC_MODIFY:
1833 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1835 /* FIXME: op->o_csn is assumed to be
1836 * on the thread's slab; this needs
1837 * to be cleared ASAP.
1838 * What happens if already present?
1840 assert( BER_BVISNULL( &op->o_csn ) );
1841 op->o_csn = a->a_vals[0];
1845 if ( BER_BVISNULL( &dni.dn ) ) {
1847 op->o_req_dn = entry->e_name;
1848 op->o_req_ndn = entry->e_nname;
1849 op->o_tag = LDAP_REQ_ADD;
1852 rc = be->be_add( op, &rs_add );
1853 Debug( LDAP_DEBUG_SYNC,
1854 "syncrepl_entry: %s be_add (%d)\n",
1855 si->si_ridtxt, rc, 0 );
1856 switch ( rs_add.sr_err ) {
1858 if ( op->ora_e == entry ) {
1859 be_entry_release_w( op, entry );
1865 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1866 * only if the suffix entry is not present */
1867 case LDAP_NO_SUCH_OBJECT:
1868 rc = syncrepl_add_glue( op, entry );
1872 /* if an entry was added via syncrepl_add_glue(),
1873 * it likely has no entryUUID, so the previous
1874 * be_search() doesn't find it. In this case,
1875 * give syncrepl a chance to modify it. Also
1876 * allow for entries that were recreated with the
1877 * same DN but a different entryUUID.
1879 case LDAP_ALREADY_EXISTS:
1881 Operation op2 = *op;
1882 SlapReply rs2 = { 0 };
1883 slap_callback cb2 = { 0 };
1885 op2.o_tag = LDAP_REQ_SEARCH;
1886 op2.o_req_dn = entry->e_name;
1887 op2.o_req_ndn = entry->e_nname;
1888 op2.ors_scope = LDAP_SCOPE_BASE;
1889 op2.ors_deref = LDAP_DEREF_NEVER;
1890 op2.ors_attrs = slap_anlist_all_attributes;
1891 op2.ors_attrsonly = 0;
1892 op2.ors_limit = NULL;
1894 op2.ors_tlimit = SLAP_NO_LIMIT;
1896 f.f_choice = LDAP_FILTER_PRESENT;
1897 f.f_desc = slap_schema.si_ad_objectClass;
1898 op2.ors_filter = &f;
1899 op2.ors_filterstr = generic_filterstr;
1901 op2.o_callback = &cb2;
1902 cb2.sc_response = dn_callback;
1903 cb2.sc_private = &dni;
1905 rc = be->be_search( &op2, &rs2 );
1906 if ( rc ) goto done;
1909 slap_op_time( &op->o_time, &op->o_tincr );
1915 Debug( LDAP_DEBUG_ANY,
1916 "syncrepl_entry: %s be_add failed (%d)\n",
1917 si->si_ridtxt, rs_add.sr_err, 0 );
1923 op->o_req_dn = dni.dn;
1924 op->o_req_ndn = dni.ndn;
1925 if ( dni.renamed ) {
1926 struct berval noldp, newp, nnewp;
1928 op->o_tag = LDAP_REQ_MODRDN;
1929 dnRdn( &entry->e_name, &op->orr_newrdn );
1930 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1932 dnParent( &dni.ndn, &noldp );
1933 dnParent( &entry->e_nname, &nnewp );
1934 if ( !dn_match( &noldp, &nnewp ) ) {
1935 dnParent( &entry->e_name, &newp );
1936 op->orr_newSup = &newp;
1937 op->orr_nnewSup = &nnewp;
1939 op->orr_newSup = NULL;
1940 op->orr_nnewSup = NULL;
1942 op->orr_deleteoldrdn = dni.delOldRDN;
1943 op->orr_modlist = NULL;
1944 if ( ( rc = slap_modrdn2mods( op, &rs_modify ) ) ) {
1948 /* RDNs must be NUL-terminated for back-ldap */
1949 noldp = op->orr_newrdn;
1950 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
1951 noldp = op->orr_nnewrdn;
1952 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
1954 /* Setup opattrs too */
1956 AttributeDescription *opattrs[] = {
1957 slap_schema.si_ad_entryCSN,
1958 slap_schema.si_ad_modifiersName,
1959 slap_schema.si_ad_modifyTimestamp,
1962 Modifications *mod, **modtail, **ml;
1965 for ( mod = op->orr_modlist;
1967 mod = mod->sml_next )
1969 modtail = &mod->sml_next;
1971 /* pull mod off incoming modlist, append to orr_modlist */
1972 for ( i = 0; opattrs[i]; i++ ) {
1973 for ( ml = modlist; *ml; ml = &(*ml)->sml_next )
1975 if ( (*ml)->sml_desc == opattrs[i] ) {
1977 *ml = mod->sml_next;
1978 mod->sml_next = NULL;
1980 modtail = &mod->sml_next;
1986 rc = be->be_modrdn( op, &rs_modify );
1987 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
1988 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
1990 slap_mods_free( op->orr_modlist, 1 );
1991 Debug( LDAP_DEBUG_SYNC,
1992 "syncrepl_entry: %s be_modrdn (%d)\n",
1993 si->si_ridtxt, rc, 0 );
1997 op->o_tag = LDAP_REQ_MODIFY;
1998 op->orm_modlist = dni.mods;
1999 op->orm_no_opattrs = 1;
2001 rc = be->be_modify( op, &rs_modify );
2002 slap_mods_free( op->orm_modlist, 1 );
2003 Debug( LDAP_DEBUG_SYNC,
2004 "syncrepl_entry: %s be_modify (%d)\n",
2005 si->si_ridtxt, rc, 0 );
2006 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2007 Debug( LDAP_DEBUG_ANY,
2008 "syncrepl_entry: %s be_modify failed (%d)\n",
2009 si->si_ridtxt, rs_modify.sr_err, 0 );
2012 Debug( LDAP_DEBUG_SYNC,
2013 "syncrepl_entry: %s entry unchanged, ignored (%s)\n",
2014 si->si_ridtxt, op->o_req_dn.bv_val, 0 );
2017 case LDAP_SYNC_DELETE :
2018 if ( !BER_BVISNULL( &dni.dn ) ) {
2019 op->o_req_dn = dni.dn;
2020 op->o_req_ndn = dni.ndn;
2021 op->o_tag = LDAP_REQ_DELETE;
2022 rc = be->be_delete( op, &rs_delete );
2023 Debug( LDAP_DEBUG_SYNC,
2024 "syncrepl_entry: %s be_delete (%d)\n",
2025 si->si_ridtxt, rc, 0 );
2027 while ( rs_delete.sr_err == LDAP_SUCCESS
2028 && op->o_delete_glue_parent ) {
2029 op->o_delete_glue_parent = 0;
2030 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
2031 slap_callback cb = { NULL };
2032 cb.sc_response = slap_null_cb;
2033 dnParent( &op->o_req_ndn, &pdn );
2035 op->o_req_ndn = pdn;
2036 op->o_callback = &cb;
2037 op->o_bd->be_delete( op, &rs_delete );
2046 Debug( LDAP_DEBUG_ANY,
2047 "syncrepl_entry: %s unknown syncstate\n", si->si_ridtxt, 0, 0 );
2052 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2053 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2054 BER_BVZERO( &syncUUID_strrep );
2056 if ( !BER_BVISNULL( &dni.ndn ) ) {
2057 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2059 if ( !BER_BVISNULL( &dni.dn ) ) {
2060 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2063 entry_free( entry );
2064 BER_BVZERO( &op->o_csn );
2068 static struct berval gcbva[] = {
2074 #define NP_DELETE_ONE 2
2077 syncrepl_del_nonpresent(
2081 struct berval *cookiecsn )
2083 Backend* be = op->o_bd;
2084 slap_callback cb = { NULL };
2085 SlapReply rs_search = {REP_RESULT};
2086 SlapReply rs_delete = {REP_RESULT};
2087 SlapReply rs_modify = {REP_RESULT};
2088 struct nonpresent_entry *np_list, *np_prev;
2090 AttributeName an[2];
2092 struct berval pdn = BER_BVNULL;
2095 op->o_req_dn = si->si_base;
2096 op->o_req_ndn = si->si_base;
2098 cb.sc_response = nonpresent_callback;
2101 op->o_callback = &cb;
2102 op->o_tag = LDAP_REQ_SEARCH;
2103 op->ors_scope = si->si_scope;
2104 op->ors_deref = LDAP_DEREF_NEVER;
2105 op->o_time = slap_get_time();
2106 op->ors_tlimit = SLAP_NO_LIMIT;
2111 #ifdef LDAP_COMP_MATCH
2112 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2114 AttributeAssertion eq = { NULL, BER_BVNULL };
2118 op->ors_attrsonly = 1;
2119 op->ors_attrs = slap_anlist_no_attrs;
2120 op->ors_limit = NULL;
2121 op->ors_filter = &uf;
2124 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2126 uf.f_choice = LDAP_FILTER_EQUALITY;
2127 si->si_refreshDelete |= NP_DELETE_ONE;
2129 for (i=0; uuids[i].bv_val; i++) {
2131 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2133 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2134 op->o_tmpfree( uf.f_av_value.bv_val, op->o_tmpmemctx );
2135 uf.f_av_value = uuids[i];
2136 rc = be->be_search( op, &rs_search );
2137 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2139 si->si_refreshDelete ^= NP_DELETE_ONE;
2141 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2142 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2143 an[0].an_desc = slap_schema.si_ad_entryUUID;
2145 op->ors_slimit = SLAP_NO_LIMIT;
2146 op->ors_attrsonly = 0;
2147 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2148 op->ors_filterstr = si->si_filterstr;
2149 op->o_nocaching = 1;
2151 if ( limits_check( op, &rs_search ) == 0 ) {
2152 rc = be->be_search( op, &rs_search );
2154 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2157 op->o_nocaching = 0;
2159 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2161 if ( cookiecsn && !BER_BVISNULL( cookiecsn ) ) {
2164 csn = si->si_syncCookie.ctxcsn[0];
2167 slap_queue_csn( op, &csn );
2169 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2170 while ( np_list != NULL ) {
2171 LDAP_LIST_REMOVE( np_list, npe_link );
2173 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2174 op->o_tag = LDAP_REQ_DELETE;
2175 op->o_callback = &cb;
2176 cb.sc_response = null_callback;
2178 op->o_req_dn = *np_prev->npe_name;
2179 op->o_req_ndn = *np_prev->npe_nname;
2180 rc = op->o_bd->be_delete( op, &rs_delete );
2181 Debug( LDAP_DEBUG_SYNC,
2182 "syncrepl_del_nonpresent: %s be_delete %s (%d)\n",
2183 si->si_ridtxt, op->o_req_dn.bv_val, rc );
2185 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2186 Modifications mod1, mod2;
2187 mod1.sml_op = LDAP_MOD_REPLACE;
2189 mod1.sml_desc = slap_schema.si_ad_objectClass;
2190 mod1.sml_type = mod1.sml_desc->ad_cname;
2191 mod1.sml_values = &gcbva[0];
2192 mod1.sml_nvalues = NULL;
2193 mod1.sml_next = &mod2;
2195 mod2.sml_op = LDAP_MOD_REPLACE;
2197 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2198 mod2.sml_type = mod2.sml_desc->ad_cname;
2199 mod2.sml_values = &gcbva[1];
2200 mod2.sml_nvalues = NULL;
2201 mod2.sml_next = NULL;
2203 op->o_tag = LDAP_REQ_MODIFY;
2204 op->orm_modlist = &mod1;
2206 rc = be->be_modify( op, &rs_modify );
2207 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2210 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2211 op->o_delete_glue_parent ) {
2212 op->o_delete_glue_parent = 0;
2213 if ( !be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
2214 slap_callback cb = { NULL };
2215 cb.sc_response = slap_null_cb;
2216 dnParent( &op->o_req_ndn, &pdn );
2218 op->o_req_ndn = pdn;
2219 op->o_callback = &cb;
2220 /* give it a root privil ? */
2221 op->o_bd->be_delete( op, &rs_delete );
2227 op->o_delete_glue_parent = 0;
2229 ber_bvfree( np_prev->npe_name );
2230 ber_bvfree( np_prev->npe_nname );
2234 slap_graduate_commit_csn( op );
2236 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2237 BER_BVZERO( &op->o_csn );
2248 Backend *be = op->o_bd;
2249 slap_callback cb = { NULL };
2254 struct berval dn = BER_BVNULL;
2255 struct berval ndn = BER_BVNULL;
2257 SlapReply rs_add = {REP_RESULT};
2258 struct berval ptr, nptr;
2261 op->o_tag = LDAP_REQ_ADD;
2262 op->o_callback = &cb;
2263 cb.sc_response = null_callback;
2264 cb.sc_private = NULL;
2269 /* count RDNs in suffix */
2270 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2271 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2273 ptr.bv_len -= comma - ptr.bv_val;
2283 /* Start with BE suffix */
2285 for ( i = 0; i < suffrdns; i++ ) {
2286 comma = ber_bvrchr( &ptr, ',' );
2287 if ( comma != NULL ) {
2288 ptr.bv_len = comma - ptr.bv_val;
2295 if ( !BER_BVISEMPTY( &ptr ) ) {
2296 dn.bv_len -= ptr.bv_len + 1;
2297 dn.bv_val += ptr.bv_len + 1;
2300 /* the normalizedDNs are always the same length, no counting
2304 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2305 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2306 ndn.bv_len = be->be_nsuffix[0].bv_len;
2308 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2314 while ( ndn.bv_val > e->e_nname.bv_val ) {
2315 glue = entry_alloc();
2316 ber_dupbv( &glue->e_name, &dn );
2317 ber_dupbv( &glue->e_nname, &ndn );
2319 a = attr_alloc( slap_schema.si_ad_objectClass );
2321 a->a_vals = ch_calloc( 3, sizeof( struct berval ) );
2322 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2323 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2324 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2326 a->a_nvals = a->a_vals;
2328 a->a_next = glue->e_attrs;
2331 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2333 a->a_vals = ch_calloc( 2, sizeof( struct berval ) );
2334 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2335 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2337 a->a_nvals = a->a_vals;
2339 a->a_next = glue->e_attrs;
2342 op->o_req_dn = glue->e_name;
2343 op->o_req_ndn = glue->e_nname;
2345 rc = be->be_add ( op, &rs_add );
2346 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2347 if ( op->ora_e == glue )
2348 be_entry_release_w( op, glue );
2350 /* incl. ALREADY EXIST */
2352 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2358 /* Move to next child */
2359 comma = ber_bvrchr( &ptr, ',' );
2360 if ( comma == NULL ) {
2363 ptr.bv_len = comma - ptr.bv_val;
2365 dn.bv_val = ++comma;
2366 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2368 comma = ber_bvrchr( &nptr, ',' );
2369 assert( comma != NULL );
2370 nptr.bv_len = comma - nptr.bv_val;
2372 ndn.bv_val = ++comma;
2373 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2376 op->o_req_dn = e->e_name;
2377 op->o_req_ndn = e->e_nname;
2379 rc = be->be_add ( op, &rs_add );
2380 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2381 if ( op->ora_e == e )
2382 be_entry_release_w( op, e );
2391 syncrepl_updateCookie(
2395 struct sync_cookie *syncCookie )
2397 Backend *be = op->o_bd;
2398 Modifications mod[2];
2399 struct berval first = BER_BVNULL;
2403 slap_callback cb = { NULL };
2404 SlapReply rs_modify = {REP_RESULT};
2406 mod[0].sml_op = LDAP_MOD_DELETE;
2407 mod[0].sml_desc = slap_schema.si_ad_contextCSN;
2408 mod[0].sml_type = mod[0].sml_desc->ad_cname;
2409 mod[0].sml_values = NULL;
2410 mod[0].sml_nvalues = NULL;
2411 mod[0].sml_next = &mod[1];
2413 mod[1].sml_op = LDAP_MOD_ADD;
2414 mod[1].sml_desc = slap_schema.si_ad_contextCSN;
2415 mod[1].sml_type = mod[0].sml_desc->ad_cname;
2416 mod[1].sml_values = NULL;
2417 mod[1].sml_nvalues = NULL;
2418 mod[1].sml_next = NULL;
2420 ldap_pvt_thread_mutex_lock( &si->si_cookieState->cs_mutex );
2422 for ( i=0; i<syncCookie->numcsns; i++ ) {
2423 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2424 if ( syncCookie->sids[i] != si->si_cookieState->cs_sids[j] )
2426 if ( ber_bvcmp( &syncCookie->ctxcsn[i],
2427 &si->si_cookieState->cs_vals[j] ) > 0 ) {
2428 ber_bvarray_add_x( &mod[0].sml_values,
2429 &si->si_cookieState->cs_vals[j], op->o_tmpmemctx );
2430 ber_bvarray_add_x( &mod[1].sml_values,
2431 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2432 if ( BER_BVISNULL( &first ))
2433 first = syncCookie->ctxcsn[i];
2437 /* there was no match for this SID, it's a new CSN */
2438 if ( j == si->si_cookieState->cs_num ) {
2439 ber_bvarray_add_x( &mod[1].sml_values,
2440 &syncCookie->ctxcsn[i], op->o_tmpmemctx );
2441 if ( BER_BVISNULL( &first ))
2442 first = syncCookie->ctxcsn[i];
2445 slap_queue_csn( op, &first );
2447 op->o_tag = LDAP_REQ_MODIFY;
2449 cb.sc_response = null_callback;
2452 op->o_callback = &cb;
2453 op->o_req_dn = op->o_bd->be_suffix[0];
2454 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2456 /* update contextCSN */
2457 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2459 if ( mod[0].sml_values )
2460 op->orm_modlist = mod;
2462 op->orm_modlist = &mod[1];
2464 op->orm_no_opattrs = 1;
2465 rc = be->be_modify( op, &rs_modify );
2468 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2469 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2470 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2471 /* If we replaced any old values */
2472 if ( mod[0].sml_values ) {
2473 for ( i=0; !BER_BVISNULL( &mod[0].sml_values[i] ); i++ ) {
2474 for ( j=0; j<si->si_cookieState->cs_num; j++ ) {
2475 if ( mod[0].sml_values[i].bv_val !=
2476 si->si_cookieState->cs_vals[j].bv_val )
2478 ber_bvreplace( &si->si_cookieState->cs_vals[j],
2479 &mod[1].sml_values[i] );
2484 /* Else we just added */
2485 si->si_cookieState->cs_num += syncCookie->numcsns;
2486 value_add( &si->si_cookieState->cs_vals, syncCookie->ctxcsn );
2487 free( si->si_cookieState->cs_sids );
2488 si->si_cookieState->cs_sids = slap_parse_csn_sids(
2489 si->si_cookieState->cs_vals, si->si_cookieState->cs_num );
2492 si->si_cookieState->cs_age++;
2493 si->si_cookieAge = si->si_cookieState->cs_age;
2495 Debug( LDAP_DEBUG_ANY,
2496 "syncrepl_updateCookie: %s be_modify failed (%d)\n",
2497 si->si_ridtxt, rs_modify.sr_err, 0 );
2499 ldap_pvt_thread_mutex_unlock( &si->si_cookieState->cs_mutex );
2501 slap_graduate_commit_csn( op );
2502 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2503 BER_BVZERO( &op->o_csn );
2504 if ( mod[1].sml_next ) slap_mods_free( mod[1].sml_next, 1 );
2505 op->o_tmpfree( mod[1].sml_values, op->o_tmpmemctx );
2506 op->o_tmpfree( mod[0].sml_values, op->o_tmpmemctx );
2512 attr_cmp( Operation *op, Attribute *old, Attribute *new,
2513 Modifications ***mret, Modifications ***mcur )
2516 Modifications *mod, **modtail;
2521 int n, o, d, a, *adds, *dels;
2522 /* count old and new */
2523 for ( o=0; old->a_vals[o].bv_val; o++ ) ;
2524 for ( n=0; new->a_vals[n].bv_val; n++ ) ;
2526 adds = op->o_tmpalloc( sizeof(int) * n, op->o_tmpmemctx );
2527 dels = op->o_tmpalloc( sizeof(int) * o, op->o_tmpmemctx );
2533 while ( i < o && j < n ) {
2535 if ( bvmatch( &old->a_vals[i], &new->a_vals[j] ) ) {
2540 for ( k = j + 1; k<n; k++ ) {
2541 if ( bvmatch( &old->a_vals[i], &new->a_vals[k] ) ) {
2545 /* an old value was deleted */
2550 for ( k = i + 1; k < o; k++ ) {
2551 if ( bvmatch( &old->a_vals[k], &new->a_vals[j] ) ) {
2564 /* all old values were deleted, just use the replace op */
2568 /* delete some values */
2569 mod = ch_malloc( sizeof( Modifications ) );
2570 mod->sml_op = LDAP_MOD_DELETE;
2572 mod->sml_desc = old->a_desc;
2573 mod->sml_type = mod->sml_desc->ad_cname;
2574 mod->sml_values = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2575 if ( old->a_vals != old->a_nvals ) {
2576 mod->sml_nvalues = ch_malloc( ( d + 1 ) * sizeof(struct berval) );
2578 mod->sml_nvalues = NULL;
2580 for ( i = 0; i < d; i++ ) {
2581 ber_dupbv( &mod->sml_values[i], &old->a_vals[dels[i]] );
2582 if ( mod->sml_nvalues ) {
2583 ber_dupbv( &mod->sml_nvalues[i], &old->a_nvals[dels[i]] );
2586 BER_BVZERO( &mod->sml_values[i] );
2587 if ( mod->sml_nvalues ) {
2588 BER_BVZERO( &mod->sml_nvalues[i] );
2591 modtail = &mod->sml_next;
2594 op->o_tmpfree( dels, op->o_tmpmemctx );
2595 /* some values were added */
2597 mod = ch_malloc( sizeof( Modifications ) );
2598 mod->sml_op = LDAP_MOD_ADD;
2600 mod->sml_desc = old->a_desc;
2601 mod->sml_type = mod->sml_desc->ad_cname;
2602 mod->sml_values = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2603 if ( old->a_vals != old->a_nvals ) {
2604 mod->sml_nvalues = ch_malloc( ( a + 1 ) * sizeof(struct berval) );
2606 mod->sml_nvalues = NULL;
2608 for ( i = 0; i < a; i++ ) {
2609 ber_dupbv( &mod->sml_values[i], &new->a_vals[adds[i]] );
2610 if ( mod->sml_nvalues ) {
2611 ber_dupbv( &mod->sml_nvalues[i], &new->a_nvals[adds[i]] );
2614 BER_BVZERO( &mod->sml_values[i] );
2615 if ( mod->sml_nvalues ) {
2616 BER_BVZERO( &mod->sml_nvalues[i] );
2619 modtail = &mod->sml_next;
2622 op->o_tmpfree( adds, op->o_tmpmemctx );
2624 /* new attr, just use the new mod */
2628 /* advance to next element */
2632 **mcur = mod->sml_next;
2634 modtail = &mod->sml_next;
2636 *mcur = &mod->sml_next;
2647 dninfo *dni = op->o_callback->sc_private;
2649 if ( rs->sr_type == REP_SEARCH ) {
2650 if ( !BER_BVISNULL( &dni->dn ) ) {
2651 Debug( LDAP_DEBUG_ANY,
2652 "dn_callback : consistency error - "
2653 "entryUUID is not unique\n", 0, 0, 0 );
2655 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2656 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2657 /* If there is a new entry, see if it differs from the old.
2658 * We compare the non-normalized values so that cosmetic changes
2659 * in the provider are always propagated.
2661 if ( dni->new_entry ) {
2662 Modifications **modtail, **ml;
2663 Attribute *old, *new;
2666 /* Did the DN change?
2668 if ( !dn_match( &rs->sr_entry->e_name,
2669 &dni->new_entry->e_name ) )
2671 struct berval oldRDN, oldVal;
2672 AttributeDescription *ad = NULL;
2676 /* See if the oldRDN was deleted */
2677 dnRdn( &rs->sr_entry->e_nname, &oldRDN );
2678 oldVal.bv_val = strchr(oldRDN.bv_val, '=') + 1;
2679 oldVal.bv_len = oldRDN.bv_len - ( oldVal.bv_val -
2681 oldRDN.bv_len -= oldVal.bv_len + 2;
2682 slap_bv2ad( &oldRDN, &ad, &rs->sr_text );
2683 a = attr_find( dni->new_entry->e_attrs, ad );
2684 if ( !a || value_find_ex( ad,
2685 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH |
2686 SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
2687 SLAP_MR_VALUE_OF_SYNTAX, a->a_nvals,
2688 &oldVal, op->o_tmpmemctx ) != LDAP_SUCCESS )
2692 /* OK, this was just a modDN, we're done */
2693 return LDAP_SUCCESS;
2696 modtail = &dni->mods;
2699 /* Make sure new entry is actually newer than old entry */
2700 old = attr_find( rs->sr_entry->e_attrs,
2701 slap_schema.si_ad_entryCSN );
2702 new = attr_find( dni->new_entry->e_attrs,
2703 slap_schema.si_ad_entryCSN );
2704 if ( new && old && ber_bvcmp( &old->a_vals[0],
2705 &new->a_vals[0] ) >= 0 ) {
2706 return LDAP_SUCCESS;
2709 /* We assume that attributes are saved in the same order
2710 * in the remote and local databases. So if we walk through
2711 * the attributeDescriptions one by one they should match in
2712 * lock step. If not, look for an add or delete.
2714 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2717 /* If we've seen this before, use its mod now */
2718 if ( new->a_flags & SLAP_ATTR_IXADD ) {
2719 attr_cmp( op, NULL, new, &modtail, &ml );
2723 if ( old->a_desc != new->a_desc ) {
2727 /* If it's just been re-added later,
2728 * remember that we've seen it.
2730 tmp = attr_find( new, old->a_desc );
2732 tmp->a_flags |= SLAP_ATTR_IXADD;
2734 /* If it's a new attribute, pull it in.
2736 tmp = attr_find( old, new->a_desc );
2738 attr_cmp( op, NULL, new, &modtail, &ml );
2742 /* Delete old attr */
2743 mod = ch_malloc( sizeof( Modifications ) );
2744 mod->sml_op = LDAP_MOD_DELETE;
2746 mod->sml_desc = old->a_desc;
2747 mod->sml_type = mod->sml_desc->ad_cname;
2748 mod->sml_values = NULL;
2749 mod->sml_nvalues = NULL;
2751 modtail = &mod->sml_next;
2756 /* kludge - always update modifiersName so that it
2757 * stays co-located with the other mod opattrs. But only
2758 * if we know there are other valid mods.
2760 if ( old->a_desc == slap_schema.si_ad_modifiersName &&
2762 attr_cmp( op, NULL, new, &modtail, &ml );
2764 attr_cmp( op, old, new, &modtail, &ml );
2772 } else if ( rs->sr_type == REP_RESULT ) {
2773 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2774 Debug( LDAP_DEBUG_ANY,
2775 "dn_callback : consistency error - "
2776 "entryUUID is not unique\n", 0, 0, 0 );
2780 return LDAP_SUCCESS;
2784 nonpresent_callback(
2788 syncinfo_t *si = op->o_callback->sc_private;
2791 struct berval* present_uuid = NULL;
2792 struct nonpresent_entry *np_entry;
2794 if ( rs->sr_type == REP_RESULT ) {
2795 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2796 si->si_presentlist = NULL;
2798 } else if ( rs->sr_type == REP_SEARCH ) {
2799 if ( !( si->si_refreshDelete & NP_DELETE_ONE ) ) {
2800 char buf[sizeof("rid=000 not")];
2802 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2805 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2809 if ( slap_debug & LDAP_DEBUG_SYNC ) {
2810 sprintf( buf, "%s %s", si->si_ridtxt,
2811 present_uuid ? "got" : "not" );
2814 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: %s UUID %s, dn %s\n",
2815 buf, a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val );
2817 if ( a == NULL ) return 0;
2820 if ( present_uuid == NULL ) {
2821 np_entry = (struct nonpresent_entry *)
2822 ch_calloc( 1, sizeof( struct nonpresent_entry ) );
2823 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2824 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2825 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2828 avl_delete( &si->si_presentlist,
2829 &a->a_nvals[0], syncuuid_cmp );
2830 ch_free( present_uuid->bv_val );
2831 ch_free( present_uuid );
2834 return LDAP_SUCCESS;
2842 if ( rs->sr_err != LDAP_SUCCESS &&
2843 rs->sr_err != LDAP_REFERRAL &&
2844 rs->sr_err != LDAP_ALREADY_EXISTS &&
2845 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2846 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2848 Debug( LDAP_DEBUG_ANY,
2849 "null_callback : error code 0x%x\n",
2852 return LDAP_SUCCESS;
2855 static struct berval *
2856 slap_uuidstr_from_normalized(
2857 struct berval* uuidstr,
2858 struct berval* normalized,
2862 unsigned char nibble;
2865 if ( normalized == NULL ) return NULL;
2866 if ( normalized->bv_len != 16 ) return NULL;
2871 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2872 if ( new == NULL ) {
2879 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2880 if ( new != uuidstr ) {
2881 slap_sl_free( new, ctx );
2886 for ( i = 0; i < 16; i++ ) {
2887 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2888 new->bv_val[(i<<1)+d] = '-';
2892 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2893 if ( nibble < 10 ) {
2894 new->bv_val[(i<<1)+d] = nibble + '0';
2896 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2899 nibble = (normalized->bv_val[i]) & 0xF;
2900 if ( nibble < 10 ) {
2901 new->bv_val[(i<<1)+d+1] = nibble + '0';
2903 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2907 new->bv_val[new->bv_len] = '\0';
2912 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2914 const struct berval *uuid1 = v_uuid1;
2915 const struct berval *uuid2 = v_uuid2;
2916 int rc = uuid1->bv_len - uuid2->bv_len;
2917 if ( rc ) return rc;
2918 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2922 avl_ber_bvfree( void *v_bv )
2924 struct berval *bv = (struct berval *)v_bv;
2926 if( v_bv == NULL ) return;
2927 if ( !BER_BVISNULL( bv ) ) {
2928 ch_free( bv->bv_val );
2930 ch_free( (char *) bv );
2934 syncinfo_free( syncinfo_t *sie )
2937 if ( sie->si_conn_setup ) {
2939 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2940 connection_client_stop( s );
2941 sie->si_conn_setup = 0;
2943 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2946 /* re-fetch it, in case it was already removed */
2947 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2949 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2950 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2951 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2954 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2956 bindconf_free( &sie->si_bindconf );
2958 if ( sie->si_filterstr.bv_val ) {
2959 ch_free( sie->si_filterstr.bv_val );
2961 if ( sie->si_logfilterstr.bv_val ) {
2962 ch_free( sie->si_logfilterstr.bv_val );
2964 if ( sie->si_base.bv_val ) {
2965 ch_free( sie->si_base.bv_val );
2967 if ( sie->si_logbase.bv_val ) {
2968 ch_free( sie->si_logbase.bv_val );
2970 if ( sie->si_attrs ) {
2972 while ( sie->si_attrs[i] != NULL ) {
2973 ch_free( sie->si_attrs[i] );
2976 ch_free( sie->si_attrs );
2978 if ( sie->si_exattrs ) {
2980 while ( sie->si_exattrs[i] != NULL ) {
2981 ch_free( sie->si_exattrs[i] );
2984 ch_free( sie->si_exattrs );
2986 if ( sie->si_anlist ) {
2988 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2989 ch_free( sie->si_anlist[i].an_name.bv_val );
2992 ch_free( sie->si_anlist );
2994 if ( sie->si_exanlist ) {
2996 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2997 ch_free( sie->si_exanlist[i].an_name.bv_val );
3000 ch_free( sie->si_exanlist );
3002 if ( sie->si_retryinterval ) {
3003 ch_free( sie->si_retryinterval );
3005 if ( sie->si_retrynum ) {
3006 ch_free( sie->si_retrynum );
3008 if ( sie->si_retrynum_init ) {
3009 ch_free( sie->si_retrynum_init );
3011 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
3012 if ( sie->si_presentlist ) {
3013 avl_free( sie->si_presentlist, avl_ber_bvfree );
3015 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist ) ) {
3016 struct nonpresent_entry* npe;
3017 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
3018 LDAP_LIST_REMOVE( npe, npe_link );
3019 if ( npe->npe_name ) {
3020 if ( npe->npe_name->bv_val ) {
3021 ch_free( npe->npe_name->bv_val );
3023 ch_free( npe->npe_name );
3025 if ( npe->npe_nname ) {
3026 if ( npe->npe_nname->bv_val ) {
3027 ch_free( npe->npe_nname->bv_val );
3029 ch_free( npe->npe_nname );
3038 /* NOTE: used & documented in slapd.conf(5) */
3040 #define PROVIDERSTR "provider"
3041 #define SCHEMASTR "schemachecking"
3042 #define FILTERSTR "filter"
3043 #define SEARCHBASESTR "searchbase"
3044 #define SCOPESTR "scope"
3045 #define ATTRSONLYSTR "attrsonly"
3046 #define ATTRSSTR "attrs"
3047 #define TYPESTR "type"
3048 #define INTERVALSTR "interval"
3049 #define RETRYSTR "retry"
3050 #define SLIMITSTR "sizelimit"
3051 #define TLIMITSTR "timelimit"
3052 #define SYNCDATASTR "syncdata"
3054 /* FIXME: undocumented */
3055 #define LOGBASESTR "logbase"
3056 #define LOGFILTERSTR "logfilter"
3057 #define OLDAUTHCSTR "bindprincipal"
3058 #define EXATTRSSTR "exattrs"
3059 #define MANAGEDSAITSTR "manageDSAit"
3062 #define LASTMODSTR "lastmod"
3063 #define LMGENSTR "gen"
3064 #define LMNOSTR "no"
3065 #define LMREQSTR "req"
3066 #define SRVTABSTR "srvtab"
3067 #define SUFFIXSTR "suffix"
3070 #define GOT_ID 0x0001
3071 #define GOT_PROVIDER 0x0002
3072 #define GOT_BASE 0x0004
3075 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
3081 { BER_BVC("base"), LDAP_SCOPE_BASE },
3082 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
3083 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
3084 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
3085 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
3086 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
3087 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
3088 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
3092 static slap_verbmasks datamodes[] = {
3093 { BER_BVC("default"), SYNCDATA_DEFAULT },
3094 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
3095 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
3100 parse_syncrepl_line(
3108 for ( i = 1; i < c->argc; i++ ) {
3109 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
3110 STRLENOF( IDSTR "=" ) ) )
3113 /* '\0' string terminator accounts for '=' */
3114 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
3115 if ( lutil_atoi( &tmp, val ) != 0 ) {
3116 snprintf( c->msg, sizeof( c->msg ),
3117 "Error: parse_syncrepl_line: "
3118 "unable to parse syncrepl id \"%s\"", val );
3119 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3122 if ( tmp >= 1000 || tmp < 0 ) {
3123 snprintf( c->msg, sizeof( c->msg ),
3124 "Error: parse_syncrepl_line: "
3125 "syncrepl id %d is out of range [0..999]", tmp );
3126 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3130 sprintf( si->si_ridtxt, IDSTR "=%03d", si->si_rid );
3132 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
3133 STRLENOF( PROVIDERSTR "=" ) ) )
3135 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
3136 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
3137 gots |= GOT_PROVIDER;
3138 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
3139 STRLENOF( SCHEMASTR "=" ) ) )
3141 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
3142 if ( !strncasecmp( val, "on", STRLENOF( "on" ) ) ) {
3143 si->si_schemachecking = 1;
3144 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
3145 si->si_schemachecking = 0;
3147 si->si_schemachecking = 1;
3149 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
3150 STRLENOF( FILTERSTR "=" ) ) )
3152 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
3153 if ( si->si_filterstr.bv_val )
3154 ch_free( si->si_filterstr.bv_val );
3155 ber_str2bv( val, 0, 1, &si->si_filterstr );
3156 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
3157 STRLENOF( LOGFILTERSTR "=" ) ) )
3159 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
3160 if ( si->si_logfilterstr.bv_val )
3161 ch_free( si->si_logfilterstr.bv_val );
3162 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
3163 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
3164 STRLENOF( SEARCHBASESTR "=" ) ) )
3169 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
3170 if ( si->si_base.bv_val ) {
3171 ch_free( si->si_base.bv_val );
3173 ber_str2bv( val, 0, 0, &bv );
3174 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
3175 if ( rc != LDAP_SUCCESS ) {
3176 snprintf( c->msg, sizeof( c->msg ),
3177 "Invalid base DN \"%s\": %d (%s)",
3178 val, rc, ldap_err2string( rc ) );
3179 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3183 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
3184 STRLENOF( LOGBASESTR "=" ) ) )
3189 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
3190 if ( si->si_logbase.bv_val ) {
3191 ch_free( si->si_logbase.bv_val );
3193 ber_str2bv( val, 0, 0, &bv );
3194 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
3195 if ( rc != LDAP_SUCCESS ) {
3196 snprintf( c->msg, sizeof( c->msg ),
3197 "Invalid logbase DN \"%s\": %d (%s)",
3198 val, rc, ldap_err2string( rc ) );
3199 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3202 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
3203 STRLENOF( SCOPESTR "=" ) ) )
3206 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
3207 for ( j = 0; !BER_BVISNULL(&scopes[j].key); j++ ) {
3208 if (!strcasecmp( val, scopes[j].key.bv_val ) ) {
3209 si->si_scope = scopes[j].val;
3213 if ( BER_BVISNULL(&scopes[j].key) ) {
3214 snprintf( c->msg, sizeof( c->msg ),
3215 "Error: parse_syncrepl_line: "
3216 "unknown scope \"%s\"", val);
3217 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3220 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
3221 STRLENOF( ATTRSONLYSTR ) ) )
3223 si->si_attrsonly = 1;
3224 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
3225 STRLENOF( ATTRSSTR "=" ) ) )
3227 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
3228 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3230 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3231 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
3232 if ( si->si_anlist == NULL ) {
3233 ch_free( attr_fname );
3236 si->si_anfile = attr_fname;
3238 char *str, *s, *next;
3239 char delimstr[] = " ,\t";
3240 str = ch_strdup( val );
3241 for ( s = ldap_pvt_strtok( str, delimstr, &next );
3243 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
3245 if ( strlen(s) == 1 && *s == '*' ) {
3246 si->si_allattrs = 1;
3247 val[ s - str ] = delimstr[0];
3249 if ( strlen(s) == 1 && *s == '+' ) {
3250 si->si_allopattrs = 1;
3251 val [ s - str ] = delimstr[0];
3255 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
3256 if ( si->si_anlist == NULL ) {
3260 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
3261 STRLENOF( EXATTRSSTR "=" ) ) )
3263 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
3264 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
3266 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3267 si->si_exanlist = file2anlist(
3268 si->si_exanlist, attr_fname, " ,\t" );
3269 if ( si->si_exanlist == NULL ) {
3270 ch_free( attr_fname );
3273 ch_free( attr_fname );
3275 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3276 if ( si->si_exanlist == NULL ) {
3280 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3281 STRLENOF( TYPESTR "=" ) ) )
3283 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3284 if ( !strncasecmp( val, "refreshOnly",
3285 STRLENOF("refreshOnly") ) )
3287 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3288 } else if ( !strncasecmp( val, "refreshAndPersist",
3289 STRLENOF("refreshAndPersist") ) )
3291 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3292 si->si_interval = 60;
3294 snprintf( c->msg, sizeof( c->msg ),
3295 "Error: parse_syncrepl_line: "
3296 "unknown sync type \"%s\"", val);
3297 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3300 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3301 STRLENOF( INTERVALSTR "=" ) ) )
3303 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3304 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3305 si->si_interval = 0;
3306 } else if ( strchr( val, ':' ) != NULL ) {
3307 char *next, *ptr = val;
3308 unsigned dd, hh, mm, ss;
3310 /* NOTE: the test for ptr[ 0 ] == '-'
3311 * should go before the call to strtoul() */
3312 dd = strtoul( ptr, &next, 10 );
3313 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3314 snprintf( c->msg, sizeof( c->msg ),
3315 "Error: parse_syncrepl_line: "
3316 "invalid interval \"%s\", unable to parse days", val );
3317 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3321 hh = strtoul( ptr, &next, 10 );
3322 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3323 snprintf( c->msg, sizeof( c->msg ),
3324 "Error: parse_syncrepl_line: "
3325 "invalid interval \"%s\", unable to parse hours", val );
3326 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3330 mm = strtoul( ptr, &next, 10 );
3331 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3332 snprintf( c->msg, sizeof( c->msg ),
3333 "Error: parse_syncrepl_line: "
3334 "invalid interval \"%s\", unable to parse minutes", val );
3335 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3339 ss = strtoul( ptr, &next, 10 );
3340 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3341 snprintf( c->msg, sizeof( c->msg ),
3342 "Error: parse_syncrepl_line: "
3343 "invalid interval \"%s\", unable to parse seconds", val );
3344 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3347 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3351 if ( lutil_parse_time( val, &t ) != 0 ) {
3352 snprintf( c->msg, sizeof( c->msg ),
3353 "Error: parse_syncrepl_line: "
3354 "invalid interval \"%s\"", val );
3355 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3358 si->si_interval = (time_t)t;
3360 if ( si->si_interval < 0 ) {
3361 snprintf( c->msg, sizeof( c->msg ),
3362 "Error: parse_syncrepl_line: "
3363 "invalid interval \"%ld\"",
3364 (long) si->si_interval);
3365 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3368 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3369 STRLENOF( RETRYSTR "=" ) ) )
3374 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3375 retry_list = (char **) ch_calloc( 1, sizeof( char * ) );
3376 retry_list[0] = NULL;
3378 slap_str2clist( &retry_list, val, " ,\t" );
3380 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3383 snprintf( c->msg, sizeof( c->msg ),
3384 "Error: incomplete syncrepl retry list" );
3385 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3386 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3387 ch_free( retry_list[k] );
3389 ch_free( retry_list );
3392 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ) );
3393 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ) );
3394 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ) );
3395 for ( j = 0; j < n; j++ ) {
3397 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3398 snprintf( c->msg, sizeof( c->msg ),
3399 "Error: invalid retry interval \"%s\" (#%d)",
3400 retry_list[j*2], j );
3401 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3402 /* do some cleanup */
3405 si->si_retryinterval[j] = (time_t)t;
3406 if ( *retry_list[j*2+1] == '+' ) {
3407 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3408 si->si_retrynum[j] = RETRYNUM_FOREVER;
3412 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3413 || si->si_retrynum_init[j] <= 0 )
3415 snprintf( c->msg, sizeof( c->msg ),
3416 "Error: invalid initial retry number \"%s\" (#%d)",
3417 retry_list[j*2+1], j );
3418 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3419 /* do some cleanup */
3422 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3423 || si->si_retrynum[j] <= 0 )
3425 snprintf( c->msg, sizeof( c->msg ),
3426 "Error: invalid retry number \"%s\" (#%d)",
3427 retry_list[j*2+1], j );
3428 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3429 /* do some cleanup */
3434 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3435 si->si_retrynum[j] = RETRYNUM_TAIL;
3436 si->si_retryinterval[j] = 0;
3438 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3439 ch_free( retry_list[k] );
3441 ch_free( retry_list );
3442 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3443 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3445 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3446 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3447 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3449 snprintf( c->msg, sizeof( c->msg ),
3450 "invalid manageDSAit value \"%s\".\n",
3452 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3455 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3456 STRLENOF( SLIMITSTR "=") ) )
3458 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3459 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3462 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3463 snprintf( c->msg, sizeof( c->msg ),
3464 "invalid size limit value \"%s\".\n",
3466 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3469 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3470 STRLENOF( TLIMITSTR "=" ) ) )
3472 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3473 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3476 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3477 snprintf( c->msg, sizeof( c->msg ),
3478 "invalid time limit value \"%s\".\n",
3480 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3483 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3484 STRLENOF( SYNCDATASTR "=" ) ) )
3486 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3487 si->si_syncdata = verb_to_mask( val, datamodes );
3488 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3489 snprintf( c->msg, sizeof( c->msg ),
3490 "Error: parse_syncrepl_line: "
3491 "unable to parse \"%s\"\n", c->argv[ i ] );
3492 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3497 if ( gots != GOT_ALL ) {
3498 snprintf( c->msg, sizeof( c->msg ),
3499 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3500 gots & GOT_ID ? "" : " "IDSTR,
3501 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3502 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3503 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3517 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3518 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3519 "operations required for syncrepl", c->be->be_type );
3520 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3523 if ( BER_BVISEMPTY( &c->be->be_rootdn ) ) {
3524 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3525 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3528 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3531 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3535 si->si_bindconf.sb_tls = SB_TLS_OFF;
3536 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3537 si->si_schemachecking = 0;
3538 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3539 &si->si_filterstr );
3540 si->si_base.bv_val = NULL;
3541 si->si_scope = LDAP_SCOPE_SUBTREE;
3542 si->si_attrsonly = 0;
3543 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3544 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ) );
3545 si->si_attrs = NULL;
3546 si->si_allattrs = 0;
3547 si->si_allopattrs = 0;
3548 si->si_exattrs = NULL;
3549 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3550 si->si_interval = 86400;
3551 si->si_retryinterval = NULL;
3552 si->si_retrynum_init = NULL;
3553 si->si_retrynum = NULL;
3554 si->si_manageDSAit = 0;
3557 si->si_conn_setup = 0;
3559 si->si_presentlist = NULL;
3560 LDAP_LIST_INIT( &si->si_nonpresentlist );
3561 ldap_pvt_thread_mutex_init( &si->si_mutex );
3563 rc = parse_syncrepl_line( c, si );
3566 /* Must be LDAPv3 because we need controls */
3567 switch ( si->si_bindconf.sb_version ) {
3569 /* not explicitly set */
3570 si->si_bindconf.sb_version = LDAP_VERSION3;
3573 /* explicitly set */
3576 Debug( LDAP_DEBUG_ANY,
3577 "version %d incompatible with syncrepl\n",
3578 si->si_bindconf.sb_version, 0, 0 );
3579 syncinfo_free( si );
3584 if ( slapMode & SLAP_SERVER_MODE ) {
3585 Listener **l = slapd_get_listeners();
3588 /* check if URL points to current server. If so, ignore
3589 * this configuration. We require an exact match. Just
3590 * in case they really want to do this, they can vary
3591 * the case of the URL to allow it.
3593 if ( l && !SLAP_DBHIDDEN( c->be ) ) {
3595 for ( i=0; l[i]; i++ ) {
3596 if ( bvmatch( &l[i]->sl_url, &si->si_bindconf.sb_uri ) ) {
3604 init_syncrepl( si );
3605 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq,
3606 si->si_interval, do_syncrepl, si, "do_syncrepl",
3609 rc = config_sync_shadow( c ) ? -1 : 0;
3617 /* Use main slapd defaults */
3618 bindconf_tls_defaults( &si->si_bindconf );
3621 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3622 syncinfo_free( si );
3625 Debug( LDAP_DEBUG_CONFIG,
3626 "Config: ** successfully added syncrepl \"%s\"\n",
3627 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3628 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3629 if ( !si->si_schemachecking ) {
3630 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3632 if ( c->be->be_syncinfo ) {
3633 si->si_cookieState = c->be->be_syncinfo->si_cookieState;
3635 si->si_cookieState = ch_calloc( 1, sizeof( cookie_state ));
3636 ldap_pvt_thread_mutex_init( &si->si_cookieState->cs_mutex );
3638 si->si_next = c->be->be_syncinfo;
3639 c->be->be_syncinfo = si;
3645 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3647 struct berval bc, uri;
3648 char buf[BUFSIZ*2], *ptr;
3651 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3655 /* temporarily inhibit bindconf from printing URI */
3656 uri = si->si_bindconf.sb_uri;
3657 BER_BVZERO( &si->si_bindconf.sb_uri );
3658 si->si_bindconf.sb_version = 0;
3659 bindconf_unparse( &si->si_bindconf, &bc );
3660 si->si_bindconf.sb_uri = uri;
3661 si->si_bindconf.sb_version = LDAP_VERSION3;
3664 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3665 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3666 if ( ptr - buf >= sizeof( buf ) ) return;
3667 if ( !BER_BVISNULL( &bc ) ) {
3668 if ( WHATSLEFT <= bc.bv_len ) {
3672 ptr = lutil_strcopy( ptr, bc.bv_val );
3675 if ( !BER_BVISEMPTY( &si->si_filterstr ) ) {
3676 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3677 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3678 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3681 if ( !BER_BVISNULL( &si->si_base ) ) {
3682 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3683 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3684 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3687 if ( !BER_BVISEMPTY( &si->si_logfilterstr ) ) {
3688 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3689 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3690 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3693 if ( !BER_BVISNULL( &si->si_logbase ) ) {
3694 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3695 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3696 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3699 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3700 if ( si->si_scope == scopes[i].val ) {
3701 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3702 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3703 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3707 if ( si->si_attrsonly ) {
3708 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3709 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3711 if ( si->si_anfile ) {
3712 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3713 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3714 ptr = lutil_strcopy( ptr, si->si_anfile );
3716 } else if ( si->si_allattrs || si->si_allopattrs ||
3717 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ) )
3721 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3722 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3724 /* FIXME: add check for overflow */
3725 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3726 if ( si->si_allattrs ) {
3727 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3728 if ( old != ptr ) *ptr++ = ',';
3731 if ( si->si_allopattrs ) {
3732 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3733 if ( old != ptr ) *ptr++ = ',';
3738 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3739 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3740 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3741 /* FIXME: add check for overflow */
3742 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3744 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3745 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3746 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3748 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3749 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3750 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3751 "refreshAndPersist" : "refreshOnly" );
3753 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3756 dd = si->si_interval;
3763 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3764 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3765 if ( ptr - buf >= sizeof( buf ) ) return;
3766 } else if ( si->si_retryinterval ) {
3768 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3769 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3770 for (i=0; si->si_retryinterval[i]; i++) {
3771 if ( space ) *ptr++ = ' ';
3773 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3774 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3777 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3779 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3783 if ( si->si_slimit ) {
3784 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3785 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3786 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3789 if ( si->si_tlimit ) {
3790 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3791 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3792 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3795 if ( si->si_syncdata ) {
3796 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3797 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3798 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3799 ptr = lutil_strcopy( ptr, bc.bv_val );
3802 bc.bv_len = ptr - buf;
3804 ber_dupbv( bv, &bc );
3808 syncrepl_config( ConfigArgs *c )
3810 if (c->op == SLAP_CONFIG_EMIT) {
3811 if ( c->be->be_syncinfo ) {
3815 for ( si = c->be->be_syncinfo; si; si=si->si_next ) {
3816 syncrepl_unparse( si, &bv );
3817 ber_bvarray_add( &c->rvalue_vals, &bv );
3822 } else if ( c->op == LDAP_MOD_DELETE ) {
3823 cookie_state *cs = NULL;
3824 if ( c->be->be_syncinfo ) {
3825 syncinfo_t *si, **sip;
3828 cs = c->be->be_syncinfo->si_cookieState;
3829 for ( sip = &c->be->be_syncinfo, i=0; *sip; i++ ) {
3831 if ( c->valx == -1 || i == c->valx ) {
3833 syncinfo_free( si );
3839 if ( !c->be->be_syncinfo ) {
3840 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3842 ber_bvarray_free( cs->cs_vals );
3843 ldap_pvt_thread_mutex_destroy( &cs->cs_mutex );
3849 if ( SLAP_SLURP_SHADOW( c->be ) ) {
3850 Debug(LDAP_DEBUG_ANY, "%s: "
3851 "syncrepl: database already shadowed.\n",
3855 return add_syncrepl( c );