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"
32 /* FIXME: for ldap_ld_free() */
34 #include "../../libraries/libldap/ldap-int.h"
36 static int syncuuid_cmp( const void *, const void * );
37 static void avl_ber_bvfree( void * );
38 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
40 /* callback functions */
41 static int dn_callback( struct slap_op *, struct slap_rep * );
42 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
43 static int null_callback( struct slap_op *, struct slap_rep * );
45 static AttributeDescription *sync_descs[4];
48 init_syncrepl(syncinfo_t *si)
51 char **attrs, **exattrs;
53 if ( !sync_descs[0] ) {
54 sync_descs[0] = slap_schema.si_ad_objectClass;
55 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
56 sync_descs[2] = slap_schema.si_ad_entryCSN;
60 if ( si->si_allattrs && si->si_allopattrs )
63 attrs = anlist2attrs( si->si_anlist );
66 if ( si->si_allattrs ) {
69 if ( !is_at_operational( at_find( attrs[i] ))) {
70 for ( j = i; attrs[j] != NULL; j++ ) {
73 attrs[j] = attrs[j+1];
79 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
80 attrs[i] = ch_strdup("*");
83 } else if ( si->si_allopattrs ) {
86 if ( is_at_operational( at_find( attrs[i] ))) {
87 for ( j = i; attrs[j] != NULL; j++ ) {
90 attrs[j] = attrs[j+1];
96 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
97 attrs[i] = ch_strdup("+");
101 for ( i = 0; sync_descs[i] != NULL; i++ ) {
104 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
105 for ( k = j; attrs[k] != NULL; k++ ) {
108 attrs[k] = attrs[k+1];
116 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
118 if ( si->si_allopattrs ) {
119 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
121 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
124 if ( attrs == NULL ) {
125 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
129 if ( si->si_allopattrs ) {
130 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
132 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
133 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
141 if ( si->si_allattrs == si->si_allopattrs ) {
142 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
143 attrs[i++] = ch_strdup( "*" );
144 attrs[i++] = ch_strdup( "+" );
145 } else if ( si->si_allattrs && !si->si_allopattrs ) {
146 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
147 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
148 attrs[i++] = ch_strdup( "*" );
149 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
150 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
152 } else if ( !si->si_allattrs && si->si_allopattrs ) {
153 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
154 attrs[i++] = ch_strdup( "+" );
155 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
160 si->si_attrs = attrs;
162 exattrs = anlist2attrs( si->si_exanlist );
165 for ( n = 0; exattrs[n] != NULL; n++ ) ;
167 for ( i = 0; sync_descs[i] != NULL; i++ ) {
169 while ( exattrs[j] != NULL ) {
170 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
171 for ( k = j; exattrs[k] != NULL; k++ ) {
173 ch_free( exattrs[k] );
174 exattrs[k] = exattrs[k+1];
182 for ( i = 0; exattrs[i] != NULL; i++ ) {
183 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
185 if ( ( oc = si->si_anlist[j].an_oc ) ) {
187 while ( oc->soc_required[k] ) {
188 if ( !strcmp( exattrs[i],
189 oc->soc_required[k]->sat_cname.bv_val )) {
190 for ( l = i; exattrs[l]; l++ ) {
192 ch_free( exattrs[i] );
193 exattrs[l] = exattrs[l+1];
203 for ( i = 0; exattrs[i] != NULL; i++ ) ;
206 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
209 si->si_exattrs = exattrs;
217 BerElementBuffer berbuf;
218 BerElement *ber = (BerElement *)&berbuf;
219 LDAPControl c[2], *ctrls[3];
220 struct timeval timeout;
224 /* setup LDAP SYNC control */
225 ber_init2( ber, NULL, LBER_USE_DER );
226 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
228 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
230 ber_printf( ber, "{eO}",
232 &si->si_syncCookie.octet_str );
234 ber_printf( ber, "{e}",
238 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
243 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
244 c[0].ldctl_iscritical = si->si_type < 0;
247 if ( si->si_bindconf.sb_authzId ) {
248 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
249 ber_str2bv( si->si_bindconf.sb_authzId, 0, 0, &c[1].ldctl_value );
250 c[1].ldctl_iscritical = 1;
257 timeout.tv_sec = si->si_tlimit;
260 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
261 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
262 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
263 si->si_slimit, &msgid );
274 int cmdline_cookie_found = 0;
276 struct sync_cookie *sc = NULL;
282 psub = &si->si_be->be_nsuffix[0];
284 /* Init connection to master */
285 rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
286 if ( rc != LDAP_SUCCESS ) {
287 Debug( LDAP_DEBUG_ANY,
288 "do_syncrep1: ldap_initialize failed (%s)\n",
289 si->si_provideruri.bv_val, 0, 0 );
293 op->o_protocol = LDAP_VERSION3;
294 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
298 if ( si->si_bindconf.sb_tls ) {
299 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
300 if( rc != LDAP_SUCCESS ) {
301 Debug( LDAP_DEBUG_ANY,
302 "%s: ldap_start_tls failed (%d)\n",
303 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
305 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
309 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
310 #ifdef HAVE_CYRUS_SASL
313 if ( si->si_bindconf.sb_secprops != NULL ) {
314 rc = ldap_set_option( si->si_ld,
315 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
317 if( rc != LDAP_OPT_SUCCESS ) {
318 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
319 "(%s,SECPROPS,\"%s\") failed!\n",
320 si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
325 defaults = lutil_sasl_defaults( si->si_ld, si->si_bindconf.sb_saslmech,
326 si->si_bindconf.sb_realm, si->si_bindconf.sb_authcId,
327 si->si_bindconf.sb_cred, si->si_bindconf.sb_authzId );
329 rc = ldap_sasl_interactive_bind_s( si->si_ld,
330 si->si_bindconf.sb_binddn,
331 si->si_bindconf.sb_saslmech,
337 lutil_sasl_freedefs( defaults );
339 /* FIXME: different error behaviors according to
341 * 2) on err policy : exit, retry, backoff ...
343 if ( rc != LDAP_SUCCESS ) {
344 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
345 "ldap_sasl_interactive_bind_s failed (%d)\n",
348 /* FIXME (see above comment) */
349 /* if Kerberos credentials cache is not active, retry */
350 if ( strcmp( si->si_bindconf.sb_saslmech, "GSSAPI" ) == 0 &&
351 rc == LDAP_LOCAL_ERROR )
353 rc = LDAP_SERVER_DOWN;
358 #else /* HAVE_CYRUS_SASL */
359 /* Should never get here, we trapped this at config time */
361 fprintf( stderr, "not compiled with SASL support\n" );
367 rc = ldap_bind_s( si->si_ld, si->si_bindconf.sb_binddn,
368 si->si_bindconf.sb_cred, si->si_bindconf.sb_method );
369 if ( rc != LDAP_SUCCESS ) {
370 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
371 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
376 /* Set SSF to strongest of TLS, SASL SSFs */
379 op->o_transport_ssf = 0;
381 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
382 == LDAP_SUCCESS && ssl != NULL )
384 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
386 #endif /* HAVE_TLS */
387 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
388 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
389 ? op->o_sasl_ssf : op->o_tls_ssf;
392 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
393 /* get contextCSN shadow replica from database */
394 BerVarray csn = NULL;
395 struct berval newcookie;
397 assert( si->si_rid < 1000 );
398 op->o_req_ndn = op->o_bd->be_nsuffix[0];
399 op->o_req_dn = op->o_req_ndn;
401 /* try to read stored contextCSN */
402 backend_attribute( op, NULL, &op->o_req_ndn,
403 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
405 ch_free( si->si_syncCookie.ctxcsn.bv_val );
406 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
407 ber_bvarray_free_x( csn, op->o_tmpmemctx );
410 si->si_syncCookie.rid = si->si_rid;
412 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
413 if ( si->si_rid == sc->rid ) {
414 cmdline_cookie_found = 1;
419 if ( cmdline_cookie_found ) {
420 /* cookie is supplied in the command line */
422 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
424 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
425 /* if cmdline cookie does not have ctxcsn */
426 /* component, set it to an initial value */
427 slap_init_sync_cookie_ctxcsn( sc );
429 slap_sync_cookie_free( &si->si_syncCookie, 0 );
430 slap_dup_sync_cookie( &si->si_syncCookie, sc );
431 slap_sync_cookie_free( sc, 1 );
434 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
435 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
438 rc = ldap_sync_search( si, op->o_tmpmemctx );
440 if( rc != LDAP_SUCCESS ) {
441 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
442 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
448 ldap_unbind( si->si_ld );
461 LDAPControl **rctrls = NULL;
464 BerElementBuffer berbuf;
465 BerElement *ber = (BerElement *)&berbuf;
467 LDAPMessage *res = NULL;
468 LDAPMessage *msg = NULL;
471 struct berval *retdata = NULL;
476 struct berval syncUUID = BER_BVNULL;
477 struct sync_cookie syncCookie = { 0 };
478 struct sync_cookie syncCookie_req = { 0 };
479 struct berval cookie = BER_BVNULL;
487 Modifications *modlist = NULL;
492 struct timeval *tout_p = NULL;
493 struct timeval tout = { 0, 0 };
495 int refreshDeletes = 0;
497 BerVarray syncUUIDs = NULL;
500 if ( slapd_shutdown ) {
505 ber_init2( ber, NULL, LBER_USE_DER );
506 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
508 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
510 psub = &si->si_be->be_nsuffix[0];
512 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
514 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
520 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
521 tout_p, &res )) > 0 )
523 if ( slapd_shutdown ) {
527 for( msg = ldap_first_message( si->si_ld, res );
529 msg = ldap_next_message( si->si_ld, msg ) )
531 if ( slapd_shutdown ) {
535 switch( ldap_msgtype( msg ) ) {
536 case LDAP_RES_SEARCH_ENTRY:
537 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
538 /* we can't work without the control */
540 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
541 "got search entry without "
542 "control\n", 0, 0, 0 );
547 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
548 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
549 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
550 ber_scanf( ber, /*"{"*/ "m}", &cookie );
551 if ( !BER_BVISNULL( &cookie ) ) {
552 ch_free( syncCookie.octet_str.bv_val );
553 ber_dupbv( &syncCookie.octet_str, &cookie );
555 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
557 slap_parse_sync_cookie( &syncCookie );
560 if ( syncrepl_message_to_entry( si, op, msg,
561 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
562 rc_efree = syncrepl_entry( si, op, entry, &modlist,
563 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
564 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
566 syncrepl_updateCookie( si, op, psub, &syncCookie );
569 ldap_controls_free( rctrls );
571 slap_mods_free( modlist );
573 if ( rc_efree && entry ) {
579 case LDAP_RES_SEARCH_REFERENCE:
580 Debug( LDAP_DEBUG_ANY,
581 "do_syncrep2: reference received error\n", 0, 0, 0 );
584 case LDAP_RES_SEARCH_RESULT:
585 Debug( LDAP_DEBUG_SYNC,
586 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
587 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
591 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
593 ber_scanf( ber, "{" /*"}"*/);
594 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
595 ber_scanf( ber, "m", &cookie );
596 if ( !BER_BVISNULL( &cookie ) ) {
597 ch_free( syncCookie.octet_str.bv_val );
598 ber_dupbv( &syncCookie.octet_str, &cookie);
600 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
602 slap_parse_sync_cookie( &syncCookie );
605 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
607 ber_scanf( ber, "b", &refreshDeletes );
609 ber_scanf( ber, /*"{"*/ "}" );
611 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
613 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
616 value_match( &match, slap_schema.si_ad_entryCSN,
617 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
618 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
619 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
622 if ( !BER_BVISNULL( &syncCookie.octet_str ) &&
623 match < 0 && err == LDAP_SUCCESS )
625 syncrepl_updateCookie( si, op, psub, &syncCookie );
628 ldap_controls_free( rctrls );
630 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
631 /* FIXME : different error behaviors according to
632 * 1) err code : LDAP_BUSY ...
633 * 2) on err policy : stop service, stop sync, retry
635 if ( refreshDeletes == 0 && match < 0 &&
636 err == LDAP_SUCCESS )
638 syncrepl_del_nonpresent( op, si, NULL );
640 avl_free( si->si_presentlist, avl_ber_bvfree );
641 si->si_presentlist = NULL;
648 case LDAP_RES_INTERMEDIATE:
649 rc = ldap_parse_intermediate( si->si_ld, msg,
650 &retoid, &retdata, NULL, 0 );
651 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
652 ber_init2( ber, retdata, LBER_USE_DER );
654 switch ( si_tag = ber_peek_tag( ber, &len )) {
656 case LDAP_TAG_SYNC_NEW_COOKIE:
657 Debug( LDAP_DEBUG_SYNC,
658 "do_syncrep2: %s - %s%s\n",
659 "LDAP_RES_INTERMEDIATE",
660 "NEW_COOKIE", "\n" );
661 ber_scanf( ber, "tm", &tag, &cookie );
663 case LDAP_TAG_SYNC_REFRESH_DELETE:
664 case LDAP_TAG_SYNC_REFRESH_PRESENT:
665 Debug( LDAP_DEBUG_SYNC,
666 "do_syncrep2: %s - %s%s\n",
667 "LDAP_RES_INTERMEDIATE",
668 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
669 "REFRESH_PRESENT" : "REFRESH_DELETE",
671 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
672 si->si_refreshDelete = 1;
674 si->si_refreshPresent = 1;
676 ber_scanf( ber, "t{" /*"}"*/, &tag );
677 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
679 ber_scanf( ber, "m", &cookie );
680 if ( !BER_BVISNULL( &cookie ) ) {
681 ch_free( syncCookie.octet_str.bv_val );
682 ber_dupbv( &syncCookie.octet_str, &cookie );
684 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
686 slap_parse_sync_cookie( &syncCookie );
689 if ( ber_peek_tag( ber, &len ) ==
690 LDAP_TAG_REFRESHDONE )
692 ber_scanf( ber, "b", &refreshDone );
694 ber_scanf( ber, /*"{"*/ "}" );
696 case LDAP_TAG_SYNC_ID_SET:
697 Debug( LDAP_DEBUG_SYNC,
698 "do_syncrep2: %s - %s%s\n",
699 "LDAP_RES_INTERMEDIATE",
702 ber_scanf( ber, "t{" /*"}"*/, &tag );
703 if ( ber_peek_tag( ber, &len ) ==
704 LDAP_TAG_SYNC_COOKIE )
706 ber_scanf( ber, "m", &cookie );
707 if ( !BER_BVISNULL( &cookie ) ) {
708 ch_free( syncCookie.octet_str.bv_val );
709 ber_dupbv( &syncCookie.octet_str, &cookie );
711 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
713 slap_parse_sync_cookie( &syncCookie );
716 if ( ber_peek_tag( ber, &len ) ==
717 LDAP_TAG_REFRESHDELETES )
719 ber_scanf( ber, "b", &refreshDeletes );
721 ber_scanf( ber, "[W]", &syncUUIDs );
722 ber_scanf( ber, /*"{"*/ "}" );
723 if ( refreshDeletes ) {
724 syncrepl_del_nonpresent( op, si, syncUUIDs );
725 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
727 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
728 struct berval *syncuuid_bv;
729 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
730 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
731 avl_insert( &si->si_presentlist,
732 (caddr_t) syncuuid_bv,
733 syncuuid_cmp, avl_dup_error );
735 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
739 Debug( LDAP_DEBUG_ANY,
740 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
741 (long) si_tag, 0, 0 );
742 ldap_memfree( retoid );
743 ber_bvfree( retdata );
747 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
749 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
752 value_match( &match, slap_schema.si_ad_entryCSN,
753 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
754 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
755 &syncCookie_req.ctxcsn,
756 &syncCookie.ctxcsn, &text );
759 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
762 syncrepl_updateCookie( si, op, psub, &syncCookie);
765 if ( si->si_refreshPresent == 1 ) {
767 syncrepl_del_nonpresent( op, si, NULL );
771 ldap_memfree( retoid );
772 ber_bvfree( retdata );
776 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
777 "unknown intermediate response (%d)\n",
779 ldap_memfree( retoid );
780 ber_bvfree( retdata );
786 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
787 "unknown message\n", 0, 0, 0 );
791 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
792 slap_sync_cookie_free( &syncCookie_req, 0 );
793 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
794 slap_sync_cookie_free( &syncCookie, 0 );
804 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
805 errstr = ldap_err2string( rc );
807 Debug( LDAP_DEBUG_ANY,
808 "do_syncrep2 : %s\n", errstr, 0, 0 );
812 slap_sync_cookie_free( &syncCookie, 0 );
813 slap_sync_cookie_free( &syncCookie_req, 0 );
815 if ( res ) ldap_msgfree( res );
817 if ( rc && si->si_ld ) {
818 ldap_unbind( si->si_ld );
830 struct re_s* rtask = arg;
831 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
832 Connection conn = {0};
833 char opbuf[OPERATION_BUFFER_SIZE];
835 int rc = LDAP_SUCCESS;
842 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
847 ldap_pvt_thread_mutex_lock( &si->si_mutex );
849 switch( abs( si->si_type )) {
850 case LDAP_SYNC_REFRESH_ONLY:
851 case LDAP_SYNC_REFRESH_AND_PERSIST:
854 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
858 if ( slapd_shutdown ) {
860 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
861 connection_client_stop( s );
862 ldap_unbind( si->si_ld );
865 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
869 op = (Operation *)opbuf;
870 connection_fake_init( &conn, op, ctx );
872 /* use global malloc for now */
873 op->o_tmpmemctx = NULL;
874 op->o_tmpmfuncs = &ch_mfuncs;
876 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
877 op->o_bd = be = si->si_be;
878 op->o_dn = op->o_bd->be_rootdn;
879 op->o_ndn = op->o_bd->be_rootndn;
881 /* Establish session, do search */
884 si->si_refreshDelete = 0;
885 si->si_refreshPresent = 0;
886 rc = do_syncrep1( op, si );
889 /* Process results */
890 if ( rc == LDAP_SUCCESS ) {
891 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
893 rc = do_syncrep2( op, si );
895 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
896 /* If we succeeded, enable the connection for further listening.
897 * If we failed, tear down the connection and reschedule.
899 if ( rc == LDAP_SUCCESS ) {
901 rc = connection_client_setup( s, do_syncrepl, arg );
903 connection_client_enable( s );
905 } else if ( !first ) {
909 if ( rc == -2 ) rc = 0;
913 /* At this point, we have 4 cases:
914 * 1) for any hard failure, give up and remove this task
915 * 2) for ServerDown, reschedule this task to run
916 * 3) for Refresh and Success, reschedule to run
917 * 4) for Persist and Success, reschedule to defer
919 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
921 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
922 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
926 connection_client_stop( s );
929 if ( rc == LDAP_SUCCESS ) {
930 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
933 rtask->interval.tv_sec = si->si_interval;
934 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
935 if ( si->si_retrynum ) {
936 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
937 si->si_retrynum[i] = si->si_retrynum_init[i];
939 si->si_retrynum[i] = -2;
942 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
943 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
947 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
948 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
949 } else if ( si->si_retrynum[i] >= -1 ) {
950 if ( si->si_retrynum[i] > 0 )
951 si->si_retrynum[i]--;
952 rtask->interval.tv_sec = si->si_retryinterval[i];
953 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
954 slap_wake_listener();
958 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
959 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
965 syncrepl_message_to_entry(
969 Modifications **modlist,
975 BerElement *ber = NULL;
978 Modifications **modtail = modlist;
981 char txtbuf[SLAP_TEXT_BUFLEN];
982 size_t textlen = sizeof txtbuf;
984 struct berval bdn = {0, NULL}, dn, ndn;
989 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
990 Debug( LDAP_DEBUG_ANY,
991 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
995 op->o_tag = LDAP_REQ_ADD;
997 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
999 if ( rc != LDAP_SUCCESS ) {
1000 Debug( LDAP_DEBUG_ANY,
1001 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1005 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1006 ber_dupbv( &op->o_req_dn, &dn );
1007 ber_dupbv( &op->o_req_ndn, &ndn );
1008 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1009 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1011 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1014 return LDAP_SUCCESS;
1017 if ( entry == NULL ) {
1021 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1023 e->e_name = op->o_req_dn;
1024 e->e_nname = op->o_req_ndn;
1026 while ( ber_remaining( ber ) ) {
1027 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1028 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1033 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1035 mod->sml_op = LDAP_MOD_REPLACE;
1036 mod->sml_next = NULL;
1037 mod->sml_desc = NULL;
1038 mod->sml_type = tmp.sml_type;
1039 mod->sml_values = tmp.sml_values;
1040 mod->sml_nvalues = NULL;
1043 modtail = &mod->sml_next;
1046 if ( *modlist == NULL ) {
1047 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1053 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1055 if ( rc != LDAP_SUCCESS ) {
1056 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1061 /* Strip out dynamically generated attrs */
1062 for ( modtail = modlist; *modtail ; ) {
1064 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1065 *modtail = mod->sml_next;
1066 slap_mod_free( &mod->sml_mod, 0 );
1069 modtail = &mod->sml_next;
1073 /* Strip out attrs in exattrs list */
1074 for ( modtail = modlist; *modtail ; ) {
1076 if ( ldap_charray_inlist( si->si_exattrs,
1077 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1078 *modtail = mod->sml_next;
1079 slap_mod_free( &mod->sml_mod, 0 );
1082 modtail = &mod->sml_next;
1086 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1087 if( rc != LDAP_SUCCESS ) {
1088 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1093 ber_free ( ber, 0 );
1094 if ( rc != LDAP_SUCCESS ) {
1104 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1106 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1107 * entry if a previous refresh was interrupted before sending us a new
1108 * context state. We try to compare the new entry to the existing entry
1109 * and ignore the new entry if they are the same.
1111 * Also, we may get an update where the entryDN has changed, due to
1112 * a ModDn on the provider. We detect this as well, so we can issue
1113 * the corresponding operation locally.
1115 * In the case of a modify, we get a list of all the attributes
1116 * in the original entry. Rather than deleting the entry and re-adding it,
1117 * we issue a Modify request that deletes all the attributes and adds all
1118 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1121 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1122 * a ModDN we will issue both operations on the local database.
1124 typedef struct dninfo {
1128 int renamed; /* Was an existing entry renamed? */
1129 int wasChanged; /* are the attributes changed? */
1130 int attrs; /* how many attribute types are in the ads list */
1131 AttributeDescription **ads;
1139 Modifications** modlist,
1141 struct berval* syncUUID,
1142 struct sync_cookie* syncCookie_req,
1143 struct berval* syncCSN )
1145 Backend *be = op->o_bd;
1146 slap_callback cb = { NULL };
1147 struct berval *syncuuid_bv = NULL;
1148 struct berval syncUUID_strrep = BER_BVNULL;
1149 struct berval uuid_bv = BER_BVNULL;
1151 SlapReply rs_search = {REP_RESULT};
1152 SlapReply rs_delete = {REP_RESULT};
1153 SlapReply rs_add = {REP_RESULT};
1154 SlapReply rs_modify = {REP_RESULT};
1156 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1157 int rc = LDAP_SUCCESS;
1158 int ret = LDAP_SUCCESS;
1160 struct berval pdn = BER_BVNULL;
1164 switch( syncstate ) {
1165 case LDAP_SYNC_PRESENT:
1166 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1168 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1171 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1173 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1175 case LDAP_SYNC_DELETE:
1176 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1178 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1180 case LDAP_SYNC_MODIFY:
1181 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1183 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1186 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1188 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1191 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1192 if ( !si->si_refreshPresent ) {
1193 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1194 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1195 syncuuid_cmp, avl_dup_error );
1199 if ( syncstate == LDAP_SYNC_PRESENT ) {
1201 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1202 if ( entry == NULL ) {
1207 f.f_choice = LDAP_FILTER_EQUALITY;
1209 ava.aa_desc = slap_schema.si_ad_entryUUID;
1210 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1211 ava.aa_value = *syncUUID;
1212 op->ors_filter = &f;
1214 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1215 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1216 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1217 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1218 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1219 syncUUID->bv_val, syncUUID->bv_len );
1220 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1221 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1223 op->o_tag = LDAP_REQ_SEARCH;
1224 op->ors_scope = LDAP_SCOPE_SUBTREE;
1226 /* get the entry for this UUID */
1227 op->o_req_dn = si->si_base;
1228 op->o_req_ndn = si->si_base;
1230 op->o_time = slap_get_time();
1231 op->ors_tlimit = SLAP_NO_LIMIT;
1234 op->ors_attrs = slap_anlist_all_attributes;
1235 op->ors_attrsonly = 0;
1237 /* set callback function */
1238 op->o_callback = &cb;
1239 cb.sc_response = dn_callback;
1240 cb.sc_private = &dni;
1241 dni.new_entry = entry;
1243 if ( limits_check( op, &rs_search ) == 0 ) {
1244 rc = be->be_search( op, &rs_search );
1245 Debug( LDAP_DEBUG_SYNC,
1246 "syncrepl_entry: %s (%d)\n",
1247 "be_search", rc, 0 );
1250 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1251 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1254 cb.sc_response = null_callback;
1257 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1258 Debug( LDAP_DEBUG_SYNC,
1259 "syncrepl_entry: %s\n",
1260 entry->e_name.bv_val, 0, 0 );
1262 Debug( LDAP_DEBUG_SYNC,
1263 "syncrepl_entry: %s\n",
1264 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1267 if ( syncstate != LDAP_SYNC_DELETE ) {
1268 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1271 /* add if missing */
1272 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1273 &syncUUID_strrep, syncUUID );
1275 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1276 /* replace only if necessary */
1277 if ( a->a_nvals != a->a_vals ) {
1278 ber_memfree( a->a_nvals[0].bv_val );
1279 ber_dupbv( &a->a_nvals[0], syncUUID );
1281 ber_memfree( a->a_vals[0].bv_val );
1282 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1286 switch ( syncstate ) {
1288 case LDAP_SYNC_MODIFY:
1290 if ( BER_BVISNULL( &dni.dn )) {
1292 op->o_req_dn = entry->e_name;
1293 op->o_req_ndn = entry->e_nname;
1294 op->o_tag = LDAP_REQ_ADD;
1297 rc = be->be_add( op, &rs_add );
1298 Debug( LDAP_DEBUG_SYNC,
1299 "syncrepl_entry: %s (%d)\n",
1301 switch ( rs_add.sr_err ) {
1303 be_entry_release_w( op, entry );
1308 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1309 * only if the suffix entry is not present */
1310 case LDAP_NO_SUCH_OBJECT:
1311 syncrepl_add_glue( op, entry );
1315 /* if an entry was added via syncrepl_add_glue(),
1316 * it likely has no entryUUID, so the previous
1317 * be_search() doesn't find it. In this case,
1318 * give syncrepl a chance to modify it. Also
1319 * allow for entries that were recreated with the
1320 * same DN but a different entryUUID.
1322 case LDAP_ALREADY_EXISTS:
1324 Operation op2 = *op;
1325 SlapReply rs2 = { 0 };
1326 slap_callback cb2 = { 0 };
1328 op2.o_tag = LDAP_REQ_SEARCH;
1329 op2.o_req_dn = entry->e_name;
1330 op2.o_req_ndn = entry->e_nname;
1331 op2.ors_scope = LDAP_SCOPE_BASE;
1332 op2.ors_attrs = slap_anlist_all_attributes;
1333 op2.ors_attrsonly = 0;
1334 op2.ors_limit = NULL;
1336 op2.ors_tlimit = SLAP_NO_LIMIT;
1338 f.f_choice = LDAP_FILTER_PRESENT;
1339 f.f_desc = slap_schema.si_ad_objectClass;
1340 op2.ors_filter = &f;
1341 op2.ors_filterstr = generic_filterstr;
1343 op2.o_callback = &cb2;
1344 cb2.sc_response = dn_callback;
1345 cb2.sc_private = &dni;
1347 be->be_search( &op2, &rs2 );
1355 Debug( LDAP_DEBUG_ANY,
1356 "syncrepl_entry : be_add failed (%d)\n",
1357 rs_add.sr_err, 0, 0 );
1364 op->o_req_dn = dni.dn;
1365 op->o_req_ndn = dni.ndn;
1366 if ( dni.renamed ) {
1367 struct berval noldp, newp, nnewp;
1369 op->o_tag = LDAP_REQ_MODRDN;
1370 dnRdn( &entry->e_name, &op->orr_newrdn );
1371 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1373 dnParent( &dni.ndn, &noldp );
1374 dnParent( &entry->e_nname, &nnewp );
1375 if ( !dn_match( &noldp, &newp )) {
1376 dnParent( &entry->e_name, &newp );
1377 op->orr_newSup = &newp;
1378 op->orr_nnewSup = &nnewp;
1380 op->orr_deleteoldrdn = 0;
1381 rc = be->be_modrdn( op, &rs_modify );
1382 Debug( LDAP_DEBUG_SYNC,
1383 "syncrepl_entry: %s (%d)\n",
1384 "be_modrdn", rc, 0 );
1385 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1386 op->o_req_dn = entry->e_name;
1387 op->o_req_ndn = entry->e_nname;
1393 if ( dni.wasChanged ) {
1394 Modifications *mod, *modhead = NULL;
1395 Modifications *modtail = NULL;
1398 op->o_tag = LDAP_REQ_MODIFY;
1402 /* Delete all the old attrs */
1403 for ( i = 0; i < dni.attrs; i++ ) {
1404 mod = ch_malloc( sizeof( Modifications ) );
1405 mod->sml_op = LDAP_MOD_DELETE;
1406 mod->sml_desc = dni.ads[i];
1407 mod->sml_type = mod->sml_desc->ad_cname;
1408 mod->sml_values = NULL;
1409 mod->sml_nvalues = NULL;
1410 if ( !modhead ) modhead = mod;
1412 modtail->sml_next = mod;
1417 /* Append passed in list to ours */
1419 modtail->sml_next = *modlist;
1425 /* Find end of this list */
1426 for ( ; mod != NULL; mod = mod->sml_next ) {
1430 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1431 mod->sml_op = LDAP_MOD_REPLACE;
1432 mod->sml_desc = slap_schema.si_ad_entryUUID;
1433 mod->sml_type = mod->sml_desc->ad_cname;
1434 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1435 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1436 ber_dupbv( &uuid_bv, syncUUID );
1437 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1438 modtail->sml_next = mod;
1440 op->o_tag = LDAP_REQ_MODIFY;
1441 op->orm_modlist = *modlist;
1443 rc = be->be_modify( op, &rs_modify );
1444 Debug( LDAP_DEBUG_SYNC,
1445 "syncrepl_entry: %s (%d)\n",
1446 "be_modify", rc, 0 );
1447 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1448 Debug( LDAP_DEBUG_ANY,
1449 "syncrepl_entry : be_modify failed (%d)\n",
1450 rs_modify.sr_err, 0, 0 );
1455 case LDAP_SYNC_DELETE :
1456 if ( !BER_BVISNULL( &dni.dn )) {
1457 op->o_req_dn = dni.dn;
1458 op->o_req_ndn = dni.ndn;
1459 op->o_tag = LDAP_REQ_DELETE;
1460 rc = be->be_delete( op, &rs_delete );
1461 Debug( LDAP_DEBUG_SYNC,
1462 "syncrepl_entry: %s (%d)\n",
1463 "be_delete", rc, 0 );
1465 while ( rs_delete.sr_err == LDAP_SUCCESS
1466 && op->o_delete_glue_parent ) {
1467 op->o_delete_glue_parent = 0;
1468 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1469 slap_callback cb = { NULL };
1470 cb.sc_response = slap_null_cb;
1471 dnParent( &op->o_req_ndn, &pdn );
1473 op->o_req_ndn = pdn;
1474 op->o_callback = &cb;
1475 op->o_bd->be_delete( op, &rs_delete );
1485 Debug( LDAP_DEBUG_ANY,
1486 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1492 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1493 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1494 BER_BVZERO( &syncUUID_strrep );
1497 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1499 if ( !BER_BVISNULL( &dni.ndn ) ) {
1500 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1502 if ( !BER_BVISNULL( &dni.dn ) ) {
1503 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1508 static struct berval gcbva[] = {
1514 #define NP_DELETE_ONE 2
1517 syncrepl_del_nonpresent(
1522 Backend* be = op->o_bd;
1523 slap_callback cb = { NULL };
1524 SlapReply rs_search = {REP_RESULT};
1525 SlapReply rs_delete = {REP_RESULT};
1526 SlapReply rs_modify = {REP_RESULT};
1527 struct nonpresent_entry *np_list, *np_prev;
1529 AttributeName an[2];
1531 struct berval pdn = BER_BVNULL;
1532 struct berval org_req_dn = BER_BVNULL;
1533 struct berval org_req_ndn = BER_BVNULL;
1535 op->o_req_dn = si->si_base;
1536 op->o_req_ndn = si->si_base;
1538 cb.sc_response = nonpresent_callback;
1541 op->o_callback = &cb;
1542 op->o_tag = LDAP_REQ_SEARCH;
1543 op->ors_scope = si->si_scope;
1544 op->ors_deref = LDAP_DEREF_NEVER;
1545 op->o_time = slap_get_time();
1546 op->ors_tlimit = SLAP_NO_LIMIT;
1551 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1554 op->ors_attrsonly = 1;
1555 op->ors_attrs = slap_anlist_no_attrs;
1556 op->ors_limit = NULL;
1557 op->ors_filter = &uf;
1560 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1562 uf.f_choice = LDAP_FILTER_EQUALITY;
1563 si->si_refreshDelete |= NP_DELETE_ONE;
1565 for (i=0; uuids[i].bv_val; i++) {
1567 uf.f_av_value = uuids[i];
1568 rc = be->be_search( op, &rs_search );
1570 si->si_refreshDelete ^= NP_DELETE_ONE;
1572 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1573 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1574 an[0].an_desc = slap_schema.si_ad_entryUUID;
1576 op->ors_slimit = SLAP_NO_LIMIT;
1577 op->ors_attrsonly = 0;
1578 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1579 op->ors_filterstr = si->si_filterstr;
1580 op->o_nocaching = 1;
1582 if ( limits_check( op, &rs_search ) == 0 ) {
1583 rc = be->be_search( op, &rs_search );
1585 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1588 op->o_nocaching = 0;
1590 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1592 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1594 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1595 while ( np_list != NULL ) {
1596 LDAP_LIST_REMOVE( np_list, npe_link );
1598 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1599 op->o_tag = LDAP_REQ_DELETE;
1600 op->o_callback = &cb;
1601 cb.sc_response = null_callback;
1603 op->o_req_dn = *np_prev->npe_name;
1604 op->o_req_ndn = *np_prev->npe_nname;
1605 rc = op->o_bd->be_delete( op, &rs_delete );
1607 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1608 Modifications mod1, mod2;
1609 mod1.sml_op = LDAP_MOD_REPLACE;
1610 mod1.sml_desc = slap_schema.si_ad_objectClass;
1611 mod1.sml_type = mod1.sml_desc->ad_cname;
1612 mod1.sml_values = &gcbva[0];
1613 mod1.sml_nvalues = NULL;
1614 mod1.sml_next = &mod2;
1616 mod2.sml_op = LDAP_MOD_REPLACE;
1617 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
1618 mod2.sml_type = mod2.sml_desc->ad_cname;
1619 mod2.sml_values = &gcbva[1];
1620 mod2.sml_nvalues = NULL;
1621 mod2.sml_next = NULL;
1623 op->o_tag = LDAP_REQ_MODIFY;
1624 op->orm_modlist = &mod1;
1626 rc = be->be_modify( op, &rs_modify );
1629 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1630 op->o_delete_glue_parent ) {
1631 op->o_delete_glue_parent = 0;
1632 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1633 slap_callback cb = { NULL };
1634 cb.sc_response = slap_null_cb;
1635 dnParent( &op->o_req_ndn, &pdn );
1637 op->o_req_ndn = pdn;
1638 op->o_callback = &cb;
1639 /* give it a root privil ? */
1640 op->o_bd->be_delete( op, &rs_delete );
1646 op->o_delete_glue_parent = 0;
1648 ber_bvfree( np_prev->npe_name );
1649 ber_bvfree( np_prev->npe_nname );
1653 slap_graduate_commit_csn( op );
1664 Backend *be = op->o_bd;
1665 slap_callback cb = { NULL };
1670 struct berval dn = {0, NULL};
1671 struct berval ndn = {0, NULL};
1673 SlapReply rs_add = {REP_RESULT};
1676 op->o_tag = LDAP_REQ_ADD;
1677 op->o_callback = &cb;
1678 cb.sc_response = null_callback;
1679 cb.sc_private = NULL;
1684 /* count RDNs in suffix */
1685 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1686 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1696 /* Start with BE suffix */
1697 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1698 comma = strrchr( dn.bv_val, ',' );
1699 if ( ptr ) *ptr = ',';
1700 if ( comma ) *comma = '\0';
1705 dn.bv_len -= ptr - dn.bv_val;
1708 /* the normalizedDNs are always the same length, no counting
1711 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1712 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1713 ndn.bv_len = be->be_nsuffix[0].bv_len;
1716 while ( ndn.bv_val > e->e_nname.bv_val ) {
1717 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1718 ber_dupbv( &glue->e_name, &dn );
1719 ber_dupbv( &glue->e_nname, &ndn );
1721 a = ch_calloc( 1, sizeof( Attribute ));
1722 a->a_desc = slap_schema.si_ad_objectClass;
1724 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1725 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1726 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1727 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1729 a->a_nvals = a->a_vals;
1731 a->a_next = glue->e_attrs;
1734 a = ch_calloc( 1, sizeof( Attribute ));
1735 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1737 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1738 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1739 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1741 a->a_nvals = a->a_vals;
1743 a->a_next = glue->e_attrs;
1746 op->o_req_dn = glue->e_name;
1747 op->o_req_ndn = glue->e_nname;
1749 rc = be->be_add ( op, &rs_add );
1750 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1751 be_entry_release_w( op, glue );
1753 /* incl. ALREADY EXIST */
1757 /* Move to next child */
1758 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1761 if ( ptr == e->e_name.bv_val ) break;
1763 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1764 for( ptr = ndn.bv_val-2;
1765 ptr > e->e_nname.bv_val && *ptr != ',';
1771 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1774 op->o_req_dn = e->e_name;
1775 op->o_req_ndn = e->e_nname;
1777 rc = be->be_add ( op, &rs_add );
1778 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1779 be_entry_release_w( op, e );
1788 syncrepl_updateCookie(
1792 struct sync_cookie *syncCookie )
1794 Backend *be = op->o_bd;
1795 Modifications mod = {0};
1796 struct berval vals[2];
1799 char txtbuf[SLAP_TEXT_BUFLEN];
1800 size_t textlen = sizeof txtbuf;
1804 slap_callback cb = { NULL };
1805 SlapReply rs_modify = {REP_RESULT};
1807 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1808 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1810 mod.sml_op = LDAP_MOD_REPLACE;
1811 mod.sml_desc = slap_schema.si_ad_contextCSN;
1812 mod.sml_type = mod.sml_desc->ad_cname;
1813 mod.sml_values = vals;
1814 vals[0] = si->si_syncCookie.ctxcsn;
1815 vals[1].bv_val = NULL;
1818 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1820 op->o_tag = LDAP_REQ_MODIFY;
1822 assert( si->si_rid < 1000 );
1824 cb.sc_response = null_callback;
1827 op->o_callback = &cb;
1828 op->o_req_dn = op->o_bd->be_suffix[0];
1829 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1831 /* update contextCSN */
1832 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
1833 op->orm_modlist = &mod;
1834 rc = be->be_modify( op, &rs_modify );
1837 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1838 Debug( LDAP_DEBUG_ANY,
1839 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1843 slap_graduate_commit_csn( op );
1853 dninfo *dni = op->o_callback->sc_private;
1855 if ( rs->sr_type == REP_SEARCH ) {
1856 if ( !BER_BVISNULL( &dni->dn ) ) {
1857 Debug( LDAP_DEBUG_ANY,
1858 "dn_callback : consistency error - "
1859 "entryUUID is not unique\n", 0, 0, 0 );
1861 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
1862 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1863 /* If there is a new entry, see if it differs from the old.
1864 * We compare the non-normalized values so that cosmetic changes
1865 * in the provider are always propagated.
1867 if ( dni->new_entry ) {
1868 Attribute *old, *new;
1871 /* Did the DN change? Note that we don't explicitly try to
1872 * discover if the deleteOldRdn argument applies here. It
1873 * would save an unnecessary Modify if we detected it, but
1874 * that's a fair amount of trouble to compare the two attr
1877 if ( !dn_match( &rs->sr_entry->e_name,
1878 &dni->new_entry->e_name ) )
1883 for ( i = 0, old = rs->sr_entry->e_attrs;
1885 i++, old = old->a_next )
1890 /* We assume that attributes are saved in the same order
1891 * in the remote and local databases. So if we walk through
1892 * the attributeDescriptions one by one they should match in
1893 * lock step. If not, we signal a change. Otherwise we test
1896 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
1898 old = old->a_next, new = new->a_next )
1900 if ( old->a_desc != new->a_desc ) {
1901 dni->wasChanged = 1;
1904 for ( i = 0; ; i++ ) {
1906 nold = BER_BVISNULL( &old->a_vals[i] );
1907 nnew = BER_BVISNULL( &new->a_vals[i] );
1908 /* If both are empty, stop looking */
1909 if ( nold && nnew ) {
1912 /* If they are different, stop looking */
1913 if ( nold != nnew ) {
1914 dni->wasChanged = 1;
1917 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
1918 dni->wasChanged = 1;
1922 if ( dni->wasChanged ) break;
1924 if ( dni->wasChanged ) {
1925 dni->ads = op->o_tmpalloc( dni->attrs *
1926 sizeof(AttributeDescription *), op->o_tmpmemctx );
1928 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
1929 dni->ads[i] = old->a_desc;
1935 } else if ( rs->sr_type == REP_RESULT ) {
1936 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1937 Debug( LDAP_DEBUG_ANY,
1938 "dn_callback : consistency error - "
1939 "entryUUID is not unique\n", 0, 0, 0 );
1943 return LDAP_SUCCESS;
1947 nonpresent_callback(
1951 syncinfo_t *si = op->o_callback->sc_private;
1954 struct berval* present_uuid = NULL;
1955 struct nonpresent_entry *np_entry;
1957 if ( rs->sr_type == REP_RESULT ) {
1958 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1959 si->si_presentlist = NULL;
1961 } else if ( rs->sr_type == REP_SEARCH ) {
1962 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
1963 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1965 if ( a == NULL ) return 0;
1967 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1971 if ( present_uuid == NULL ) {
1972 np_entry = (struct nonpresent_entry *)
1973 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1974 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1975 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1976 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1979 avl_delete( &si->si_presentlist,
1980 &a->a_nvals[0], syncuuid_cmp );
1981 ch_free( present_uuid->bv_val );
1982 ch_free( present_uuid );
1985 return LDAP_SUCCESS;
1993 if ( rs->sr_err != LDAP_SUCCESS &&
1994 rs->sr_err != LDAP_REFERRAL &&
1995 rs->sr_err != LDAP_ALREADY_EXISTS &&
1996 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1997 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1999 Debug( LDAP_DEBUG_ANY,
2000 "null_callback : error code 0x%x\n",
2003 return LDAP_SUCCESS;
2007 slap_uuidstr_from_normalized(
2008 struct berval* uuidstr,
2009 struct berval* normalized,
2013 unsigned char nibble;
2016 if ( normalized == NULL ) return NULL;
2017 if ( normalized->bv_len != 16 ) return NULL;
2022 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2023 if ( new == NULL ) {
2030 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2031 if ( new != uuidstr ) {
2032 slap_sl_free( new, ctx );
2037 for ( i = 0; i < 16; i++ ) {
2038 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2039 new->bv_val[(i<<1)+d] = '-';
2043 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2044 if ( nibble < 10 ) {
2045 new->bv_val[(i<<1)+d] = nibble + '0';
2047 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2050 nibble = (normalized->bv_val[i]) & 0xF;
2051 if ( nibble < 10 ) {
2052 new->bv_val[(i<<1)+d+1] = nibble + '0';
2054 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2058 new->bv_val[new->bv_len] = '\0';
2063 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2065 const struct berval *uuid1 = v_uuid1;
2066 const struct berval *uuid2 = v_uuid2;
2067 int rc = uuid1->bv_len - uuid2->bv_len;
2068 if ( rc ) return rc;
2069 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2073 avl_ber_bvfree( void *v_bv )
2075 struct berval *bv = (struct berval *)v_bv;
2077 if( v_bv == NULL ) return;
2078 if ( !BER_BVISNULL( bv ) ) {
2079 ch_free( bv->bv_val );
2081 ch_free( (char *) bv );
2085 syncinfo_free( syncinfo_t *sie )
2087 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2088 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2089 ch_free( sie->si_provideruri.bv_val );
2092 bindconf_free( &sie->si_bindconf );
2094 if ( sie->si_filterstr.bv_val ) {
2095 ch_free( sie->si_filterstr.bv_val );
2097 if ( sie->si_base.bv_val ) {
2098 ch_free( sie->si_base.bv_val );
2100 if ( sie->si_attrs ) {
2102 while ( sie->si_attrs[i] != NULL ) {
2103 ch_free( sie->si_attrs[i] );
2106 ch_free( sie->si_attrs );
2108 if ( sie->si_exattrs ) {
2110 while ( sie->si_exattrs[i] != NULL ) {
2111 ch_free( sie->si_exattrs[i] );
2114 ch_free( sie->si_exattrs );
2116 if ( sie->si_anlist ) {
2118 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2119 ch_free( sie->si_anlist[i].an_name.bv_val );
2122 ch_free( sie->si_anlist );
2124 if ( sie->si_exanlist ) {
2126 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2127 ch_free( sie->si_exanlist[i].an_name.bv_val );
2130 ch_free( sie->si_exanlist );
2132 if ( sie->si_retryinterval ) {
2133 ch_free( sie->si_retryinterval );
2135 if ( sie->si_retrynum ) {
2136 ch_free( sie->si_retrynum );
2138 if ( sie->si_retrynum_init ) {
2139 ch_free( sie->si_retrynum_init );
2141 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2142 if ( sie->si_presentlist ) {
2143 avl_free( sie->si_presentlist, avl_ber_bvfree );
2146 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2148 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2149 struct nonpresent_entry* npe;
2150 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2151 LDAP_LIST_REMOVE( npe, npe_link );
2152 if ( npe->npe_name ) {
2153 if ( npe->npe_name->bv_val ) {
2154 ch_free( npe->npe_name->bv_val );
2156 ch_free( npe->npe_name );
2158 if ( npe->npe_nname ) {
2159 if ( npe->npe_nname->bv_val ) {
2160 ch_free( npe->npe_nname->bv_val );
2162 ch_free( npe->npe_nname );