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-2005 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 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 typedef struct syncinfo_s {
48 struct slap_backend_db *si_be;
51 struct berval si_provideruri;
52 slap_bindconf si_bindconf;
53 struct berval si_base;
54 struct berval si_logbase;
55 struct berval si_filterstr;
56 struct berval si_logfilterstr;
60 AttributeName *si_anlist;
61 AttributeName *si_exanlist;
66 int si_schemachecking;
69 time_t *si_retryinterval;
70 int *si_retrynum_init;
72 struct sync_cookie si_syncCookie;
77 int si_refreshPresent;
80 Avlnode *si_presentlist;
82 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
83 ldap_pvt_thread_mutex_t si_mutex;
86 static int syncuuid_cmp( const void *, const void * );
87 static void avl_ber_bvfree( void * );
88 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
89 static int syncrepl_message_to_op(
90 syncinfo_t *, Operation *, LDAPMessage * );
91 static int syncrepl_message_to_entry(
92 syncinfo_t *, Operation *, LDAPMessage *,
93 Modifications **, Entry **, int );
94 static int syncrepl_entry(
95 syncinfo_t *, Operation*, Entry*,
96 Modifications**,int, struct berval*,
99 static void syncrepl_updateCookie(
100 syncinfo_t *, Operation *, struct berval *,
101 struct sync_cookie * );
102 static struct berval * slap_uuidstr_from_normalized(
103 struct berval *, struct berval *, void * );
105 /* callback functions */
106 static int dn_callback( struct slap_op *, struct slap_rep * );
107 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
108 static int null_callback( struct slap_op *, struct slap_rep * );
110 static AttributeDescription *sync_descs[4];
113 init_syncrepl(syncinfo_t *si)
116 char **attrs, **exattrs;
118 if ( !sync_descs[0] ) {
119 sync_descs[0] = slap_schema.si_ad_objectClass;
120 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
121 sync_descs[2] = slap_schema.si_ad_entryCSN;
122 sync_descs[3] = NULL;
125 if ( si->si_allattrs && si->si_allopattrs )
128 attrs = anlist2attrs( si->si_anlist );
131 if ( si->si_allattrs ) {
134 if ( !is_at_operational( at_find( attrs[i] ))) {
135 for ( j = i; attrs[j] != NULL; j++ ) {
138 attrs[j] = attrs[j+1];
144 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
145 attrs[i] = ch_strdup("*");
148 } else if ( si->si_allopattrs ) {
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("+");
166 for ( i = 0; sync_descs[i] != NULL; i++ ) {
169 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
170 for ( k = j; attrs[k] != NULL; k++ ) {
173 attrs[k] = attrs[k+1];
181 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
183 if ( si->si_allopattrs ) {
184 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
186 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
189 if ( attrs == NULL ) {
190 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
194 if ( si->si_allopattrs ) {
195 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
197 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
198 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
206 if ( si->si_allattrs == si->si_allopattrs ) {
207 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
208 attrs[i++] = ch_strdup( "*" );
209 attrs[i++] = ch_strdup( "+" );
210 } else if ( si->si_allattrs && !si->si_allopattrs ) {
211 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
212 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
213 attrs[i++] = ch_strdup( "*" );
214 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
215 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
217 } else if ( !si->si_allattrs && si->si_allopattrs ) {
218 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
219 attrs[i++] = ch_strdup( "+" );
220 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
225 si->si_attrs = attrs;
227 exattrs = anlist2attrs( si->si_exanlist );
230 for ( n = 0; exattrs[n] != NULL; n++ ) ;
232 for ( i = 0; sync_descs[i] != NULL; i++ ) {
234 while ( exattrs[j] != NULL ) {
235 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
236 ch_free( exattrs[j] );
237 for ( k = j; exattrs[k] != NULL; k++ ) {
238 exattrs[k] = exattrs[k+1];
246 for ( i = 0; exattrs[i] != NULL; i++ ) {
247 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
249 if ( ( oc = si->si_anlist[j].an_oc ) ) {
251 while ( oc->soc_required[k] ) {
252 if ( !strcmp( exattrs[i],
253 oc->soc_required[k]->sat_cname.bv_val )) {
254 ch_free( exattrs[i] );
255 for ( l = i; exattrs[l]; l++ ) {
256 exattrs[l] = exattrs[l+1];
266 for ( i = 0; exattrs[i] != NULL; i++ ) ;
269 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
272 si->si_exattrs = exattrs;
275 static char *allattrs[] = {"*", "+", NULL};
282 BerElementBuffer berbuf;
283 BerElement *ber = (BerElement *)&berbuf;
284 LDAPControl c[2], *ctrls[3];
285 struct timeval timeout;
296 /* setup LDAP SYNC control */
297 ber_init2( ber, NULL, LBER_USE_DER );
298 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
300 /* If we're using a log but we have no state, then fallback to
301 * normal mode for a full refresh.
303 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
304 si->si_logstate = SYNCLOG_FALLBACK;
306 /* Use the log parameters if we're in log mode */
307 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
309 base = si->si_logbase.bv_val;
310 filter = si->si_logfilterstr.bv_val;
313 scope = LDAP_SCOPE_ONELEVEL;
316 base = si->si_base.bv_val;
317 filter = si->si_filterstr.bv_val;
318 attrs = si->si_attrs;
319 attrsonly = si->si_attrsonly;
320 scope = si->si_scope;
322 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
323 stype = LDAP_SYNC_REFRESH_ONLY;
328 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
330 ber_printf( ber, "{eOb}",
331 abs(stype), &si->si_syncCookie.octet_str, rhint );
333 ber_printf( ber, "{eb}",
337 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
342 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
343 c[0].ldctl_iscritical = si->si_type < 0;
346 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
347 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
348 c[1].ldctl_value = si->si_bindconf.sb_authzId;
349 c[1].ldctl_iscritical = 1;
356 timeout.tv_sec = si->si_tlimit;
359 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
360 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
361 si->si_slimit, &msgid );
372 int cmdline_cookie_found = 0;
374 struct sync_cookie *sc = NULL;
380 psub = &si->si_be->be_nsuffix[0];
382 /* Init connection to master */
383 rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
384 if ( rc != LDAP_SUCCESS ) {
385 Debug( LDAP_DEBUG_ANY,
386 "do_syncrep1: ldap_initialize failed (%s)\n",
387 si->si_provideruri.bv_val, 0, 0 );
391 op->o_protocol = LDAP_VERSION3;
392 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
396 if ( si->si_bindconf.sb_tls ) {
397 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
398 if( rc != LDAP_SUCCESS ) {
399 Debug( LDAP_DEBUG_ANY,
400 "%s: ldap_start_tls failed (%d)\n",
401 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
403 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
407 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
408 #ifdef HAVE_CYRUS_SASL
411 if ( si->si_bindconf.sb_secprops != NULL ) {
412 rc = ldap_set_option( si->si_ld,
413 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
415 if( rc != LDAP_OPT_SUCCESS ) {
416 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
417 "(%s,SECPROPS,\"%s\") failed!\n",
418 si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
423 defaults = lutil_sasl_defaults( si->si_ld,
424 si->si_bindconf.sb_saslmech.bv_val,
425 si->si_bindconf.sb_realm.bv_val,
426 si->si_bindconf.sb_authcId.bv_val,
427 si->si_bindconf.sb_cred.bv_val,
428 si->si_bindconf.sb_authzId.bv_val );
430 rc = ldap_sasl_interactive_bind_s( si->si_ld,
431 si->si_bindconf.sb_binddn.bv_val,
432 si->si_bindconf.sb_saslmech.bv_val,
438 lutil_sasl_freedefs( defaults );
440 /* FIXME: different error behaviors according to
442 * 2) on err policy : exit, retry, backoff ...
444 if ( rc != LDAP_SUCCESS ) {
445 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
447 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
448 "ldap_sasl_interactive_bind_s failed (%d)\n",
451 /* FIXME (see above comment) */
452 /* if Kerberos credentials cache is not active, retry */
453 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
454 rc == LDAP_LOCAL_ERROR )
456 rc = LDAP_SERVER_DOWN;
461 #else /* HAVE_CYRUS_SASL */
462 /* Should never get here, we trapped this at config time */
464 fprintf( stderr, "not compiled with SASL support\n" );
469 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
470 rc = ldap_sasl_bind_s( si->si_ld,
471 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
472 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
473 if ( rc != LDAP_SUCCESS ) {
474 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
475 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
480 /* Set SSF to strongest of TLS, SASL SSFs */
483 op->o_transport_ssf = 0;
485 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
486 == LDAP_SUCCESS && ssl != NULL )
488 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
490 #endif /* HAVE_TLS */
491 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
492 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
493 ? op->o_sasl_ssf : op->o_tls_ssf;
496 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
497 /* get contextCSN shadow replica from database */
498 BerVarray csn = NULL;
500 assert( si->si_rid < 1000 );
501 op->o_req_ndn = op->o_bd->be_nsuffix[0];
502 op->o_req_dn = op->o_req_ndn;
504 /* try to read stored contextCSN */
505 backend_attribute( op, NULL, &op->o_req_ndn,
506 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
508 ch_free( si->si_syncCookie.ctxcsn.bv_val );
509 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
510 ber_bvarray_free_x( csn, op->o_tmpmemctx );
513 si->si_syncCookie.rid = si->si_rid;
515 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
516 if ( si->si_rid == sc->rid ) {
517 cmdline_cookie_found = 1;
522 if ( cmdline_cookie_found ) {
523 /* cookie is supplied in the command line */
525 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
527 /* ctxcsn wasn't parsed yet, do it now */
528 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
529 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
530 /* if cmdline cookie does not have ctxcsn */
531 /* component, set it to an initial value */
532 slap_init_sync_cookie_ctxcsn( sc );
534 slap_sync_cookie_free( &si->si_syncCookie, 0 );
535 slap_dup_sync_cookie( &si->si_syncCookie, sc );
536 slap_sync_cookie_free( sc, 1 );
539 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
540 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
543 rc = ldap_sync_search( si, op->o_tmpmemctx );
545 if( rc != LDAP_SUCCESS ) {
546 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
547 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
553 ldap_unbind_ext( si->si_ld, NULL, NULL );
566 LDAPControl **rctrls = NULL;
569 BerElementBuffer berbuf;
570 BerElement *ber = (BerElement *)&berbuf;
572 LDAPMessage *res = NULL;
573 LDAPMessage *msg = NULL;
576 struct berval *retdata = NULL;
581 struct berval syncUUID = BER_BVNULL;
582 struct sync_cookie syncCookie = { BER_BVNULL };
583 struct sync_cookie syncCookie_req = { BER_BVNULL };
584 struct berval cookie = BER_BVNULL;
592 Modifications *modlist = NULL;
597 struct timeval *tout_p = NULL;
598 struct timeval tout = { 0, 0 };
600 int refreshDeletes = 0;
602 BerVarray syncUUIDs = NULL;
605 if ( slapd_shutdown ) {
610 ber_init2( ber, NULL, LBER_USE_DER );
611 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
613 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
615 psub = &si->si_be->be_nsuffix[0];
617 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
619 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
625 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
626 tout_p, &res )) > 0 )
628 if ( slapd_shutdown ) {
632 for( msg = ldap_first_message( si->si_ld, res );
634 msg = ldap_next_message( si->si_ld, msg ) )
636 if ( slapd_shutdown ) {
640 switch( ldap_msgtype( msg ) ) {
641 case LDAP_RES_SEARCH_ENTRY:
642 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
643 /* we can't work without the control */
645 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
646 "got search entry without "
647 "control\n", 0, 0, 0 );
652 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
653 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
654 /* FIXME: what if syncUUID is NULL or empty? */
655 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
656 ber_scanf( ber, /*"{"*/ "m}", &cookie );
657 if ( !BER_BVISNULL( &cookie ) ) {
658 ch_free( syncCookie.octet_str.bv_val );
659 ber_dupbv( &syncCookie.octet_str, &cookie );
661 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
663 slap_parse_sync_cookie( &syncCookie, NULL );
666 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
669 if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
670 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
671 syncrepl_updateCookie( si, op, psub, &syncCookie );
673 } else if ( syncrepl_message_to_entry( si, op, msg,
674 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
675 rc_efree = syncrepl_entry( si, op, entry, &modlist,
676 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
677 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
679 syncrepl_updateCookie( si, op, psub, &syncCookie );
682 ldap_controls_free( rctrls );
684 slap_mods_free( modlist, 1 );
686 if ( rc_efree && entry ) {
692 case LDAP_RES_SEARCH_REFERENCE:
693 Debug( LDAP_DEBUG_ANY,
694 "do_syncrep2: reference received error\n", 0, 0, 0 );
697 case LDAP_RES_SEARCH_RESULT:
698 Debug( LDAP_DEBUG_SYNC,
699 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
700 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
702 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
703 if ( si->si_logstate == SYNCLOG_LOGGING ) {
704 si->si_logstate = SYNCLOG_FALLBACK;
711 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
713 ber_scanf( ber, "{" /*"}"*/);
714 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
715 ber_scanf( ber, "m", &cookie );
716 if ( !BER_BVISNULL( &cookie ) ) {
717 ch_free( syncCookie.octet_str.bv_val );
718 ber_dupbv( &syncCookie.octet_str, &cookie);
720 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
722 slap_parse_sync_cookie( &syncCookie, NULL );
725 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
727 ber_scanf( ber, "b", &refreshDeletes );
729 ber_scanf( ber, /*"{"*/ "}" );
731 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
733 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
736 value_match( &match, slap_schema.si_ad_entryCSN,
737 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
738 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
739 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
742 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
743 match < 0 && err == LDAP_SUCCESS )
745 syncrepl_updateCookie( si, op, psub, &syncCookie );
748 ldap_controls_free( rctrls );
750 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
751 /* FIXME : different error behaviors according to
752 * 1) err code : LDAP_BUSY ...
753 * 2) on err policy : stop service, stop sync, retry
755 if ( refreshDeletes == 0 && match < 0 &&
756 err == LDAP_SUCCESS )
758 syncrepl_del_nonpresent( op, si, NULL );
760 avl_free( si->si_presentlist, avl_ber_bvfree );
761 si->si_presentlist = NULL;
764 if ( err == LDAP_SUCCESS
765 && si->si_logstate == SYNCLOG_FALLBACK ) {
766 si->si_logstate = SYNCLOG_LOGGING;
767 rc = LDAP_SYNC_REFRESH_REQUIRED;
774 case LDAP_RES_INTERMEDIATE:
775 rc = ldap_parse_intermediate( si->si_ld, msg,
776 &retoid, &retdata, NULL, 0 );
777 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
778 ber_init2( ber, retdata, LBER_USE_DER );
780 switch ( si_tag = ber_peek_tag( ber, &len )) {
782 case LDAP_TAG_SYNC_NEW_COOKIE:
783 Debug( LDAP_DEBUG_SYNC,
784 "do_syncrep2: %s - %s%s\n",
785 "LDAP_RES_INTERMEDIATE",
786 "NEW_COOKIE", "\n" );
787 ber_scanf( ber, "tm", &tag, &cookie );
789 case LDAP_TAG_SYNC_REFRESH_DELETE:
790 case LDAP_TAG_SYNC_REFRESH_PRESENT:
791 Debug( LDAP_DEBUG_SYNC,
792 "do_syncrep2: %s - %s%s\n",
793 "LDAP_RES_INTERMEDIATE",
794 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
795 "REFRESH_PRESENT" : "REFRESH_DELETE",
797 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
798 si->si_refreshDelete = 1;
800 si->si_refreshPresent = 1;
802 ber_scanf( ber, "t{" /*"}"*/, &tag );
803 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
805 ber_scanf( ber, "m", &cookie );
806 if ( !BER_BVISNULL( &cookie ) ) {
807 ch_free( syncCookie.octet_str.bv_val );
808 ber_dupbv( &syncCookie.octet_str, &cookie );
810 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
812 slap_parse_sync_cookie( &syncCookie, NULL );
815 if ( ber_peek_tag( ber, &len ) ==
816 LDAP_TAG_REFRESHDONE )
818 ber_scanf( ber, "b", &refreshDone );
820 ber_scanf( ber, /*"{"*/ "}" );
822 case LDAP_TAG_SYNC_ID_SET:
823 Debug( LDAP_DEBUG_SYNC,
824 "do_syncrep2: %s - %s%s\n",
825 "LDAP_RES_INTERMEDIATE",
828 ber_scanf( ber, "t{" /*"}"*/, &tag );
829 if ( ber_peek_tag( ber, &len ) ==
830 LDAP_TAG_SYNC_COOKIE )
832 ber_scanf( ber, "m", &cookie );
833 if ( !BER_BVISNULL( &cookie ) ) {
834 ch_free( syncCookie.octet_str.bv_val );
835 ber_dupbv( &syncCookie.octet_str, &cookie );
837 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
839 slap_parse_sync_cookie( &syncCookie, NULL );
842 if ( ber_peek_tag( ber, &len ) ==
843 LDAP_TAG_REFRESHDELETES )
845 ber_scanf( ber, "b", &refreshDeletes );
847 ber_scanf( ber, "[W]", &syncUUIDs );
848 ber_scanf( ber, /*"{"*/ "}" );
849 if ( refreshDeletes ) {
850 syncrepl_del_nonpresent( op, si, syncUUIDs );
851 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
853 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
854 struct berval *syncuuid_bv;
855 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
856 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
857 avl_insert( &si->si_presentlist,
858 (caddr_t) syncuuid_bv,
859 syncuuid_cmp, avl_dup_error );
861 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
865 Debug( LDAP_DEBUG_ANY,
866 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
867 (long) si_tag, 0, 0 );
868 ldap_memfree( retoid );
869 ber_bvfree( retdata );
873 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
875 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
878 value_match( &match, slap_schema.si_ad_entryCSN,
879 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
880 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
881 &syncCookie_req.ctxcsn,
882 &syncCookie.ctxcsn, &text );
885 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
888 syncrepl_updateCookie( si, op, psub, &syncCookie);
891 if ( si->si_refreshPresent == 1 ) {
893 syncrepl_del_nonpresent( op, si, NULL );
897 ldap_memfree( retoid );
898 ber_bvfree( retdata );
902 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
903 "unknown intermediate response (%d)\n",
905 ldap_memfree( retoid );
906 ber_bvfree( retdata );
912 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
913 "unknown message\n", 0, 0, 0 );
917 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
918 slap_sync_cookie_free( &syncCookie_req, 0 );
919 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
920 slap_sync_cookie_free( &syncCookie, 0 );
930 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
931 errstr = ldap_err2string( rc );
933 Debug( LDAP_DEBUG_ANY,
934 "do_syncrep2 : %s\n", errstr, 0, 0 );
938 slap_sync_cookie_free( &syncCookie, 0 );
939 slap_sync_cookie_free( &syncCookie_req, 0 );
941 if ( res ) ldap_msgfree( res );
943 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
944 ldap_unbind_ext( si->si_ld, NULL, NULL );
956 struct re_s* rtask = arg;
957 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
958 Connection conn = {0};
959 OperationBuffer opbuf;
961 int rc = LDAP_SUCCESS;
968 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
973 ldap_pvt_thread_mutex_lock( &si->si_mutex );
975 switch( abs( si->si_type )) {
976 case LDAP_SYNC_REFRESH_ONLY:
977 case LDAP_SYNC_REFRESH_AND_PERSIST:
980 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
984 if ( slapd_shutdown ) {
986 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
987 connection_client_stop( s );
988 ldap_unbind_ext( si->si_ld, NULL, NULL );
991 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
995 op = (Operation *) &opbuf;
996 connection_fake_init( &conn, op, ctx );
998 /* use global malloc for now */
999 op->o_tmpmemctx = NULL;
1000 op->o_tmpmfuncs = &ch_mfuncs;
1002 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1003 op->o_bd = be = si->si_be;
1004 op->o_dn = op->o_bd->be_rootdn;
1005 op->o_ndn = op->o_bd->be_rootndn;
1007 /* Establish session, do search */
1011 si->si_refreshDelete = 0;
1012 si->si_refreshPresent = 0;
1013 rc = do_syncrep1( op, si );
1016 /* Process results */
1017 if ( rc == LDAP_SUCCESS ) {
1018 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1020 rc = do_syncrep2( op, si );
1021 if ( rc == LDAP_SYNC_REFRESH_REQUIRED )
1024 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1025 /* If we succeeded, enable the connection for further listening.
1026 * If we failed, tear down the connection and reschedule.
1028 if ( rc == LDAP_SUCCESS ) {
1030 rc = connection_client_setup( s, do_syncrepl, arg );
1032 connection_client_enable( s );
1034 } else if ( !first ) {
1038 if ( rc == -2 ) rc = 0;
1042 /* At this point, we have 4 cases:
1043 * 1) for any hard failure, give up and remove this task
1044 * 2) for ServerDown, reschedule this task to run
1045 * 3) for Refresh and Success, reschedule to run
1046 * 4) for Persist and Success, reschedule to defer
1048 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1050 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1051 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1055 connection_client_stop( s );
1058 if ( rc == LDAP_SUCCESS ) {
1059 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1062 rtask->interval.tv_sec = si->si_interval;
1063 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1064 if ( si->si_retrynum ) {
1065 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
1066 si->si_retrynum[i] = si->si_retrynum_init[i];
1068 si->si_retrynum[i] = -2;
1071 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1072 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
1076 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1077 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1078 } else if ( si->si_retrynum[i] >= -1 ) {
1079 if ( si->si_retrynum[i] > 0 )
1080 si->si_retrynum[i]--;
1081 rtask->interval.tv_sec = si->si_retryinterval[i];
1082 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1083 slap_wake_listener();
1087 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1088 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1093 typedef struct logschema {
1094 struct berval ls_dn;
1095 struct berval ls_req;
1096 struct berval ls_mod;
1097 struct berval ls_newRdn;
1098 struct berval ls_delRdn;
1099 struct berval ls_newSup;
1102 static logschema changelog_sc = {
1103 BER_BVC("targetDN"),
1104 BER_BVC("changeType"),
1107 BER_BVC("deleteOldRDN"),
1108 BER_BVC("newSuperior")
1111 static logschema accesslog_sc = {
1115 BER_BVC("reqNewRDN"),
1116 BER_BVC("reqDeleteOldRDN"),
1117 BER_BVC("reqNewSuperior")
1120 static slap_verbmasks modops[] = {
1121 { BER_BVC("add"), LDAP_REQ_ADD },
1122 { BER_BVC("delete"), LDAP_REQ_DELETE },
1123 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1124 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1128 static Modifications *
1129 syncrepl_accesslog_mods(
1136 AttributeDescription *ad;
1137 struct berval bv, bv2;
1139 Modifications *mod = NULL, *modlist = NULL, **modtail;
1144 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1148 colon = strchr( bv.bv_val, ':' );
1150 continue; /* invalid */
1151 bv.bv_len = colon - bv.bv_val;
1152 if ( slap_bv2ad( &bv, &ad, &text )) {
1156 /* Ignore dynamically generated attrs */
1157 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1159 /* Ignore excluded attrs */
1160 if ( ldap_charray_inlist( si->si_exattrs,
1161 ad->ad_type->sat_cname.bv_val ))
1165 case '+': op = LDAP_MOD_ADD; break;
1166 case '-': op = LDAP_MOD_DELETE; break;
1167 case '=': op = LDAP_MOD_REPLACE; break;
1168 case '#': op = LDAP_MOD_INCREMENT; break;
1172 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1173 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1176 mod->sml_next = NULL;
1178 mod->sml_type = ad->ad_cname;
1179 mod->sml_values = NULL;
1180 mod->sml_nvalues = NULL;
1183 modtail = &mod->sml_next;
1185 bv.bv_val = colon + 3;
1186 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1187 ber_dupbv( &bv2, &bv );
1188 ber_bvarray_add( &mod->sml_values, &bv2 );
1193 static Modifications *
1194 syncrepl_changelog_mods(
1199 return NULL; /* FIXME */
1203 syncrepl_message_to_op(
1209 BerElement *ber = NULL;
1210 Modifications *modlist = NULL;
1212 SlapReply rs = { REP_RESULT };
1213 slap_callback cb = { NULL, null_callback, NULL, NULL };
1216 char txtbuf[SLAP_TEXT_BUFLEN];
1217 size_t textlen = sizeof txtbuf;
1219 struct berval bdn, dn = BER_BVNULL, ndn;
1220 struct berval bv, *bvals = NULL;
1221 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1222 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1223 psup = BER_BVNULL, nsup = BER_BVNULL;
1224 int rc, deleteOldRdn = 0;
1226 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1227 Debug( LDAP_DEBUG_ANY,
1228 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1232 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1237 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1239 if ( rc != LDAP_SUCCESS ) {
1240 Debug( LDAP_DEBUG_ANY,
1241 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1245 op->o_tag = LBER_DEFAULT;
1247 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1249 if ( bv.bv_val == NULL )
1252 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1254 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1255 ber_dupbv( &op->o_req_dn, &dn );
1256 ber_dupbv( &op->o_req_ndn, &ndn );
1257 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1258 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1259 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1260 int i = verb_to_mask( bvals[0].bv_val, modops );
1262 Debug( LDAP_DEBUG_ANY,
1263 "syncrepl_message_to_op : unknown op %s",
1264 bvals[0].bv_val, 0, 0 );
1269 op->o_tag = modops[i].mask;
1270 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1271 /* Parse attribute into modlist */
1272 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1273 modlist = syncrepl_accesslog_mods( si, bvals );
1275 modlist = syncrepl_changelog_mods( si, bvals );
1276 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1278 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1279 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1281 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1283 } else if ( !ber_bvstrcasecmp( &bv,
1284 &slap_schema.si_ad_entryCSN->ad_cname )) {
1285 slap_queue_csn( op, bvals );
1290 /* If we didn't get a mod type or a target DN, bail out */
1291 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1296 op->o_callback = &cb;
1298 switch( op->o_tag ) {
1300 case LDAP_REQ_MODIFY:
1301 /* If we didn't get required data, bail */
1302 if ( !modlist ) goto done;
1304 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1306 if ( rc != LDAP_SUCCESS ) {
1307 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1312 if ( op->o_tag == LDAP_REQ_ADD ) {
1313 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1314 op->ora_e->e_name = op->o_req_dn;
1315 op->ora_e->e_nname = op->o_req_ndn;
1316 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1317 if( rc != LDAP_SUCCESS ) {
1318 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1321 rc = op->o_bd->be_add( op, &rs );
1323 be_entry_release_w( op, op->ora_e );
1325 op->orm_modlist = modlist;
1326 rc = op->o_bd->be_modify( op, &rs );
1329 case LDAP_REQ_MODRDN:
1330 if ( BER_BVISNULL( &rdn )) goto done;
1332 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1334 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1336 if ( !BER_BVISNULL( &sup )) {
1337 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1339 op->orr_newSup = &psup;
1340 op->orr_nnewSup = ⊅
1342 op->orr_newrdn = prdn;
1343 op->orr_nnewrdn = nrdn;
1344 op->orr_deleteoldrdn = deleteOldRdn;
1345 rc = op->o_bd->be_modrdn( op, &rs );
1349 slap_graduate_commit_csn( op );
1351 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1352 if ( !BER_BVISNULL( &rdn )) {
1353 if ( !BER_BVISNULL( &nsup ))
1354 ch_free( nsup.bv_val );
1355 if ( !BER_BVISNULL( &psup ))
1356 ch_free( psup.bv_val );
1357 if ( !BER_BVISNULL( &nrdn ))
1358 ch_free( nrdn.bv_val );
1359 if ( !BER_BVISNULL( &prdn ))
1360 ch_free( prdn.bv_val );
1362 ber_free ( ber, 0 );
1367 syncrepl_message_to_entry(
1371 Modifications **modlist,
1377 BerElement *ber = NULL;
1380 Modifications **modtail = modlist;
1383 char txtbuf[SLAP_TEXT_BUFLEN];
1384 size_t textlen = sizeof txtbuf;
1386 struct berval bdn = {0, NULL}, dn, ndn;
1391 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1392 Debug( LDAP_DEBUG_ANY,
1393 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1397 op->o_tag = LDAP_REQ_ADD;
1399 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1401 if ( rc != LDAP_SUCCESS ) {
1402 Debug( LDAP_DEBUG_ANY,
1403 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1407 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1408 ber_dupbv( &op->o_req_dn, &dn );
1409 ber_dupbv( &op->o_req_ndn, &ndn );
1410 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1411 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1413 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1416 return LDAP_SUCCESS;
1419 if ( entry == NULL ) {
1423 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1425 e->e_name = op->o_req_dn;
1426 e->e_nname = op->o_req_ndn;
1428 while ( ber_remaining( ber ) ) {
1429 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1430 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1435 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1437 mod->sml_op = LDAP_MOD_REPLACE;
1439 mod->sml_next = NULL;
1440 mod->sml_desc = NULL;
1441 mod->sml_type = tmp.sml_type;
1442 mod->sml_values = tmp.sml_values;
1443 mod->sml_nvalues = NULL;
1446 modtail = &mod->sml_next;
1449 if ( *modlist == NULL ) {
1450 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1456 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1458 if ( rc != LDAP_SUCCESS ) {
1459 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1464 /* Strip out dynamically generated attrs */
1465 for ( modtail = modlist; *modtail ; ) {
1467 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1468 *modtail = mod->sml_next;
1469 slap_mod_free( &mod->sml_mod, 0 );
1472 modtail = &mod->sml_next;
1476 /* Strip out attrs in exattrs list */
1477 for ( modtail = modlist; *modtail ; ) {
1479 if ( ldap_charray_inlist( si->si_exattrs,
1480 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1481 *modtail = mod->sml_next;
1482 slap_mod_free( &mod->sml_mod, 0 );
1485 modtail = &mod->sml_next;
1489 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1490 if( rc != LDAP_SUCCESS ) {
1491 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1496 ber_free ( ber, 0 );
1497 if ( rc != LDAP_SUCCESS ) {
1507 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1509 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1510 * entry if a previous refresh was interrupted before sending us a new
1511 * context state. We try to compare the new entry to the existing entry
1512 * and ignore the new entry if they are the same.
1514 * Also, we may get an update where the entryDN has changed, due to
1515 * a ModDn on the provider. We detect this as well, so we can issue
1516 * the corresponding operation locally.
1518 * In the case of a modify, we get a list of all the attributes
1519 * in the original entry. Rather than deleting the entry and re-adding it,
1520 * we issue a Modify request that deletes all the attributes and adds all
1521 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1524 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1525 * a ModDN we will issue both operations on the local database.
1527 typedef struct dninfo {
1531 int renamed; /* Was an existing entry renamed? */
1532 int wasChanged; /* are the attributes changed? */
1533 int attrs; /* how many attribute types are in the ads list */
1534 AttributeDescription **ads;
1542 Modifications** modlist,
1544 struct berval* syncUUID,
1545 struct sync_cookie* syncCookie_req,
1546 struct berval* syncCSN )
1548 Backend *be = op->o_bd;
1549 slap_callback cb = { NULL, NULL, NULL, NULL };
1550 struct berval *syncuuid_bv = NULL;
1551 struct berval syncUUID_strrep = BER_BVNULL;
1552 struct berval uuid_bv = BER_BVNULL;
1554 SlapReply rs_search = {REP_RESULT};
1555 SlapReply rs_delete = {REP_RESULT};
1556 SlapReply rs_add = {REP_RESULT};
1557 SlapReply rs_modify = {REP_RESULT};
1559 #ifdef LDAP_COMP_MATCH
1560 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1562 AttributeAssertion ava = { NULL, BER_BVNULL };
1564 int rc = LDAP_SUCCESS;
1565 int ret = LDAP_SUCCESS;
1567 struct berval pdn = BER_BVNULL;
1571 switch( syncstate ) {
1572 case LDAP_SYNC_PRESENT:
1573 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1575 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1578 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1580 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1582 case LDAP_SYNC_DELETE:
1583 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1585 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1587 case LDAP_SYNC_MODIFY:
1588 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1590 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1593 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1595 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1598 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1599 if ( !si->si_refreshPresent ) {
1600 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1601 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1602 syncuuid_cmp, avl_dup_error );
1606 if ( syncstate == LDAP_SYNC_PRESENT ) {
1608 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1609 if ( entry == NULL ) {
1614 f.f_choice = LDAP_FILTER_EQUALITY;
1616 ava.aa_desc = slap_schema.si_ad_entryUUID;
1617 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1618 ava.aa_value = *syncUUID;
1619 op->ors_filter = &f;
1621 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1622 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1623 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1624 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1625 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1626 syncUUID->bv_val, syncUUID->bv_len );
1627 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1628 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1630 op->o_tag = LDAP_REQ_SEARCH;
1631 op->ors_scope = LDAP_SCOPE_SUBTREE;
1632 op->ors_deref = LDAP_DEREF_NEVER;
1634 /* get the entry for this UUID */
1635 op->o_req_dn = si->si_base;
1636 op->o_req_ndn = si->si_base;
1638 op->o_time = slap_get_time();
1639 op->ors_tlimit = SLAP_NO_LIMIT;
1642 op->ors_attrs = slap_anlist_all_attributes;
1643 op->ors_attrsonly = 0;
1645 /* set callback function */
1646 op->o_callback = &cb;
1647 cb.sc_response = dn_callback;
1648 cb.sc_private = &dni;
1649 dni.new_entry = entry;
1651 if ( limits_check( op, &rs_search ) == 0 ) {
1652 rc = be->be_search( op, &rs_search );
1653 Debug( LDAP_DEBUG_SYNC,
1654 "syncrepl_entry: %s (%d)\n",
1655 "be_search", rc, 0 );
1658 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1659 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1662 cb.sc_response = null_callback;
1665 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1666 Debug( LDAP_DEBUG_SYNC,
1667 "syncrepl_entry: %s\n",
1668 entry->e_name.bv_val, 0, 0 );
1670 Debug( LDAP_DEBUG_SYNC,
1671 "syncrepl_entry: %s\n",
1672 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1675 if ( syncstate != LDAP_SYNC_DELETE ) {
1676 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1679 /* add if missing */
1680 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1681 &syncUUID_strrep, syncUUID );
1683 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1684 /* replace only if necessary */
1685 if ( a->a_nvals != a->a_vals ) {
1686 ber_memfree( a->a_nvals[0].bv_val );
1687 ber_dupbv( &a->a_nvals[0], syncUUID );
1689 ber_memfree( a->a_vals[0].bv_val );
1690 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1694 switch ( syncstate ) {
1696 case LDAP_SYNC_MODIFY:
1698 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1700 op->o_csn = a->a_vals[0];
1703 if ( BER_BVISNULL( &dni.dn )) {
1705 op->o_req_dn = entry->e_name;
1706 op->o_req_ndn = entry->e_nname;
1707 op->o_tag = LDAP_REQ_ADD;
1710 rc = be->be_add( op, &rs_add );
1711 Debug( LDAP_DEBUG_SYNC,
1712 "syncrepl_entry: %s (%d)\n",
1714 switch ( rs_add.sr_err ) {
1716 be_entry_release_w( op, entry );
1721 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1722 * only if the suffix entry is not present */
1723 case LDAP_NO_SUCH_OBJECT:
1724 syncrepl_add_glue( op, entry );
1728 /* if an entry was added via syncrepl_add_glue(),
1729 * it likely has no entryUUID, so the previous
1730 * be_search() doesn't find it. In this case,
1731 * give syncrepl a chance to modify it. Also
1732 * allow for entries that were recreated with the
1733 * same DN but a different entryUUID.
1735 case LDAP_ALREADY_EXISTS:
1737 Operation op2 = *op;
1738 SlapReply rs2 = { 0 };
1739 slap_callback cb2 = { 0 };
1741 op2.o_tag = LDAP_REQ_SEARCH;
1742 op2.o_req_dn = entry->e_name;
1743 op2.o_req_ndn = entry->e_nname;
1744 op2.ors_scope = LDAP_SCOPE_BASE;
1745 op2.ors_deref = LDAP_DEREF_NEVER;
1746 op2.ors_attrs = slap_anlist_all_attributes;
1747 op2.ors_attrsonly = 0;
1748 op2.ors_limit = NULL;
1750 op2.ors_tlimit = SLAP_NO_LIMIT;
1752 f.f_choice = LDAP_FILTER_PRESENT;
1753 f.f_desc = slap_schema.si_ad_objectClass;
1754 op2.ors_filter = &f;
1755 op2.ors_filterstr = generic_filterstr;
1757 op2.o_callback = &cb2;
1758 cb2.sc_response = dn_callback;
1759 cb2.sc_private = &dni;
1761 be->be_search( &op2, &rs2 );
1769 Debug( LDAP_DEBUG_ANY,
1770 "syncrepl_entry : be_add failed (%d)\n",
1771 rs_add.sr_err, 0, 0 );
1778 op->o_req_dn = dni.dn;
1779 op->o_req_ndn = dni.ndn;
1780 if ( dni.renamed ) {
1781 struct berval noldp, newp, nnewp;
1783 op->o_tag = LDAP_REQ_MODRDN;
1784 dnRdn( &entry->e_name, &op->orr_newrdn );
1785 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1787 dnParent( &dni.ndn, &noldp );
1788 dnParent( &entry->e_nname, &nnewp );
1789 if ( !dn_match( &noldp, &newp )) {
1790 dnParent( &entry->e_name, &newp );
1791 op->orr_newSup = &newp;
1792 op->orr_nnewSup = &nnewp;
1794 op->orr_deleteoldrdn = 0;
1795 rc = be->be_modrdn( op, &rs_modify );
1796 Debug( LDAP_DEBUG_SYNC,
1797 "syncrepl_entry: %s (%d)\n",
1798 "be_modrdn", rc, 0 );
1799 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1800 op->o_req_dn = entry->e_name;
1801 op->o_req_ndn = entry->e_nname;
1807 if ( dni.wasChanged ) {
1808 Modifications *mod, *modhead = NULL;
1809 Modifications *modtail = NULL;
1812 op->o_tag = LDAP_REQ_MODIFY;
1814 assert( *modlist != NULL );
1816 /* Delete all the old attrs */
1817 for ( i = 0; i < dni.attrs; i++ ) {
1818 mod = ch_malloc( sizeof( Modifications ) );
1819 mod->sml_op = LDAP_MOD_DELETE;
1821 mod->sml_desc = dni.ads[i];
1822 mod->sml_type = mod->sml_desc->ad_cname;
1823 mod->sml_values = NULL;
1824 mod->sml_nvalues = NULL;
1825 if ( !modhead ) modhead = mod;
1827 modtail->sml_next = mod;
1832 /* Append passed in list to ours */
1834 modtail->sml_next = *modlist;
1840 /* Find end of this list */
1841 for ( ; mod != NULL; mod = mod->sml_next ) {
1845 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1846 mod->sml_op = LDAP_MOD_REPLACE;
1848 mod->sml_desc = slap_schema.si_ad_entryUUID;
1849 mod->sml_type = mod->sml_desc->ad_cname;
1850 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1851 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1852 ber_dupbv( &uuid_bv, syncUUID );
1853 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1854 modtail->sml_next = mod;
1856 op->o_tag = LDAP_REQ_MODIFY;
1857 op->orm_modlist = *modlist;
1859 rc = be->be_modify( op, &rs_modify );
1860 Debug( LDAP_DEBUG_SYNC,
1861 "syncrepl_entry: %s (%d)\n",
1862 "be_modify", rc, 0 );
1863 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1864 Debug( LDAP_DEBUG_ANY,
1865 "syncrepl_entry : be_modify failed (%d)\n",
1866 rs_modify.sr_err, 0, 0 );
1871 case LDAP_SYNC_DELETE :
1872 if ( !BER_BVISNULL( &dni.dn )) {
1873 op->o_req_dn = dni.dn;
1874 op->o_req_ndn = dni.ndn;
1875 op->o_tag = LDAP_REQ_DELETE;
1876 rc = be->be_delete( op, &rs_delete );
1877 Debug( LDAP_DEBUG_SYNC,
1878 "syncrepl_entry: %s (%d)\n",
1879 "be_delete", rc, 0 );
1881 while ( rs_delete.sr_err == LDAP_SUCCESS
1882 && op->o_delete_glue_parent ) {
1883 op->o_delete_glue_parent = 0;
1884 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1885 slap_callback cb = { NULL };
1886 cb.sc_response = slap_null_cb;
1887 dnParent( &op->o_req_ndn, &pdn );
1889 op->o_req_ndn = pdn;
1890 op->o_callback = &cb;
1891 op->o_bd->be_delete( op, &rs_delete );
1901 Debug( LDAP_DEBUG_ANY,
1902 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1908 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1909 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1910 BER_BVZERO( &syncUUID_strrep );
1913 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1915 if ( !BER_BVISNULL( &dni.ndn ) ) {
1916 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1918 if ( !BER_BVISNULL( &dni.dn ) ) {
1919 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1921 BER_BVZERO( &op->o_csn );
1925 static struct berval gcbva[] = {
1931 #define NP_DELETE_ONE 2
1934 syncrepl_del_nonpresent(
1939 Backend* be = op->o_bd;
1940 slap_callback cb = { NULL };
1941 SlapReply rs_search = {REP_RESULT};
1942 SlapReply rs_delete = {REP_RESULT};
1943 SlapReply rs_modify = {REP_RESULT};
1944 struct nonpresent_entry *np_list, *np_prev;
1946 AttributeName an[2];
1948 struct berval pdn = BER_BVNULL;
1950 op->o_req_dn = si->si_base;
1951 op->o_req_ndn = si->si_base;
1953 cb.sc_response = nonpresent_callback;
1956 op->o_callback = &cb;
1957 op->o_tag = LDAP_REQ_SEARCH;
1958 op->ors_scope = si->si_scope;
1959 op->ors_deref = LDAP_DEREF_NEVER;
1960 op->o_time = slap_get_time();
1961 op->ors_tlimit = SLAP_NO_LIMIT;
1966 #ifdef LDAP_COMP_MATCH
1967 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1969 AttributeAssertion eq = { NULL, BER_BVNULL };
1973 op->ors_attrsonly = 1;
1974 op->ors_attrs = slap_anlist_no_attrs;
1975 op->ors_limit = NULL;
1976 op->ors_filter = &uf;
1979 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1981 uf.f_choice = LDAP_FILTER_EQUALITY;
1982 si->si_refreshDelete |= NP_DELETE_ONE;
1984 for (i=0; uuids[i].bv_val; i++) {
1986 uf.f_av_value = uuids[i];
1987 rc = be->be_search( op, &rs_search );
1989 si->si_refreshDelete ^= NP_DELETE_ONE;
1991 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1992 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1993 an[0].an_desc = slap_schema.si_ad_entryUUID;
1995 op->ors_slimit = SLAP_NO_LIMIT;
1996 op->ors_attrsonly = 0;
1997 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1998 op->ors_filterstr = si->si_filterstr;
1999 op->o_nocaching = 1;
2001 if ( limits_check( op, &rs_search ) == 0 ) {
2002 rc = be->be_search( op, &rs_search );
2004 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2007 op->o_nocaching = 0;
2009 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2011 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2013 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2014 while ( np_list != NULL ) {
2015 LDAP_LIST_REMOVE( np_list, npe_link );
2017 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2018 op->o_tag = LDAP_REQ_DELETE;
2019 op->o_callback = &cb;
2020 cb.sc_response = null_callback;
2022 op->o_req_dn = *np_prev->npe_name;
2023 op->o_req_ndn = *np_prev->npe_nname;
2024 rc = op->o_bd->be_delete( op, &rs_delete );
2026 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2027 Modifications mod1, mod2;
2028 mod1.sml_op = LDAP_MOD_REPLACE;
2030 mod1.sml_desc = slap_schema.si_ad_objectClass;
2031 mod1.sml_type = mod1.sml_desc->ad_cname;
2032 mod1.sml_values = &gcbva[0];
2033 mod1.sml_nvalues = NULL;
2034 mod1.sml_next = &mod2;
2036 mod2.sml_op = LDAP_MOD_REPLACE;
2038 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2039 mod2.sml_type = mod2.sml_desc->ad_cname;
2040 mod2.sml_values = &gcbva[1];
2041 mod2.sml_nvalues = NULL;
2042 mod2.sml_next = NULL;
2044 op->o_tag = LDAP_REQ_MODIFY;
2045 op->orm_modlist = &mod1;
2047 rc = be->be_modify( op, &rs_modify );
2050 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2051 op->o_delete_glue_parent ) {
2052 op->o_delete_glue_parent = 0;
2053 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2054 slap_callback cb = { NULL };
2055 cb.sc_response = slap_null_cb;
2056 dnParent( &op->o_req_ndn, &pdn );
2058 op->o_req_ndn = pdn;
2059 op->o_callback = &cb;
2060 /* give it a root privil ? */
2061 op->o_bd->be_delete( op, &rs_delete );
2067 op->o_delete_glue_parent = 0;
2069 ber_bvfree( np_prev->npe_name );
2070 ber_bvfree( np_prev->npe_nname );
2074 slap_graduate_commit_csn( op );
2085 Backend *be = op->o_bd;
2086 slap_callback cb = { NULL };
2091 struct berval dn = {0, NULL};
2092 struct berval ndn = {0, NULL};
2094 SlapReply rs_add = {REP_RESULT};
2097 op->o_tag = LDAP_REQ_ADD;
2098 op->o_callback = &cb;
2099 cb.sc_response = null_callback;
2100 cb.sc_private = NULL;
2105 /* count RDNs in suffix */
2106 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2107 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2117 /* Start with BE suffix */
2118 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2119 comma = strrchr( dn.bv_val, ',' );
2120 if ( ptr ) *ptr = ',';
2121 if ( comma ) *comma = '\0';
2126 dn.bv_len -= ptr - dn.bv_val;
2129 /* the normalizedDNs are always the same length, no counting
2132 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2133 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2134 ndn.bv_len = be->be_nsuffix[0].bv_len;
2137 while ( ndn.bv_val > e->e_nname.bv_val ) {
2138 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2139 ber_dupbv( &glue->e_name, &dn );
2140 ber_dupbv( &glue->e_nname, &ndn );
2142 a = ch_calloc( 1, sizeof( Attribute ));
2143 a->a_desc = slap_schema.si_ad_objectClass;
2145 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2146 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2147 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2148 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2150 a->a_nvals = a->a_vals;
2152 a->a_next = glue->e_attrs;
2155 a = ch_calloc( 1, sizeof( Attribute ));
2156 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2158 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2159 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2160 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2162 a->a_nvals = a->a_vals;
2164 a->a_next = glue->e_attrs;
2167 op->o_req_dn = glue->e_name;
2168 op->o_req_ndn = glue->e_nname;
2170 rc = be->be_add ( op, &rs_add );
2171 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2172 be_entry_release_w( op, glue );
2174 /* incl. ALREADY EXIST */
2178 /* Move to next child */
2179 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2182 if ( ptr == e->e_name.bv_val ) break;
2184 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2185 for( ptr = ndn.bv_val-2;
2186 ptr > e->e_nname.bv_val && *ptr != ',';
2192 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2195 op->o_req_dn = e->e_name;
2196 op->o_req_ndn = e->e_nname;
2198 rc = be->be_add ( op, &rs_add );
2199 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2200 be_entry_release_w( op, e );
2209 syncrepl_updateCookie(
2213 struct sync_cookie *syncCookie )
2215 Backend *be = op->o_bd;
2216 Modifications mod = { { 0 } };
2217 struct berval vals[ 2 ];
2221 slap_callback cb = { NULL };
2222 SlapReply rs_modify = {REP_RESULT};
2224 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2225 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2227 mod.sml_op = LDAP_MOD_REPLACE;
2228 mod.sml_desc = slap_schema.si_ad_contextCSN;
2229 mod.sml_type = mod.sml_desc->ad_cname;
2230 mod.sml_values = vals;
2231 vals[0] = si->si_syncCookie.ctxcsn;
2232 vals[1].bv_val = NULL;
2235 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2237 op->o_tag = LDAP_REQ_MODIFY;
2239 assert( si->si_rid < 1000 );
2241 cb.sc_response = null_callback;
2244 op->o_callback = &cb;
2245 op->o_req_dn = op->o_bd->be_suffix[0];
2246 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2248 /* update contextCSN */
2249 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2250 op->orm_modlist = &mod;
2251 rc = be->be_modify( op, &rs_modify );
2254 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2255 Debug( LDAP_DEBUG_ANY,
2256 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2259 slap_graduate_commit_csn( op );
2269 dninfo *dni = op->o_callback->sc_private;
2271 if ( rs->sr_type == REP_SEARCH ) {
2272 if ( !BER_BVISNULL( &dni->dn ) ) {
2273 Debug( LDAP_DEBUG_ANY,
2274 "dn_callback : consistency error - "
2275 "entryUUID is not unique\n", 0, 0, 0 );
2277 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2278 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2279 /* If there is a new entry, see if it differs from the old.
2280 * We compare the non-normalized values so that cosmetic changes
2281 * in the provider are always propagated.
2283 if ( dni->new_entry ) {
2284 Attribute *old, *new;
2287 /* Did the DN change? Note that we don't explicitly try to
2288 * discover if the deleteOldRdn argument applies here. It
2289 * would save an unnecessary Modify if we detected it, but
2290 * that's a fair amount of trouble to compare the two attr
2291 * lists in detail. (Just test normalized DN; we ignore
2292 * insignificant changes here.)
2294 if ( !dn_match( &rs->sr_entry->e_nname,
2295 &dni->new_entry->e_nname ) )
2300 for ( i = 0, old = rs->sr_entry->e_attrs;
2302 i++, old = old->a_next )
2307 /* We assume that attributes are saved in the same order
2308 * in the remote and local databases. So if we walk through
2309 * the attributeDescriptions one by one they should match in
2310 * lock step. If not, we signal a change. Otherwise we test
2313 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2315 old = old->a_next, new = new->a_next )
2317 if ( old->a_desc != new->a_desc ) {
2318 dni->wasChanged = 1;
2321 for ( i = 0; ; i++ ) {
2323 nold = BER_BVISNULL( &old->a_vals[i] );
2324 nnew = BER_BVISNULL( &new->a_vals[i] );
2325 /* If both are empty, stop looking */
2326 if ( nold && nnew ) {
2329 /* If they are different, stop looking */
2330 if ( nold != nnew ) {
2331 dni->wasChanged = 1;
2334 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2335 dni->wasChanged = 1;
2339 if ( dni->wasChanged ) break;
2341 if ( dni->wasChanged ) {
2342 dni->ads = op->o_tmpalloc( dni->attrs *
2343 sizeof(AttributeDescription *), op->o_tmpmemctx );
2345 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2346 dni->ads[i] = old->a_desc;
2352 } else if ( rs->sr_type == REP_RESULT ) {
2353 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2354 Debug( LDAP_DEBUG_ANY,
2355 "dn_callback : consistency error - "
2356 "entryUUID is not unique\n", 0, 0, 0 );
2360 return LDAP_SUCCESS;
2364 nonpresent_callback(
2368 syncinfo_t *si = op->o_callback->sc_private;
2371 struct berval* present_uuid = NULL;
2372 struct nonpresent_entry *np_entry;
2374 if ( rs->sr_type == REP_RESULT ) {
2375 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2376 si->si_presentlist = NULL;
2378 } else if ( rs->sr_type == REP_SEARCH ) {
2379 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2380 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2382 if ( a == NULL ) return 0;
2384 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2388 if ( present_uuid == NULL ) {
2389 np_entry = (struct nonpresent_entry *)
2390 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2391 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2392 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2393 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2396 avl_delete( &si->si_presentlist,
2397 &a->a_nvals[0], syncuuid_cmp );
2398 ch_free( present_uuid->bv_val );
2399 ch_free( present_uuid );
2402 return LDAP_SUCCESS;
2410 if ( rs->sr_err != LDAP_SUCCESS &&
2411 rs->sr_err != LDAP_REFERRAL &&
2412 rs->sr_err != LDAP_ALREADY_EXISTS &&
2413 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2414 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2416 Debug( LDAP_DEBUG_ANY,
2417 "null_callback : error code 0x%x\n",
2420 return LDAP_SUCCESS;
2423 static struct berval *
2424 slap_uuidstr_from_normalized(
2425 struct berval* uuidstr,
2426 struct berval* normalized,
2430 unsigned char nibble;
2433 if ( normalized == NULL ) return NULL;
2434 if ( normalized->bv_len != 16 ) return NULL;
2439 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2440 if ( new == NULL ) {
2447 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2448 if ( new != uuidstr ) {
2449 slap_sl_free( new, ctx );
2454 for ( i = 0; i < 16; i++ ) {
2455 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2456 new->bv_val[(i<<1)+d] = '-';
2460 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2461 if ( nibble < 10 ) {
2462 new->bv_val[(i<<1)+d] = nibble + '0';
2464 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2467 nibble = (normalized->bv_val[i]) & 0xF;
2468 if ( nibble < 10 ) {
2469 new->bv_val[(i<<1)+d+1] = nibble + '0';
2471 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2475 new->bv_val[new->bv_len] = '\0';
2480 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2482 const struct berval *uuid1 = v_uuid1;
2483 const struct berval *uuid2 = v_uuid2;
2484 int rc = uuid1->bv_len - uuid2->bv_len;
2485 if ( rc ) return rc;
2486 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2490 avl_ber_bvfree( void *v_bv )
2492 struct berval *bv = (struct berval *)v_bv;
2494 if( v_bv == NULL ) return;
2495 if ( !BER_BVISNULL( bv ) ) {
2496 ch_free( bv->bv_val );
2498 ch_free( (char *) bv );
2502 syncinfo_free( syncinfo_t *sie )
2504 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2505 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2506 ch_free( sie->si_provideruri.bv_val );
2509 bindconf_free( &sie->si_bindconf );
2511 if ( sie->si_filterstr.bv_val ) {
2512 ch_free( sie->si_filterstr.bv_val );
2514 if ( sie->si_base.bv_val ) {
2515 ch_free( sie->si_base.bv_val );
2517 if ( sie->si_attrs ) {
2519 while ( sie->si_attrs[i] != NULL ) {
2520 ch_free( sie->si_attrs[i] );
2523 ch_free( sie->si_attrs );
2525 if ( sie->si_exattrs ) {
2527 while ( sie->si_exattrs[i] != NULL ) {
2528 ch_free( sie->si_exattrs[i] );
2531 ch_free( sie->si_exattrs );
2533 if ( sie->si_anlist ) {
2535 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2536 ch_free( sie->si_anlist[i].an_name.bv_val );
2539 ch_free( sie->si_anlist );
2541 if ( sie->si_exanlist ) {
2543 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2544 ch_free( sie->si_exanlist[i].an_name.bv_val );
2547 ch_free( sie->si_exanlist );
2549 if ( sie->si_retryinterval ) {
2550 ch_free( sie->si_retryinterval );
2552 if ( sie->si_retrynum ) {
2553 ch_free( sie->si_retrynum );
2555 if ( sie->si_retrynum_init ) {
2556 ch_free( sie->si_retrynum_init );
2558 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2559 if ( sie->si_presentlist ) {
2560 avl_free( sie->si_presentlist, avl_ber_bvfree );
2563 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2565 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2566 struct nonpresent_entry* npe;
2567 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2568 LDAP_LIST_REMOVE( npe, npe_link );
2569 if ( npe->npe_name ) {
2570 if ( npe->npe_name->bv_val ) {
2571 ch_free( npe->npe_name->bv_val );
2573 ch_free( npe->npe_name );
2575 if ( npe->npe_nname ) {
2576 if ( npe->npe_nname->bv_val ) {
2577 ch_free( npe->npe_nname->bv_val );
2579 ch_free( npe->npe_nname );
2588 /* NOTE: used & documented in slapd.conf(5) */
2590 #define PROVIDERSTR "provider"
2591 #define SCHEMASTR "schemachecking"
2592 #define FILTERSTR "filter"
2593 #define SEARCHBASESTR "searchbase"
2594 #define SCOPESTR "scope"
2595 #define ATTRSONLYSTR "attrsonly"
2596 #define ATTRSSTR "attrs"
2597 #define TYPESTR "type"
2598 #define INTERVALSTR "interval"
2599 #define RETRYSTR "retry"
2600 #define SLIMITSTR "sizelimit"
2601 #define TLIMITSTR "timelimit"
2602 #define SYNCDATASTR "syncdata"
2604 /* FIXME: undocumented */
2605 #define LOGBASESTR "logbase"
2606 #define LOGFILTERSTR "logfilter"
2607 #define OLDAUTHCSTR "bindprincipal"
2608 #define EXATTRSSTR "exattrs"
2609 #define MANAGEDSAITSTR "manageDSAit"
2612 #define LASTMODSTR "lastmod"
2613 #define LMGENSTR "gen"
2614 #define LMNOSTR "no"
2615 #define LMREQSTR "req"
2616 #define SRVTABSTR "srvtab"
2617 #define SUFFIXSTR "suffix"
2620 #define GOT_ID 0x0001
2621 #define GOT_PROVIDER 0x0002
2624 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2630 { BER_BVC("base"), LDAP_SCOPE_BASE },
2631 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2632 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2633 #ifdef LDAP_SCOPE_SUBORDINATE
2634 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2635 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2637 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2638 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2642 static slap_verbmasks datamodes[] = {
2643 { BER_BVC("default"), SYNCDATA_DEFAULT },
2644 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2645 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2650 parse_syncrepl_line(
2660 for ( i = 1; i < cargc; i++ ) {
2661 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2662 STRLENOF( IDSTR "=" ) ) )
2665 /* '\0' string terminator accounts for '=' */
2666 val = cargv[ i ] + STRLENOF( IDSTR "=" );
2668 if ( tmp >= 1000 || tmp < 0 ) {
2669 fprintf( stderr, "Error: parse_syncrepl_line: "
2670 "syncrepl id %d is out of range [0..999]\n", tmp );
2675 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2676 STRLENOF( PROVIDERSTR "=" ) ) )
2678 val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2679 ber_str2bv( val, 0, 1, &si->si_provideruri );
2680 gots |= GOT_PROVIDER;
2681 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2682 STRLENOF( SCHEMASTR "=" ) ) )
2684 val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2685 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2686 si->si_schemachecking = 1;
2687 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2688 si->si_schemachecking = 0;
2690 si->si_schemachecking = 1;
2692 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2693 STRLENOF( FILTERSTR "=" ) ) )
2695 val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2696 if ( si->si_filterstr.bv_val )
2697 ch_free( si->si_filterstr.bv_val );
2698 ber_str2bv( val, 0, 1, &si->si_filterstr );
2699 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2700 STRLENOF( LOGFILTERSTR "=" ) ) )
2702 val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2703 if ( si->si_logfilterstr.bv_val )
2704 ch_free( si->si_logfilterstr.bv_val );
2705 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2706 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2707 STRLENOF( SEARCHBASESTR "=" ) ) )
2712 val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2713 if ( si->si_base.bv_val ) {
2714 ch_free( si->si_base.bv_val );
2716 ber_str2bv( val, 0, 0, &bv );
2717 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2718 if ( rc != LDAP_SUCCESS ) {
2719 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2720 val, rc, ldap_err2string( rc ) );
2723 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2724 STRLENOF( LOGBASESTR "=" ) ) )
2729 val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2730 if ( si->si_logbase.bv_val ) {
2731 ch_free( si->si_logbase.bv_val );
2733 ber_str2bv( val, 0, 0, &bv );
2734 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2735 if ( rc != LDAP_SUCCESS ) {
2736 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2737 val, rc, ldap_err2string( rc ) );
2740 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2741 STRLENOF( SCOPESTR "=" ) ) )
2744 val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2745 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2746 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2747 si->si_scope = scopes[j].val;
2751 if ( BER_BVISNULL(&scopes[j].key) ) {
2752 fprintf( stderr, "Error: parse_syncrepl_line: "
2753 "unknown scope \"%s\"\n", val);
2756 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2757 STRLENOF( ATTRSONLYSTR "=" ) ) )
2759 si->si_attrsonly = 1;
2760 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2761 STRLENOF( ATTRSSTR "=" ) ) )
2763 val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2764 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2766 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2767 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2768 if ( si->si_anlist == NULL ) {
2769 ch_free( attr_fname );
2772 si->si_anfile = attr_fname;
2774 char *str, *s, *next;
2775 char delimstr[] = " ,\t";
2776 str = ch_strdup( val );
2777 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2779 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2781 if ( strlen(s) == 1 && *s == '*' ) {
2782 si->si_allattrs = 1;
2783 *(val + ( s - str )) = delimstr[0];
2785 if ( strlen(s) == 1 && *s == '+' ) {
2786 si->si_allopattrs = 1;
2787 *(val + ( s - str )) = delimstr[0];
2791 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2792 if ( si->si_anlist == NULL ) {
2796 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2797 STRLENOF( EXATTRSSTR "=" ) ) )
2799 val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2800 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2802 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2803 si->si_exanlist = file2anlist(
2804 si->si_exanlist, attr_fname, " ,\t" );
2805 if ( si->si_exanlist == NULL ) {
2806 ch_free( attr_fname );
2809 ch_free( attr_fname );
2811 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2812 if ( si->si_exanlist == NULL ) {
2816 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2817 STRLENOF( TYPESTR "=" ) ) )
2819 val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2820 if ( !strncasecmp( val, "refreshOnly",
2821 STRLENOF("refreshOnly") ))
2823 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2824 } else if ( !strncasecmp( val, "refreshAndPersist",
2825 STRLENOF("refreshAndPersist") ))
2827 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2828 si->si_interval = 60;
2830 fprintf( stderr, "Error: parse_syncrepl_line: "
2831 "unknown sync type \"%s\"\n", val);
2834 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2835 STRLENOF( INTERVALSTR "=" ) ) )
2837 val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2838 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2839 si->si_interval = 0;
2847 hstr = strchr( dstr, ':' );
2848 if ( hstr == NULL ) {
2849 fprintf( stderr, "Error: parse_syncrepl_line: "
2850 "invalid interval \"%s\"\n", val );
2854 mstr = strchr( hstr, ':' );
2855 if ( mstr == NULL ) {
2856 fprintf( stderr, "Error: parse_syncrepl_line: "
2857 "invalid interval \"%s\"\n", val );
2861 sstr = strchr( mstr, ':' );
2862 if ( sstr == NULL ) {
2863 fprintf( stderr, "Error: parse_syncrepl_line: "
2864 "invalid interval \"%s\"\n", val );
2873 if (( hh > 24 ) || ( hh < 0 ) ||
2874 ( mm > 60 ) || ( mm < 0 ) ||
2875 ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2876 fprintf( stderr, "Error: parse_syncrepl_line: "
2877 "invalid interval \"%s\"\n", val );
2880 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2882 if ( si->si_interval < 0 ) {
2883 fprintf( stderr, "Error: parse_syncrepl_line: "
2884 "invalid interval \"%ld\"\n",
2885 (long) si->si_interval);
2888 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2889 STRLENOF( RETRYSTR "=" ) ) )
2894 val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2895 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2896 retry_list[0] = NULL;
2898 slap_str2clist( &retry_list, val, " ,\t" );
2900 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2904 "Error: incomplete syncrepl retry list\n" );
2905 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2906 ch_free( retry_list[k] );
2908 ch_free( retry_list );
2909 exit( EXIT_FAILURE );
2911 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2912 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2913 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2914 for ( j = 0; j < n; j++ ) {
2915 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2916 if ( *retry_list[j*2+1] == '+' ) {
2917 si->si_retrynum_init[j] = -1;
2918 si->si_retrynum[j] = -1;
2922 si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2923 si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2926 si->si_retrynum_init[j] = -2;
2927 si->si_retrynum[j] = -2;
2928 si->si_retryinterval[j] = 0;
2930 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2931 ch_free( retry_list[k] );
2933 ch_free( retry_list );
2934 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2935 STRLENOF( MANAGEDSAITSTR "=" ) ) )
2937 val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2938 si->si_manageDSAit = atoi( val );
2939 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2940 STRLENOF( SLIMITSTR "=") ) )
2942 val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2943 si->si_slimit = atoi( val );
2944 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2945 STRLENOF( TLIMITSTR "=" ) ) )
2947 val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2948 si->si_tlimit = atoi( val );
2949 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2950 STRLENOF( SYNCDATASTR "=" ) ) )
2952 val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2953 si->si_syncdata = verb_to_mask( val, datamodes );
2954 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2955 fprintf( stderr, "Error: parse_syncrepl_line: "
2956 "unknown keyword \"%s\"\n", cargv[ i ] );
2961 if ( gots != GOT_ALL ) {
2963 "Error: Malformed \"syncrepl\" line in slapd config file" );
2980 if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
2981 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
2982 "required for syncrepl\n", be->be_type, 0, 0 );
2985 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2988 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2992 si->si_bindconf.sb_tls = SB_TLS_OFF;
2993 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2994 si->si_schemachecking = 0;
2995 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2996 &si->si_filterstr );
2997 si->si_base.bv_val = NULL;
2998 si->si_scope = LDAP_SCOPE_SUBTREE;
2999 si->si_attrsonly = 0;
3000 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3001 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3002 si->si_attrs = NULL;
3003 si->si_allattrs = 0;
3004 si->si_allopattrs = 0;
3005 si->si_exattrs = NULL;
3006 si->si_type = LDAP_SYNC_REFRESH_ONLY;
3007 si->si_interval = 86400;
3008 si->si_retryinterval = NULL;
3009 si->si_retrynum_init = NULL;
3010 si->si_retrynum = NULL;
3011 si->si_manageDSAit = 0;
3015 si->si_presentlist = NULL;
3016 LDAP_LIST_INIT( &si->si_nonpresentlist );
3017 ldap_pvt_thread_mutex_init( &si->si_mutex );
3019 rc = parse_syncrepl_line( cargv, cargc, si );
3023 init_syncrepl( si );
3024 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3025 do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3030 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3031 syncinfo_free( si );
3034 Debug( LDAP_DEBUG_CONFIG,
3035 "Config: ** successfully added syncrepl \"%s\"\n",
3036 BER_BVISNULL( &si->si_provideruri ) ?
3037 "(null)" : si->si_provideruri.bv_val, 0, 0 );
3038 if ( !si->si_schemachecking ) {
3039 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3041 be->be_syncinfo = si;
3047 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3050 char buf[BUFSIZ*2], *ptr;
3053 bindconf_unparse( &si->si_bindconf, &bc );
3055 ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3056 si->si_rid, si->si_provideruri.bv_val );
3057 if ( !BER_BVISNULL( &bc )) {
3058 ptr = lutil_strcopy( ptr, bc.bv_val );
3061 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3062 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3063 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3066 if ( !BER_BVISNULL( &si->si_base )) {
3067 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3068 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3071 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3072 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3073 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3076 if ( !BER_BVISNULL( &si->si_logbase )) {
3077 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3078 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3081 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3082 if ( si->si_scope == scopes[i].val ) {
3083 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3084 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3088 if ( si->si_attrsonly ) {
3089 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3091 if ( si->si_anfile ) {
3092 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3093 ptr = lutil_strcopy( ptr, si->si_anfile );
3094 } else if ( si->si_allattrs || si->si_allopattrs ||
3095 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3097 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3099 ptr = anlist_unparse( si->si_anlist, ptr );
3100 if ( si->si_allattrs ) {
3101 if ( old != ptr ) *ptr++ = ',';
3104 if ( si->si_allopattrs ) {
3105 if ( old != ptr ) *ptr++ = ',';
3110 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3111 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3112 ptr = anlist_unparse( si->si_exanlist, ptr );
3114 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3115 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3117 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3118 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3119 "refreshAndPersist" : "refreshOnly" );
3121 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3124 dd = si->si_interval;
3131 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3132 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3133 } else if ( si->si_retryinterval ) {
3135 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3136 for (i=0; si->si_retryinterval[i]; i++) {
3137 if ( space ) *ptr++ = ' ';
3139 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3140 if ( si->si_retrynum_init[i] == -1 )
3143 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3148 if ( si->si_slimit ) {
3149 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3150 ptr += sprintf( ptr, "%d", si->si_slimit );
3153 if ( si->si_tlimit ) {
3154 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3155 ptr += sprintf( ptr, "%d", si->si_tlimit );
3158 if ( si->si_syncdata ) {
3159 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3160 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3161 ptr = lutil_strcopy( ptr, bc.bv_val );
3164 bc.bv_len = ptr - buf;
3166 ber_dupbv( bv, &bc );
3170 syncrepl_config(ConfigArgs *c) {
3171 if (c->op == SLAP_CONFIG_EMIT) {
3172 if ( c->be->be_syncinfo ) {
3174 syncrepl_unparse( c->be->be_syncinfo, &bv );
3175 ber_bvarray_add( &c->rvalue_vals, &bv );
3179 } else if ( c->op == LDAP_MOD_DELETE ) {
3182 if ( c->be->be_syncinfo ) {
3183 re = c->be->be_syncinfo->si_re;
3185 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3186 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3187 ldap_pvt_runqueue_remove( &slapd_rq, re );
3189 syncinfo_free( c->be->be_syncinfo );
3190 c->be->be_syncinfo = NULL;
3192 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3195 if(SLAP_SHADOW(c->be)) {
3196 Debug(LDAP_DEBUG_ANY, "%s: "
3197 "syncrepl: database already shadowed.\n",
3200 } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3203 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);