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 };
198 psub = &si->si_be->be_nsuffix[0];
200 /* Init connection to master */
202 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
203 if ( rc != LDAP_SUCCESS ) {
205 LDAP_LOG( OPERATION, ERR,
206 "do_syncrep1: ldap_initialize failed (%s)\n",
207 si->si_provideruri, 0, 0 );
209 Debug( LDAP_DEBUG_ANY,
210 "do_syncrep1: ldap_initialize failed (%s)\n",
211 si->si_provideruri, 0, 0 );
216 op->o_protocol = LDAP_VERSION3;
217 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
222 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
223 if( rc != LDAP_SUCCESS ) {
225 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
226 "%s: ldap_start_tls failed (%d)\n",
227 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
230 Debug( LDAP_DEBUG_ANY,
231 "%s: ldap_start_tls failed (%d)\n",
232 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
235 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
239 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
240 #ifdef HAVE_CYRUS_SASL
243 if ( si->si_secprops != NULL ) {
244 rc = ldap_set_option( si->si_ld,
245 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
247 if( rc != LDAP_OPT_SUCCESS ) {
249 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
250 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
251 si->si_provideruri, si->si_secprops, 0 );
253 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
254 "(%s,SECPROPS,\"%s\") failed!\n",
255 si->si_provideruri, si->si_secprops, 0 );
261 defaults = lutil_sasl_defaults( si->si_ld,
262 si->si_saslmech, si->si_realm,
263 si->si_authcId, si->si_passwd, si->si_authzId );
265 rc = ldap_sasl_interactive_bind_s( si->si_ld,
273 lutil_sasl_freedefs( defaults );
275 /* FIXME : different error behaviors according to
277 * 2) on err policy : exit, retry, backoff ...
279 if ( rc != LDAP_SUCCESS ) {
281 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
282 "ldap_sasl_interactive_bind_s failed (%d)\n",
285 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
286 "ldap_sasl_interactive_bind_s failed (%d)\n",
291 #else /* HAVE_CYRUS_SASL */
292 /* Should never get here, we trapped this at config time */
293 fprintf( stderr, "not compiled with SASL support\n" );
298 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
299 if ( rc != LDAP_SUCCESS ) {
301 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
302 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
304 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
305 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
311 /* get syncrepl cookie of shadow replica from subentry */
313 assert( si->si_rid < 1000 );
314 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
315 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
316 CN_STR "syncrepl%d", si->si_rid );
317 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
318 op->o_req_dn = op->o_req_ndn;
320 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
321 if ( si->si_rid == sc->rid ) {
322 cmdline_cookie_found = 1;
327 if ( cmdline_cookie_found ) {
328 /* cookie is supplied in the command line */
329 BerVarray cookie = NULL;
330 struct berval cookie_bv;
332 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
333 slap_sync_cookie_free( &si->si_syncCookie, 0 );
335 /* read stored cookie if it exists */
336 backend_attribute( op, NULL, &op->o_req_ndn,
337 slap_schema.si_ad_syncreplCookie, &cookie );
340 /* no stored cookie */
341 if ( sc->ctxcsn == NULL ||
342 sc->ctxcsn->bv_val == NULL ) {
343 /* if cmdline cookie does not have ctxcsn */
344 /* component, set it to an initial value */
345 slap_init_sync_cookie_ctxcsn( sc );
347 slap_dup_sync_cookie( &si->si_syncCookie, sc );
348 slap_sync_cookie_free( sc, 1 );
352 struct berval newcookie = { 0, NULL };
353 ber_dupbv( &cookie_bv, &cookie[0] );
354 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
355 slap_parse_sync_cookie( &si->si_syncCookie );
356 ber_bvarray_free( si->si_syncCookie.octet_str );
357 si->si_syncCookie.octet_str = NULL;
358 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
359 if ( sc->sid != -1 ) {
360 /* command line cookie wins */
361 si->si_syncCookie.sid = sc->sid;
363 if ( sc->ctxcsn != NULL ) {
364 /* command line cookie wins */
365 if ( si->si_syncCookie.ctxcsn ) {
366 ber_bvarray_free( si->si_syncCookie.ctxcsn );
367 si->si_syncCookie.ctxcsn = NULL;
369 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
370 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
372 if ( sc->rid != -1 ) {
373 /* command line cookie wins */
374 si->si_syncCookie.rid = sc->rid;
376 slap_sync_cookie_free( sc, 1 );
378 slap_compose_sync_cookie( NULL, &newcookie,
379 &si->si_syncCookie.ctxcsn[0],
380 si->si_syncCookie.sid, si->si_syncCookie.rid );
381 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
384 /* no command line cookie is specified */
385 if ( si->si_syncCookie.octet_str == NULL ) {
386 BerVarray cookie = NULL;
387 struct berval cookie_bv;
388 /* try to read stored cookie */
389 backend_attribute( op, NULL, &op->o_req_ndn,
390 slap_schema.si_ad_syncreplCookie, &cookie );
392 ber_dupbv( &cookie_bv, &cookie[0] );
393 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
394 slap_parse_sync_cookie( &si->si_syncCookie );
395 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
400 rc = ldap_sync_search( si, op->o_tmpmemctx );
402 if( rc != LDAP_SUCCESS ) {
404 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
405 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
407 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
408 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
415 ldap_unbind( si->si_ld );
428 LDAPControl **rctrls = NULL;
431 BerElementBuffer berbuf;
432 BerElement *ber = (BerElement *)&berbuf;
434 LDAPMessage *res = NULL;
435 LDAPMessage *msg = NULL;
438 struct berval *retdata = NULL;
443 struct berval syncUUID = { 0, NULL };
444 struct sync_cookie syncCookie = { NULL, -1, NULL };
445 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
446 struct berval cookie = { 0, NULL };
456 Modifications *modlist = NULL;
461 struct timeval *tout_p = NULL;
462 struct timeval tout = { 0, 0 };
464 int refreshDeletes = 0;
466 BerVarray syncUUIDs = NULL;
469 if ( slapd_abrupt_shutdown ) {
474 ber_init2( ber, NULL, LBER_USE_DER );
475 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
478 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
480 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
483 op->o_callback = &cb;
485 psub = &si->si_be->be_nsuffix[0];
487 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
489 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
495 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
498 if ( slapd_abrupt_shutdown ) {
502 for( msg = ldap_first_message( si->si_ld, res );
504 msg = ldap_next_message( si->si_ld, msg ) )
506 switch( ldap_msgtype( msg ) ) {
507 case LDAP_RES_SEARCH_ENTRY:
508 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
509 /* we can't work without the control */
515 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
516 ber_scanf( ber, "{em", &syncstate, &syncUUID );
517 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
518 ber_scanf( ber, "m}", &cookie );
519 if ( cookie.bv_val ) {
520 struct berval tmp_bv;
521 ber_dupbv( &tmp_bv, &cookie );
522 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
524 if ( syncCookie.octet_str &&
525 syncCookie.octet_str[0].bv_val )
526 slap_parse_sync_cookie( &syncCookie );
528 entry = syncrepl_message_to_entry( si, op, msg,
529 &modlist, syncstate );
530 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
531 &syncUUID, &syncCookie_req );
532 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
533 syncrepl_updateCookie( si, op, psub, &syncCookie );
535 ldap_controls_free( rctrls );
537 slap_mods_free( modlist );
539 if ( rc_efree && entry ) {
544 case LDAP_RES_SEARCH_REFERENCE:
546 LDAP_LOG( OPERATION, ERR,
547 "do_syncrep2 : reference received\n", 0, 0, 0 );
549 Debug( LDAP_DEBUG_ANY,
550 "do_syncrep2 : reference received\n", 0, 0, 0 );
554 case LDAP_RES_SEARCH_RESULT:
555 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
559 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
561 ber_scanf( ber, "{" /*"}"*/);
562 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
564 ber_scanf( ber, "m", &cookie );
565 if ( cookie.bv_val ) {
566 struct berval tmp_bv;
567 ber_dupbv( &tmp_bv, &cookie );
568 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
570 if ( syncCookie.octet_str &&
571 syncCookie.octet_str[0].bv_val )
572 slap_parse_sync_cookie( &syncCookie );
574 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
576 ber_scanf( ber, "b", &refreshDeletes );
578 ber_scanf( ber, "}" );
580 if ( syncCookie_req.ctxcsn == NULL ) {
582 } else if ( syncCookie.ctxcsn == NULL ) {
585 value_match( &match, slap_schema.si_ad_entryCSN,
586 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
587 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
588 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
590 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
592 syncrepl_updateCookie( si, op, psub, &syncCookie );
595 ldap_controls_free( rctrls );
597 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
598 /* FIXME : different error behaviors according to
599 * 1) err code : LDAP_BUSY ...
600 * 2) on err policy : stop service, stop sync, retry
602 if ( refreshDeletes == 0 && match < 0 ) {
603 syncrepl_del_nonpresent( op, si );
605 avl_free( si->si_presentlist, avl_ber_bvfree );
606 si->si_presentlist = NULL;
613 case LDAP_RES_INTERMEDIATE:
614 rc = ldap_parse_intermediate( si->si_ld, msg,
615 &retoid, &retdata, NULL, 0 );
616 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
617 int si_refreshDelete = 0;
618 int si_refreshPresent = 0;
619 ber_init2( ber, retdata, LBER_USE_DER );
621 switch ( si_tag = ber_peek_tag( ber, &len )) {
623 case LDAP_TAG_SYNC_NEW_COOKIE:
624 ber_scanf( ber, "tm", &tag, &cookie );
626 case LDAP_TAG_SYNC_REFRESH_DELETE:
627 si_refreshDelete = 1;
628 case LDAP_TAG_SYNC_REFRESH_PRESENT:
629 si_refreshPresent = 1;
630 ber_scanf( ber, "t{", &tag );
631 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
633 ber_scanf( ber, "m", &cookie );
634 if ( cookie.bv_val ) {
635 struct berval tmp_bv;
636 ber_dupbv( &tmp_bv, &cookie );
637 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
639 if ( syncCookie.octet_str &&
640 syncCookie.octet_str[0].bv_val )
641 slap_parse_sync_cookie( &syncCookie );
643 if ( ber_peek_tag( ber, &len ) ==
644 LDAP_TAG_REFRESHDONE )
646 ber_scanf( ber, "b", &refreshDone );
648 ber_scanf( ber, "}" );
650 case LDAP_TAG_SYNC_ID_SET:
651 ber_scanf( ber, "t{", &tag );
652 if ( ber_peek_tag( ber, &len ) ==
653 LDAP_TAG_SYNC_COOKIE ) {
654 ber_scanf( ber, "m", &cookie );
655 if ( cookie.bv_val ) {
656 struct berval tmp_bv;
657 ber_dupbv( &tmp_bv, &cookie );
658 ber_bvarray_add( &syncCookie.octet_str,
661 if ( syncCookie.octet_str &&
662 syncCookie.octet_str[0].bv_val )
663 slap_parse_sync_cookie( &syncCookie );
665 if ( ber_peek_tag( ber, &len ) ==
666 LDAP_TAG_REFRESHDELETES )
668 ber_scanf( ber, "b", &refreshDeletes );
670 ber_scanf( ber, "[W]", &syncUUIDs );
671 ber_scanf( ber, "}" );
672 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
673 struct berval *syncuuid_bv;
674 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
675 avl_insert( &si->si_presentlist,
676 (caddr_t) syncuuid_bv,
677 syncuuid_cmp, avl_dup_error );
679 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
683 LDAP_LOG( OPERATION, ERR,
684 "do_syncrep2 : unknown syncinfo tag (%d)\n",
687 Debug( LDAP_DEBUG_ANY,
688 "do_syncrep2 : unknown syncinfo tag (%d)\n",
691 ldap_memfree( retoid );
692 ber_bvfree( retdata );
696 if ( syncCookie_req.ctxcsn == NULL ) {
698 } else if ( syncCookie.ctxcsn == NULL ) {
701 value_match( &match, slap_schema.si_ad_entryCSN,
702 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
703 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
704 &syncCookie_req.ctxcsn[0],
705 &syncCookie.ctxcsn[0], &text );
708 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
710 syncrepl_updateCookie( si, op, psub, &syncCookie);
713 if ( si_refreshPresent == 1 ) {
715 syncrepl_del_nonpresent( op, si );
719 ldap_memfree( retoid );
720 ber_bvfree( retdata );
724 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
725 " unknown intermediate "
726 "response\n", 0, 0, 0 );
728 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
729 "unknown intermediate response (%d)\n",
732 ldap_memfree( retoid );
733 ber_bvfree( retdata );
739 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
740 "unknown message\n", 0, 0, 0 );
742 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
743 "unknown message\n", 0, 0, 0 );
748 if ( syncCookie.octet_str ) {
749 slap_sync_cookie_free( &syncCookie_req, 0 );
750 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
751 slap_sync_cookie_free( &syncCookie, 0 );
761 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
762 errstr = ldap_err2string( rc );
765 LDAP_LOG( OPERATION, ERR,
766 "do_syncrep2 : %s\n", errstr, 0, 0 );
768 Debug( LDAP_DEBUG_ANY,
769 "do_syncrep2 : %s\n", errstr, 0, 0 );
774 slap_sync_cookie_free( &syncCookie, 0 );
775 slap_sync_cookie_free( &syncCookie_req, 0 );
777 if ( res ) ldap_msgfree( res );
779 if ( rc && si->si_ld ) {
780 ldap_unbind( si->si_ld );
792 struct re_s* rtask = arg;
793 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
794 Connection conn = {0};
796 int rc = LDAP_SUCCESS;
802 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
804 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
810 switch( abs( si->si_type )) {
811 case LDAP_SYNC_REFRESH_ONLY:
812 case LDAP_SYNC_REFRESH_AND_PERSIST:
818 if ( slapd_abrupt_shutdown && si->si_ld ) {
819 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
820 connection_client_stop( s );
821 ldap_unbind( si->si_ld );
827 conn.c_send_ldap_result = slap_send_ldap_result;
828 conn.c_send_search_entry = slap_send_search_entry;
829 conn.c_send_search_reference = slap_send_search_reference;
830 conn.c_listener = (Listener *)&dummy_list;
831 conn.c_peer_name = slap_empty_bv;
833 /* set memory context */
834 #define SLAB_SIZE 1048576
835 op.o_tmpmemctx = sl_mem_create( SLAB_SIZE, ctx );
836 op.o_tmpmfuncs = &sl_mfuncs;
838 op.o_dn = si->si_updatedn;
839 op.o_ndn = si->si_updatedn;
840 op.o_time = slap_get_time();
841 op.o_threadctx = ctx;
842 op.o_managedsait = 1;
845 op.o_connid = op.o_conn->c_connid;
847 op.o_sync_state.ctxcsn = NULL;
848 op.o_sync_state.sid = -1;
849 op.o_sync_state.octet_str = NULL;
850 op.o_sync_slog_size = -1;
851 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
852 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
854 /* Establish session, do search */
857 rc = do_syncrep1( &op, si );
860 /* Process results */
861 if ( rc == LDAP_SUCCESS ) {
862 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
864 rc = do_syncrep2( &op, si );
866 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
867 /* If we succeeded, enable the connection for further listening.
868 * If we failed, tear down the connection and reschedule.
870 if ( rc == LDAP_SUCCESS ) {
872 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
875 connection_client_enable( s );
877 } else if ( !first ) {
881 if ( rc == -2 ) rc = 0;
885 /* At this point, we have 4 cases:
886 * 1) for any hard failure, give up and remove this task
887 * 2) for ServerDown, reschedule this task to run
888 * 3) for Refresh and Success, reschedule to run
889 * 4) for Persist and Success, reschedule to defer
891 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
892 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
893 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
897 connection_client_stop( s );
900 if ( rc && rc != LDAP_SERVER_DOWN ) {
901 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
903 if ( rc == LDAP_SERVER_DOWN ||
904 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
909 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
911 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
917 syncrepl_message_to_entry(
921 Modifications **modlist,
926 BerElement *ber = NULL;
929 Modifications **modtail = modlist;
932 char txtbuf[SLAP_TEXT_BUFLEN];
933 size_t textlen = sizeof txtbuf;
935 struct berval bdn = {0, NULL}, dn, ndn;
940 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
942 LDAP_LOG( OPERATION, ERR,
943 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
945 Debug( LDAP_DEBUG_ANY,
946 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
951 op->o_tag = LDAP_REQ_ADD;
953 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
955 if ( rc != LDAP_SUCCESS ) {
957 LDAP_LOG( OPERATION, ERR,
958 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
960 Debug( LDAP_DEBUG_ANY,
961 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
966 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
967 ber_dupbv( &op->o_req_dn, &dn );
968 ber_dupbv( &op->o_req_ndn, &ndn );
969 sl_free( ndn.bv_val, op->o_tmpmemctx );
970 sl_free( dn.bv_val, op->o_tmpmemctx );
972 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
977 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
978 e->e_name = op->o_req_dn;
979 e->e_nname = op->o_req_ndn;
981 while ( ber_remaining( ber ) ) {
982 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
983 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
988 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
990 mod->sml_op = LDAP_MOD_REPLACE;
991 mod->sml_next = NULL;
992 mod->sml_desc = NULL;
993 mod->sml_type = tmp.sml_type;
994 mod->sml_bvalues = tmp.sml_bvalues;
995 mod->sml_nvalues = NULL;
998 modtail = &mod->sml_next;
1001 if ( *modlist == NULL ) {
1003 LDAP_LOG( OPERATION, ERR,
1004 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1006 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1011 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1013 if ( rc != LDAP_SUCCESS ) {
1015 LDAP_LOG( OPERATION, ERR,
1016 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1018 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1024 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1025 if( rc != LDAP_SUCCESS ) {
1027 LDAP_LOG( OPERATION, ERR,
1028 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1030 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1036 ber_free ( ber, 0 );
1037 if ( rc != LDAP_SUCCESS ) {
1050 Modifications* modlist,
1052 struct berval* syncUUID,
1053 struct sync_cookie* syncCookie_req
1056 Backend *be = op->o_bd;
1058 struct berval *syncuuid_bv = NULL;
1059 struct berval syncUUID_strrep = { 0, NULL };
1061 SlapReply rs = {REP_RESULT};
1063 AttributeAssertion ava = {0};
1064 int rc = LDAP_SUCCESS;
1065 int ret = LDAP_SUCCESS;
1068 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1070 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1071 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1072 syncuuid_cmp, avl_dup_error );
1075 if ( syncstate == LDAP_SYNC_PRESENT ) {
1079 f.f_choice = LDAP_FILTER_EQUALITY;
1081 ava.aa_desc = slap_schema.si_ad_entryUUID;
1082 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1083 ava.aa_value = *syncUUID;
1084 op->ors_filter = &f;
1086 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1087 op->ors_filterstr.bv_val = (char *) sl_malloc(
1088 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1089 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1090 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1091 syncUUID->bv_val, syncUUID->bv_len );
1092 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1094 op->ors_scope = LDAP_SCOPE_SUBTREE;
1096 /* get syncrepl cookie of shadow replica from subentry */
1097 op->o_req_dn = si->si_base;
1098 op->o_req_ndn = si->si_base;
1100 /* set callback function */
1101 op->o_callback = &cb;
1102 cb.sc_response = dn_callback;
1105 si->si_syncUUID_ndn.bv_val = NULL;
1107 rc = be->be_search( op, &rs );
1109 if ( op->ors_filterstr.bv_val ) {
1110 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1113 cb.sc_response = null_callback;
1116 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1120 if ( syncstate != LDAP_SYNC_DELETE ) {
1121 op->o_no_psearch = 1;
1124 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1125 if ( op->o_sync_csn.bv_val ) {
1126 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1131 op->o_req_dn = si->si_syncUUID_ndn;
1132 op->o_req_ndn = si->si_syncUUID_ndn;
1133 op->o_tag = LDAP_REQ_DELETE;
1134 rc = be->be_delete( op, &rs );
1135 op->o_no_psearch = 0;
1138 switch ( syncstate ) {
1140 case LDAP_SYNC_MODIFY:
1141 if ( rc == LDAP_SUCCESS ||
1142 rc == LDAP_REFERRAL ||
1143 rc == LDAP_NO_SUCH_OBJECT )
1145 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1146 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1147 syncUUID, &ava.aa_value );
1149 op->o_tag = LDAP_REQ_ADD;
1151 op->o_req_dn = e->e_name;
1152 op->o_req_ndn = e->e_nname;
1153 rc = be->be_add( op, &rs );
1155 if ( rc != LDAP_SUCCESS ) {
1156 if ( rc == LDAP_ALREADY_EXISTS ) {
1157 op->o_tag = LDAP_REQ_MODIFY;
1158 op->orm_modlist = modlist;
1159 op->o_req_dn = e->e_name;
1160 op->o_req_ndn = e->e_nname;
1161 rc = be->be_modify( op, &rs );
1162 if ( rc != LDAP_SUCCESS ) {
1164 LDAP_LOG( OPERATION, ERR,
1165 "syncrepl_entry : be_modify failed (%d)\n",
1168 Debug( LDAP_DEBUG_ANY,
1169 "syncrepl_entry : be_modify failed (%d)\n",
1175 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1176 syncrepl_add_glue( op, e );
1181 LDAP_LOG( OPERATION, ERR,
1182 "syncrepl_entry : be_add failed (%d)\n",
1185 Debug( LDAP_DEBUG_ANY,
1186 "syncrepl_entry : be_add failed (%d)\n",
1193 be_entry_release_w( op, e );
1199 LDAP_LOG( OPERATION, ERR,
1200 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1202 Debug( LDAP_DEBUG_ANY,
1203 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1209 case LDAP_SYNC_DELETE :
1210 /* Already deleted */
1216 LDAP_LOG( OPERATION, ERR,
1217 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1219 Debug( LDAP_DEBUG_ANY,
1220 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1228 if ( syncUUID_strrep.bv_val ) {
1229 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1231 if ( si->si_syncUUID_ndn.bv_val ) {
1232 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1238 syncrepl_del_nonpresent(
1243 Backend* be = op->o_bd;
1245 SlapReply rs = {REP_RESULT};
1246 struct nonpresent_entry *np_list, *np_prev;
1248 op->o_req_dn = si->si_base;
1249 op->o_req_ndn = si->si_base;
1251 cb.sc_response = nonpresent_callback;
1254 op->o_callback = &cb;
1255 op->o_tag = LDAP_REQ_SEARCH;
1256 op->ors_scope = si->si_scope;
1257 op->ors_deref = LDAP_DEREF_NEVER;
1260 op->ors_attrsonly = 0;
1261 op->ors_attrs = NULL;
1262 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1263 op->ors_filterstr = si->si_filterstr;
1265 op->o_nocaching = 1;
1266 be->be_search( op, &rs );
1267 op->o_nocaching = 0;
1269 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1271 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1272 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1273 while ( np_list != NULL ) {
1274 LDAP_LIST_REMOVE( np_list, npe_link );
1276 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1277 op->o_tag = LDAP_REQ_DELETE;
1278 op->o_callback = &cb;
1279 cb.sc_response = null_callback;
1281 op->o_req_dn = *np_prev->npe_name;
1282 op->o_req_ndn = *np_prev->npe_nname;
1283 op->o_bd->be_delete( op, &rs );
1284 ber_bvfree( np_prev->npe_name );
1285 ber_bvfree( np_prev->npe_nname );
1286 op->o_req_dn.bv_val = NULL;
1287 op->o_req_ndn.bv_val = NULL;
1296 static struct berval gcbva[] = {
1307 Backend *be = op->o_bd;
1313 struct berval dn = {0, NULL};
1314 struct berval ndn = {0, NULL};
1316 SlapReply rs = {REP_RESULT};
1319 op->o_tag = LDAP_REQ_ADD;
1320 op->o_callback = &cb;
1321 cb.sc_response = null_callback;
1322 cb.sc_private = NULL;
1327 /* count RDNs in suffix */
1328 if ( be->be_nsuffix[0].bv_len ) {
1329 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1339 /* Start with BE suffix */
1340 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1341 comma = strrchr(dn.bv_val, ',');
1342 if ( ptr ) *ptr = ',';
1343 if ( comma ) *comma = '\0';
1348 dn.bv_len -= ptr - dn.bv_val;
1351 /* the normalizedDNs are always the same length, no counting
1354 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1355 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1356 ndn.bv_len = be->be_nsuffix[0].bv_len;
1359 while ( ndn.bv_val > e->e_nname.bv_val ) {
1360 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1361 ber_dupbv( &glue->e_name, &dn );
1362 ber_dupbv( &glue->e_nname, &ndn );
1364 a = ch_calloc( 1, sizeof( Attribute ));
1365 a->a_desc = slap_schema.si_ad_objectClass;
1367 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1368 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1369 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1370 a->a_vals[2].bv_len = 0;
1371 a->a_vals[2].bv_val = NULL;
1373 a->a_nvals = a->a_vals;
1375 a->a_next = glue->e_attrs;
1378 a = ch_calloc( 1, sizeof( Attribute ));
1379 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1381 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1382 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1383 a->a_vals[1].bv_len = 0;
1384 a->a_vals[1].bv_val = NULL;
1386 a->a_nvals = a->a_vals;
1388 a->a_next = glue->e_attrs;
1391 op->o_req_dn = glue->e_name;
1392 op->o_req_ndn = glue->e_nname;
1394 rc = be->be_add ( op, &rs );
1395 if ( rc == LDAP_SUCCESS ) {
1396 be_entry_release_w( op, glue );
1398 /* incl. ALREADY EXIST */
1402 /* Move to next child */
1403 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1406 if ( ptr == e->e_name.bv_val ) break;
1408 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1409 for( ptr = ndn.bv_val-2;
1410 ptr > e->e_nname.bv_val && *ptr != ',';
1416 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1419 op->o_req_dn = e->e_name;
1420 op->o_req_ndn = e->e_nname;
1422 rc = be->be_add ( op, &rs );
1423 if ( rc == LDAP_SUCCESS ) {
1424 be_entry_release_w( op, e );
1432 static struct berval ocbva[] = {
1434 BER_BVC("subentry"),
1435 BER_BVC("syncConsumerSubentry"),
1439 static struct berval cnbva[] = {
1444 static struct berval ssbva[] = {
1449 static struct berval scbva[] = {
1455 syncrepl_updateCookie(
1459 struct sync_cookie *syncCookie
1462 Backend *be = op->o_bd;
1464 Modifications *mlnext;
1466 Modifications *modlist = NULL;
1467 Modifications **modtail = &modlist;
1470 char txtbuf[SLAP_TEXT_BUFLEN];
1471 size_t textlen = sizeof txtbuf;
1476 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1477 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1478 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1481 SlapReply rs = {REP_RESULT};
1483 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1484 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1486 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1487 mod->sml_op = LDAP_MOD_REPLACE;
1488 mod->sml_desc = slap_schema.si_ad_objectClass;
1489 mod->sml_type = mod->sml_desc->ad_cname;
1490 mod->sml_bvalues = ocbva;
1492 modtail = &mod->sml_next;
1494 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1495 assert( si->si_rid < 1000 );
1496 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1497 slap_syncrepl_bvc.bv_len,
1498 "syncrepl%d", si->si_rid );
1499 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1500 mod->sml_op = LDAP_MOD_REPLACE;
1501 mod->sml_desc = slap_schema.si_ad_cn;
1502 mod->sml_type = mod->sml_desc->ad_cname;
1503 mod->sml_bvalues = cnbva;
1505 modtail = &mod->sml_next;
1507 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1508 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1509 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1510 mod->sml_op = LDAP_MOD_REPLACE;
1511 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1512 mod->sml_type = mod->sml_desc->ad_cname;
1513 mod->sml_bvalues = scbva;
1515 modtail = &mod->sml_next;
1517 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1518 mod->sml_op = LDAP_MOD_REPLACE;
1519 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1520 mod->sml_type = mod->sml_desc->ad_cname;
1521 mod->sml_bvalues = ssbva;
1523 modtail = &mod->sml_next;
1527 op->o_tag = LDAP_REQ_ADD;
1528 rc = slap_mods_opattrs( op, modlist, modtail,
1529 &text,txtbuf, textlen );
1531 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1532 ml->sml_op = LDAP_MOD_REPLACE;
1535 if( rc != LDAP_SUCCESS ) {
1537 LDAP_LOG( OPERATION, ERR,
1538 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1540 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1545 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1547 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1548 assert( si->si_rid < 1000 );
1549 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1550 slap_syncrepl_cn_bvc.bv_len,
1551 "cn=syncrepl%d", si->si_rid );
1553 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1555 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1556 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1558 if ( slap_syncrepl_dn_bv.bv_val ) {
1559 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1564 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1566 if( rc != LDAP_SUCCESS ) {
1568 LDAP_LOG( OPERATION, ERR,
1569 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1571 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1576 cb.sc_response = null_callback;
1579 op->o_callback = &cb;
1580 op->o_req_dn = e->e_name;
1581 op->o_req_ndn = e->e_nname;
1583 /* update persistent cookie */
1584 update_cookie_retry:
1585 op->o_tag = LDAP_REQ_MODIFY;
1586 op->orm_modlist = modlist;
1587 rc = be->be_modify( op, &rs );
1589 if ( rc != LDAP_SUCCESS ) {
1590 if ( rc == LDAP_REFERRAL ||
1591 rc == LDAP_NO_SUCH_OBJECT ) {
1592 op->o_tag = LDAP_REQ_ADD;
1594 rc = be->be_add( op, &rs );
1595 if ( rc != LDAP_SUCCESS ) {
1596 if ( rc == LDAP_ALREADY_EXISTS ) {
1597 goto update_cookie_retry;
1598 } else if ( rc == LDAP_REFERRAL ||
1599 rc == LDAP_NO_SUCH_OBJECT ) {
1601 LDAP_LOG( OPERATION, ERR,
1602 "cookie will be non-persistent\n",
1605 Debug( LDAP_DEBUG_ANY,
1606 "cookie will be non-persistent\n",
1611 LDAP_LOG( OPERATION, ERR,
1612 "be_add failed (%d)\n",
1615 Debug( LDAP_DEBUG_ANY,
1616 "be_add failed (%d)\n",
1621 be_entry_release_w( op, e );
1626 LDAP_LOG( OPERATION, ERR,
1627 "be_modify failed (%d)\n", rc, 0, 0 );
1629 Debug( LDAP_DEBUG_ANY,
1630 "be_modify failed (%d)\n", rc, 0, 0 );
1641 if ( cnbva[0].bv_val ) {
1642 ch_free( cnbva[0].bv_val );
1643 cnbva[0].bv_val = NULL;
1645 if ( scbva[0].bv_val ) {
1646 ch_free( scbva[0].bv_val );
1647 scbva[0].bv_val = NULL;
1650 if ( mlnext->sml_next ) {
1651 slap_mods_free( mlnext->sml_next );
1652 mlnext->sml_next = NULL;
1655 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1656 mlnext = ml->sml_next;
1669 syncinfo_t *si = op->o_callback->sc_private;
1671 if ( rs->sr_type == REP_SEARCH ) {
1672 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1674 LDAP_LOG( OPERATION, ERR,
1675 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1677 Debug( LDAP_DEBUG_ANY,
1678 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1681 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1685 return LDAP_SUCCESS;
1689 nonpresent_callback(
1694 syncinfo_t *si = op->o_callback->sc_private;
1697 struct berval* present_uuid = NULL;
1698 struct nonpresent_entry *np_entry;
1700 if ( rs->sr_type == REP_RESULT ) {
1701 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1702 si->si_presentlist = NULL;
1704 } else if ( rs->sr_type == REP_SEARCH ) {
1705 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1707 if ( a == NULL ) return 0;
1709 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1712 if ( present_uuid == NULL ) {
1713 np_entry = (struct nonpresent_entry *)
1714 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1715 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1716 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1717 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1720 avl_delete( &si->si_presentlist,
1721 &a->a_vals[0], syncuuid_cmp );
1722 ch_free( present_uuid->bv_val );
1723 ch_free( present_uuid );
1726 return LDAP_SUCCESS;
1735 if ( rs->sr_err != LDAP_SUCCESS &&
1736 rs->sr_err != LDAP_REFERRAL &&
1737 rs->sr_err != LDAP_ALREADY_EXISTS &&
1738 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1741 LDAP_LOG( OPERATION, ERR,
1742 "null_callback : error code 0x%x\n",
1745 Debug( LDAP_DEBUG_ANY,
1746 "null_callback : error code 0x%x\n",
1750 return LDAP_SUCCESS;
1754 slap_create_syncrepl_entry(
1756 struct berval *context_csn,
1765 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1767 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1769 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1772 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1774 if ( context_csn ) {
1775 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1776 context_csn, NULL );
1780 bv.bv_len = sizeof("{}")-1;
1781 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1783 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1784 ber_dupbv( &e->e_nname, &e->e_name );
1790 slap_uuidstr_from_normalized(
1791 struct berval* uuidstr,
1792 struct berval* normalized,
1796 unsigned char nibble;
1799 if ( normalized == NULL )
1802 if ( normalized->bv_len != 16 ) {
1809 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1814 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1816 sl_free( new, ctx );
1820 for ( i = 0; i < 16; i++ ) {
1821 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1822 new->bv_val[(i<<1)+d] = '-';
1826 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1827 if ( nibble < 10 ) {
1828 new->bv_val[(i<<1)+d] = nibble + '0';
1830 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1833 nibble = (normalized->bv_val[i]) & 0xF;
1834 if ( nibble < 10 ) {
1835 new->bv_val[(i<<1)+d+1] = nibble + '0';
1837 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1841 new->bv_val[new->bv_len] = '\0';
1847 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1849 const struct berval *uuid1 = v_uuid1;
1850 const struct berval *uuid2 = v_uuid2;
1851 int rc = uuid1->bv_len - uuid2->bv_len;
1852 if ( rc ) return rc;
1853 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1857 avl_ber_bvfree( void *bv )
1862 if ( ((struct berval *)bv)->bv_val != NULL ) {
1863 ch_free ( ((struct berval *)bv)->bv_val );
1865 ch_free ( (char *) bv );