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 if ( colon[2] == ' ' ) {
1186 bv.bv_val = colon + 3;
1187 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1188 ber_dupbv( &bv2, &bv );
1189 ber_bvarray_add( &mod->sml_values, &bv2 );
1195 static Modifications *
1196 syncrepl_changelog_mods(
1201 return NULL; /* FIXME */
1205 syncrepl_message_to_op(
1211 BerElement *ber = NULL;
1212 Modifications *modlist = NULL;
1214 SlapReply rs = { REP_RESULT };
1215 slap_callback cb = { NULL, null_callback, NULL, NULL };
1218 char txtbuf[SLAP_TEXT_BUFLEN];
1219 size_t textlen = sizeof txtbuf;
1221 struct berval bdn, dn = BER_BVNULL, ndn;
1222 struct berval bv, *bvals = NULL;
1223 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1224 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1225 psup = BER_BVNULL, nsup = BER_BVNULL;
1226 int rc, deleteOldRdn = 0;
1228 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1229 Debug( LDAP_DEBUG_ANY,
1230 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1234 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1239 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1241 if ( rc != LDAP_SUCCESS ) {
1242 Debug( LDAP_DEBUG_ANY,
1243 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1247 op->o_tag = LBER_DEFAULT;
1249 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1251 if ( bv.bv_val == NULL )
1254 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1256 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1257 ber_dupbv( &op->o_req_dn, &dn );
1258 ber_dupbv( &op->o_req_ndn, &ndn );
1259 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1260 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1261 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1262 int i = verb_to_mask( bvals[0].bv_val, modops );
1264 Debug( LDAP_DEBUG_ANY,
1265 "syncrepl_message_to_op : unknown op %s",
1266 bvals[0].bv_val, 0, 0 );
1271 op->o_tag = modops[i].mask;
1272 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1273 /* Parse attribute into modlist */
1274 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1275 modlist = syncrepl_accesslog_mods( si, bvals );
1277 modlist = syncrepl_changelog_mods( si, bvals );
1278 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1280 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1281 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1283 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1285 } else if ( !ber_bvstrcasecmp( &bv,
1286 &slap_schema.si_ad_entryCSN->ad_cname )) {
1287 slap_queue_csn( op, bvals );
1292 /* If we didn't get a mod type or a target DN, bail out */
1293 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1298 op->o_callback = &cb;
1300 switch( op->o_tag ) {
1302 case LDAP_REQ_MODIFY:
1303 /* If we didn't get required data, bail */
1304 if ( !modlist ) goto done;
1306 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1308 if ( rc != LDAP_SUCCESS ) {
1309 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1314 if ( op->o_tag == LDAP_REQ_ADD ) {
1315 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1316 op->ora_e->e_name = op->o_req_dn;
1317 op->ora_e->e_nname = op->o_req_ndn;
1318 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1319 if( rc != LDAP_SUCCESS ) {
1320 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1323 rc = op->o_bd->be_add( op, &rs );
1325 be_entry_release_w( op, op->ora_e );
1327 op->orm_modlist = modlist;
1328 rc = op->o_bd->be_modify( op, &rs );
1331 case LDAP_REQ_MODRDN:
1332 if ( BER_BVISNULL( &rdn )) goto done;
1334 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1336 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1338 if ( !BER_BVISNULL( &sup )) {
1339 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1341 op->orr_newSup = &psup;
1342 op->orr_nnewSup = ⊅
1344 op->orr_newrdn = prdn;
1345 op->orr_nnewrdn = nrdn;
1346 op->orr_deleteoldrdn = deleteOldRdn;
1347 rc = op->o_bd->be_modrdn( op, &rs );
1349 case LDAP_REQ_DELETE:
1350 rc = op->o_bd->be_delete( op, &rs );
1354 slap_graduate_commit_csn( op );
1356 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1357 if ( !BER_BVISNULL( &rdn )) {
1358 if ( !BER_BVISNULL( &nsup ))
1359 ch_free( nsup.bv_val );
1360 if ( !BER_BVISNULL( &psup ))
1361 ch_free( psup.bv_val );
1362 if ( !BER_BVISNULL( &nrdn ))
1363 ch_free( nrdn.bv_val );
1364 if ( !BER_BVISNULL( &prdn ))
1365 ch_free( prdn.bv_val );
1367 ber_free ( ber, 0 );
1372 syncrepl_message_to_entry(
1376 Modifications **modlist,
1382 BerElement *ber = NULL;
1385 Modifications **modtail = modlist;
1388 char txtbuf[SLAP_TEXT_BUFLEN];
1389 size_t textlen = sizeof txtbuf;
1391 struct berval bdn = {0, NULL}, dn, ndn;
1396 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1397 Debug( LDAP_DEBUG_ANY,
1398 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1402 op->o_tag = LDAP_REQ_ADD;
1404 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1406 if ( rc != LDAP_SUCCESS ) {
1407 Debug( LDAP_DEBUG_ANY,
1408 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1412 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1413 ber_dupbv( &op->o_req_dn, &dn );
1414 ber_dupbv( &op->o_req_ndn, &ndn );
1415 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1416 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1418 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1421 return LDAP_SUCCESS;
1424 if ( entry == NULL ) {
1428 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1430 e->e_name = op->o_req_dn;
1431 e->e_nname = op->o_req_ndn;
1433 while ( ber_remaining( ber ) ) {
1434 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1435 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1440 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1442 mod->sml_op = LDAP_MOD_REPLACE;
1444 mod->sml_next = NULL;
1445 mod->sml_desc = NULL;
1446 mod->sml_type = tmp.sml_type;
1447 mod->sml_values = tmp.sml_values;
1448 mod->sml_nvalues = NULL;
1451 modtail = &mod->sml_next;
1454 if ( *modlist == NULL ) {
1455 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1461 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1463 if ( rc != LDAP_SUCCESS ) {
1464 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1469 /* Strip out dynamically generated attrs */
1470 for ( modtail = modlist; *modtail ; ) {
1472 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1473 *modtail = mod->sml_next;
1474 slap_mod_free( &mod->sml_mod, 0 );
1477 modtail = &mod->sml_next;
1481 /* Strip out attrs in exattrs list */
1482 for ( modtail = modlist; *modtail ; ) {
1484 if ( ldap_charray_inlist( si->si_exattrs,
1485 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1486 *modtail = mod->sml_next;
1487 slap_mod_free( &mod->sml_mod, 0 );
1490 modtail = &mod->sml_next;
1494 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1495 if( rc != LDAP_SUCCESS ) {
1496 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1501 ber_free ( ber, 0 );
1502 if ( rc != LDAP_SUCCESS ) {
1512 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1514 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1515 * entry if a previous refresh was interrupted before sending us a new
1516 * context state. We try to compare the new entry to the existing entry
1517 * and ignore the new entry if they are the same.
1519 * Also, we may get an update where the entryDN has changed, due to
1520 * a ModDn on the provider. We detect this as well, so we can issue
1521 * the corresponding operation locally.
1523 * In the case of a modify, we get a list of all the attributes
1524 * in the original entry. Rather than deleting the entry and re-adding it,
1525 * we issue a Modify request that deletes all the attributes and adds all
1526 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1529 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1530 * a ModDN we will issue both operations on the local database.
1532 typedef struct dninfo {
1536 int renamed; /* Was an existing entry renamed? */
1537 int wasChanged; /* are the attributes changed? */
1538 int attrs; /* how many attribute types are in the ads list */
1539 AttributeDescription **ads;
1547 Modifications** modlist,
1549 struct berval* syncUUID,
1550 struct sync_cookie* syncCookie_req,
1551 struct berval* syncCSN )
1553 Backend *be = op->o_bd;
1554 slap_callback cb = { NULL, NULL, NULL, NULL };
1555 struct berval *syncuuid_bv = NULL;
1556 struct berval syncUUID_strrep = BER_BVNULL;
1557 struct berval uuid_bv = BER_BVNULL;
1559 SlapReply rs_search = {REP_RESULT};
1560 SlapReply rs_delete = {REP_RESULT};
1561 SlapReply rs_add = {REP_RESULT};
1562 SlapReply rs_modify = {REP_RESULT};
1564 #ifdef LDAP_COMP_MATCH
1565 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1567 AttributeAssertion ava = { NULL, BER_BVNULL };
1569 int rc = LDAP_SUCCESS;
1570 int ret = LDAP_SUCCESS;
1572 struct berval pdn = BER_BVNULL;
1576 switch( syncstate ) {
1577 case LDAP_SYNC_PRESENT:
1578 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1580 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1583 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1585 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1587 case LDAP_SYNC_DELETE:
1588 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1590 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1592 case LDAP_SYNC_MODIFY:
1593 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1595 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1598 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1600 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1603 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1604 if ( !si->si_refreshPresent ) {
1605 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1606 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1607 syncuuid_cmp, avl_dup_error );
1611 if ( syncstate == LDAP_SYNC_PRESENT ) {
1613 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1614 if ( entry == NULL ) {
1619 f.f_choice = LDAP_FILTER_EQUALITY;
1621 ava.aa_desc = slap_schema.si_ad_entryUUID;
1622 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1623 ava.aa_value = *syncUUID;
1624 op->ors_filter = &f;
1626 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1627 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1628 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1629 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1630 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1631 syncUUID->bv_val, syncUUID->bv_len );
1632 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1633 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1635 op->o_tag = LDAP_REQ_SEARCH;
1636 op->ors_scope = LDAP_SCOPE_SUBTREE;
1637 op->ors_deref = LDAP_DEREF_NEVER;
1639 /* get the entry for this UUID */
1640 op->o_req_dn = si->si_base;
1641 op->o_req_ndn = si->si_base;
1643 op->o_time = slap_get_time();
1644 op->ors_tlimit = SLAP_NO_LIMIT;
1647 op->ors_attrs = slap_anlist_all_attributes;
1648 op->ors_attrsonly = 0;
1650 /* set callback function */
1651 op->o_callback = &cb;
1652 cb.sc_response = dn_callback;
1653 cb.sc_private = &dni;
1654 dni.new_entry = entry;
1656 if ( limits_check( op, &rs_search ) == 0 ) {
1657 rc = be->be_search( op, &rs_search );
1658 Debug( LDAP_DEBUG_SYNC,
1659 "syncrepl_entry: %s (%d)\n",
1660 "be_search", rc, 0 );
1663 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1664 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1667 cb.sc_response = null_callback;
1670 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1671 Debug( LDAP_DEBUG_SYNC,
1672 "syncrepl_entry: %s\n",
1673 entry->e_name.bv_val, 0, 0 );
1675 Debug( LDAP_DEBUG_SYNC,
1676 "syncrepl_entry: %s\n",
1677 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1680 if ( syncstate != LDAP_SYNC_DELETE ) {
1681 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1684 /* add if missing */
1685 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1686 &syncUUID_strrep, syncUUID );
1688 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1689 /* replace only if necessary */
1690 if ( a->a_nvals != a->a_vals ) {
1691 ber_memfree( a->a_nvals[0].bv_val );
1692 ber_dupbv( &a->a_nvals[0], syncUUID );
1694 ber_memfree( a->a_vals[0].bv_val );
1695 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1699 switch ( syncstate ) {
1701 case LDAP_SYNC_MODIFY:
1703 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1705 op->o_csn = a->a_vals[0];
1708 if ( BER_BVISNULL( &dni.dn )) {
1710 op->o_req_dn = entry->e_name;
1711 op->o_req_ndn = entry->e_nname;
1712 op->o_tag = LDAP_REQ_ADD;
1715 rc = be->be_add( op, &rs_add );
1716 Debug( LDAP_DEBUG_SYNC,
1717 "syncrepl_entry: %s (%d)\n",
1719 switch ( rs_add.sr_err ) {
1721 be_entry_release_w( op, entry );
1726 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1727 * only if the suffix entry is not present */
1728 case LDAP_NO_SUCH_OBJECT:
1729 syncrepl_add_glue( op, entry );
1733 /* if an entry was added via syncrepl_add_glue(),
1734 * it likely has no entryUUID, so the previous
1735 * be_search() doesn't find it. In this case,
1736 * give syncrepl a chance to modify it. Also
1737 * allow for entries that were recreated with the
1738 * same DN but a different entryUUID.
1740 case LDAP_ALREADY_EXISTS:
1742 Operation op2 = *op;
1743 SlapReply rs2 = { 0 };
1744 slap_callback cb2 = { 0 };
1746 op2.o_tag = LDAP_REQ_SEARCH;
1747 op2.o_req_dn = entry->e_name;
1748 op2.o_req_ndn = entry->e_nname;
1749 op2.ors_scope = LDAP_SCOPE_BASE;
1750 op2.ors_deref = LDAP_DEREF_NEVER;
1751 op2.ors_attrs = slap_anlist_all_attributes;
1752 op2.ors_attrsonly = 0;
1753 op2.ors_limit = NULL;
1755 op2.ors_tlimit = SLAP_NO_LIMIT;
1757 f.f_choice = LDAP_FILTER_PRESENT;
1758 f.f_desc = slap_schema.si_ad_objectClass;
1759 op2.ors_filter = &f;
1760 op2.ors_filterstr = generic_filterstr;
1762 op2.o_callback = &cb2;
1763 cb2.sc_response = dn_callback;
1764 cb2.sc_private = &dni;
1766 be->be_search( &op2, &rs2 );
1774 Debug( LDAP_DEBUG_ANY,
1775 "syncrepl_entry : be_add failed (%d)\n",
1776 rs_add.sr_err, 0, 0 );
1783 op->o_req_dn = dni.dn;
1784 op->o_req_ndn = dni.ndn;
1785 if ( dni.renamed ) {
1786 struct berval noldp, newp, nnewp;
1788 op->o_tag = LDAP_REQ_MODRDN;
1789 dnRdn( &entry->e_name, &op->orr_newrdn );
1790 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1792 dnParent( &dni.ndn, &noldp );
1793 dnParent( &entry->e_nname, &nnewp );
1794 if ( !dn_match( &noldp, &newp )) {
1795 dnParent( &entry->e_name, &newp );
1796 op->orr_newSup = &newp;
1797 op->orr_nnewSup = &nnewp;
1799 op->orr_deleteoldrdn = 0;
1800 rc = be->be_modrdn( op, &rs_modify );
1801 Debug( LDAP_DEBUG_SYNC,
1802 "syncrepl_entry: %s (%d)\n",
1803 "be_modrdn", rc, 0 );
1804 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1805 op->o_req_dn = entry->e_name;
1806 op->o_req_ndn = entry->e_nname;
1812 if ( dni.wasChanged ) {
1813 Modifications *mod, *modhead = NULL;
1814 Modifications *modtail = NULL;
1817 op->o_tag = LDAP_REQ_MODIFY;
1819 assert( *modlist != NULL );
1821 /* Delete all the old attrs */
1822 for ( i = 0; i < dni.attrs; i++ ) {
1823 mod = ch_malloc( sizeof( Modifications ) );
1824 mod->sml_op = LDAP_MOD_DELETE;
1826 mod->sml_desc = dni.ads[i];
1827 mod->sml_type = mod->sml_desc->ad_cname;
1828 mod->sml_values = NULL;
1829 mod->sml_nvalues = NULL;
1830 if ( !modhead ) modhead = mod;
1832 modtail->sml_next = mod;
1837 /* Append passed in list to ours */
1839 modtail->sml_next = *modlist;
1845 /* Find end of this list */
1846 for ( ; mod != NULL; mod = mod->sml_next ) {
1850 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1851 mod->sml_op = LDAP_MOD_REPLACE;
1853 mod->sml_desc = slap_schema.si_ad_entryUUID;
1854 mod->sml_type = mod->sml_desc->ad_cname;
1855 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1856 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1857 ber_dupbv( &uuid_bv, syncUUID );
1858 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1859 modtail->sml_next = mod;
1861 op->o_tag = LDAP_REQ_MODIFY;
1862 op->orm_modlist = *modlist;
1864 rc = be->be_modify( op, &rs_modify );
1865 Debug( LDAP_DEBUG_SYNC,
1866 "syncrepl_entry: %s (%d)\n",
1867 "be_modify", rc, 0 );
1868 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1869 Debug( LDAP_DEBUG_ANY,
1870 "syncrepl_entry : be_modify failed (%d)\n",
1871 rs_modify.sr_err, 0, 0 );
1876 case LDAP_SYNC_DELETE :
1877 if ( !BER_BVISNULL( &dni.dn )) {
1878 op->o_req_dn = dni.dn;
1879 op->o_req_ndn = dni.ndn;
1880 op->o_tag = LDAP_REQ_DELETE;
1881 rc = be->be_delete( op, &rs_delete );
1882 Debug( LDAP_DEBUG_SYNC,
1883 "syncrepl_entry: %s (%d)\n",
1884 "be_delete", rc, 0 );
1886 while ( rs_delete.sr_err == LDAP_SUCCESS
1887 && op->o_delete_glue_parent ) {
1888 op->o_delete_glue_parent = 0;
1889 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1890 slap_callback cb = { NULL };
1891 cb.sc_response = slap_null_cb;
1892 dnParent( &op->o_req_ndn, &pdn );
1894 op->o_req_ndn = pdn;
1895 op->o_callback = &cb;
1896 op->o_bd->be_delete( op, &rs_delete );
1906 Debug( LDAP_DEBUG_ANY,
1907 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1913 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1914 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1915 BER_BVZERO( &syncUUID_strrep );
1918 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1920 if ( !BER_BVISNULL( &dni.ndn ) ) {
1921 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1923 if ( !BER_BVISNULL( &dni.dn ) ) {
1924 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1926 BER_BVZERO( &op->o_csn );
1930 static struct berval gcbva[] = {
1936 #define NP_DELETE_ONE 2
1939 syncrepl_del_nonpresent(
1944 Backend* be = op->o_bd;
1945 slap_callback cb = { NULL };
1946 SlapReply rs_search = {REP_RESULT};
1947 SlapReply rs_delete = {REP_RESULT};
1948 SlapReply rs_modify = {REP_RESULT};
1949 struct nonpresent_entry *np_list, *np_prev;
1951 AttributeName an[2];
1953 struct berval pdn = BER_BVNULL;
1955 op->o_req_dn = si->si_base;
1956 op->o_req_ndn = si->si_base;
1958 cb.sc_response = nonpresent_callback;
1961 op->o_callback = &cb;
1962 op->o_tag = LDAP_REQ_SEARCH;
1963 op->ors_scope = si->si_scope;
1964 op->ors_deref = LDAP_DEREF_NEVER;
1965 op->o_time = slap_get_time();
1966 op->ors_tlimit = SLAP_NO_LIMIT;
1971 #ifdef LDAP_COMP_MATCH
1972 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1974 AttributeAssertion eq = { NULL, BER_BVNULL };
1978 op->ors_attrsonly = 1;
1979 op->ors_attrs = slap_anlist_no_attrs;
1980 op->ors_limit = NULL;
1981 op->ors_filter = &uf;
1984 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1986 uf.f_choice = LDAP_FILTER_EQUALITY;
1987 si->si_refreshDelete |= NP_DELETE_ONE;
1989 for (i=0; uuids[i].bv_val; i++) {
1991 uf.f_av_value = uuids[i];
1992 rc = be->be_search( op, &rs_search );
1994 si->si_refreshDelete ^= NP_DELETE_ONE;
1996 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1997 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1998 an[0].an_desc = slap_schema.si_ad_entryUUID;
2000 op->ors_slimit = SLAP_NO_LIMIT;
2001 op->ors_attrsonly = 0;
2002 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2003 op->ors_filterstr = si->si_filterstr;
2004 op->o_nocaching = 1;
2006 if ( limits_check( op, &rs_search ) == 0 ) {
2007 rc = be->be_search( op, &rs_search );
2009 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2012 op->o_nocaching = 0;
2014 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2016 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2018 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2019 while ( np_list != NULL ) {
2020 LDAP_LIST_REMOVE( np_list, npe_link );
2022 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2023 op->o_tag = LDAP_REQ_DELETE;
2024 op->o_callback = &cb;
2025 cb.sc_response = null_callback;
2027 op->o_req_dn = *np_prev->npe_name;
2028 op->o_req_ndn = *np_prev->npe_nname;
2029 rc = op->o_bd->be_delete( op, &rs_delete );
2031 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2032 Modifications mod1, mod2;
2033 mod1.sml_op = LDAP_MOD_REPLACE;
2035 mod1.sml_desc = slap_schema.si_ad_objectClass;
2036 mod1.sml_type = mod1.sml_desc->ad_cname;
2037 mod1.sml_values = &gcbva[0];
2038 mod1.sml_nvalues = NULL;
2039 mod1.sml_next = &mod2;
2041 mod2.sml_op = LDAP_MOD_REPLACE;
2043 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2044 mod2.sml_type = mod2.sml_desc->ad_cname;
2045 mod2.sml_values = &gcbva[1];
2046 mod2.sml_nvalues = NULL;
2047 mod2.sml_next = NULL;
2049 op->o_tag = LDAP_REQ_MODIFY;
2050 op->orm_modlist = &mod1;
2052 rc = be->be_modify( op, &rs_modify );
2055 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2056 op->o_delete_glue_parent ) {
2057 op->o_delete_glue_parent = 0;
2058 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2059 slap_callback cb = { NULL };
2060 cb.sc_response = slap_null_cb;
2061 dnParent( &op->o_req_ndn, &pdn );
2063 op->o_req_ndn = pdn;
2064 op->o_callback = &cb;
2065 /* give it a root privil ? */
2066 op->o_bd->be_delete( op, &rs_delete );
2072 op->o_delete_glue_parent = 0;
2074 ber_bvfree( np_prev->npe_name );
2075 ber_bvfree( np_prev->npe_nname );
2079 slap_graduate_commit_csn( op );
2090 Backend *be = op->o_bd;
2091 slap_callback cb = { NULL };
2096 struct berval dn = {0, NULL};
2097 struct berval ndn = {0, NULL};
2099 SlapReply rs_add = {REP_RESULT};
2102 op->o_tag = LDAP_REQ_ADD;
2103 op->o_callback = &cb;
2104 cb.sc_response = null_callback;
2105 cb.sc_private = NULL;
2110 /* count RDNs in suffix */
2111 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2112 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2122 /* Start with BE suffix */
2123 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2124 comma = strrchr( dn.bv_val, ',' );
2125 if ( ptr ) *ptr = ',';
2126 if ( comma ) *comma = '\0';
2131 dn.bv_len -= ptr - dn.bv_val;
2134 /* the normalizedDNs are always the same length, no counting
2137 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2138 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2139 ndn.bv_len = be->be_nsuffix[0].bv_len;
2142 while ( ndn.bv_val > e->e_nname.bv_val ) {
2143 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2144 ber_dupbv( &glue->e_name, &dn );
2145 ber_dupbv( &glue->e_nname, &ndn );
2147 a = ch_calloc( 1, sizeof( Attribute ));
2148 a->a_desc = slap_schema.si_ad_objectClass;
2150 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2151 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2152 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2153 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2155 a->a_nvals = a->a_vals;
2157 a->a_next = glue->e_attrs;
2160 a = ch_calloc( 1, sizeof( Attribute ));
2161 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2163 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2164 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2165 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2167 a->a_nvals = a->a_vals;
2169 a->a_next = glue->e_attrs;
2172 op->o_req_dn = glue->e_name;
2173 op->o_req_ndn = glue->e_nname;
2175 rc = be->be_add ( op, &rs_add );
2176 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2177 be_entry_release_w( op, glue );
2179 /* incl. ALREADY EXIST */
2183 /* Move to next child */
2184 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2187 if ( ptr == e->e_name.bv_val ) break;
2189 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2190 for( ptr = ndn.bv_val-2;
2191 ptr > e->e_nname.bv_val && *ptr != ',';
2197 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2200 op->o_req_dn = e->e_name;
2201 op->o_req_ndn = e->e_nname;
2203 rc = be->be_add ( op, &rs_add );
2204 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2205 be_entry_release_w( op, e );
2214 syncrepl_updateCookie(
2218 struct sync_cookie *syncCookie )
2220 Backend *be = op->o_bd;
2221 Modifications mod = { { 0 } };
2222 struct berval vals[ 2 ];
2226 slap_callback cb = { NULL };
2227 SlapReply rs_modify = {REP_RESULT};
2229 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2230 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2232 mod.sml_op = LDAP_MOD_REPLACE;
2233 mod.sml_desc = slap_schema.si_ad_contextCSN;
2234 mod.sml_type = mod.sml_desc->ad_cname;
2235 mod.sml_values = vals;
2236 vals[0] = si->si_syncCookie.ctxcsn;
2237 vals[1].bv_val = NULL;
2240 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2242 op->o_tag = LDAP_REQ_MODIFY;
2244 assert( si->si_rid < 1000 );
2246 cb.sc_response = null_callback;
2249 op->o_callback = &cb;
2250 op->o_req_dn = op->o_bd->be_suffix[0];
2251 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2253 /* update contextCSN */
2254 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2255 op->orm_modlist = &mod;
2256 rc = be->be_modify( op, &rs_modify );
2259 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2260 Debug( LDAP_DEBUG_ANY,
2261 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2264 slap_graduate_commit_csn( op );
2274 dninfo *dni = op->o_callback->sc_private;
2276 if ( rs->sr_type == REP_SEARCH ) {
2277 if ( !BER_BVISNULL( &dni->dn ) ) {
2278 Debug( LDAP_DEBUG_ANY,
2279 "dn_callback : consistency error - "
2280 "entryUUID is not unique\n", 0, 0, 0 );
2282 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2283 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2284 /* If there is a new entry, see if it differs from the old.
2285 * We compare the non-normalized values so that cosmetic changes
2286 * in the provider are always propagated.
2288 if ( dni->new_entry ) {
2289 Attribute *old, *new;
2292 /* Did the DN change? Note that we don't explicitly try to
2293 * discover if the deleteOldRdn argument applies here. It
2294 * would save an unnecessary Modify if we detected it, but
2295 * that's a fair amount of trouble to compare the two attr
2296 * lists in detail. (Just test normalized DN; we ignore
2297 * insignificant changes here.)
2299 if ( !dn_match( &rs->sr_entry->e_nname,
2300 &dni->new_entry->e_nname ) )
2305 for ( i = 0, old = rs->sr_entry->e_attrs;
2307 i++, old = old->a_next )
2312 /* We assume that attributes are saved in the same order
2313 * in the remote and local databases. So if we walk through
2314 * the attributeDescriptions one by one they should match in
2315 * lock step. If not, we signal a change. Otherwise we test
2318 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2320 old = old->a_next, new = new->a_next )
2322 if ( old->a_desc != new->a_desc ) {
2323 dni->wasChanged = 1;
2326 for ( i = 0; ; i++ ) {
2328 nold = BER_BVISNULL( &old->a_vals[i] );
2329 nnew = BER_BVISNULL( &new->a_vals[i] );
2330 /* If both are empty, stop looking */
2331 if ( nold && nnew ) {
2334 /* If they are different, stop looking */
2335 if ( nold != nnew ) {
2336 dni->wasChanged = 1;
2339 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2340 dni->wasChanged = 1;
2344 if ( dni->wasChanged ) break;
2346 if ( dni->wasChanged ) {
2347 dni->ads = op->o_tmpalloc( dni->attrs *
2348 sizeof(AttributeDescription *), op->o_tmpmemctx );
2350 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2351 dni->ads[i] = old->a_desc;
2357 } else if ( rs->sr_type == REP_RESULT ) {
2358 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2359 Debug( LDAP_DEBUG_ANY,
2360 "dn_callback : consistency error - "
2361 "entryUUID is not unique\n", 0, 0, 0 );
2365 return LDAP_SUCCESS;
2369 nonpresent_callback(
2373 syncinfo_t *si = op->o_callback->sc_private;
2376 struct berval* present_uuid = NULL;
2377 struct nonpresent_entry *np_entry;
2379 if ( rs->sr_type == REP_RESULT ) {
2380 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2381 si->si_presentlist = NULL;
2383 } else if ( rs->sr_type == REP_SEARCH ) {
2384 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2385 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2387 if ( a == NULL ) return 0;
2389 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2393 if ( present_uuid == NULL ) {
2394 np_entry = (struct nonpresent_entry *)
2395 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2396 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2397 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2398 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2401 avl_delete( &si->si_presentlist,
2402 &a->a_nvals[0], syncuuid_cmp );
2403 ch_free( present_uuid->bv_val );
2404 ch_free( present_uuid );
2407 return LDAP_SUCCESS;
2415 if ( rs->sr_err != LDAP_SUCCESS &&
2416 rs->sr_err != LDAP_REFERRAL &&
2417 rs->sr_err != LDAP_ALREADY_EXISTS &&
2418 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2419 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2421 Debug( LDAP_DEBUG_ANY,
2422 "null_callback : error code 0x%x\n",
2425 return LDAP_SUCCESS;
2428 static struct berval *
2429 slap_uuidstr_from_normalized(
2430 struct berval* uuidstr,
2431 struct berval* normalized,
2435 unsigned char nibble;
2438 if ( normalized == NULL ) return NULL;
2439 if ( normalized->bv_len != 16 ) return NULL;
2444 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2445 if ( new == NULL ) {
2452 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2453 if ( new != uuidstr ) {
2454 slap_sl_free( new, ctx );
2459 for ( i = 0; i < 16; i++ ) {
2460 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2461 new->bv_val[(i<<1)+d] = '-';
2465 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2466 if ( nibble < 10 ) {
2467 new->bv_val[(i<<1)+d] = nibble + '0';
2469 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2472 nibble = (normalized->bv_val[i]) & 0xF;
2473 if ( nibble < 10 ) {
2474 new->bv_val[(i<<1)+d+1] = nibble + '0';
2476 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2480 new->bv_val[new->bv_len] = '\0';
2485 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2487 const struct berval *uuid1 = v_uuid1;
2488 const struct berval *uuid2 = v_uuid2;
2489 int rc = uuid1->bv_len - uuid2->bv_len;
2490 if ( rc ) return rc;
2491 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2495 avl_ber_bvfree( void *v_bv )
2497 struct berval *bv = (struct berval *)v_bv;
2499 if( v_bv == NULL ) return;
2500 if ( !BER_BVISNULL( bv ) ) {
2501 ch_free( bv->bv_val );
2503 ch_free( (char *) bv );
2507 syncinfo_free( syncinfo_t *sie )
2509 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2510 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2511 ch_free( sie->si_provideruri.bv_val );
2514 bindconf_free( &sie->si_bindconf );
2516 if ( sie->si_filterstr.bv_val ) {
2517 ch_free( sie->si_filterstr.bv_val );
2519 if ( sie->si_base.bv_val ) {
2520 ch_free( sie->si_base.bv_val );
2522 if ( sie->si_attrs ) {
2524 while ( sie->si_attrs[i] != NULL ) {
2525 ch_free( sie->si_attrs[i] );
2528 ch_free( sie->si_attrs );
2530 if ( sie->si_exattrs ) {
2532 while ( sie->si_exattrs[i] != NULL ) {
2533 ch_free( sie->si_exattrs[i] );
2536 ch_free( sie->si_exattrs );
2538 if ( sie->si_anlist ) {
2540 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2541 ch_free( sie->si_anlist[i].an_name.bv_val );
2544 ch_free( sie->si_anlist );
2546 if ( sie->si_exanlist ) {
2548 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2549 ch_free( sie->si_exanlist[i].an_name.bv_val );
2552 ch_free( sie->si_exanlist );
2554 if ( sie->si_retryinterval ) {
2555 ch_free( sie->si_retryinterval );
2557 if ( sie->si_retrynum ) {
2558 ch_free( sie->si_retrynum );
2560 if ( sie->si_retrynum_init ) {
2561 ch_free( sie->si_retrynum_init );
2563 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2564 if ( sie->si_presentlist ) {
2565 avl_free( sie->si_presentlist, avl_ber_bvfree );
2568 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2570 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2571 struct nonpresent_entry* npe;
2572 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2573 LDAP_LIST_REMOVE( npe, npe_link );
2574 if ( npe->npe_name ) {
2575 if ( npe->npe_name->bv_val ) {
2576 ch_free( npe->npe_name->bv_val );
2578 ch_free( npe->npe_name );
2580 if ( npe->npe_nname ) {
2581 if ( npe->npe_nname->bv_val ) {
2582 ch_free( npe->npe_nname->bv_val );
2584 ch_free( npe->npe_nname );
2593 /* NOTE: used & documented in slapd.conf(5) */
2595 #define PROVIDERSTR "provider"
2596 #define SCHEMASTR "schemachecking"
2597 #define FILTERSTR "filter"
2598 #define SEARCHBASESTR "searchbase"
2599 #define SCOPESTR "scope"
2600 #define ATTRSONLYSTR "attrsonly"
2601 #define ATTRSSTR "attrs"
2602 #define TYPESTR "type"
2603 #define INTERVALSTR "interval"
2604 #define RETRYSTR "retry"
2605 #define SLIMITSTR "sizelimit"
2606 #define TLIMITSTR "timelimit"
2607 #define SYNCDATASTR "syncdata"
2609 /* FIXME: undocumented */
2610 #define LOGBASESTR "logbase"
2611 #define LOGFILTERSTR "logfilter"
2612 #define OLDAUTHCSTR "bindprincipal"
2613 #define EXATTRSSTR "exattrs"
2614 #define MANAGEDSAITSTR "manageDSAit"
2617 #define LASTMODSTR "lastmod"
2618 #define LMGENSTR "gen"
2619 #define LMNOSTR "no"
2620 #define LMREQSTR "req"
2621 #define SRVTABSTR "srvtab"
2622 #define SUFFIXSTR "suffix"
2625 #define GOT_ID 0x0001
2626 #define GOT_PROVIDER 0x0002
2629 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2635 { BER_BVC("base"), LDAP_SCOPE_BASE },
2636 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2637 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2638 #ifdef LDAP_SCOPE_SUBORDINATE
2639 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2640 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2642 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2643 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2647 static slap_verbmasks datamodes[] = {
2648 { BER_BVC("default"), SYNCDATA_DEFAULT },
2649 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2650 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2655 parse_syncrepl_line(
2665 for ( i = 1; i < cargc; i++ ) {
2666 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2667 STRLENOF( IDSTR "=" ) ) )
2670 /* '\0' string terminator accounts for '=' */
2671 val = cargv[ i ] + STRLENOF( IDSTR "=" );
2673 if ( tmp >= 1000 || tmp < 0 ) {
2674 fprintf( stderr, "Error: parse_syncrepl_line: "
2675 "syncrepl id %d is out of range [0..999]\n", tmp );
2680 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2681 STRLENOF( PROVIDERSTR "=" ) ) )
2683 val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2684 ber_str2bv( val, 0, 1, &si->si_provideruri );
2685 gots |= GOT_PROVIDER;
2686 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2687 STRLENOF( SCHEMASTR "=" ) ) )
2689 val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2690 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2691 si->si_schemachecking = 1;
2692 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2693 si->si_schemachecking = 0;
2695 si->si_schemachecking = 1;
2697 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2698 STRLENOF( FILTERSTR "=" ) ) )
2700 val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2701 if ( si->si_filterstr.bv_val )
2702 ch_free( si->si_filterstr.bv_val );
2703 ber_str2bv( val, 0, 1, &si->si_filterstr );
2704 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2705 STRLENOF( LOGFILTERSTR "=" ) ) )
2707 val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2708 if ( si->si_logfilterstr.bv_val )
2709 ch_free( si->si_logfilterstr.bv_val );
2710 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2711 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2712 STRLENOF( SEARCHBASESTR "=" ) ) )
2717 val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2718 if ( si->si_base.bv_val ) {
2719 ch_free( si->si_base.bv_val );
2721 ber_str2bv( val, 0, 0, &bv );
2722 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2723 if ( rc != LDAP_SUCCESS ) {
2724 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2725 val, rc, ldap_err2string( rc ) );
2728 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2729 STRLENOF( LOGBASESTR "=" ) ) )
2734 val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2735 if ( si->si_logbase.bv_val ) {
2736 ch_free( si->si_logbase.bv_val );
2738 ber_str2bv( val, 0, 0, &bv );
2739 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2740 if ( rc != LDAP_SUCCESS ) {
2741 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2742 val, rc, ldap_err2string( rc ) );
2745 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2746 STRLENOF( SCOPESTR "=" ) ) )
2749 val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2750 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2751 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2752 si->si_scope = scopes[j].val;
2756 if ( BER_BVISNULL(&scopes[j].key) ) {
2757 fprintf( stderr, "Error: parse_syncrepl_line: "
2758 "unknown scope \"%s\"\n", val);
2761 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2762 STRLENOF( ATTRSONLYSTR "=" ) ) )
2764 si->si_attrsonly = 1;
2765 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2766 STRLENOF( ATTRSSTR "=" ) ) )
2768 val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2769 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2771 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2772 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2773 if ( si->si_anlist == NULL ) {
2774 ch_free( attr_fname );
2777 si->si_anfile = attr_fname;
2779 char *str, *s, *next;
2780 char delimstr[] = " ,\t";
2781 str = ch_strdup( val );
2782 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2784 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2786 if ( strlen(s) == 1 && *s == '*' ) {
2787 si->si_allattrs = 1;
2788 *(val + ( s - str )) = delimstr[0];
2790 if ( strlen(s) == 1 && *s == '+' ) {
2791 si->si_allopattrs = 1;
2792 *(val + ( s - str )) = delimstr[0];
2796 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2797 if ( si->si_anlist == NULL ) {
2801 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2802 STRLENOF( EXATTRSSTR "=" ) ) )
2804 val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2805 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2807 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2808 si->si_exanlist = file2anlist(
2809 si->si_exanlist, attr_fname, " ,\t" );
2810 if ( si->si_exanlist == NULL ) {
2811 ch_free( attr_fname );
2814 ch_free( attr_fname );
2816 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2817 if ( si->si_exanlist == NULL ) {
2821 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2822 STRLENOF( TYPESTR "=" ) ) )
2824 val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2825 if ( !strncasecmp( val, "refreshOnly",
2826 STRLENOF("refreshOnly") ))
2828 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2829 } else if ( !strncasecmp( val, "refreshAndPersist",
2830 STRLENOF("refreshAndPersist") ))
2832 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2833 si->si_interval = 60;
2835 fprintf( stderr, "Error: parse_syncrepl_line: "
2836 "unknown sync type \"%s\"\n", val);
2839 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2840 STRLENOF( INTERVALSTR "=" ) ) )
2842 val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2843 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2844 si->si_interval = 0;
2852 hstr = strchr( dstr, ':' );
2853 if ( hstr == NULL ) {
2854 fprintf( stderr, "Error: parse_syncrepl_line: "
2855 "invalid interval \"%s\"\n", val );
2859 mstr = strchr( hstr, ':' );
2860 if ( mstr == NULL ) {
2861 fprintf( stderr, "Error: parse_syncrepl_line: "
2862 "invalid interval \"%s\"\n", val );
2866 sstr = strchr( mstr, ':' );
2867 if ( sstr == NULL ) {
2868 fprintf( stderr, "Error: parse_syncrepl_line: "
2869 "invalid interval \"%s\"\n", val );
2878 if (( hh > 24 ) || ( hh < 0 ) ||
2879 ( mm > 60 ) || ( mm < 0 ) ||
2880 ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2881 fprintf( stderr, "Error: parse_syncrepl_line: "
2882 "invalid interval \"%s\"\n", val );
2885 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2887 if ( si->si_interval < 0 ) {
2888 fprintf( stderr, "Error: parse_syncrepl_line: "
2889 "invalid interval \"%ld\"\n",
2890 (long) si->si_interval);
2893 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2894 STRLENOF( RETRYSTR "=" ) ) )
2899 val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2900 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2901 retry_list[0] = NULL;
2903 slap_str2clist( &retry_list, val, " ,\t" );
2905 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2909 "Error: incomplete syncrepl retry list\n" );
2910 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2911 ch_free( retry_list[k] );
2913 ch_free( retry_list );
2914 exit( EXIT_FAILURE );
2916 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2917 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2918 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2919 for ( j = 0; j < n; j++ ) {
2920 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2921 if ( *retry_list[j*2+1] == '+' ) {
2922 si->si_retrynum_init[j] = -1;
2923 si->si_retrynum[j] = -1;
2927 si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2928 si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2931 si->si_retrynum_init[j] = -2;
2932 si->si_retrynum[j] = -2;
2933 si->si_retryinterval[j] = 0;
2935 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2936 ch_free( retry_list[k] );
2938 ch_free( retry_list );
2939 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2940 STRLENOF( MANAGEDSAITSTR "=" ) ) )
2942 val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2943 si->si_manageDSAit = atoi( val );
2944 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2945 STRLENOF( SLIMITSTR "=") ) )
2947 val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2948 si->si_slimit = atoi( val );
2949 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2950 STRLENOF( TLIMITSTR "=" ) ) )
2952 val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2953 si->si_tlimit = atoi( val );
2954 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2955 STRLENOF( SYNCDATASTR "=" ) ) )
2957 val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2958 si->si_syncdata = verb_to_mask( val, datamodes );
2959 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2960 fprintf( stderr, "Error: parse_syncrepl_line: "
2961 "unknown keyword \"%s\"\n", cargv[ i ] );
2966 if ( gots != GOT_ALL ) {
2968 "Error: Malformed \"syncrepl\" line in slapd config file" );
2985 if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
2986 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
2987 "required for syncrepl\n", be->be_type, 0, 0 );
2990 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2993 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2997 si->si_bindconf.sb_tls = SB_TLS_OFF;
2998 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2999 si->si_schemachecking = 0;
3000 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3001 &si->si_filterstr );
3002 si->si_base.bv_val = NULL;
3003 si->si_scope = LDAP_SCOPE_SUBTREE;
3004 si->si_attrsonly = 0;
3005 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3006 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3007 si->si_attrs = NULL;
3008 si->si_allattrs = 0;
3009 si->si_allopattrs = 0;
3010 si->si_exattrs = NULL;
3011 si->si_type = LDAP_SYNC_REFRESH_ONLY;
3012 si->si_interval = 86400;
3013 si->si_retryinterval = NULL;
3014 si->si_retrynum_init = NULL;
3015 si->si_retrynum = NULL;
3016 si->si_manageDSAit = 0;
3020 si->si_presentlist = NULL;
3021 LDAP_LIST_INIT( &si->si_nonpresentlist );
3022 ldap_pvt_thread_mutex_init( &si->si_mutex );
3024 rc = parse_syncrepl_line( cargv, cargc, si );
3028 init_syncrepl( si );
3029 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3030 do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3035 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3036 syncinfo_free( si );
3039 Debug( LDAP_DEBUG_CONFIG,
3040 "Config: ** successfully added syncrepl \"%s\"\n",
3041 BER_BVISNULL( &si->si_provideruri ) ?
3042 "(null)" : si->si_provideruri.bv_val, 0, 0 );
3043 if ( !si->si_schemachecking ) {
3044 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3046 be->be_syncinfo = si;
3052 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3055 char buf[BUFSIZ*2], *ptr;
3058 bindconf_unparse( &si->si_bindconf, &bc );
3060 ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3061 si->si_rid, si->si_provideruri.bv_val );
3062 if ( !BER_BVISNULL( &bc )) {
3063 ptr = lutil_strcopy( ptr, bc.bv_val );
3066 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3067 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3068 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3071 if ( !BER_BVISNULL( &si->si_base )) {
3072 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3073 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3076 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3077 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3078 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3081 if ( !BER_BVISNULL( &si->si_logbase )) {
3082 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3083 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3086 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3087 if ( si->si_scope == scopes[i].val ) {
3088 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3089 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3093 if ( si->si_attrsonly ) {
3094 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3096 if ( si->si_anfile ) {
3097 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3098 ptr = lutil_strcopy( ptr, si->si_anfile );
3099 } else if ( si->si_allattrs || si->si_allopattrs ||
3100 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3102 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3104 ptr = anlist_unparse( si->si_anlist, ptr );
3105 if ( si->si_allattrs ) {
3106 if ( old != ptr ) *ptr++ = ',';
3109 if ( si->si_allopattrs ) {
3110 if ( old != ptr ) *ptr++ = ',';
3115 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3116 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3117 ptr = anlist_unparse( si->si_exanlist, ptr );
3119 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3120 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3122 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3123 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3124 "refreshAndPersist" : "refreshOnly" );
3126 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3129 dd = si->si_interval;
3136 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3137 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3138 } else if ( si->si_retryinterval ) {
3140 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3141 for (i=0; si->si_retryinterval[i]; i++) {
3142 if ( space ) *ptr++ = ' ';
3144 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3145 if ( si->si_retrynum_init[i] == -1 )
3148 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3153 if ( si->si_slimit ) {
3154 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3155 ptr += sprintf( ptr, "%d", si->si_slimit );
3158 if ( si->si_tlimit ) {
3159 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3160 ptr += sprintf( ptr, "%d", si->si_tlimit );
3163 if ( si->si_syncdata ) {
3164 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3165 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3166 ptr = lutil_strcopy( ptr, bc.bv_val );
3169 bc.bv_len = ptr - buf;
3171 ber_dupbv( bv, &bc );
3175 syncrepl_config(ConfigArgs *c) {
3176 if (c->op == SLAP_CONFIG_EMIT) {
3177 if ( c->be->be_syncinfo ) {
3179 syncrepl_unparse( c->be->be_syncinfo, &bv );
3180 ber_bvarray_add( &c->rvalue_vals, &bv );
3184 } else if ( c->op == LDAP_MOD_DELETE ) {
3187 if ( c->be->be_syncinfo ) {
3188 re = c->be->be_syncinfo->si_re;
3190 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3191 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3192 ldap_pvt_runqueue_remove( &slapd_rq, re );
3194 syncinfo_free( c->be->be_syncinfo );
3195 c->be->be_syncinfo = NULL;
3197 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3200 if(SLAP_SHADOW(c->be)) {
3201 Debug(LDAP_DEBUG_ANY, "%s: "
3202 "syncrepl: database already shadowed.\n",
3205 } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3208 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);