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 ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
248 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
249 c[1].ldctl_value = si->si_bindconf.sb_authzId;
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,
326 si->si_bindconf.sb_saslmech.bv_val,
327 si->si_bindconf.sb_realm.bv_val,
328 si->si_bindconf.sb_authcId.bv_val,
329 si->si_bindconf.sb_cred.bv_val,
330 si->si_bindconf.sb_authzId.bv_val );
332 rc = ldap_sasl_interactive_bind_s( si->si_ld,
333 si->si_bindconf.sb_binddn.bv_val,
334 si->si_bindconf.sb_saslmech.bv_val,
340 lutil_sasl_freedefs( defaults );
342 /* FIXME: different error behaviors according to
344 * 2) on err policy : exit, retry, backoff ...
346 if ( rc != LDAP_SUCCESS ) {
347 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
349 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
350 "ldap_sasl_interactive_bind_s failed (%d)\n",
353 /* FIXME (see above comment) */
354 /* if Kerberos credentials cache is not active, retry */
355 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
356 rc == LDAP_LOCAL_ERROR )
358 rc = LDAP_SERVER_DOWN;
363 #else /* HAVE_CYRUS_SASL */
364 /* Should never get here, we trapped this at config time */
366 fprintf( stderr, "not compiled with SASL support\n" );
371 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
372 rc = ldap_sasl_bind_s( si->si_ld,
373 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
374 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
375 if ( rc != LDAP_SUCCESS ) {
376 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
377 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
382 /* Set SSF to strongest of TLS, SASL SSFs */
385 op->o_transport_ssf = 0;
387 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
388 == LDAP_SUCCESS && ssl != NULL )
390 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
392 #endif /* HAVE_TLS */
393 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
394 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
395 ? op->o_sasl_ssf : op->o_tls_ssf;
398 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
399 /* get contextCSN shadow replica from database */
400 BerVarray csn = NULL;
402 assert( si->si_rid < 1000 );
403 op->o_req_ndn = op->o_bd->be_nsuffix[0];
404 op->o_req_dn = op->o_req_ndn;
406 /* try to read stored contextCSN */
407 backend_attribute( op, NULL, &op->o_req_ndn,
408 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
410 ch_free( si->si_syncCookie.ctxcsn.bv_val );
411 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
412 ber_bvarray_free_x( csn, op->o_tmpmemctx );
415 si->si_syncCookie.rid = si->si_rid;
417 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
418 if ( si->si_rid == sc->rid ) {
419 cmdline_cookie_found = 1;
424 if ( cmdline_cookie_found ) {
425 /* cookie is supplied in the command line */
427 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
429 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
430 /* if cmdline cookie does not have ctxcsn */
431 /* component, set it to an initial value */
432 slap_init_sync_cookie_ctxcsn( sc );
434 slap_sync_cookie_free( &si->si_syncCookie, 0 );
435 slap_dup_sync_cookie( &si->si_syncCookie, sc );
436 slap_sync_cookie_free( sc, 1 );
439 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
440 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
443 rc = ldap_sync_search( si, op->o_tmpmemctx );
445 if( rc != LDAP_SUCCESS ) {
446 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
447 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
453 ldap_unbind_ext( si->si_ld, NULL, NULL );
466 LDAPControl **rctrls = NULL;
469 BerElementBuffer berbuf;
470 BerElement *ber = (BerElement *)&berbuf;
472 LDAPMessage *res = NULL;
473 LDAPMessage *msg = NULL;
476 struct berval *retdata = NULL;
481 struct berval syncUUID = BER_BVNULL;
482 struct sync_cookie syncCookie = { 0 };
483 struct sync_cookie syncCookie_req = { 0 };
484 struct berval cookie = BER_BVNULL;
492 Modifications *modlist = NULL;
497 struct timeval *tout_p = NULL;
498 struct timeval tout = { 0, 0 };
500 int refreshDeletes = 0;
502 BerVarray syncUUIDs = NULL;
505 if ( slapd_shutdown ) {
510 ber_init2( ber, NULL, LBER_USE_DER );
511 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
513 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
515 psub = &si->si_be->be_nsuffix[0];
517 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
519 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
525 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
526 tout_p, &res )) > 0 )
528 if ( slapd_shutdown ) {
532 for( msg = ldap_first_message( si->si_ld, res );
534 msg = ldap_next_message( si->si_ld, msg ) )
536 if ( slapd_shutdown ) {
540 switch( ldap_msgtype( msg ) ) {
541 case LDAP_RES_SEARCH_ENTRY:
542 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
543 /* we can't work without the control */
545 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
546 "got search entry without "
547 "control\n", 0, 0, 0 );
552 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
553 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
554 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
555 ber_scanf( ber, /*"{"*/ "m}", &cookie );
556 if ( !BER_BVISNULL( &cookie ) ) {
557 ch_free( syncCookie.octet_str.bv_val );
558 ber_dupbv( &syncCookie.octet_str, &cookie );
560 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
562 slap_parse_sync_cookie( &syncCookie );
565 if ( syncrepl_message_to_entry( si, op, msg,
566 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
567 rc_efree = syncrepl_entry( si, op, entry, &modlist,
568 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
569 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
571 syncrepl_updateCookie( si, op, psub, &syncCookie );
574 ldap_controls_free( rctrls );
576 slap_mods_free( modlist );
578 if ( rc_efree && entry ) {
584 case LDAP_RES_SEARCH_REFERENCE:
585 Debug( LDAP_DEBUG_ANY,
586 "do_syncrep2: reference received error\n", 0, 0, 0 );
589 case LDAP_RES_SEARCH_RESULT:
590 Debug( LDAP_DEBUG_SYNC,
591 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
592 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
596 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
598 ber_scanf( ber, "{" /*"}"*/);
599 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
600 ber_scanf( ber, "m", &cookie );
601 if ( !BER_BVISNULL( &cookie ) ) {
602 ch_free( syncCookie.octet_str.bv_val );
603 ber_dupbv( &syncCookie.octet_str, &cookie);
605 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
607 slap_parse_sync_cookie( &syncCookie );
610 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
612 ber_scanf( ber, "b", &refreshDeletes );
614 ber_scanf( ber, /*"{"*/ "}" );
616 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
618 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
621 value_match( &match, slap_schema.si_ad_entryCSN,
622 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
623 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
624 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
627 if ( !BER_BVISNULL( &syncCookie.octet_str ) &&
628 match < 0 && err == LDAP_SUCCESS )
630 syncrepl_updateCookie( si, op, psub, &syncCookie );
633 ldap_controls_free( rctrls );
635 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
636 /* FIXME : different error behaviors according to
637 * 1) err code : LDAP_BUSY ...
638 * 2) on err policy : stop service, stop sync, retry
640 if ( refreshDeletes == 0 && match < 0 &&
641 err == LDAP_SUCCESS )
643 syncrepl_del_nonpresent( op, si, NULL );
645 avl_free( si->si_presentlist, avl_ber_bvfree );
646 si->si_presentlist = NULL;
653 case LDAP_RES_INTERMEDIATE:
654 rc = ldap_parse_intermediate( si->si_ld, msg,
655 &retoid, &retdata, NULL, 0 );
656 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
657 ber_init2( ber, retdata, LBER_USE_DER );
659 switch ( si_tag = ber_peek_tag( ber, &len )) {
661 case LDAP_TAG_SYNC_NEW_COOKIE:
662 Debug( LDAP_DEBUG_SYNC,
663 "do_syncrep2: %s - %s%s\n",
664 "LDAP_RES_INTERMEDIATE",
665 "NEW_COOKIE", "\n" );
666 ber_scanf( ber, "tm", &tag, &cookie );
668 case LDAP_TAG_SYNC_REFRESH_DELETE:
669 case LDAP_TAG_SYNC_REFRESH_PRESENT:
670 Debug( LDAP_DEBUG_SYNC,
671 "do_syncrep2: %s - %s%s\n",
672 "LDAP_RES_INTERMEDIATE",
673 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
674 "REFRESH_PRESENT" : "REFRESH_DELETE",
676 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
677 si->si_refreshDelete = 1;
679 si->si_refreshPresent = 1;
681 ber_scanf( ber, "t{" /*"}"*/, &tag );
682 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
684 ber_scanf( ber, "m", &cookie );
685 if ( !BER_BVISNULL( &cookie ) ) {
686 ch_free( syncCookie.octet_str.bv_val );
687 ber_dupbv( &syncCookie.octet_str, &cookie );
689 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
691 slap_parse_sync_cookie( &syncCookie );
694 if ( ber_peek_tag( ber, &len ) ==
695 LDAP_TAG_REFRESHDONE )
697 ber_scanf( ber, "b", &refreshDone );
699 ber_scanf( ber, /*"{"*/ "}" );
701 case LDAP_TAG_SYNC_ID_SET:
702 Debug( LDAP_DEBUG_SYNC,
703 "do_syncrep2: %s - %s%s\n",
704 "LDAP_RES_INTERMEDIATE",
707 ber_scanf( ber, "t{" /*"}"*/, &tag );
708 if ( ber_peek_tag( ber, &len ) ==
709 LDAP_TAG_SYNC_COOKIE )
711 ber_scanf( ber, "m", &cookie );
712 if ( !BER_BVISNULL( &cookie ) ) {
713 ch_free( syncCookie.octet_str.bv_val );
714 ber_dupbv( &syncCookie.octet_str, &cookie );
716 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
718 slap_parse_sync_cookie( &syncCookie );
721 if ( ber_peek_tag( ber, &len ) ==
722 LDAP_TAG_REFRESHDELETES )
724 ber_scanf( ber, "b", &refreshDeletes );
726 ber_scanf( ber, "[W]", &syncUUIDs );
727 ber_scanf( ber, /*"{"*/ "}" );
728 if ( refreshDeletes ) {
729 syncrepl_del_nonpresent( op, si, syncUUIDs );
730 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
732 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
733 struct berval *syncuuid_bv;
734 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
735 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
736 avl_insert( &si->si_presentlist,
737 (caddr_t) syncuuid_bv,
738 syncuuid_cmp, avl_dup_error );
740 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
744 Debug( LDAP_DEBUG_ANY,
745 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
746 (long) si_tag, 0, 0 );
747 ldap_memfree( retoid );
748 ber_bvfree( retdata );
752 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
754 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
757 value_match( &match, slap_schema.si_ad_entryCSN,
758 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
759 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
760 &syncCookie_req.ctxcsn,
761 &syncCookie.ctxcsn, &text );
764 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
767 syncrepl_updateCookie( si, op, psub, &syncCookie);
770 if ( si->si_refreshPresent == 1 ) {
772 syncrepl_del_nonpresent( op, si, NULL );
776 ldap_memfree( retoid );
777 ber_bvfree( retdata );
781 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
782 "unknown intermediate response (%d)\n",
784 ldap_memfree( retoid );
785 ber_bvfree( retdata );
791 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
792 "unknown message\n", 0, 0, 0 );
796 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
797 slap_sync_cookie_free( &syncCookie_req, 0 );
798 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
799 slap_sync_cookie_free( &syncCookie, 0 );
809 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
810 errstr = ldap_err2string( rc );
812 Debug( LDAP_DEBUG_ANY,
813 "do_syncrep2 : %s\n", errstr, 0, 0 );
817 slap_sync_cookie_free( &syncCookie, 0 );
818 slap_sync_cookie_free( &syncCookie_req, 0 );
820 if ( res ) ldap_msgfree( res );
822 if ( rc && si->si_ld ) {
823 ldap_unbind_ext( si->si_ld, NULL, NULL );
835 struct re_s* rtask = arg;
836 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
837 Connection conn = {0};
838 char opbuf[OPERATION_BUFFER_SIZE];
840 int rc = LDAP_SUCCESS;
847 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
852 ldap_pvt_thread_mutex_lock( &si->si_mutex );
854 switch( abs( si->si_type )) {
855 case LDAP_SYNC_REFRESH_ONLY:
856 case LDAP_SYNC_REFRESH_AND_PERSIST:
859 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
863 if ( slapd_shutdown ) {
865 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
866 connection_client_stop( s );
867 ldap_unbind_ext( si->si_ld, NULL, NULL );
870 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
874 op = (Operation *)opbuf;
875 connection_fake_init( &conn, op, ctx );
877 /* use global malloc for now */
878 op->o_tmpmemctx = NULL;
879 op->o_tmpmfuncs = &ch_mfuncs;
881 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
882 op->o_bd = be = si->si_be;
883 op->o_dn = op->o_bd->be_rootdn;
884 op->o_ndn = op->o_bd->be_rootndn;
886 /* Establish session, do search */
889 si->si_refreshDelete = 0;
890 si->si_refreshPresent = 0;
891 rc = do_syncrep1( op, si );
894 /* Process results */
895 if ( rc == LDAP_SUCCESS ) {
896 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
898 rc = do_syncrep2( op, si );
900 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
901 /* If we succeeded, enable the connection for further listening.
902 * If we failed, tear down the connection and reschedule.
904 if ( rc == LDAP_SUCCESS ) {
906 rc = connection_client_setup( s, do_syncrepl, arg );
908 connection_client_enable( s );
910 } else if ( !first ) {
914 if ( rc == -2 ) rc = 0;
918 /* At this point, we have 4 cases:
919 * 1) for any hard failure, give up and remove this task
920 * 2) for ServerDown, reschedule this task to run
921 * 3) for Refresh and Success, reschedule to run
922 * 4) for Persist and Success, reschedule to defer
924 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
926 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
927 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
931 connection_client_stop( s );
934 if ( rc == LDAP_SUCCESS ) {
935 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
938 rtask->interval.tv_sec = si->si_interval;
939 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
940 if ( si->si_retrynum ) {
941 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
942 si->si_retrynum[i] = si->si_retrynum_init[i];
944 si->si_retrynum[i] = -2;
947 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
948 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
952 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
953 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
954 } else if ( si->si_retrynum[i] >= -1 ) {
955 if ( si->si_retrynum[i] > 0 )
956 si->si_retrynum[i]--;
957 rtask->interval.tv_sec = si->si_retryinterval[i];
958 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
959 slap_wake_listener();
963 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
964 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
970 syncrepl_message_to_entry(
974 Modifications **modlist,
980 BerElement *ber = NULL;
983 Modifications **modtail = modlist;
986 char txtbuf[SLAP_TEXT_BUFLEN];
987 size_t textlen = sizeof txtbuf;
989 struct berval bdn = {0, NULL}, dn, ndn;
994 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
995 Debug( LDAP_DEBUG_ANY,
996 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1000 op->o_tag = LDAP_REQ_ADD;
1002 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1004 if ( rc != LDAP_SUCCESS ) {
1005 Debug( LDAP_DEBUG_ANY,
1006 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1010 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1011 ber_dupbv( &op->o_req_dn, &dn );
1012 ber_dupbv( &op->o_req_ndn, &ndn );
1013 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1014 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1016 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1019 return LDAP_SUCCESS;
1022 if ( entry == NULL ) {
1026 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1028 e->e_name = op->o_req_dn;
1029 e->e_nname = op->o_req_ndn;
1031 while ( ber_remaining( ber ) ) {
1032 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1033 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1038 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1040 mod->sml_op = LDAP_MOD_REPLACE;
1041 mod->sml_next = NULL;
1042 mod->sml_desc = NULL;
1043 mod->sml_type = tmp.sml_type;
1044 mod->sml_values = tmp.sml_values;
1045 mod->sml_nvalues = NULL;
1048 modtail = &mod->sml_next;
1051 if ( *modlist == NULL ) {
1052 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1058 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1060 if ( rc != LDAP_SUCCESS ) {
1061 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1066 /* Strip out dynamically generated attrs */
1067 for ( modtail = modlist; *modtail ; ) {
1069 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1070 *modtail = mod->sml_next;
1071 slap_mod_free( &mod->sml_mod, 0 );
1074 modtail = &mod->sml_next;
1078 /* Strip out attrs in exattrs list */
1079 for ( modtail = modlist; *modtail ; ) {
1081 if ( ldap_charray_inlist( si->si_exattrs,
1082 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1083 *modtail = mod->sml_next;
1084 slap_mod_free( &mod->sml_mod, 0 );
1087 modtail = &mod->sml_next;
1091 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1092 if( rc != LDAP_SUCCESS ) {
1093 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1098 ber_free ( ber, 0 );
1099 if ( rc != LDAP_SUCCESS ) {
1109 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1111 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1112 * entry if a previous refresh was interrupted before sending us a new
1113 * context state. We try to compare the new entry to the existing entry
1114 * and ignore the new entry if they are the same.
1116 * Also, we may get an update where the entryDN has changed, due to
1117 * a ModDn on the provider. We detect this as well, so we can issue
1118 * the corresponding operation locally.
1120 * In the case of a modify, we get a list of all the attributes
1121 * in the original entry. Rather than deleting the entry and re-adding it,
1122 * we issue a Modify request that deletes all the attributes and adds all
1123 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1126 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1127 * a ModDN we will issue both operations on the local database.
1129 typedef struct dninfo {
1133 int renamed; /* Was an existing entry renamed? */
1134 int wasChanged; /* are the attributes changed? */
1135 int attrs; /* how many attribute types are in the ads list */
1136 AttributeDescription **ads;
1144 Modifications** modlist,
1146 struct berval* syncUUID,
1147 struct sync_cookie* syncCookie_req,
1148 struct berval* syncCSN )
1150 Backend *be = op->o_bd;
1151 slap_callback cb = { NULL };
1152 struct berval *syncuuid_bv = NULL;
1153 struct berval syncUUID_strrep = BER_BVNULL;
1154 struct berval uuid_bv = BER_BVNULL;
1156 SlapReply rs_search = {REP_RESULT};
1157 SlapReply rs_delete = {REP_RESULT};
1158 SlapReply rs_add = {REP_RESULT};
1159 SlapReply rs_modify = {REP_RESULT};
1161 #ifdef LDAP_COMP_MATCH
1162 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1164 AttributeAssertion ava = { NULL, BER_BVNULL };
1166 int rc = LDAP_SUCCESS;
1167 int ret = LDAP_SUCCESS;
1169 struct berval pdn = BER_BVNULL;
1173 switch( syncstate ) {
1174 case LDAP_SYNC_PRESENT:
1175 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1177 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1180 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1182 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1184 case LDAP_SYNC_DELETE:
1185 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1187 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1189 case LDAP_SYNC_MODIFY:
1190 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1192 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1195 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1197 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1200 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1201 if ( !si->si_refreshPresent ) {
1202 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1203 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1204 syncuuid_cmp, avl_dup_error );
1208 if ( syncstate == LDAP_SYNC_PRESENT ) {
1210 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1211 if ( entry == NULL ) {
1216 f.f_choice = LDAP_FILTER_EQUALITY;
1218 ava.aa_desc = slap_schema.si_ad_entryUUID;
1219 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1220 ava.aa_value = *syncUUID;
1221 op->ors_filter = &f;
1223 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1224 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1225 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1226 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1227 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1228 syncUUID->bv_val, syncUUID->bv_len );
1229 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1230 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1232 op->o_tag = LDAP_REQ_SEARCH;
1233 op->ors_scope = LDAP_SCOPE_SUBTREE;
1235 /* get the entry for this UUID */
1236 op->o_req_dn = si->si_base;
1237 op->o_req_ndn = si->si_base;
1239 op->o_time = slap_get_time();
1240 op->ors_tlimit = SLAP_NO_LIMIT;
1243 op->ors_attrs = slap_anlist_all_attributes;
1244 op->ors_attrsonly = 0;
1246 /* set callback function */
1247 op->o_callback = &cb;
1248 cb.sc_response = dn_callback;
1249 cb.sc_private = &dni;
1250 dni.new_entry = entry;
1252 if ( limits_check( op, &rs_search ) == 0 ) {
1253 rc = be->be_search( op, &rs_search );
1254 Debug( LDAP_DEBUG_SYNC,
1255 "syncrepl_entry: %s (%d)\n",
1256 "be_search", rc, 0 );
1259 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1260 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1263 cb.sc_response = null_callback;
1266 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1267 Debug( LDAP_DEBUG_SYNC,
1268 "syncrepl_entry: %s\n",
1269 entry->e_name.bv_val, 0, 0 );
1271 Debug( LDAP_DEBUG_SYNC,
1272 "syncrepl_entry: %s\n",
1273 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1276 if ( syncstate != LDAP_SYNC_DELETE ) {
1277 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1280 /* add if missing */
1281 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1282 &syncUUID_strrep, syncUUID );
1284 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1285 /* replace only if necessary */
1286 if ( a->a_nvals != a->a_vals ) {
1287 ber_memfree( a->a_nvals[0].bv_val );
1288 ber_dupbv( &a->a_nvals[0], syncUUID );
1290 ber_memfree( a->a_vals[0].bv_val );
1291 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1295 switch ( syncstate ) {
1297 case LDAP_SYNC_MODIFY:
1299 if ( BER_BVISNULL( &dni.dn )) {
1301 op->o_req_dn = entry->e_name;
1302 op->o_req_ndn = entry->e_nname;
1303 op->o_tag = LDAP_REQ_ADD;
1306 rc = be->be_add( op, &rs_add );
1307 Debug( LDAP_DEBUG_SYNC,
1308 "syncrepl_entry: %s (%d)\n",
1310 switch ( rs_add.sr_err ) {
1312 be_entry_release_w( op, entry );
1317 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1318 * only if the suffix entry is not present */
1319 case LDAP_NO_SUCH_OBJECT:
1320 syncrepl_add_glue( op, entry );
1324 /* if an entry was added via syncrepl_add_glue(),
1325 * it likely has no entryUUID, so the previous
1326 * be_search() doesn't find it. In this case,
1327 * give syncrepl a chance to modify it. Also
1328 * allow for entries that were recreated with the
1329 * same DN but a different entryUUID.
1331 case LDAP_ALREADY_EXISTS:
1333 Operation op2 = *op;
1334 SlapReply rs2 = { 0 };
1335 slap_callback cb2 = { 0 };
1337 op2.o_tag = LDAP_REQ_SEARCH;
1338 op2.o_req_dn = entry->e_name;
1339 op2.o_req_ndn = entry->e_nname;
1340 op2.ors_scope = LDAP_SCOPE_BASE;
1341 op2.ors_attrs = slap_anlist_all_attributes;
1342 op2.ors_attrsonly = 0;
1343 op2.ors_limit = NULL;
1345 op2.ors_tlimit = SLAP_NO_LIMIT;
1347 f.f_choice = LDAP_FILTER_PRESENT;
1348 f.f_desc = slap_schema.si_ad_objectClass;
1349 op2.ors_filter = &f;
1350 op2.ors_filterstr = generic_filterstr;
1352 op2.o_callback = &cb2;
1353 cb2.sc_response = dn_callback;
1354 cb2.sc_private = &dni;
1356 be->be_search( &op2, &rs2 );
1364 Debug( LDAP_DEBUG_ANY,
1365 "syncrepl_entry : be_add failed (%d)\n",
1366 rs_add.sr_err, 0, 0 );
1373 op->o_req_dn = dni.dn;
1374 op->o_req_ndn = dni.ndn;
1375 if ( dni.renamed ) {
1376 struct berval noldp, newp, nnewp;
1378 op->o_tag = LDAP_REQ_MODRDN;
1379 dnRdn( &entry->e_name, &op->orr_newrdn );
1380 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1382 dnParent( &dni.ndn, &noldp );
1383 dnParent( &entry->e_nname, &nnewp );
1384 if ( !dn_match( &noldp, &newp )) {
1385 dnParent( &entry->e_name, &newp );
1386 op->orr_newSup = &newp;
1387 op->orr_nnewSup = &nnewp;
1389 op->orr_deleteoldrdn = 0;
1390 rc = be->be_modrdn( op, &rs_modify );
1391 Debug( LDAP_DEBUG_SYNC,
1392 "syncrepl_entry: %s (%d)\n",
1393 "be_modrdn", rc, 0 );
1394 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1395 op->o_req_dn = entry->e_name;
1396 op->o_req_ndn = entry->e_nname;
1402 if ( dni.wasChanged ) {
1403 Modifications *mod, *modhead = NULL;
1404 Modifications *modtail = NULL;
1407 op->o_tag = LDAP_REQ_MODIFY;
1411 /* Delete all the old attrs */
1412 for ( i = 0; i < dni.attrs; i++ ) {
1413 mod = ch_malloc( sizeof( Modifications ) );
1414 mod->sml_op = LDAP_MOD_DELETE;
1415 mod->sml_desc = dni.ads[i];
1416 mod->sml_type = mod->sml_desc->ad_cname;
1417 mod->sml_values = NULL;
1418 mod->sml_nvalues = NULL;
1419 if ( !modhead ) modhead = mod;
1421 modtail->sml_next = mod;
1426 /* Append passed in list to ours */
1428 modtail->sml_next = *modlist;
1434 /* Find end of this list */
1435 for ( ; mod != NULL; mod = mod->sml_next ) {
1439 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1440 mod->sml_op = LDAP_MOD_REPLACE;
1441 mod->sml_desc = slap_schema.si_ad_entryUUID;
1442 mod->sml_type = mod->sml_desc->ad_cname;
1443 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1444 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1445 ber_dupbv( &uuid_bv, syncUUID );
1446 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1447 modtail->sml_next = mod;
1449 op->o_tag = LDAP_REQ_MODIFY;
1450 op->orm_modlist = *modlist;
1452 rc = be->be_modify( op, &rs_modify );
1453 Debug( LDAP_DEBUG_SYNC,
1454 "syncrepl_entry: %s (%d)\n",
1455 "be_modify", rc, 0 );
1456 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1457 Debug( LDAP_DEBUG_ANY,
1458 "syncrepl_entry : be_modify failed (%d)\n",
1459 rs_modify.sr_err, 0, 0 );
1464 case LDAP_SYNC_DELETE :
1465 if ( !BER_BVISNULL( &dni.dn )) {
1466 op->o_req_dn = dni.dn;
1467 op->o_req_ndn = dni.ndn;
1468 op->o_tag = LDAP_REQ_DELETE;
1469 rc = be->be_delete( op, &rs_delete );
1470 Debug( LDAP_DEBUG_SYNC,
1471 "syncrepl_entry: %s (%d)\n",
1472 "be_delete", rc, 0 );
1474 while ( rs_delete.sr_err == LDAP_SUCCESS
1475 && op->o_delete_glue_parent ) {
1476 op->o_delete_glue_parent = 0;
1477 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1478 slap_callback cb = { NULL };
1479 cb.sc_response = slap_null_cb;
1480 dnParent( &op->o_req_ndn, &pdn );
1482 op->o_req_ndn = pdn;
1483 op->o_callback = &cb;
1484 op->o_bd->be_delete( op, &rs_delete );
1494 Debug( LDAP_DEBUG_ANY,
1495 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1501 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1502 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1503 BER_BVZERO( &syncUUID_strrep );
1506 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1508 if ( !BER_BVISNULL( &dni.ndn ) ) {
1509 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1511 if ( !BER_BVISNULL( &dni.dn ) ) {
1512 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1517 static struct berval gcbva[] = {
1523 #define NP_DELETE_ONE 2
1526 syncrepl_del_nonpresent(
1531 Backend* be = op->o_bd;
1532 slap_callback cb = { NULL };
1533 SlapReply rs_search = {REP_RESULT};
1534 SlapReply rs_delete = {REP_RESULT};
1535 SlapReply rs_modify = {REP_RESULT};
1536 struct nonpresent_entry *np_list, *np_prev;
1538 AttributeName an[2];
1540 struct berval pdn = BER_BVNULL;
1542 op->o_req_dn = si->si_base;
1543 op->o_req_ndn = si->si_base;
1545 cb.sc_response = nonpresent_callback;
1548 op->o_callback = &cb;
1549 op->o_tag = LDAP_REQ_SEARCH;
1550 op->ors_scope = si->si_scope;
1551 op->ors_deref = LDAP_DEREF_NEVER;
1552 op->o_time = slap_get_time();
1553 op->ors_tlimit = SLAP_NO_LIMIT;
1558 #ifdef LDAP_COMP_MATCH
1559 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1561 AttributeAssertion eq = { NULL, BER_BVNULL };
1565 op->ors_attrsonly = 1;
1566 op->ors_attrs = slap_anlist_no_attrs;
1567 op->ors_limit = NULL;
1568 op->ors_filter = &uf;
1571 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1573 uf.f_choice = LDAP_FILTER_EQUALITY;
1574 si->si_refreshDelete |= NP_DELETE_ONE;
1576 for (i=0; uuids[i].bv_val; i++) {
1578 uf.f_av_value = uuids[i];
1579 rc = be->be_search( op, &rs_search );
1581 si->si_refreshDelete ^= NP_DELETE_ONE;
1583 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1584 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1585 an[0].an_desc = slap_schema.si_ad_entryUUID;
1587 op->ors_slimit = SLAP_NO_LIMIT;
1588 op->ors_attrsonly = 0;
1589 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1590 op->ors_filterstr = si->si_filterstr;
1591 op->o_nocaching = 1;
1593 if ( limits_check( op, &rs_search ) == 0 ) {
1594 rc = be->be_search( op, &rs_search );
1596 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1599 op->o_nocaching = 0;
1601 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1603 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1605 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1606 while ( np_list != NULL ) {
1607 LDAP_LIST_REMOVE( np_list, npe_link );
1609 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1610 op->o_tag = LDAP_REQ_DELETE;
1611 op->o_callback = &cb;
1612 cb.sc_response = null_callback;
1614 op->o_req_dn = *np_prev->npe_name;
1615 op->o_req_ndn = *np_prev->npe_nname;
1616 rc = op->o_bd->be_delete( op, &rs_delete );
1618 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1619 Modifications mod1, mod2;
1620 mod1.sml_op = LDAP_MOD_REPLACE;
1621 mod1.sml_desc = slap_schema.si_ad_objectClass;
1622 mod1.sml_type = mod1.sml_desc->ad_cname;
1623 mod1.sml_values = &gcbva[0];
1624 mod1.sml_nvalues = NULL;
1625 mod1.sml_next = &mod2;
1627 mod2.sml_op = LDAP_MOD_REPLACE;
1628 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
1629 mod2.sml_type = mod2.sml_desc->ad_cname;
1630 mod2.sml_values = &gcbva[1];
1631 mod2.sml_nvalues = NULL;
1632 mod2.sml_next = NULL;
1634 op->o_tag = LDAP_REQ_MODIFY;
1635 op->orm_modlist = &mod1;
1637 rc = be->be_modify( op, &rs_modify );
1640 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1641 op->o_delete_glue_parent ) {
1642 op->o_delete_glue_parent = 0;
1643 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1644 slap_callback cb = { NULL };
1645 cb.sc_response = slap_null_cb;
1646 dnParent( &op->o_req_ndn, &pdn );
1648 op->o_req_ndn = pdn;
1649 op->o_callback = &cb;
1650 /* give it a root privil ? */
1651 op->o_bd->be_delete( op, &rs_delete );
1657 op->o_delete_glue_parent = 0;
1659 ber_bvfree( np_prev->npe_name );
1660 ber_bvfree( np_prev->npe_nname );
1664 slap_graduate_commit_csn( op );
1675 Backend *be = op->o_bd;
1676 slap_callback cb = { NULL };
1681 struct berval dn = {0, NULL};
1682 struct berval ndn = {0, NULL};
1684 SlapReply rs_add = {REP_RESULT};
1687 op->o_tag = LDAP_REQ_ADD;
1688 op->o_callback = &cb;
1689 cb.sc_response = null_callback;
1690 cb.sc_private = NULL;
1695 /* count RDNs in suffix */
1696 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1697 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1707 /* Start with BE suffix */
1708 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1709 comma = strrchr( dn.bv_val, ',' );
1710 if ( ptr ) *ptr = ',';
1711 if ( comma ) *comma = '\0';
1716 dn.bv_len -= ptr - dn.bv_val;
1719 /* the normalizedDNs are always the same length, no counting
1722 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1723 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1724 ndn.bv_len = be->be_nsuffix[0].bv_len;
1727 while ( ndn.bv_val > e->e_nname.bv_val ) {
1728 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1729 ber_dupbv( &glue->e_name, &dn );
1730 ber_dupbv( &glue->e_nname, &ndn );
1732 a = ch_calloc( 1, sizeof( Attribute ));
1733 a->a_desc = slap_schema.si_ad_objectClass;
1735 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1736 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1737 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1738 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1740 a->a_nvals = a->a_vals;
1742 a->a_next = glue->e_attrs;
1745 a = ch_calloc( 1, sizeof( Attribute ));
1746 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1748 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1749 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1750 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1752 a->a_nvals = a->a_vals;
1754 a->a_next = glue->e_attrs;
1757 op->o_req_dn = glue->e_name;
1758 op->o_req_ndn = glue->e_nname;
1760 rc = be->be_add ( op, &rs_add );
1761 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1762 be_entry_release_w( op, glue );
1764 /* incl. ALREADY EXIST */
1768 /* Move to next child */
1769 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1772 if ( ptr == e->e_name.bv_val ) break;
1774 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1775 for( ptr = ndn.bv_val-2;
1776 ptr > e->e_nname.bv_val && *ptr != ',';
1782 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1785 op->o_req_dn = e->e_name;
1786 op->o_req_ndn = e->e_nname;
1788 rc = be->be_add ( op, &rs_add );
1789 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1790 be_entry_release_w( op, e );
1799 syncrepl_updateCookie(
1803 struct sync_cookie *syncCookie )
1805 Backend *be = op->o_bd;
1806 Modifications mod = {0};
1807 struct berval vals[2];
1811 slap_callback cb = { NULL };
1812 SlapReply rs_modify = {REP_RESULT};
1814 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1815 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1817 mod.sml_op = LDAP_MOD_REPLACE;
1818 mod.sml_desc = slap_schema.si_ad_contextCSN;
1819 mod.sml_type = mod.sml_desc->ad_cname;
1820 mod.sml_values = vals;
1821 vals[0] = si->si_syncCookie.ctxcsn;
1822 vals[1].bv_val = NULL;
1825 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1827 op->o_tag = LDAP_REQ_MODIFY;
1829 assert( si->si_rid < 1000 );
1831 cb.sc_response = null_callback;
1834 op->o_callback = &cb;
1835 op->o_req_dn = op->o_bd->be_suffix[0];
1836 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1838 /* update contextCSN */
1839 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
1840 op->orm_modlist = &mod;
1841 rc = be->be_modify( op, &rs_modify );
1844 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1845 Debug( LDAP_DEBUG_ANY,
1846 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1849 slap_graduate_commit_csn( op );
1859 dninfo *dni = op->o_callback->sc_private;
1861 if ( rs->sr_type == REP_SEARCH ) {
1862 if ( !BER_BVISNULL( &dni->dn ) ) {
1863 Debug( LDAP_DEBUG_ANY,
1864 "dn_callback : consistency error - "
1865 "entryUUID is not unique\n", 0, 0, 0 );
1867 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
1868 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1869 /* If there is a new entry, see if it differs from the old.
1870 * We compare the non-normalized values so that cosmetic changes
1871 * in the provider are always propagated.
1873 if ( dni->new_entry ) {
1874 Attribute *old, *new;
1877 /* Did the DN change? Note that we don't explicitly try to
1878 * discover if the deleteOldRdn argument applies here. It
1879 * would save an unnecessary Modify if we detected it, but
1880 * that's a fair amount of trouble to compare the two attr
1883 if ( !dn_match( &rs->sr_entry->e_name,
1884 &dni->new_entry->e_name ) )
1889 for ( i = 0, old = rs->sr_entry->e_attrs;
1891 i++, old = old->a_next )
1896 /* We assume that attributes are saved in the same order
1897 * in the remote and local databases. So if we walk through
1898 * the attributeDescriptions one by one they should match in
1899 * lock step. If not, we signal a change. Otherwise we test
1902 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
1904 old = old->a_next, new = new->a_next )
1906 if ( old->a_desc != new->a_desc ) {
1907 dni->wasChanged = 1;
1910 for ( i = 0; ; i++ ) {
1912 nold = BER_BVISNULL( &old->a_vals[i] );
1913 nnew = BER_BVISNULL( &new->a_vals[i] );
1914 /* If both are empty, stop looking */
1915 if ( nold && nnew ) {
1918 /* If they are different, stop looking */
1919 if ( nold != nnew ) {
1920 dni->wasChanged = 1;
1923 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
1924 dni->wasChanged = 1;
1928 if ( dni->wasChanged ) break;
1930 if ( dni->wasChanged ) {
1931 dni->ads = op->o_tmpalloc( dni->attrs *
1932 sizeof(AttributeDescription *), op->o_tmpmemctx );
1934 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
1935 dni->ads[i] = old->a_desc;
1941 } else if ( rs->sr_type == REP_RESULT ) {
1942 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1943 Debug( LDAP_DEBUG_ANY,
1944 "dn_callback : consistency error - "
1945 "entryUUID is not unique\n", 0, 0, 0 );
1949 return LDAP_SUCCESS;
1953 nonpresent_callback(
1957 syncinfo_t *si = op->o_callback->sc_private;
1960 struct berval* present_uuid = NULL;
1961 struct nonpresent_entry *np_entry;
1963 if ( rs->sr_type == REP_RESULT ) {
1964 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1965 si->si_presentlist = NULL;
1967 } else if ( rs->sr_type == REP_SEARCH ) {
1968 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
1969 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1971 if ( a == NULL ) return 0;
1973 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1977 if ( present_uuid == NULL ) {
1978 np_entry = (struct nonpresent_entry *)
1979 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1980 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1981 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1982 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1985 avl_delete( &si->si_presentlist,
1986 &a->a_nvals[0], syncuuid_cmp );
1987 ch_free( present_uuid->bv_val );
1988 ch_free( present_uuid );
1991 return LDAP_SUCCESS;
1999 if ( rs->sr_err != LDAP_SUCCESS &&
2000 rs->sr_err != LDAP_REFERRAL &&
2001 rs->sr_err != LDAP_ALREADY_EXISTS &&
2002 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2003 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2005 Debug( LDAP_DEBUG_ANY,
2006 "null_callback : error code 0x%x\n",
2009 return LDAP_SUCCESS;
2013 slap_uuidstr_from_normalized(
2014 struct berval* uuidstr,
2015 struct berval* normalized,
2019 unsigned char nibble;
2022 if ( normalized == NULL ) return NULL;
2023 if ( normalized->bv_len != 16 ) return NULL;
2028 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2029 if ( new == NULL ) {
2036 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2037 if ( new != uuidstr ) {
2038 slap_sl_free( new, ctx );
2043 for ( i = 0; i < 16; i++ ) {
2044 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2045 new->bv_val[(i<<1)+d] = '-';
2049 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2050 if ( nibble < 10 ) {
2051 new->bv_val[(i<<1)+d] = nibble + '0';
2053 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2056 nibble = (normalized->bv_val[i]) & 0xF;
2057 if ( nibble < 10 ) {
2058 new->bv_val[(i<<1)+d+1] = nibble + '0';
2060 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2064 new->bv_val[new->bv_len] = '\0';
2069 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2071 const struct berval *uuid1 = v_uuid1;
2072 const struct berval *uuid2 = v_uuid2;
2073 int rc = uuid1->bv_len - uuid2->bv_len;
2074 if ( rc ) return rc;
2075 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2079 avl_ber_bvfree( void *v_bv )
2081 struct berval *bv = (struct berval *)v_bv;
2083 if( v_bv == NULL ) return;
2084 if ( !BER_BVISNULL( bv ) ) {
2085 ch_free( bv->bv_val );
2087 ch_free( (char *) bv );
2091 syncinfo_free( syncinfo_t *sie )
2093 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2094 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2095 ch_free( sie->si_provideruri.bv_val );
2098 bindconf_free( &sie->si_bindconf );
2100 if ( sie->si_filterstr.bv_val ) {
2101 ch_free( sie->si_filterstr.bv_val );
2103 if ( sie->si_base.bv_val ) {
2104 ch_free( sie->si_base.bv_val );
2106 if ( sie->si_attrs ) {
2108 while ( sie->si_attrs[i] != NULL ) {
2109 ch_free( sie->si_attrs[i] );
2112 ch_free( sie->si_attrs );
2114 if ( sie->si_exattrs ) {
2116 while ( sie->si_exattrs[i] != NULL ) {
2117 ch_free( sie->si_exattrs[i] );
2120 ch_free( sie->si_exattrs );
2122 if ( sie->si_anlist ) {
2124 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2125 ch_free( sie->si_anlist[i].an_name.bv_val );
2128 ch_free( sie->si_anlist );
2130 if ( sie->si_exanlist ) {
2132 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2133 ch_free( sie->si_exanlist[i].an_name.bv_val );
2136 ch_free( sie->si_exanlist );
2138 if ( sie->si_retryinterval ) {
2139 ch_free( sie->si_retryinterval );
2141 if ( sie->si_retrynum ) {
2142 ch_free( sie->si_retrynum );
2144 if ( sie->si_retrynum_init ) {
2145 ch_free( sie->si_retrynum_init );
2147 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2148 if ( sie->si_presentlist ) {
2149 avl_free( sie->si_presentlist, avl_ber_bvfree );
2152 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2154 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2155 struct nonpresent_entry* npe;
2156 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2157 LDAP_LIST_REMOVE( npe, npe_link );
2158 if ( npe->npe_name ) {
2159 if ( npe->npe_name->bv_val ) {
2160 ch_free( npe->npe_name->bv_val );
2162 ch_free( npe->npe_name );
2164 if ( npe->npe_nname ) {
2165 if ( npe->npe_nname->bv_val ) {
2166 ch_free( npe->npe_nname->bv_val );
2168 ch_free( npe->npe_nname );