3 * Replication Engine which uses the LDAP Sync protocol
6 * Copyright 2003 The OpenLDAP Foundation, All Rights Reserved.
7 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
9 /* Copyright (c) 2003 by International Business Machines, Inc.
11 * International Business Machines, Inc. (hereinafter called IBM) grants
12 * permission under its copyrights to use, copy, modify, and distribute this
13 * Software with or without fee, provided that the above copyright notice and
14 * all paragraphs of this notice appear in all copies, and that the name of IBM
15 * not be used in connection with the marketing of any product incorporating
16 * the Software or modifications thereof, without specific, written prior
19 * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
20 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21 * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
22 * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
23 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
24 * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
26 /* Modified by Howard Chu
28 * Copyright (c) 2003 by Howard Chu, Symas Corporation
30 * Modifications provided under the terms of the OpenLDAP public license.
37 #include <ac/string.h>
38 #include <ac/socket.h>
43 #include "lutil_ldap.h"
47 #define SYNCREPL_STR "syncreplxxx"
50 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
51 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
53 static int syncuuid_cmp( const void *, const void * );
54 static void avl_ber_bvfree( void * );
55 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
57 /* callback functions */
58 static int dn_callback( struct slap_op *, struct slap_rep * );
59 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
60 static int null_callback( struct slap_op *, struct slap_rep * );
62 static AttributeDescription *sync_descs[4];
64 struct runqueue_s syncrepl_rq;
67 init_syncrepl(syncinfo_t *si)
72 if ( !sync_descs[0] ) {
73 sync_descs[0] = slap_schema.si_ad_objectClass;
74 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
75 sync_descs[2] = slap_schema.si_ad_entryCSN;
79 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 /* Delete Attributes */
83 for ( i = 0; sync_descs[i] != NULL; i++ ) {
84 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
85 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
88 ch_free( si->si_attrs[j] );
89 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
90 si->si_attrs[k] = si->si_attrs[k+1];
95 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
96 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
99 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
101 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
105 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
108 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
110 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
113 tmp[ n++ ] = ch_strdup( "*" );
120 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
121 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
122 si->si_attrs[ n ] = NULL;
132 BerElementBuffer berbuf;
133 BerElement *ber = (BerElement *)&berbuf;
134 LDAPControl c[2], *ctrls[3];
135 struct timeval timeout;
139 /* setup LDAP SYNC control */
140 ber_init2( ber, NULL, LBER_USE_DER );
141 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
143 if ( si->si_syncCookie.octet_str &&
144 si->si_syncCookie.octet_str[0].bv_val ) {
145 ber_printf( ber, "{eO}", abs(si->si_type),
146 &si->si_syncCookie.octet_str[0] );
148 ber_printf( ber, "{e}", abs(si->si_type) );
151 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
156 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
157 c[0].ldctl_iscritical = si->si_type < 0;
160 if ( si->si_authzId ) {
161 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
162 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
163 c[1].ldctl_iscritical = 1;
170 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
173 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
174 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
175 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
176 si->si_slimit, &msgid );
182 static const Listener dummy_list = { {0, ""}, {0, ""} };
190 int cmdline_cookie_found = 0;
192 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
193 struct berval syncrepl_cn_bv;
194 struct sync_cookie *sc = NULL;
195 struct sync_cookie syncCookie = { NULL, -1, NULL };
197 /* Init connection to master */
199 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
200 if ( rc != LDAP_SUCCESS ) {
202 LDAP_LOG( OPERATION, ERR,
203 "do_syncrep1: ldap_initialize failed (%s)\n",
204 si->si_provideruri, 0, 0 );
206 Debug( LDAP_DEBUG_ANY,
207 "do_syncrep1: ldap_initialize failed (%s)\n",
208 si->si_provideruri, 0, 0 );
213 op->o_protocol = LDAP_VERSION3;
214 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
219 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
220 if( rc != LDAP_SUCCESS ) {
222 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
223 "%s: ldap_start_tls failed (%d)\n",
224 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
227 Debug( LDAP_DEBUG_ANY,
228 "%s: ldap_start_tls failed (%d)\n",
229 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
232 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
236 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
237 #ifdef HAVE_CYRUS_SASL
240 if ( si->si_secprops != NULL ) {
241 rc = ldap_set_option( si->si_ld,
242 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
244 if( rc != LDAP_OPT_SUCCESS ) {
246 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
247 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
248 si->si_provideruri, si->si_secprops, 0 );
250 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
251 "(%s,SECPROPS,\"%s\") failed!\n",
252 si->si_provideruri, si->si_secprops, 0 );
258 defaults = lutil_sasl_defaults( si->si_ld,
259 si->si_saslmech, si->si_realm,
260 si->si_authcId, si->si_passwd, si->si_authzId );
262 rc = ldap_sasl_interactive_bind_s( si->si_ld,
270 lutil_sasl_freedefs( defaults );
272 /* FIXME : different error behaviors according to
274 * 2) on err policy : exit, retry, backoff ...
276 if ( rc != LDAP_SUCCESS ) {
278 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
279 "ldap_sasl_interactive_bind_s failed (%d)\n",
282 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
283 "ldap_sasl_interactive_bind_s failed (%d)\n",
288 #else /* HAVE_CYRUS_SASL */
289 /* Should never get here, we trapped this at config time */
290 fprintf( stderr, "not compiled with SASL support\n" );
295 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
296 if ( rc != LDAP_SUCCESS ) {
298 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
299 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
301 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
302 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
308 /* get syncrepl cookie of shadow replica from subentry */
310 assert( si->si_id < 1000 );
311 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
312 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
313 CN_STR "syncrepl%d", si->si_id );
314 build_new_dn( &op->o_req_ndn, &si->si_base, &syncrepl_cn_bv,
316 op->o_req_dn = op->o_req_ndn;
318 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
319 if ( si->si_id == sc->rid ) {
320 cmdline_cookie_found = 1;
325 if ( cmdline_cookie_found ) {
326 /* cookie is supplied in the command line */
327 BerVarray cookie = NULL;
328 struct berval cookie_bv;
330 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
331 slap_sync_cookie_free( &si->si_syncCookie, 0 );
333 /* read stored cookie if it exists */
334 backend_attribute( op, NULL, &op->o_req_ndn,
335 slap_schema.si_ad_syncreplCookie, &cookie );
338 /* no stored cookie */
339 if ( sc->ctxcsn == NULL ||
340 sc->ctxcsn->bv_val == NULL ) {
341 /* if cmdline cookie does not have ctxcsn */
342 /* component, set it to an initial value */
343 slap_init_sync_cookie_ctxcsn( sc );
345 slap_dup_sync_cookie( &si->si_syncCookie, sc );
346 slap_sync_cookie_free( sc, 1 );
350 ber_dupbv( &cookie_bv, &cookie[0] );
351 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
352 slap_parse_sync_cookie( &si->si_syncCookie );
353 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
354 if ( sc->sid != -1 ) {
355 /* command line cookie wins */
356 si->si_syncCookie.sid = sc->sid;
358 if ( sc->ctxcsn != NULL ) {
359 /* command line cookie wins */
360 if ( si->si_syncCookie.ctxcsn ) {
361 ber_bvarray_free( si->si_syncCookie.ctxcsn );
362 si->si_syncCookie.ctxcsn = NULL;
364 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
365 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
367 if ( sc->rid != -1 ) {
368 /* command line cookie wins */
369 si->si_syncCookie.rid = sc->rid;
371 slap_sync_cookie_free( sc, 1 );
375 /* no command line cookie is specified */
376 if ( si->si_syncCookie.octet_str == NULL ) {
377 BerVarray cookie = NULL;
378 struct berval cookie_bv;
379 /* try to read stored cookie */
380 backend_attribute( op, NULL, &op->o_req_ndn,
381 slap_schema.si_ad_syncreplCookie, &cookie );
383 ber_dupbv( &cookie_bv, &cookie[0] );
384 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
385 slap_parse_sync_cookie( &si->si_syncCookie );
386 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
391 rc = ldap_sync_search( si, op->o_tmpmemctx );
393 if( rc != LDAP_SUCCESS ) {
395 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
396 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
398 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
399 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
406 ldap_unbind( si->si_ld );
419 LDAPControl **rctrls = NULL;
422 BerElementBuffer berbuf;
423 BerElement *ber = (BerElement *)&berbuf;
425 LDAPMessage *res = NULL;
426 LDAPMessage *msg = NULL;
429 struct berval *retdata = NULL;
434 struct berval syncUUID = { 0, NULL };
435 struct sync_cookie syncCookie = { NULL, -1, NULL };
436 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
437 struct berval cookie = { 0, NULL };
447 Modifications *modlist = NULL;
452 struct timeval *tout_p = NULL;
453 struct timeval tout = { 0, 0 };
455 int refreshDeletes = 0;
457 BerVarray syncUUIDs = NULL;
460 if ( slapd_abrupt_shutdown ) {
465 ber_init2( ber, NULL, LBER_USE_DER );
466 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
469 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
471 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
474 op->o_callback = &cb;
476 psub = &si->si_be->be_nsuffix[0];
478 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
480 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
486 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
489 if ( slapd_abrupt_shutdown ) {
493 for( msg = ldap_first_message( si->si_ld, res );
495 msg = ldap_next_message( si->si_ld, msg ) )
497 switch( ldap_msgtype( msg ) ) {
498 case LDAP_RES_SEARCH_ENTRY:
499 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
500 /* we can't work without the control */
506 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
507 ber_scanf( ber, "{em", &syncstate, &syncUUID );
508 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
509 ber_scanf( ber, "m}", &cookie );
510 if ( cookie.bv_val ) {
511 struct berval tmp_bv;
512 ber_dupbv( &tmp_bv, &cookie );
513 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
515 if ( syncCookie.octet_str &&
516 syncCookie.octet_str[0].bv_val )
517 slap_parse_sync_cookie( &syncCookie );
519 entry = syncrepl_message_to_entry( si, op, msg,
520 &modlist, syncstate );
521 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
522 &syncUUID, &syncCookie_req );
523 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
524 syncrepl_updateCookie( si, op, psub, &syncCookie );
526 ldap_controls_free( rctrls );
528 slap_mods_free( modlist );
530 if ( rc_efree && entry ) {
535 case LDAP_RES_SEARCH_REFERENCE:
537 LDAP_LOG( OPERATION, ERR,
538 "do_syncrep2 : reference received\n", 0, 0, 0 );
540 Debug( LDAP_DEBUG_ANY,
541 "do_syncrep2 : reference received\n", 0, 0, 0 );
545 case LDAP_RES_SEARCH_RESULT:
546 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
550 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
552 ber_scanf( ber, "{" /*"}"*/);
553 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
555 ber_scanf( ber, "m", &cookie );
556 if ( cookie.bv_val ) {
557 struct berval tmp_bv;
558 ber_dupbv( &tmp_bv, &cookie );
559 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
561 if ( syncCookie.octet_str &&
562 syncCookie.octet_str[0].bv_val )
563 slap_parse_sync_cookie( &syncCookie );
565 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
567 ber_scanf( ber, "b", &refreshDeletes );
569 ber_scanf( ber, "}" );
571 if ( syncCookie_req.ctxcsn == NULL ) {
573 } else if ( syncCookie.ctxcsn == NULL ) {
576 value_match( &match, slap_schema.si_ad_entryCSN,
577 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
578 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
579 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
581 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
583 syncrepl_updateCookie( si, op, psub, &syncCookie );
586 ldap_controls_free( rctrls );
588 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
589 /* FIXME : different error behaviors according to
590 * 1) err code : LDAP_BUSY ...
591 * 2) on err policy : stop service, stop sync, retry
593 if ( refreshDeletes == 0 && match < 0 ) {
594 syncrepl_del_nonpresent( op, si );
596 avl_free( si->si_presentlist, avl_ber_bvfree );
597 si->si_presentlist = NULL;
604 case LDAP_RES_INTERMEDIATE:
605 rc = ldap_parse_intermediate( si->si_ld, msg,
606 &retoid, &retdata, NULL, 0 );
607 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
608 int si_refreshDelete = 0;
609 int si_refreshPresent = 0;
610 ber_init2( ber, retdata, LBER_USE_DER );
612 switch ( si_tag = ber_peek_tag( ber, &len )) {
614 case LDAP_TAG_SYNC_NEW_COOKIE:
615 ber_scanf( ber, "tm", &tag, &cookie );
617 case LDAP_TAG_SYNC_REFRESH_DELETE:
618 si_refreshDelete = 1;
619 case LDAP_TAG_SYNC_REFRESH_PRESENT:
620 si_refreshPresent = 1;
621 ber_scanf( ber, "t{", &tag );
622 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
624 ber_scanf( ber, "m", &cookie );
625 if ( cookie.bv_val ) {
626 struct berval tmp_bv;
627 ber_dupbv( &tmp_bv, &cookie );
628 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
630 if ( syncCookie.octet_str &&
631 syncCookie.octet_str[0].bv_val )
632 slap_parse_sync_cookie( &syncCookie );
634 if ( ber_peek_tag( ber, &len ) ==
635 LDAP_TAG_REFRESHDONE )
637 ber_scanf( ber, "b", &refreshDone );
639 ber_scanf( ber, "}" );
641 case LDAP_TAG_SYNC_ID_SET:
642 ber_scanf( ber, "t{", &tag );
643 if ( ber_peek_tag( ber, &len ) ==
644 LDAP_TAG_SYNC_COOKIE ) {
645 ber_scanf( ber, "m", &cookie );
646 if ( cookie.bv_val ) {
647 struct berval tmp_bv;
648 ber_dupbv( &tmp_bv, &cookie );
649 ber_bvarray_add( &syncCookie.octet_str,
652 if ( syncCookie.octet_str &&
653 syncCookie.octet_str[0].bv_val )
654 slap_parse_sync_cookie( &syncCookie );
656 if ( ber_peek_tag( ber, &len ) ==
657 LDAP_TAG_REFRESHDELETES )
659 ber_scanf( ber, "b", &refreshDeletes );
661 ber_scanf( ber, "[W]", &syncUUIDs );
662 ber_scanf( ber, "}" );
663 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
664 struct berval *syncuuid_bv;
665 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
666 avl_insert( &si->si_presentlist,
667 (caddr_t) syncuuid_bv,
668 syncuuid_cmp, avl_dup_error );
670 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
674 LDAP_LOG( OPERATION, ERR,
675 "do_syncrep2 : unknown syncinfo tag (%d)\n",
678 Debug( LDAP_DEBUG_ANY,
679 "do_syncrep2 : unknown syncinfo tag (%d)\n",
682 ldap_memfree( retoid );
683 ber_bvfree( retdata );
687 if ( syncCookie_req.ctxcsn == NULL ) {
689 } else if ( syncCookie.ctxcsn == NULL ) {
692 value_match( &match, slap_schema.si_ad_entryCSN,
693 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
694 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
695 &syncCookie_req.ctxcsn[0],
696 &syncCookie.ctxcsn[0], &text );
699 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
701 syncrepl_updateCookie( si, op, psub, &syncCookie);
704 if ( si_refreshPresent == 1 ) {
706 syncrepl_del_nonpresent( op, si );
710 ldap_memfree( retoid );
711 ber_bvfree( retdata );
715 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
716 " unknown intermediate "
717 "response\n", 0, 0, 0 );
719 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
720 "unknown intermediate response (%d)\n",
723 ldap_memfree( retoid );
724 ber_bvfree( retdata );
730 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
731 "unknown message\n", 0, 0, 0 );
733 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
734 "unknown message\n", 0, 0, 0 );
739 if ( syncCookie.octet_str ) {
740 slap_sync_cookie_free( &syncCookie_req, 0 );
741 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
742 slap_sync_cookie_free( &syncCookie, 0 );
752 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
753 errstr = ldap_err2string( rc );
756 LDAP_LOG( OPERATION, ERR,
757 "do_syncrep2 : %s\n", errstr, 0, 0 );
759 Debug( LDAP_DEBUG_ANY,
760 "do_syncrep2 : %s\n", errstr, 0, 0 );
765 slap_sync_cookie_free( &syncCookie, 0 );
766 slap_sync_cookie_free( &syncCookie_req, 0 );
768 if ( res ) ldap_msgfree( res );
770 if ( rc && si->si_ld ) {
771 ldap_unbind( si->si_ld );
783 struct re_s* rtask = arg;
784 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
785 Connection conn = {0};
787 int rc = LDAP_SUCCESS;
793 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
795 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
801 switch( abs( si->si_type )) {
802 case LDAP_SYNC_REFRESH_ONLY:
803 case LDAP_SYNC_REFRESH_AND_PERSIST:
809 if ( slapd_abrupt_shutdown && si->si_ld ) {
810 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
811 connection_client_stop( s );
812 ldap_unbind( si->si_ld );
818 conn.c_send_ldap_result = slap_send_ldap_result;
819 conn.c_send_search_entry = slap_send_search_entry;
820 conn.c_send_search_reference = slap_send_search_reference;
821 conn.c_listener = (Listener *)&dummy_list;
822 conn.c_peer_name = slap_empty_bv;
824 /* set memory context */
825 #define SLAB_SIZE 1048576
826 op.o_tmpmemctx = sl_mem_create( SLAB_SIZE, ctx );
827 op.o_tmpmfuncs = &sl_mfuncs;
829 op.o_dn = si->si_updatedn;
830 op.o_ndn = si->si_updatedn;
831 op.o_time = slap_get_time();
832 op.o_threadctx = ctx;
833 op.o_managedsait = 1;
836 op.o_connid = op.o_conn->c_connid;
838 op.o_sync_state.ctxcsn = NULL;
839 op.o_sync_state.sid = -1;
840 op.o_sync_state.octet_str = NULL;
841 op.o_sync_slog_size = -1;
842 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
843 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
845 /* Establish session, do search */
848 rc = do_syncrep1( &op, si );
851 /* Process results */
852 if ( rc == LDAP_SUCCESS ) {
853 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
855 rc = do_syncrep2( &op, si );
857 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
858 /* If we succeeded, enable the connection for further listening.
859 * If we failed, tear down the connection and reschedule.
861 if ( rc == LDAP_SUCCESS ) {
863 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
866 connection_client_enable( s );
868 } else if ( !first ) {
872 if ( rc == -2 ) rc = 0;
876 /* At this point, we have 4 cases:
877 * 1) for any hard failure, give up and remove this task
878 * 2) for ServerDown, reschedule this task to run
879 * 3) for Refresh and Success, reschedule to run
880 * 4) for Persist and Success, reschedule to defer
882 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
883 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
884 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
888 connection_client_stop( s );
891 if ( rc && rc != LDAP_SERVER_DOWN ) {
892 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
894 if ( rc == LDAP_SERVER_DOWN ||
895 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
900 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
902 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
908 syncrepl_message_to_entry(
912 Modifications **modlist,
917 BerElement *ber = NULL;
920 Modifications **modtail = modlist;
923 char txtbuf[SLAP_TEXT_BUFLEN];
924 size_t textlen = sizeof txtbuf;
926 struct berval bdn = {0, NULL}, dn, ndn;
931 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
933 LDAP_LOG( OPERATION, ERR,
934 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
936 Debug( LDAP_DEBUG_ANY,
937 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
942 op->o_tag = LDAP_REQ_ADD;
944 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
946 if ( rc != LDAP_SUCCESS ) {
948 LDAP_LOG( OPERATION, ERR,
949 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
951 Debug( LDAP_DEBUG_ANY,
952 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
957 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
958 ber_dupbv( &op->o_req_dn, &dn );
959 ber_dupbv( &op->o_req_ndn, &ndn );
960 sl_free( ndn.bv_val, op->o_tmpmemctx );
961 sl_free( dn.bv_val, op->o_tmpmemctx );
963 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
968 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
969 e->e_name = op->o_req_dn;
970 e->e_nname = op->o_req_ndn;
972 while ( ber_remaining( ber ) ) {
973 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
974 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
979 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
981 mod->sml_op = LDAP_MOD_REPLACE;
982 mod->sml_next = NULL;
983 mod->sml_desc = NULL;
984 mod->sml_type = tmp.sml_type;
985 mod->sml_bvalues = tmp.sml_bvalues;
986 mod->sml_nvalues = NULL;
989 modtail = &mod->sml_next;
992 if ( *modlist == NULL ) {
994 LDAP_LOG( OPERATION, ERR,
995 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
997 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1002 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1004 if ( rc != LDAP_SUCCESS ) {
1006 LDAP_LOG( OPERATION, ERR,
1007 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1009 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1015 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1016 if( rc != LDAP_SUCCESS ) {
1018 LDAP_LOG( OPERATION, ERR,
1019 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1021 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1027 ber_free ( ber, 0 );
1028 if ( rc != LDAP_SUCCESS ) {
1041 Modifications* modlist,
1043 struct berval* syncUUID,
1044 struct sync_cookie* syncCookie_req
1047 Backend *be = op->o_bd;
1049 struct berval *syncuuid_bv = NULL;
1050 struct berval syncUUID_strrep = { 0, NULL };
1052 SlapReply rs = {REP_RESULT};
1054 AttributeAssertion ava = {0};
1055 int rc = LDAP_SUCCESS;
1056 int ret = LDAP_SUCCESS;
1059 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1061 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1062 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1063 syncuuid_cmp, avl_dup_error );
1066 if ( syncstate == LDAP_SYNC_PRESENT ) {
1070 f.f_choice = LDAP_FILTER_EQUALITY;
1072 ava.aa_desc = slap_schema.si_ad_entryUUID;
1073 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1074 ava.aa_value = *syncUUID;
1075 op->ors_filter = &f;
1077 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1078 op->ors_filterstr.bv_val = (char *) sl_malloc(
1079 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1080 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1081 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1082 syncUUID->bv_val, syncUUID->bv_len );
1083 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1085 op->ors_scope = LDAP_SCOPE_SUBTREE;
1087 /* get syncrepl cookie of shadow replica from subentry */
1088 op->o_req_dn = si->si_base;
1089 op->o_req_ndn = si->si_base;
1091 /* set callback function */
1092 op->o_callback = &cb;
1093 cb.sc_response = dn_callback;
1096 si->si_syncUUID_ndn.bv_val = NULL;
1098 rc = be->be_search( op, &rs );
1100 if ( op->ors_filterstr.bv_val ) {
1101 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1104 cb.sc_response = null_callback;
1107 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1111 if ( syncstate != LDAP_SYNC_DELETE ) {
1112 op->o_no_psearch = 1;
1115 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1116 if ( op->o_sync_csn.bv_val ) {
1117 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1122 op->o_req_dn = si->si_syncUUID_ndn;
1123 op->o_req_ndn = si->si_syncUUID_ndn;
1124 op->o_tag = LDAP_REQ_DELETE;
1125 rc = be->be_delete( op, &rs );
1126 op->o_no_psearch = 0;
1129 switch ( syncstate ) {
1131 case LDAP_SYNC_MODIFY:
1132 if ( rc == LDAP_SUCCESS ||
1133 rc == LDAP_REFERRAL ||
1134 rc == LDAP_NO_SUCH_OBJECT )
1136 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1137 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1138 syncUUID, &ava.aa_value );
1140 op->o_tag = LDAP_REQ_ADD;
1142 op->o_req_dn = e->e_name;
1143 op->o_req_ndn = e->e_nname;
1144 rc = be->be_add( op, &rs );
1146 if ( rc != LDAP_SUCCESS ) {
1147 if ( rc == LDAP_ALREADY_EXISTS ) {
1148 op->o_tag = LDAP_REQ_MODIFY;
1149 op->orm_modlist = modlist;
1150 op->o_req_dn = e->e_name;
1151 op->o_req_ndn = e->e_nname;
1152 rc = be->be_modify( op, &rs );
1153 if ( rc != LDAP_SUCCESS ) {
1155 LDAP_LOG( OPERATION, ERR,
1156 "syncrepl_entry : be_modify failed (%d)\n",
1159 Debug( LDAP_DEBUG_ANY,
1160 "syncrepl_entry : be_modify failed (%d)\n",
1166 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1167 syncrepl_add_glue( op, e );
1172 LDAP_LOG( OPERATION, ERR,
1173 "syncrepl_entry : be_add failed (%d)\n",
1176 Debug( LDAP_DEBUG_ANY,
1177 "syncrepl_entry : be_add failed (%d)\n",
1184 be_entry_release_w( op, e );
1190 LDAP_LOG( OPERATION, ERR,
1191 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1193 Debug( LDAP_DEBUG_ANY,
1194 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1200 case LDAP_SYNC_DELETE :
1201 /* Already deleted */
1207 LDAP_LOG( OPERATION, ERR,
1208 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1210 Debug( LDAP_DEBUG_ANY,
1211 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1219 if ( syncUUID_strrep.bv_val ) {
1220 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1222 if ( si->si_syncUUID_ndn.bv_val ) {
1223 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1229 syncrepl_del_nonpresent(
1234 Backend* be = op->o_bd;
1236 SlapReply rs = {REP_RESULT};
1237 struct nonpresent_entry *np_list, *np_prev;
1239 op->o_req_dn = si->si_base;
1240 op->o_req_ndn = si->si_base;
1242 cb.sc_response = nonpresent_callback;
1245 op->o_callback = &cb;
1246 op->o_tag = LDAP_REQ_SEARCH;
1247 op->ors_scope = si->si_scope;
1248 op->ors_deref = LDAP_DEREF_NEVER;
1251 op->ors_attrsonly = 0;
1252 op->ors_attrs = NULL;
1253 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1254 op->ors_filterstr = si->si_filterstr;
1256 op->o_nocaching = 1;
1257 be->be_search( op, &rs );
1258 op->o_nocaching = 0;
1260 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1262 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1263 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1264 while ( np_list != NULL ) {
1265 LDAP_LIST_REMOVE( np_list, npe_link );
1267 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1268 op->o_tag = LDAP_REQ_DELETE;
1269 op->o_callback = &cb;
1270 cb.sc_response = null_callback;
1272 op->o_req_dn = *np_prev->npe_name;
1273 op->o_req_ndn = *np_prev->npe_nname;
1274 op->o_bd->be_delete( op, &rs );
1275 ber_bvfree( np_prev->npe_name );
1276 ber_bvfree( np_prev->npe_nname );
1277 op->o_req_dn.bv_val = NULL;
1278 op->o_req_ndn.bv_val = NULL;
1287 static struct berval gcbva[] = {
1298 Backend *be = op->o_bd;
1304 struct berval dn = {0, NULL};
1305 struct berval ndn = {0, NULL};
1307 SlapReply rs = {REP_RESULT};
1310 op->o_tag = LDAP_REQ_ADD;
1311 op->o_callback = &cb;
1312 cb.sc_response = null_callback;
1313 cb.sc_private = NULL;
1318 /* count RDNs in suffix */
1319 if ( be->be_nsuffix[0].bv_len ) {
1320 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1330 /* Start with BE suffix */
1331 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1332 comma = strrchr(dn.bv_val, ',');
1333 if ( ptr ) *ptr = ',';
1334 if ( comma ) *comma = '\0';
1339 dn.bv_len -= ptr - dn.bv_val;
1342 /* the normalizedDNs are always the same length, no counting
1345 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1346 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1347 ndn.bv_len = be->be_nsuffix[0].bv_len;
1350 while ( ndn.bv_val > e->e_nname.bv_val ) {
1351 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1352 ber_dupbv( &glue->e_name, &dn );
1353 ber_dupbv( &glue->e_nname, &ndn );
1355 a = ch_calloc( 1, sizeof( Attribute ));
1356 a->a_desc = slap_schema.si_ad_objectClass;
1358 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1359 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1360 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1361 a->a_vals[2].bv_len = 0;
1362 a->a_vals[2].bv_val = NULL;
1364 a->a_nvals = a->a_vals;
1366 a->a_next = glue->e_attrs;
1369 a = ch_calloc( 1, sizeof( Attribute ));
1370 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1372 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1373 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1374 a->a_vals[1].bv_len = 0;
1375 a->a_vals[1].bv_val = NULL;
1377 a->a_nvals = a->a_vals;
1379 a->a_next = glue->e_attrs;
1382 op->o_req_dn = glue->e_name;
1383 op->o_req_ndn = glue->e_nname;
1385 rc = be->be_add ( op, &rs );
1386 if ( rc == LDAP_SUCCESS ) {
1387 be_entry_release_w( op, glue );
1389 /* incl. ALREADY EXIST */
1393 /* Move to next child */
1394 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1397 if ( ptr == e->e_name.bv_val ) break;
1399 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1400 for( ptr = ndn.bv_val-2;
1401 ptr > e->e_nname.bv_val && *ptr != ',';
1407 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1410 op->o_req_dn = e->e_name;
1411 op->o_req_ndn = e->e_nname;
1413 rc = be->be_add ( op, &rs );
1414 if ( rc == LDAP_SUCCESS ) {
1415 be_entry_release_w( op, e );
1423 static struct berval ocbva[] = {
1425 BER_BVC("subentry"),
1426 BER_BVC("syncConsumerSubentry"),
1430 static struct berval cnbva[] = {
1435 static struct berval ssbva[] = {
1440 static struct berval scbva[] = {
1446 syncrepl_updateCookie(
1450 struct sync_cookie *syncCookie
1453 Backend *be = op->o_bd;
1455 Modifications *mlnext;
1457 Modifications *modlist = NULL;
1458 Modifications **modtail = &modlist;
1461 char txtbuf[SLAP_TEXT_BUFLEN];
1462 size_t textlen = sizeof txtbuf;
1467 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1468 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1469 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1472 SlapReply rs = {REP_RESULT};
1474 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1475 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1477 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1478 mod->sml_op = LDAP_MOD_REPLACE;
1479 mod->sml_desc = slap_schema.si_ad_objectClass;
1480 mod->sml_type = mod->sml_desc->ad_cname;
1481 mod->sml_bvalues = ocbva;
1483 modtail = &mod->sml_next;
1485 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1486 assert( si->si_id < 1000 );
1487 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1488 slap_syncrepl_bvc.bv_len,
1489 "syncrepl%d", si->si_id );
1490 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1491 mod->sml_op = LDAP_MOD_REPLACE;
1492 mod->sml_desc = slap_schema.si_ad_cn;
1493 mod->sml_type = mod->sml_desc->ad_cname;
1494 mod->sml_bvalues = cnbva;
1496 modtail = &mod->sml_next;
1498 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1499 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1500 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1501 mod->sml_op = LDAP_MOD_REPLACE;
1502 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1503 mod->sml_type = mod->sml_desc->ad_cname;
1504 mod->sml_bvalues = scbva;
1506 modtail = &mod->sml_next;
1508 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1509 mod->sml_op = LDAP_MOD_REPLACE;
1510 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1511 mod->sml_type = mod->sml_desc->ad_cname;
1512 mod->sml_bvalues = ssbva;
1514 modtail = &mod->sml_next;
1518 op->o_tag = LDAP_REQ_ADD;
1519 rc = slap_mods_opattrs( op, modlist, modtail,
1520 &text,txtbuf, textlen );
1522 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1523 ml->sml_op = LDAP_MOD_REPLACE;
1526 if( rc != LDAP_SUCCESS ) {
1528 LDAP_LOG( OPERATION, ERR,
1529 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1531 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1536 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1538 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1539 assert( si->si_id < 1000 );
1540 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1541 slap_syncrepl_cn_bvc.bv_len,
1542 "cn=syncrepl%d", si->si_id );
1544 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1546 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1547 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1549 if ( slap_syncrepl_dn_bv.bv_val ) {
1550 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1555 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1557 if( rc != LDAP_SUCCESS ) {
1559 LDAP_LOG( OPERATION, ERR,
1560 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1562 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1567 cb.sc_response = null_callback;
1570 op->o_callback = &cb;
1571 op->o_req_dn = e->e_name;
1572 op->o_req_ndn = e->e_nname;
1574 /* update persistent cookie */
1575 update_cookie_retry:
1576 op->o_tag = LDAP_REQ_MODIFY;
1577 op->orm_modlist = modlist;
1578 rc = be->be_modify( op, &rs );
1580 if ( rc != LDAP_SUCCESS ) {
1581 if ( rc == LDAP_REFERRAL ||
1582 rc == LDAP_NO_SUCH_OBJECT ) {
1583 op->o_tag = LDAP_REQ_ADD;
1585 rc = be->be_add( op, &rs );
1586 if ( rc != LDAP_SUCCESS ) {
1587 if ( rc == LDAP_ALREADY_EXISTS ) {
1588 goto update_cookie_retry;
1589 } else if ( rc == LDAP_REFERRAL ||
1590 rc == LDAP_NO_SUCH_OBJECT ) {
1592 LDAP_LOG( OPERATION, ERR,
1593 "cookie will be non-persistent\n",
1596 Debug( LDAP_DEBUG_ANY,
1597 "cookie will be non-persistent\n",
1602 LDAP_LOG( OPERATION, ERR,
1603 "be_add failed (%d)\n",
1606 Debug( LDAP_DEBUG_ANY,
1607 "be_add failed (%d)\n",
1612 be_entry_release_w( op, e );
1617 LDAP_LOG( OPERATION, ERR,
1618 "be_modify failed (%d)\n", rc, 0, 0 );
1620 Debug( LDAP_DEBUG_ANY,
1621 "be_modify failed (%d)\n", rc, 0, 0 );
1632 if ( cnbva[0].bv_val ) {
1633 ch_free( cnbva[0].bv_val );
1634 cnbva[0].bv_val = NULL;
1636 if ( scbva[0].bv_val ) {
1637 ch_free( scbva[0].bv_val );
1638 scbva[0].bv_val = NULL;
1641 if ( mlnext->sml_next ) {
1642 slap_mods_free( mlnext->sml_next );
1643 mlnext->sml_next = NULL;
1646 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1647 mlnext = ml->sml_next;
1660 syncinfo_t *si = op->o_callback->sc_private;
1662 if ( rs->sr_type == REP_SEARCH ) {
1663 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1665 LDAP_LOG( OPERATION, ERR,
1666 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1668 Debug( LDAP_DEBUG_ANY,
1669 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1672 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1676 return LDAP_SUCCESS;
1680 nonpresent_callback(
1685 syncinfo_t *si = op->o_callback->sc_private;
1688 struct berval* present_uuid = NULL;
1689 struct nonpresent_entry *np_entry;
1691 if ( rs->sr_type == REP_RESULT ) {
1692 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1693 si->si_presentlist = NULL;
1695 } else if ( rs->sr_type == REP_SEARCH ) {
1696 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1698 if ( a == NULL ) return 0;
1700 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1703 if ( present_uuid == NULL ) {
1704 np_entry = (struct nonpresent_entry *)
1705 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1706 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1707 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1708 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1711 avl_delete( &si->si_presentlist,
1712 &a->a_vals[0], syncuuid_cmp );
1713 ch_free( present_uuid->bv_val );
1714 ch_free( present_uuid );
1717 return LDAP_SUCCESS;
1726 if ( rs->sr_err != LDAP_SUCCESS &&
1727 rs->sr_err != LDAP_REFERRAL &&
1728 rs->sr_err != LDAP_ALREADY_EXISTS &&
1729 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1732 LDAP_LOG( OPERATION, ERR,
1733 "null_callback : error code 0x%x\n",
1736 Debug( LDAP_DEBUG_ANY,
1737 "null_callback : error code 0x%x\n",
1741 return LDAP_SUCCESS;
1745 slap_create_syncrepl_entry(
1747 struct berval *context_csn,
1756 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1758 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1760 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1763 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1765 if ( context_csn ) {
1766 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1767 context_csn, NULL );
1771 bv.bv_len = sizeof("{}")-1;
1772 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1774 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1775 ber_dupbv( &e->e_nname, &e->e_name );
1781 slap_uuidstr_from_normalized(
1782 struct berval* uuidstr,
1783 struct berval* normalized,
1787 unsigned char nibble;
1790 if ( normalized == NULL )
1793 if ( normalized->bv_len != 16 ) {
1800 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1805 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1807 sl_free( new, ctx );
1811 for ( i = 0; i < 16; i++ ) {
1812 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1813 new->bv_val[(i<<1)+d] = '-';
1817 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1818 if ( nibble < 10 ) {
1819 new->bv_val[(i<<1)+d] = nibble + '0';
1821 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1824 nibble = (normalized->bv_val[i]) & 0xF;
1825 if ( nibble < 10 ) {
1826 new->bv_val[(i<<1)+d+1] = nibble + '0';
1828 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1832 new->bv_val[new->bv_len] = '\0';
1838 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1840 const struct berval *uuid1 = v_uuid1;
1841 const struct berval *uuid2 = v_uuid2;
1842 int rc = uuid1->bv_len - uuid2->bv_len;
1843 if ( rc ) return rc;
1844 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1848 avl_ber_bvfree( void *bv )
1853 if ( ((struct berval *)bv)->bv_val != NULL ) {
1854 ch_free ( ((struct berval *)bv)->bv_val );
1856 ch_free ( (char *) bv );