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 void avl_ber_bvfree( void * );
56 syncrepl_del_nonpresent( Operation *, syncinfo_t * );
58 /* callback functions */
59 static int dn_callback( struct slap_op *, struct slap_rep * );
60 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
61 static int null_callback( struct slap_op *, struct slap_rep * );
63 static AttributeDescription *sync_descs[4];
65 struct runqueue_s syncrepl_rq;
68 init_syncrepl(syncinfo_t *si)
73 if ( !sync_descs[0] ) {
74 sync_descs[0] = slap_schema.si_ad_objectClass;
75 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
76 sync_descs[2] = slap_schema.si_ad_entryCSN;
80 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
83 /* Delete Attributes */
84 for ( i = 0; sync_descs[i] != NULL; i++ ) {
85 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
86 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
89 ch_free( si->si_attrs[j] );
90 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
91 si->si_attrs[k] = si->si_attrs[k+1];
96 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
97 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
100 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
102 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
106 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
109 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
111 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
114 tmp[ n++ ] = ch_strdup( "*" );
121 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
122 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
123 si->si_attrs[ n ] = NULL;
133 BerElementBuffer berbuf;
134 BerElement *ber = (BerElement *)&berbuf;
135 LDAPControl c[2], *ctrls[3];
136 struct timeval timeout;
140 /* setup LDAP SYNC control */
141 ber_init2( ber, NULL, LBER_USE_DER );
142 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
144 if ( si->si_syncCookie.octet_str &&
145 si->si_syncCookie.octet_str[0].bv_val ) {
146 ber_printf( ber, "{eO}", abs(si->si_type),
147 &si->si_syncCookie.octet_str[0] );
149 ber_printf( ber, "{e}", abs(si->si_type) );
152 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
157 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
158 c[0].ldctl_iscritical = si->si_type < 0;
161 if ( si->si_authzId ) {
162 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
163 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
164 c[1].ldctl_iscritical = 1;
171 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
174 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
175 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
176 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
177 si->si_slimit, &msgid );
183 static const Listener dummy_list = { {0, ""}, {0, ""} };
192 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
193 struct berval syncrepl_cn_bv;
194 struct sync_cookie syncCookie = { NULL, -1, NULL };
196 /* Init connection to master */
198 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
199 if ( rc != LDAP_SUCCESS ) {
201 LDAP_LOG( OPERATION, ERR,
202 "do_syncrep1: ldap_initialize failed (%s)\n",
203 si->si_provideruri, 0, 0 );
205 Debug( LDAP_DEBUG_ANY,
206 "do_syncrep1: ldap_initialize failed (%s)\n",
207 si->si_provideruri, 0, 0 );
212 op->o_protocol = LDAP_VERSION3;
213 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
218 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
219 if( rc != LDAP_SUCCESS ) {
221 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
222 "%s: ldap_start_tls failed (%d)\n",
223 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
226 Debug( LDAP_DEBUG_ANY,
227 "%s: ldap_start_tls failed (%d)\n",
228 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
231 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
235 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
236 #ifdef HAVE_CYRUS_SASL
239 if ( si->si_secprops != NULL ) {
240 rc = ldap_set_option( si->si_ld,
241 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
243 if( rc != LDAP_OPT_SUCCESS ) {
245 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
246 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
247 si->si_provideruri, si->si_secprops, 0 );
249 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
250 "(%s,SECPROPS,\"%s\") failed!\n",
251 si->si_provideruri, si->si_secprops, 0 );
257 defaults = lutil_sasl_defaults( si->si_ld,
258 si->si_saslmech, si->si_realm,
259 si->si_authcId, si->si_passwd, si->si_authzId );
261 rc = ldap_sasl_interactive_bind_s( si->si_ld,
269 lutil_sasl_freedefs( defaults );
271 /* FIXME : different error behaviors according to
273 * 2) on err policy : exit, retry, backoff ...
275 if ( rc != LDAP_SUCCESS ) {
277 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
278 "ldap_sasl_interactive_bind_s failed (%d)\n",
281 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
282 "ldap_sasl_interactive_bind_s failed (%d)\n",
287 #else /* HAVE_CYRUS_SASL */
288 /* Should never get here, we trapped this at config time */
289 fprintf( stderr, "not compiled with SASL support\n" );
294 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
295 if ( rc != LDAP_SUCCESS ) {
297 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
298 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
300 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
301 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
307 /* get syncrepl cookie of shadow replica from subentry */
309 assert( si->si_id < 1000 );
310 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
311 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
312 CN_STR "syncrepl%d", si->si_id );
313 build_new_dn( &op->o_req_ndn, &si->si_base, &syncrepl_cn_bv,
315 op->o_req_dn = op->o_req_ndn;
317 if ( slap_sync_cookie != NULL ) {
318 /* cookie is supplied in the command line */
320 BerVarray cookie = NULL;
321 struct berval cookie_bv;
323 slap_sync_cookie_free( &si->si_syncCookie, 0 );
324 slap_parse_sync_cookie( slap_sync_cookie );
326 /* read stored cookie if it exists */
327 backend_attribute( op, NULL, &op->o_req_ndn,
328 slap_schema.si_ad_syncreplCookie, &cookie );
331 /* no stored cookie */
332 if ( slap_sync_cookie->ctxcsn == NULL ||
333 slap_sync_cookie->ctxcsn->bv_val == NULL ) {
334 /* if slap_sync_cookie does not have ctxcsn component */
335 /* set it to an initial value */
336 slap_init_sync_cookie_ctxcsn( slap_sync_cookie );
338 slap_dup_sync_cookie( &si->si_syncCookie, slap_sync_cookie );
339 slap_sync_cookie_free( slap_sync_cookie, 1 );
340 slap_sync_cookie = NULL;
343 ber_dupbv( &cookie_bv, &cookie[0] );
344 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
345 slap_parse_sync_cookie( &si->si_syncCookie );
346 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
347 if ( slap_sync_cookie->sid != -1 ) {
348 /* command line cookie wins */
349 si->si_syncCookie.sid = slap_sync_cookie->sid;
351 if ( slap_sync_cookie->ctxcsn != NULL ) {
352 /* command line cookie wins */
353 if ( si->si_syncCookie.ctxcsn ) {
354 ber_bvarray_free( si->si_syncCookie.ctxcsn );
355 si->si_syncCookie.ctxcsn = NULL;
357 ber_dupbv( &cookie_bv, &slap_sync_cookie->ctxcsn[0] );
358 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
360 slap_sync_cookie_free( slap_sync_cookie, 1 );
361 slap_sync_cookie = NULL;
364 /* no command line cookie is specified */
365 if ( si->si_syncCookie.octet_str == NULL ) {
366 BerVarray cookie = NULL;
367 struct berval cookie_bv;
368 /* try to read stored cookie */
369 backend_attribute( op, NULL, &op->o_req_ndn,
370 slap_schema.si_ad_syncreplCookie, &cookie );
372 ber_dupbv( &cookie_bv, &cookie[0] );
373 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
374 slap_parse_sync_cookie( &si->si_syncCookie );
375 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
380 rc = ldap_sync_search( si, op->o_tmpmemctx );
382 if( rc != LDAP_SUCCESS ) {
384 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
385 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
387 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
388 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
395 ldap_unbind( si->si_ld );
408 LDAPControl **rctrls = NULL;
411 BerElementBuffer berbuf;
412 BerElement *ber = (BerElement *)&berbuf;
414 LDAPMessage *res = NULL;
415 LDAPMessage *msg = NULL;
418 struct berval *retdata = NULL;
423 struct berval syncUUID = { 0, NULL };
424 struct sync_cookie syncCookie = { NULL, -1, NULL };
425 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
426 struct berval cookie = { 0, NULL };
437 Modifications *modlist = NULL;
442 struct timeval *tout_p = NULL;
443 struct timeval tout = { 0, 0 };
445 int refreshDeletes = 0;
450 if ( slapd_abrupt_shutdown ) {
456 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
458 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
461 op->o_callback = &cb;
463 psub = &si->si_be->be_nsuffix[0];
465 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
467 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
473 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
476 if ( slapd_abrupt_shutdown ) {
480 for( msg = ldap_first_message( si->si_ld, res );
482 msg = ldap_next_message( si->si_ld, msg ) )
484 switch( ldap_msgtype( msg ) ) {
485 case LDAP_RES_SEARCH_ENTRY:
486 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
487 /* we can't work without the control */
493 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
494 ber_scanf( ber, "{em", &syncstate, &syncUUID );
495 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
496 ber_scanf( ber, "m}", &cookie );
497 if ( cookie.bv_val ) {
498 struct berval tmp_bv;
499 ber_dupbv( &tmp_bv, &cookie );
500 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
502 if ( syncCookie.octet_str &&
503 syncCookie.octet_str[0].bv_val )
504 slap_parse_sync_cookie( &syncCookie );
506 entry = syncrepl_message_to_entry( si, op, msg,
507 &modlist, syncstate );
508 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
509 &syncUUID, &syncCookie_req );
510 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
511 syncrepl_updateCookie( si, op, psub, &syncCookie );
513 ldap_controls_free( rctrls );
515 slap_mods_free( modlist );
517 if ( rc_efree && entry ) {
522 case LDAP_RES_SEARCH_REFERENCE:
524 LDAP_LOG( OPERATION, ERR,
525 "do_syncrep2 : reference received\n", 0, 0, 0 );
527 Debug( LDAP_DEBUG_ANY,
528 "do_syncrep2 : reference received\n", 0, 0, 0 );
532 case LDAP_RES_SEARCH_RESULT:
533 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
537 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
539 ber_scanf( ber, "{" /*"}"*/);
540 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
542 ber_scanf( ber, "m", &cookie );
543 if ( cookie.bv_val ) {
544 struct berval tmp_bv;
545 ber_dupbv( &tmp_bv, &cookie );
546 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
548 if ( syncCookie.octet_str &&
549 syncCookie.octet_str[0].bv_val )
550 slap_parse_sync_cookie( &syncCookie );
552 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
554 ber_scanf( ber, "b", &refreshDeletes );
556 ber_scanf( ber, "}" );
558 if ( syncCookie_req.ctxcsn == NULL ) {
560 } else if ( syncCookie.ctxcsn == NULL ) {
563 value_match( &match, slap_schema.si_ad_entryCSN,
564 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
565 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
566 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
568 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
570 syncrepl_updateCookie( si, op, psub, &syncCookie );
573 ldap_controls_free( rctrls );
575 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
576 /* FIXME : different error behaviors according to
577 * 1) err code : LDAP_BUSY ...
578 * 2) on err policy : stop service, stop sync, retry
580 if ( refreshDeletes == 0 && match < 0 ) {
581 syncrepl_del_nonpresent( op, si );
583 avl_free( si->si_presentlist, avl_ber_bvfree );
584 si->si_presentlist = NULL;
591 case LDAP_RES_INTERMEDIATE:
592 rc = ldap_parse_intermediate( si->si_ld, msg,
593 &retoid, &retdata, NULL, 0 );
594 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
595 int si_refreshDelete = 0;
596 int si_refreshPresent = 0;
597 ber_init2( ber, retdata, LBER_USE_DER );
599 switch ( si_tag = ber_peek_tag( ber, &len )) {
601 case LDAP_TAG_SYNC_NEW_COOKIE:
602 ber_scanf( ber, "tm", &tag, &cookie );
604 case LDAP_TAG_SYNC_REFRESH_DELETE:
605 si_refreshDelete = 1;
606 case LDAP_TAG_SYNC_REFRESH_PRESENT:
607 si_refreshPresent = 1;
608 ber_scanf( ber, "t{", &tag );
609 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
611 ber_scanf( ber, "m", &cookie );
612 if ( cookie.bv_val ) {
613 struct berval tmp_bv;
614 ber_dupbv( &tmp_bv, &cookie );
615 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
617 if ( syncCookie.octet_str &&
618 syncCookie.octet_str[0].bv_val )
619 slap_parse_sync_cookie( &syncCookie );
621 if ( ber_peek_tag( ber, &len ) ==
622 LDAP_TAG_REFRESHDONE )
624 ber_scanf( ber, "b", &refreshDone );
626 ber_scanf( ber, "}" );
628 case LDAP_TAG_SYNC_ID_SET:
629 /* FIXME : to be supported */
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_REFRESHDELETES )
646 ber_scanf( ber, "b", &refreshDeletes );
648 ber_scanf( ber, "[W]", &syncUUIDs );
649 ber_scanf( ber, "}" );
653 LDAP_LOG( OPERATION, ERR,
654 "do_syncrep2 : unknown syncinfo tag (%d)\n",
657 Debug( LDAP_DEBUG_ANY,
658 "do_syncrep2 : unknown syncinfo tag (%d)\n",
661 ldap_memfree( retoid );
662 ber_bvfree( retdata );
666 if ( syncCookie_req.ctxcsn == NULL ) {
668 } else if ( syncCookie.ctxcsn == NULL ) {
671 value_match( &match, slap_schema.si_ad_entryCSN,
672 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
673 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
674 &syncCookie_req.ctxcsn[0],
675 &syncCookie.ctxcsn[0], &text );
678 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
680 syncrepl_updateCookie( si, op, psub, &syncCookie);
683 if ( si_refreshPresent == 1 ) {
685 syncrepl_del_nonpresent( op, si );
689 ldap_memfree( retoid );
690 ber_bvfree( retdata );
694 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
695 " unknown intermediate "
696 "response\n", 0, 0, 0 );
698 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
699 "unknown intermediate response (%d)\n",
702 ldap_memfree( retoid );
703 ber_bvfree( retdata );
709 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
710 "unknown message\n", 0, 0, 0 );
712 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
713 "unknown message\n", 0, 0, 0 );
718 if ( syncCookie.octet_str ) {
719 slap_sync_cookie_free( &syncCookie_req, 0 );
720 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
721 slap_sync_cookie_free( &syncCookie, 0 );
731 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
732 errstr = ldap_err2string( rc );
735 LDAP_LOG( OPERATION, ERR,
736 "do_syncrep2 : %s\n", errstr, 0, 0 );
738 Debug( LDAP_DEBUG_ANY,
739 "do_syncrep2 : %s\n", errstr, 0, 0 );
744 slap_sync_cookie_free( &syncCookie, 0 );
745 slap_sync_cookie_free( &syncCookie_req, 0 );
747 if ( res ) ldap_msgfree( res );
749 if ( rc && si->si_ld ) {
750 ldap_unbind( si->si_ld );
762 struct re_s* rtask = arg;
763 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
764 Connection conn = {0};
766 int rc = LDAP_SUCCESS;
772 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
774 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
780 switch( abs( si->si_type )) {
781 case LDAP_SYNC_REFRESH_ONLY:
782 case LDAP_SYNC_REFRESH_AND_PERSIST:
788 if ( slapd_abrupt_shutdown && si->si_ld ) {
789 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
790 connection_client_stop( s );
791 ldap_unbind( si->si_ld );
797 conn.c_send_ldap_result = slap_send_ldap_result;
798 conn.c_send_search_entry = slap_send_search_entry;
799 conn.c_send_search_reference = slap_send_search_reference;
800 conn.c_listener = (Listener *)&dummy_list;
801 conn.c_peer_name = slap_empty_bv;
803 /* set memory context */
804 #define SLAB_SIZE 1048576
805 op.o_tmpmemctx = sl_mem_create( SLAB_SIZE, ctx );
806 op.o_tmpmfuncs = &sl_mfuncs;
808 op.o_dn = si->si_updatedn;
809 op.o_ndn = si->si_updatedn;
810 op.o_time = slap_get_time();
811 op.o_threadctx = ctx;
812 op.o_managedsait = 1;
815 op.o_connid = op.o_conn->c_connid;
817 op.o_sync_state.ctxcsn = NULL;
818 op.o_sync_state.sid = -1;
819 op.o_sync_state.octet_str = NULL;
820 op.o_sync_slog_size = -1;
821 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
822 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
824 /* Establish session, do search */
827 rc = do_syncrep1( &op, si );
830 /* Process results */
831 if ( rc == LDAP_SUCCESS ) {
832 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
834 rc = do_syncrep2( &op, si );
836 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
837 /* If we succeeded, enable the connection for further listening.
838 * If we failed, tear down the connection and reschedule.
840 if ( rc == LDAP_SUCCESS ) {
842 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
845 connection_client_enable( s );
847 } else if ( !first ) {
851 if ( rc == -2 ) rc = 0;
855 /* At this point, we have 4 cases:
856 * 1) for any hard failure, give up and remove this task
857 * 2) for ServerDown, reschedule this task to run
858 * 3) for Refresh and Success, reschedule to run
859 * 4) for Persist and Success, reschedule to defer
861 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
862 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
863 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
867 connection_client_stop( s );
870 if ( rc && rc != LDAP_SERVER_DOWN ) {
871 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
873 if ( rc == LDAP_SERVER_DOWN ||
874 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
879 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
881 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
887 syncrepl_message_to_entry(
891 Modifications **modlist,
896 BerElement *ber = NULL;
899 Modifications **modtail = modlist;
902 char txtbuf[SLAP_TEXT_BUFLEN];
903 size_t textlen = sizeof txtbuf;
905 struct berval bdn = {0, NULL}, dn, ndn;
910 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
912 LDAP_LOG( OPERATION, ERR,
913 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
915 Debug( LDAP_DEBUG_ANY,
916 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
921 op->o_tag = LDAP_REQ_ADD;
923 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
925 if ( rc != LDAP_SUCCESS ) {
927 LDAP_LOG( OPERATION, ERR,
928 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
930 Debug( LDAP_DEBUG_ANY,
931 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
936 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
937 ber_dupbv( &op->o_req_dn, &dn );
938 ber_dupbv( &op->o_req_ndn, &ndn );
939 sl_free( ndn.bv_val, op->o_tmpmemctx );
940 sl_free( dn.bv_val, op->o_tmpmemctx );
942 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
946 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
947 e->e_name = op->o_req_dn;
948 e->e_nname = op->o_req_ndn;
950 while ( ber_remaining( ber ) ) {
951 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
952 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
957 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
959 mod->sml_op = LDAP_MOD_REPLACE;
960 mod->sml_next = NULL;
961 mod->sml_desc = NULL;
962 mod->sml_type = tmp.sml_type;
963 mod->sml_bvalues = tmp.sml_bvalues;
964 mod->sml_nvalues = NULL;
967 modtail = &mod->sml_next;
970 if ( *modlist == NULL ) {
972 LDAP_LOG( OPERATION, ERR,
973 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
975 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
980 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
982 if ( rc != LDAP_SUCCESS ) {
984 LDAP_LOG( OPERATION, ERR,
985 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
987 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
993 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
994 if( rc != LDAP_SUCCESS ) {
996 LDAP_LOG( OPERATION, ERR,
997 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
999 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1005 ber_free ( ber, 0 );
1006 if ( rc != LDAP_SUCCESS ) {
1015 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1017 const struct berval *uuid1 = v_uuid1;
1018 const struct berval *uuid2 = v_uuid2;
1019 int rc = uuid1->bv_len - uuid2->bv_len;
1020 if ( rc ) return rc;
1021 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1029 Modifications* modlist,
1031 struct berval* syncUUID,
1032 struct sync_cookie* syncCookie_req
1035 Backend *be = op->o_bd;
1037 struct berval *syncuuid_bv = NULL;
1038 struct berval syncUUID_strrep = { 0, NULL };
1040 SlapReply rs = {REP_RESULT};
1042 AttributeAssertion ava = {0};
1043 int rc = LDAP_SUCCESS;
1044 int ret = LDAP_SUCCESS;
1047 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1049 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1050 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1051 syncuuid_cmp, avl_dup_error );
1054 if ( syncstate == LDAP_SYNC_PRESENT ) {
1058 f.f_choice = LDAP_FILTER_EQUALITY;
1060 ava.aa_desc = slap_schema.si_ad_entryUUID;
1061 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1062 ava.aa_value = *syncUUID;
1063 op->ors_filter = &f;
1065 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1066 op->ors_filterstr.bv_val = (char *) sl_malloc(
1067 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1068 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1069 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1070 syncUUID->bv_val, syncUUID->bv_len );
1071 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1073 op->ors_scope = LDAP_SCOPE_SUBTREE;
1075 /* get syncrepl cookie of shadow replica from subentry */
1076 op->o_req_dn = si->si_base;
1077 op->o_req_ndn = si->si_base;
1079 /* set callback function */
1080 op->o_callback = &cb;
1081 cb.sc_response = dn_callback;
1084 si->si_syncUUID_ndn.bv_val = NULL;
1086 rc = be->be_search( op, &rs );
1088 if ( op->ors_filterstr.bv_val ) {
1089 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1092 cb.sc_response = null_callback;
1095 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1099 if ( syncstate != LDAP_SYNC_DELETE ) {
1100 op->o_no_psearch = 1;
1103 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1104 if ( op->o_sync_csn.bv_val ) {
1105 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1110 op->o_req_dn = si->si_syncUUID_ndn;
1111 op->o_req_ndn = si->si_syncUUID_ndn;
1112 op->o_tag = LDAP_REQ_DELETE;
1113 rc = be->be_delete( op, &rs );
1114 op->o_no_psearch = 0;
1117 switch ( syncstate ) {
1119 case LDAP_SYNC_MODIFY:
1120 if ( rc == LDAP_SUCCESS ||
1121 rc == LDAP_REFERRAL ||
1122 rc == LDAP_NO_SUCH_OBJECT )
1124 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1125 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1126 syncUUID, &ava.aa_value );
1128 op->o_tag = LDAP_REQ_ADD;
1130 op->o_req_dn = e->e_name;
1131 op->o_req_ndn = e->e_nname;
1132 rc = be->be_add( op, &rs );
1134 if ( rc != LDAP_SUCCESS ) {
1135 if ( rc == LDAP_ALREADY_EXISTS ) {
1136 op->o_tag = LDAP_REQ_MODIFY;
1137 op->orm_modlist = modlist;
1138 op->o_req_dn = e->e_name;
1139 op->o_req_ndn = e->e_nname;
1140 rc = be->be_modify( op, &rs );
1141 if ( rc != LDAP_SUCCESS ) {
1143 LDAP_LOG( OPERATION, ERR,
1144 "syncrepl_entry : be_modify failed (%d)\n",
1147 Debug( LDAP_DEBUG_ANY,
1148 "syncrepl_entry : be_modify failed (%d)\n",
1154 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1155 syncrepl_add_glue( op, e );
1160 LDAP_LOG( OPERATION, ERR,
1161 "syncrepl_entry : be_add failed (%d)\n",
1164 Debug( LDAP_DEBUG_ANY,
1165 "syncrepl_entry : be_add failed (%d)\n",
1172 be_entry_release_w( op, e );
1178 LDAP_LOG( OPERATION, ERR,
1179 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1181 Debug( LDAP_DEBUG_ANY,
1182 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1188 case LDAP_SYNC_DELETE :
1189 /* Already deleted */
1195 LDAP_LOG( OPERATION, ERR,
1196 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1198 Debug( LDAP_DEBUG_ANY,
1199 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1207 if ( syncUUID_strrep.bv_val ) {
1208 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1210 if ( si->si_syncUUID_ndn.bv_val ) {
1211 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1217 syncrepl_del_nonpresent(
1222 Backend* be = op->o_bd;
1224 SlapReply rs = {REP_RESULT};
1225 struct nonpresent_entry *np_list, *np_prev;
1227 op->o_req_dn = si->si_base;
1228 op->o_req_ndn = si->si_base;
1230 cb.sc_response = nonpresent_callback;
1233 op->o_callback = &cb;
1234 op->o_tag = LDAP_REQ_SEARCH;
1235 op->ors_scope = si->si_scope;
1236 op->ors_deref = LDAP_DEREF_NEVER;
1239 op->ors_attrsonly = 0;
1240 op->ors_attrs = NULL;
1241 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1242 op->ors_filterstr = si->si_filterstr;
1244 op->o_nocaching = 1;
1245 be->be_search( op, &rs );
1246 op->o_nocaching = 0;
1248 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1250 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1251 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1252 while ( np_list != NULL ) {
1253 LDAP_LIST_REMOVE( np_list, npe_link );
1255 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1256 op->o_tag = LDAP_REQ_DELETE;
1257 op->o_callback = &cb;
1258 cb.sc_response = null_callback;
1260 op->o_req_dn = *np_prev->npe_name;
1261 op->o_req_ndn = *np_prev->npe_nname;
1262 op->o_bd->be_delete( op, &rs );
1263 ber_bvfree( np_prev->npe_name );
1264 ber_bvfree( np_prev->npe_nname );
1265 op->o_req_dn.bv_val = NULL;
1266 op->o_req_ndn.bv_val = NULL;
1275 static struct berval gcbva[] = {
1286 Backend *be = op->o_bd;
1292 struct berval dn = {0, NULL};
1293 struct berval ndn = {0, NULL};
1295 SlapReply rs = {REP_RESULT};
1298 op->o_tag = LDAP_REQ_ADD;
1299 op->o_callback = &cb;
1300 cb.sc_response = null_callback;
1301 cb.sc_private = NULL;
1306 /* count RDNs in suffix */
1307 if ( be->be_nsuffix[0].bv_len ) {
1308 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1318 /* Start with BE suffix */
1319 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1320 comma = strrchr(dn.bv_val, ',');
1321 if ( ptr ) *ptr = ',';
1322 if ( comma ) *comma = '\0';
1327 dn.bv_len -= ptr - dn.bv_val;
1330 /* the normalizedDNs are always the same length, no counting
1333 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1334 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1335 ndn.bv_len = be->be_nsuffix[0].bv_len;
1338 while ( ndn.bv_val > e->e_nname.bv_val ) {
1339 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1340 ber_dupbv( &glue->e_name, &dn );
1341 ber_dupbv( &glue->e_nname, &ndn );
1343 a = ch_calloc( 1, sizeof( Attribute ));
1344 a->a_desc = slap_schema.si_ad_objectClass;
1346 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1347 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1348 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1349 a->a_vals[2].bv_len = 0;
1350 a->a_vals[2].bv_val = NULL;
1352 a->a_nvals = a->a_vals;
1354 a->a_next = glue->e_attrs;
1357 a = ch_calloc( 1, sizeof( Attribute ));
1358 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1360 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1361 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1362 a->a_vals[1].bv_len = 0;
1363 a->a_vals[1].bv_val = NULL;
1365 a->a_nvals = a->a_vals;
1367 a->a_next = glue->e_attrs;
1370 op->o_req_dn = glue->e_name;
1371 op->o_req_ndn = glue->e_nname;
1373 rc = be->be_add ( op, &rs );
1374 if ( rc == LDAP_SUCCESS ) {
1375 be_entry_release_w( op, glue );
1377 /* incl. ALREADY EXIST */
1381 /* Move to next child */
1382 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1385 if ( ptr == e->e_name.bv_val ) break;
1387 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1388 for( ptr = ndn.bv_val-2;
1389 ptr > e->e_nname.bv_val && *ptr != ',';
1395 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1398 op->o_req_dn = e->e_name;
1399 op->o_req_ndn = e->e_nname;
1401 rc = be->be_add ( op, &rs );
1402 if ( rc == LDAP_SUCCESS ) {
1403 be_entry_release_w( op, e );
1411 static struct berval ocbva[] = {
1413 BER_BVC("subentry"),
1414 BER_BVC("syncConsumerSubentry"),
1418 static struct berval cnbva[] = {
1423 static struct berval ssbva[] = {
1428 static struct berval scbva[] = {
1434 syncrepl_updateCookie(
1438 struct sync_cookie *syncCookie
1441 Backend *be = op->o_bd;
1443 Modifications *mlnext;
1445 Modifications *modlist = NULL;
1446 Modifications **modtail = &modlist;
1449 char txtbuf[SLAP_TEXT_BUFLEN];
1450 size_t textlen = sizeof txtbuf;
1455 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1456 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1457 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1460 SlapReply rs = {REP_RESULT};
1462 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1463 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1465 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1466 mod->sml_op = LDAP_MOD_REPLACE;
1467 mod->sml_desc = slap_schema.si_ad_objectClass;
1468 mod->sml_type = mod->sml_desc->ad_cname;
1469 mod->sml_bvalues = ocbva;
1471 modtail = &mod->sml_next;
1473 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1474 assert( si->si_id < 1000 );
1475 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1476 slap_syncrepl_bvc.bv_len,
1477 "syncrepl%d", si->si_id );
1478 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1479 mod->sml_op = LDAP_MOD_REPLACE;
1480 mod->sml_desc = slap_schema.si_ad_cn;
1481 mod->sml_type = mod->sml_desc->ad_cname;
1482 mod->sml_bvalues = cnbva;
1484 modtail = &mod->sml_next;
1486 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1487 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1488 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1489 mod->sml_op = LDAP_MOD_REPLACE;
1490 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1491 mod->sml_type = mod->sml_desc->ad_cname;
1492 mod->sml_bvalues = scbva;
1494 modtail = &mod->sml_next;
1496 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1497 mod->sml_op = LDAP_MOD_REPLACE;
1498 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1499 mod->sml_type = mod->sml_desc->ad_cname;
1500 mod->sml_bvalues = ssbva;
1502 modtail = &mod->sml_next;
1506 op->o_tag = LDAP_REQ_ADD;
1507 rc = slap_mods_opattrs( op, modlist, modtail,
1508 &text,txtbuf, textlen );
1510 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1511 ml->sml_op = LDAP_MOD_REPLACE;
1514 if( rc != LDAP_SUCCESS ) {
1516 LDAP_LOG( OPERATION, ERR,
1517 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1519 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1524 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1526 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1527 assert( si->si_id < 1000 );
1528 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1529 slap_syncrepl_cn_bvc.bv_len,
1530 "cn=syncrepl%d", si->si_id );
1532 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1534 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1535 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1537 if ( slap_syncrepl_dn_bv.bv_val ) {
1538 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1543 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1545 if( rc != LDAP_SUCCESS ) {
1547 LDAP_LOG( OPERATION, ERR,
1548 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1550 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1555 cb.sc_response = null_callback;
1558 op->o_callback = &cb;
1559 op->o_req_dn = e->e_name;
1560 op->o_req_ndn = e->e_nname;
1562 /* update persistent cookie */
1563 update_cookie_retry:
1564 op->o_tag = LDAP_REQ_MODIFY;
1565 op->orm_modlist = modlist;
1566 rc = be->be_modify( op, &rs );
1568 if ( rc != LDAP_SUCCESS ) {
1569 if ( rc == LDAP_REFERRAL ||
1570 rc == LDAP_NO_SUCH_OBJECT ) {
1571 op->o_tag = LDAP_REQ_ADD;
1573 rc = be->be_add( op, &rs );
1574 if ( rc != LDAP_SUCCESS ) {
1575 if ( rc == LDAP_ALREADY_EXISTS ) {
1576 goto update_cookie_retry;
1577 } else if ( rc == LDAP_REFERRAL ||
1578 rc == LDAP_NO_SUCH_OBJECT ) {
1580 LDAP_LOG( OPERATION, ERR,
1581 "cookie will be non-persistent\n",
1584 Debug( LDAP_DEBUG_ANY,
1585 "cookie will be non-persistent\n",
1590 LDAP_LOG( OPERATION, ERR,
1591 "be_add failed (%d)\n",
1594 Debug( LDAP_DEBUG_ANY,
1595 "be_add failed (%d)\n",
1600 be_entry_release_w( op, e );
1605 LDAP_LOG( OPERATION, ERR,
1606 "be_modify failed (%d)\n", rc, 0, 0 );
1608 Debug( LDAP_DEBUG_ANY,
1609 "be_modify failed (%d)\n", rc, 0, 0 );
1620 if ( cnbva[0].bv_val ) {
1621 ch_free( cnbva[0].bv_val );
1622 cnbva[0].bv_val = NULL;
1624 if ( scbva[0].bv_val ) {
1625 ch_free( scbva[0].bv_val );
1626 scbva[0].bv_val = NULL;
1629 if ( mlnext->sml_next ) {
1630 slap_mods_free( mlnext->sml_next );
1631 mlnext->sml_next = NULL;
1634 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1635 mlnext = ml->sml_next;
1648 syncinfo_t *si = op->o_callback->sc_private;
1650 if ( rs->sr_type == REP_SEARCH ) {
1651 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1653 LDAP_LOG( OPERATION, ERR,
1654 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1656 Debug( LDAP_DEBUG_ANY,
1657 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1660 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1664 return LDAP_SUCCESS;
1668 nonpresent_callback(
1673 syncinfo_t *si = op->o_callback->sc_private;
1676 struct berval* present_uuid = NULL;
1677 struct nonpresent_entry *np_entry;
1679 if ( rs->sr_type == REP_RESULT ) {
1680 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1681 si->si_presentlist = NULL;
1683 } else if ( rs->sr_type == REP_SEARCH ) {
1684 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1686 if ( a == NULL ) return 0;
1688 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1691 if ( present_uuid == NULL ) {
1692 np_entry = (struct nonpresent_entry *)
1693 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1694 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1695 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1696 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1699 avl_delete( &si->si_presentlist,
1700 &a->a_vals[0], syncuuid_cmp );
1701 ch_free( present_uuid->bv_val );
1702 ch_free( present_uuid );
1705 return LDAP_SUCCESS;
1714 if ( rs->sr_err != LDAP_SUCCESS &&
1715 rs->sr_err != LDAP_REFERRAL &&
1716 rs->sr_err != LDAP_ALREADY_EXISTS &&
1717 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1720 LDAP_LOG( OPERATION, ERR,
1721 "null_callback : error code 0x%x\n",
1724 Debug( LDAP_DEBUG_ANY,
1725 "null_callback : error code 0x%x\n",
1729 return LDAP_SUCCESS;
1733 slap_create_syncrepl_entry(
1735 struct berval *context_csn,
1744 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1746 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1748 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1751 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1753 if ( context_csn ) {
1754 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1755 context_csn, NULL );
1759 bv.bv_len = sizeof("{}")-1;
1760 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1762 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1763 ber_dupbv( &e->e_nname, &e->e_name );
1769 slap_uuidstr_from_normalized(
1770 struct berval* uuidstr,
1771 struct berval* normalized,
1775 unsigned char nibble;
1778 if ( normalized == NULL )
1781 if ( normalized->bv_len != 16 ) {
1788 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1793 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1795 sl_free( new, ctx );
1799 for ( i = 0; i < 16; i++ ) {
1800 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1801 new->bv_val[(i<<1)+d] = '-';
1805 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1806 if ( nibble < 10 ) {
1807 new->bv_val[(i<<1)+d] = nibble + '0';
1809 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1812 nibble = (normalized->bv_val[i]) & 0xF;
1813 if ( nibble < 10 ) {
1814 new->bv_val[(i<<1)+d+1] = nibble + '0';
1816 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1820 new->bv_val[new->bv_len] = '\0';
1826 avl_ber_bvfree( void *bv )
1831 if ( ((struct berval *)bv)->bv_val != NULL ) {
1832 ch_free ( ((struct berval *)bv)->bv_val );
1834 ch_free ( (char *) bv );