1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
29 #include "lutil_ldap.h"
33 #define SYNCREPL_STR "syncreplxxx"
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
48 static AttributeDescription *sync_descs[4];
50 struct runqueue_s syncrepl_rq;
53 init_syncrepl(syncinfo_t *si)
58 if ( !sync_descs[0] ) {
59 sync_descs[0] = slap_schema.si_ad_objectClass;
60 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61 sync_descs[2] = slap_schema.si_ad_entryCSN;
65 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
68 /* Delete Attributes */
69 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
74 ch_free( si->si_attrs[j] );
75 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 si->si_attrs[k] = si->si_attrs[k+1];
81 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
85 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
87 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
91 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
94 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
96 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
99 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
118 BerElementBuffer berbuf;
119 BerElement *ber = (BerElement *)&berbuf;
120 LDAPControl c[2], *ctrls[3];
121 struct timeval timeout;
125 /* setup LDAP SYNC control */
126 ber_init2( ber, NULL, LBER_USE_DER );
127 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
129 if ( si->si_syncCookie.octet_str &&
130 si->si_syncCookie.octet_str[0].bv_val ) {
131 ber_printf( ber, "{eO}", abs(si->si_type),
132 &si->si_syncCookie.octet_str[0] );
134 ber_printf( ber, "{e}", abs(si->si_type) );
137 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
142 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143 c[0].ldctl_iscritical = si->si_type < 0;
146 if ( si->si_authzId ) {
147 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149 c[1].ldctl_iscritical = 1;
156 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
159 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
162 si->si_slimit, &msgid );
174 int cmdline_cookie_found = 0;
176 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
177 struct berval syncrepl_cn_bv;
178 struct sync_cookie *sc = NULL;
179 struct sync_cookie syncCookie = { NULL, -1, NULL };
182 psub = &si->si_be->be_nsuffix[0];
184 /* Init connection to master */
186 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
187 if ( rc != LDAP_SUCCESS ) {
189 LDAP_LOG( OPERATION, ERR,
190 "do_syncrep1: ldap_initialize failed (%s)\n",
191 si->si_provideruri, 0, 0 );
193 Debug( LDAP_DEBUG_ANY,
194 "do_syncrep1: ldap_initialize failed (%s)\n",
195 si->si_provideruri, 0, 0 );
200 op->o_protocol = LDAP_VERSION3;
201 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
206 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
207 if( rc != LDAP_SUCCESS ) {
209 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
210 "%s: ldap_start_tls failed (%d)\n",
211 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
214 Debug( LDAP_DEBUG_ANY,
215 "%s: ldap_start_tls failed (%d)\n",
216 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
219 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
223 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
224 #ifdef HAVE_CYRUS_SASL
227 if ( si->si_secprops != NULL ) {
228 rc = ldap_set_option( si->si_ld,
229 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
231 if( rc != LDAP_OPT_SUCCESS ) {
233 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
234 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
235 si->si_provideruri, si->si_secprops, 0 );
237 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
238 "(%s,SECPROPS,\"%s\") failed!\n",
239 si->si_provideruri, si->si_secprops, 0 );
245 defaults = lutil_sasl_defaults( si->si_ld,
246 si->si_saslmech, si->si_realm,
247 si->si_authcId, si->si_passwd, si->si_authzId );
249 rc = ldap_sasl_interactive_bind_s( si->si_ld,
257 lutil_sasl_freedefs( defaults );
259 /* FIXME : different error behaviors according to
261 * 2) on err policy : exit, retry, backoff ...
263 if ( rc != LDAP_SUCCESS ) {
265 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
266 "ldap_sasl_interactive_bind_s failed (%d)\n",
269 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
270 "ldap_sasl_interactive_bind_s failed (%d)\n",
275 #else /* HAVE_CYRUS_SASL */
276 /* Should never get here, we trapped this at config time */
277 fprintf( stderr, "not compiled with SASL support\n" );
282 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
283 if ( rc != LDAP_SUCCESS ) {
285 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
286 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
288 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
289 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
295 /* get syncrepl cookie of shadow replica from subentry */
297 assert( si->si_rid < 1000 );
298 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
299 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
300 CN_STR "syncrepl%ld", si->si_rid );
301 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
302 op->o_req_dn = op->o_req_ndn;
304 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
305 if ( si->si_rid == sc->rid ) {
306 cmdline_cookie_found = 1;
311 if ( cmdline_cookie_found ) {
312 /* cookie is supplied in the command line */
313 BerVarray cookie = NULL;
314 struct berval cookie_bv;
316 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
317 slap_sync_cookie_free( &si->si_syncCookie, 0 );
319 /* read stored cookie if it exists */
320 backend_attribute( op, NULL, &op->o_req_ndn,
321 slap_schema.si_ad_syncreplCookie, &cookie );
324 /* no stored cookie */
325 if ( sc->ctxcsn == NULL ||
326 sc->ctxcsn->bv_val == NULL ) {
327 /* if cmdline cookie does not have ctxcsn */
328 /* component, set it to an initial value */
329 slap_init_sync_cookie_ctxcsn( sc );
331 slap_dup_sync_cookie( &si->si_syncCookie, sc );
332 slap_sync_cookie_free( sc, 1 );
336 struct berval newcookie = { 0, NULL };
337 ber_dupbv( &cookie_bv, &cookie[0] );
338 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
339 slap_parse_sync_cookie( &si->si_syncCookie );
340 ber_bvarray_free( si->si_syncCookie.octet_str );
341 si->si_syncCookie.octet_str = NULL;
342 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
343 if ( sc->sid != -1 ) {
344 /* command line cookie wins */
345 si->si_syncCookie.sid = sc->sid;
347 if ( sc->ctxcsn != NULL ) {
348 /* command line cookie wins */
349 if ( si->si_syncCookie.ctxcsn ) {
350 ber_bvarray_free( si->si_syncCookie.ctxcsn );
351 si->si_syncCookie.ctxcsn = NULL;
353 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
354 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
356 if ( sc->rid != -1 ) {
357 /* command line cookie wins */
358 si->si_syncCookie.rid = sc->rid;
360 slap_sync_cookie_free( sc, 1 );
362 slap_compose_sync_cookie( NULL, &newcookie,
363 &si->si_syncCookie.ctxcsn[0],
364 si->si_syncCookie.sid, si->si_syncCookie.rid );
365 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
368 /* no command line cookie is specified */
369 if ( si->si_syncCookie.octet_str == NULL ) {
370 BerVarray cookie = NULL;
371 struct berval cookie_bv;
372 /* try to read stored cookie */
373 backend_attribute( op, NULL, &op->o_req_ndn,
374 slap_schema.si_ad_syncreplCookie, &cookie );
376 ber_dupbv( &cookie_bv, &cookie[0] );
377 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
378 slap_parse_sync_cookie( &si->si_syncCookie );
379 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
384 rc = ldap_sync_search( si, op->o_tmpmemctx );
386 if( rc != LDAP_SUCCESS ) {
388 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
389 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
391 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
392 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
399 ldap_unbind( si->si_ld );
412 LDAPControl **rctrls = NULL;
415 BerElementBuffer berbuf;
416 BerElement *ber = (BerElement *)&berbuf;
418 LDAPMessage *res = NULL;
419 LDAPMessage *msg = NULL;
422 struct berval *retdata = NULL;
427 struct berval syncUUID = { 0, NULL };
428 struct sync_cookie syncCookie = { NULL, -1, NULL };
429 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
430 struct berval cookie = { 0, NULL };
438 Modifications *modlist = NULL;
443 struct timeval *tout_p = NULL;
444 struct timeval tout = { 0, 0 };
446 int refreshDeletes = 0;
448 BerVarray syncUUIDs = NULL;
451 if ( slapd_shutdown ) {
456 ber_init2( ber, NULL, LBER_USE_DER );
457 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
460 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
462 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
465 psub = &si->si_be->be_nsuffix[0];
467 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
469 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
475 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
478 if ( slapd_shutdown ) {
482 for( msg = ldap_first_message( si->si_ld, res );
484 msg = ldap_next_message( si->si_ld, msg ) )
486 switch( ldap_msgtype( msg ) ) {
487 case LDAP_RES_SEARCH_ENTRY:
488 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
489 /* we can't work without the control */
495 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
496 ber_scanf( ber, "{em", &syncstate, &syncUUID );
497 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
498 ber_scanf( ber, "m}", &cookie );
499 if ( cookie.bv_val ) {
500 struct berval tmp_bv;
501 ber_dupbv( &tmp_bv, &cookie );
502 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
504 if ( syncCookie.octet_str &&
505 syncCookie.octet_str[0].bv_val )
506 slap_parse_sync_cookie( &syncCookie );
508 if ( syncrepl_message_to_entry( si, op, msg,
509 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
510 rc_efree = syncrepl_entry( si, op, entry, modlist,
511 syncstate, &syncUUID, &syncCookie_req );
512 if ( syncCookie.octet_str &&
513 syncCookie.octet_str[0].bv_val ) {
514 syncrepl_updateCookie( si, op, psub, &syncCookie );
517 ldap_controls_free( rctrls );
519 slap_mods_free( modlist );
521 if ( rc_efree && entry ) {
527 case LDAP_RES_SEARCH_REFERENCE:
529 LDAP_LOG( OPERATION, ERR,
530 "do_syncrep2 : reference received\n", 0, 0, 0 );
532 Debug( LDAP_DEBUG_ANY,
533 "do_syncrep2 : reference received\n", 0, 0, 0 );
537 case LDAP_RES_SEARCH_RESULT:
538 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
542 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
544 ber_scanf( ber, "{" /*"}"*/);
545 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
547 ber_scanf( ber, "m", &cookie );
548 if ( cookie.bv_val ) {
549 struct berval tmp_bv;
550 ber_dupbv( &tmp_bv, &cookie );
551 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
553 if ( syncCookie.octet_str &&
554 syncCookie.octet_str[0].bv_val )
555 slap_parse_sync_cookie( &syncCookie );
557 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
559 ber_scanf( ber, "b", &refreshDeletes );
561 ber_scanf( ber, "}" );
563 if ( syncCookie_req.ctxcsn == NULL ) {
565 } else if ( syncCookie.ctxcsn == NULL ) {
568 value_match( &match, slap_schema.si_ad_entryCSN,
569 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
570 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
571 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
573 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
574 && match < 0 && err == LDAP_SUCCESS ) {
575 syncrepl_updateCookie( si, op, psub, &syncCookie );
578 ldap_controls_free( rctrls );
580 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
581 /* FIXME : different error behaviors according to
582 * 1) err code : LDAP_BUSY ...
583 * 2) on err policy : stop service, stop sync, retry
585 if ( refreshDeletes == 0 && match < 0 &&
586 err == LDAP_SUCCESS ) {
587 syncrepl_del_nonpresent( op, si );
589 avl_free( si->si_presentlist, avl_ber_bvfree );
590 si->si_presentlist = NULL;
597 case LDAP_RES_INTERMEDIATE:
598 rc = ldap_parse_intermediate( si->si_ld, msg,
599 &retoid, &retdata, NULL, 0 );
600 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
601 int si_refreshDelete = 0;
602 int si_refreshPresent = 0;
603 ber_init2( ber, retdata, LBER_USE_DER );
605 switch ( si_tag = ber_peek_tag( ber, &len )) {
607 case LDAP_TAG_SYNC_NEW_COOKIE:
608 ber_scanf( ber, "tm", &tag, &cookie );
610 case LDAP_TAG_SYNC_REFRESH_DELETE:
611 si_refreshDelete = 1;
612 case LDAP_TAG_SYNC_REFRESH_PRESENT:
613 si_refreshPresent = 1;
614 ber_scanf( ber, "t{", &tag );
615 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
617 ber_scanf( ber, "m", &cookie );
618 if ( cookie.bv_val ) {
619 struct berval tmp_bv;
620 ber_dupbv( &tmp_bv, &cookie );
621 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
623 if ( syncCookie.octet_str &&
624 syncCookie.octet_str[0].bv_val )
625 slap_parse_sync_cookie( &syncCookie );
627 if ( ber_peek_tag( ber, &len ) ==
628 LDAP_TAG_REFRESHDONE )
630 ber_scanf( ber, "b", &refreshDone );
632 ber_scanf( ber, "}" );
634 case LDAP_TAG_SYNC_ID_SET:
635 ber_scanf( ber, "t{", &tag );
636 if ( ber_peek_tag( ber, &len ) ==
637 LDAP_TAG_SYNC_COOKIE ) {
638 ber_scanf( ber, "m", &cookie );
639 if ( cookie.bv_val ) {
640 struct berval tmp_bv;
641 ber_dupbv( &tmp_bv, &cookie );
642 ber_bvarray_add( &syncCookie.octet_str,
645 if ( syncCookie.octet_str &&
646 syncCookie.octet_str[0].bv_val )
647 slap_parse_sync_cookie( &syncCookie );
649 if ( ber_peek_tag( ber, &len ) ==
650 LDAP_TAG_REFRESHDELETES )
652 ber_scanf( ber, "b", &refreshDeletes );
654 ber_scanf( ber, "[W]", &syncUUIDs );
655 ber_scanf( ber, "}" );
656 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
657 struct berval *syncuuid_bv;
658 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
659 avl_insert( &si->si_presentlist,
660 (caddr_t) syncuuid_bv,
661 syncuuid_cmp, avl_dup_error );
663 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
667 LDAP_LOG( OPERATION, ERR,
668 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
669 (long) si_tag, 0, 0 );
671 Debug( LDAP_DEBUG_ANY,
672 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
673 (long) si_tag, 0, 0 );
675 ldap_memfree( retoid );
676 ber_bvfree( retdata );
680 if ( syncCookie_req.ctxcsn == NULL ) {
682 } else if ( syncCookie.ctxcsn == NULL ) {
685 value_match( &match, slap_schema.si_ad_entryCSN,
686 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
687 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
688 &syncCookie_req.ctxcsn[0],
689 &syncCookie.ctxcsn[0], &text );
692 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
694 syncrepl_updateCookie( si, op, psub, &syncCookie);
697 if ( si_refreshPresent == 1 ) {
699 syncrepl_del_nonpresent( op, si );
703 ldap_memfree( retoid );
704 ber_bvfree( retdata );
708 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
709 " unknown intermediate "
710 "response\n", 0, 0, 0 );
712 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
713 "unknown intermediate response (%d)\n",
716 ldap_memfree( retoid );
717 ber_bvfree( retdata );
723 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
724 "unknown message\n", 0, 0, 0 );
726 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
727 "unknown message\n", 0, 0, 0 );
732 if ( syncCookie.octet_str ) {
733 slap_sync_cookie_free( &syncCookie_req, 0 );
734 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
735 slap_sync_cookie_free( &syncCookie, 0 );
745 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
746 errstr = ldap_err2string( rc );
749 LDAP_LOG( OPERATION, ERR,
750 "do_syncrep2 : %s\n", errstr, 0, 0 );
752 Debug( LDAP_DEBUG_ANY,
753 "do_syncrep2 : %s\n", errstr, 0, 0 );
758 slap_sync_cookie_free( &syncCookie, 0 );
759 slap_sync_cookie_free( &syncCookie_req, 0 );
761 if ( res ) ldap_msgfree( res );
763 if ( rc && si->si_ld ) {
764 ldap_unbind( si->si_ld );
776 struct re_s* rtask = arg;
777 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
778 Connection conn = {0};
780 int rc = LDAP_SUCCESS;
786 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
788 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
794 switch( abs( si->si_type )) {
795 case LDAP_SYNC_REFRESH_ONLY:
796 case LDAP_SYNC_REFRESH_AND_PERSIST:
802 if ( slapd_shutdown && si->si_ld ) {
803 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
804 connection_client_stop( s );
805 ldap_unbind( si->si_ld );
810 connection_fake_init( &conn, &op, ctx );
812 op.o_dn = si->si_updatedn;
813 op.o_ndn = si->si_updatedn;
814 op.o_managedsait = 1;
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, 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,
897 BerElement *ber = NULL;
900 Modifications **modtail = modlist;
903 char txtbuf[SLAP_TEXT_BUFLEN];
904 size_t textlen = sizeof txtbuf;
906 struct berval bdn = {0, NULL}, dn, ndn;
911 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
913 LDAP_LOG( OPERATION, ERR,
914 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
916 Debug( LDAP_DEBUG_ANY,
917 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
922 op->o_tag = LDAP_REQ_ADD;
924 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
926 if ( rc != LDAP_SUCCESS ) {
928 LDAP_LOG( OPERATION, ERR,
929 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
931 Debug( LDAP_DEBUG_ANY,
932 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
937 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
938 ber_dupbv( &op->o_req_dn, &dn );
939 ber_dupbv( &op->o_req_ndn, &ndn );
940 sl_free( ndn.bv_val, op->o_tmpmemctx );
941 sl_free( dn.bv_val, op->o_tmpmemctx );
943 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
948 if ( entry == NULL ) {
952 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
954 e->e_name = op->o_req_dn;
955 e->e_nname = op->o_req_ndn;
957 while ( ber_remaining( ber ) ) {
958 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
959 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
964 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
966 mod->sml_op = LDAP_MOD_REPLACE;
967 mod->sml_next = NULL;
968 mod->sml_desc = NULL;
969 mod->sml_type = tmp.sml_type;
970 mod->sml_bvalues = tmp.sml_bvalues;
971 mod->sml_nvalues = NULL;
974 modtail = &mod->sml_next;
977 if ( *modlist == NULL ) {
979 LDAP_LOG( OPERATION, ERR,
980 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
982 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
987 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
989 if ( rc != LDAP_SUCCESS ) {
991 LDAP_LOG( OPERATION, ERR,
992 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
994 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1000 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1001 if( rc != LDAP_SUCCESS ) {
1003 LDAP_LOG( OPERATION, ERR,
1004 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1006 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1012 ber_free ( ber, 0 );
1013 if ( rc != LDAP_SUCCESS ) {
1021 static struct berval uuidbva[] = {
1031 Modifications* modlist,
1033 struct berval* syncUUID,
1034 struct sync_cookie* syncCookie_req
1037 Backend *be = op->o_bd;
1038 slap_callback cb = { NULL };
1039 struct berval *syncuuid_bv = NULL;
1040 struct berval syncUUID_strrep = { 0, NULL };
1042 SlapReply rs = {REP_RESULT};
1044 AttributeAssertion ava = {0};
1045 int rc = LDAP_SUCCESS;
1046 int ret = LDAP_SUCCESS;
1049 struct berval pdn = { 0, NULL };
1050 struct berval org_req_dn = { 0, NULL };
1051 struct berval org_req_ndn = { 0, NULL };
1052 struct berval org_dn = { 0, NULL };
1053 struct berval org_ndn = { 0, NULL };
1054 int org_managedsait;
1056 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1058 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1059 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1060 syncuuid_cmp, avl_dup_error );
1063 if ( syncstate == LDAP_SYNC_PRESENT ) {
1067 f.f_choice = LDAP_FILTER_EQUALITY;
1069 ava.aa_desc = slap_schema.si_ad_entryUUID;
1070 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1071 ava.aa_value = *syncUUID;
1072 op->ors_filter = &f;
1074 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1075 op->ors_filterstr.bv_val = (char *) sl_malloc(
1076 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1077 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1078 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1079 syncUUID->bv_val, syncUUID->bv_len );
1080 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1082 op->o_tag = LDAP_REQ_SEARCH;
1083 op->ors_scope = LDAP_SCOPE_SUBTREE;
1085 /* get syncrepl cookie of shadow replica from subentry */
1086 op->o_req_dn = si->si_base;
1087 op->o_req_ndn = si->si_base;
1089 /* set callback function */
1090 op->o_callback = &cb;
1091 cb.sc_response = dn_callback;
1094 si->si_syncUUID_ndn.bv_val = NULL;
1096 if ( limits_check( op, &rs ) == 0 ) {
1097 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 );
1127 org_req_dn = op->o_req_dn;
1128 org_req_ndn = op->o_req_ndn;
1130 org_ndn = op->o_ndn;
1131 org_managedsait = get_manageDSAit( op );
1132 op->o_dn = op->o_bd->be_rootdn;
1133 op->o_ndn = op->o_bd->be_rootndn;
1134 op->o_managedsait = 1;
1136 while ( rs.sr_err == LDAP_SUCCESS &&
1137 op->o_delete_glue_parent ) {
1138 op->o_delete_glue_parent = 0;
1139 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1140 slap_callback cb = { NULL };
1141 cb.sc_response = slap_null_cb;
1142 dnParent( &op->o_req_ndn, &pdn );
1144 op->o_req_ndn = pdn;
1145 op->o_callback = &cb;
1146 op->o_bd->be_delete( op, &rs );
1152 op->o_managedsait = org_managedsait;
1154 op->o_ndn = org_ndn;
1155 op->o_req_dn = org_req_dn;
1156 op->o_req_ndn = org_req_ndn;
1157 op->o_delete_glue_parent = 0;
1159 op->o_no_psearch = 0;
1162 switch ( syncstate ) {
1164 case LDAP_SYNC_MODIFY:
1165 if ( rc == LDAP_SUCCESS ||
1166 rc == LDAP_REFERRAL ||
1167 rc == LDAP_NO_SUCH_OBJECT ||
1168 rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1170 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1171 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1172 syncUUID, &ava.aa_value );
1174 op->o_tag = LDAP_REQ_ADD;
1176 op->o_req_dn = e->e_name;
1177 op->o_req_ndn = e->e_nname;
1178 rc = be->be_add( op, &rs );
1180 if ( rc != LDAP_SUCCESS ) {
1181 if ( rc == LDAP_ALREADY_EXISTS ) {
1183 Modifications *modtail;
1185 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1189 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1190 ber_dupbv( &uuidbva[0], syncUUID );
1191 mod->sml_op = LDAP_MOD_REPLACE;
1192 mod->sml_desc = slap_schema.si_ad_entryUUID;
1193 mod->sml_type = mod->sml_desc->ad_cname;
1194 mod->sml_bvalues = uuidbva;
1195 modtail->sml_next = mod;
1197 op->o_tag = LDAP_REQ_MODIFY;
1198 op->orm_modlist = modlist;
1199 op->o_req_dn = e->e_name;
1200 op->o_req_ndn = e->e_nname;
1202 rc = be->be_modify( op, &rs );
1203 if ( rc != LDAP_SUCCESS ) {
1205 LDAP_LOG( OPERATION, ERR,
1206 "syncrepl_entry : be_modify failed (%d)\n",
1209 Debug( LDAP_DEBUG_ANY,
1210 "syncrepl_entry : be_modify failed (%d)\n",
1216 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1217 syncrepl_add_glue( op, e );
1222 LDAP_LOG( OPERATION, ERR,
1223 "syncrepl_entry : be_add failed (%d)\n",
1226 Debug( LDAP_DEBUG_ANY,
1227 "syncrepl_entry : be_add failed (%d)\n",
1234 be_entry_release_w( op, e );
1240 LDAP_LOG( OPERATION, ERR,
1241 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1243 Debug( LDAP_DEBUG_ANY,
1244 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1250 case LDAP_SYNC_DELETE :
1251 /* Already deleted */
1257 LDAP_LOG( OPERATION, ERR,
1258 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1260 Debug( LDAP_DEBUG_ANY,
1261 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1269 if ( syncUUID_strrep.bv_val ) {
1270 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1272 if ( si->si_syncUUID_ndn.bv_val ) {
1273 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1278 static struct berval gcbva[] = {
1285 syncrepl_del_nonpresent(
1290 Backend* be = op->o_bd;
1291 slap_callback cb = { NULL };
1292 SlapReply rs = {REP_RESULT};
1293 struct nonpresent_entry *np_list, *np_prev;
1296 Modifications *mlnext;
1298 Modifications *modlist = NULL;
1299 Modifications **modtail = &modlist;
1302 struct berval pdn = { 0, NULL };
1303 struct berval org_req_dn = { 0, NULL };
1304 struct berval org_req_ndn = { 0, NULL };
1305 struct berval org_dn = { 0, NULL };
1306 struct berval org_ndn = { 0, NULL };
1307 int org_managedsait;
1309 op->o_req_dn = si->si_base;
1310 op->o_req_ndn = si->si_base;
1312 cb.sc_response = nonpresent_callback;
1315 op->o_callback = &cb;
1316 op->o_tag = LDAP_REQ_SEARCH;
1317 op->ors_scope = si->si_scope;
1318 op->ors_deref = LDAP_DEREF_NEVER;
1321 op->ors_attrsonly = 0;
1322 op->ors_attrs = NULL;
1323 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1324 op->ors_filterstr = si->si_filterstr;
1326 op->o_nocaching = 1;
1327 op->o_managedsait = 0;
1329 if ( limits_check( op, &rs ) == 0 ) {
1330 be->be_search( op, &rs );
1333 op->o_managedsait = 1;
1334 op->o_nocaching = 0;
1336 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1338 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1339 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1340 while ( np_list != NULL ) {
1341 LDAP_LIST_REMOVE( np_list, npe_link );
1343 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1344 op->o_tag = LDAP_REQ_DELETE;
1345 op->o_callback = &cb;
1346 cb.sc_response = null_callback;
1348 op->o_req_dn = *np_prev->npe_name;
1349 op->o_req_ndn = *np_prev->npe_nname;
1350 rc = op->o_bd->be_delete( op, &rs );
1352 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1353 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1354 mod->sml_op = LDAP_MOD_REPLACE;
1355 mod->sml_desc = slap_schema.si_ad_objectClass;
1356 mod->sml_type = mod->sml_desc->ad_cname;
1357 mod->sml_bvalues = &gcbva[0];
1359 modtail = &mod->sml_next;
1361 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1362 mod->sml_op = LDAP_MOD_REPLACE;
1363 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1364 mod->sml_type = mod->sml_desc->ad_cname;
1365 mod->sml_bvalues = &gcbva[1];
1367 modtail = &mod->sml_next;
1369 op->o_tag = LDAP_REQ_MODIFY;
1370 op->orm_modlist = modlist;
1372 rc = be->be_modify( op, &rs );
1374 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1375 mlnext = ml->sml_next;
1380 org_req_dn = op->o_req_dn;
1381 org_req_ndn = op->o_req_ndn;
1383 org_ndn = op->o_ndn;
1384 org_managedsait = get_manageDSAit( op );
1385 op->o_dn = op->o_bd->be_rootdn;
1386 op->o_ndn = op->o_bd->be_rootndn;
1387 op->o_managedsait = 1;
1389 while ( rs.sr_err == LDAP_SUCCESS &&
1390 op->o_delete_glue_parent ) {
1391 op->o_delete_glue_parent = 0;
1392 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1393 slap_callback cb = { NULL };
1394 cb.sc_response = slap_null_cb;
1395 dnParent( &op->o_req_ndn, &pdn );
1397 op->o_req_ndn = pdn;
1398 op->o_callback = &cb;
1399 /* give it a root privil ? */
1400 op->o_bd->be_delete( op, &rs );
1406 op->o_managedsait = org_managedsait;
1408 op->o_ndn = org_ndn;
1409 op->o_req_dn = org_req_dn;
1410 op->o_req_ndn = org_req_ndn;
1411 op->o_delete_glue_parent = 0;
1413 ber_bvfree( np_prev->npe_name );
1414 ber_bvfree( np_prev->npe_nname );
1415 op->o_req_dn.bv_val = NULL;
1416 op->o_req_ndn.bv_val = NULL;
1431 Backend *be = op->o_bd;
1432 slap_callback cb = { NULL };
1437 struct berval dn = {0, NULL};
1438 struct berval ndn = {0, NULL};
1440 SlapReply rs = {REP_RESULT};
1443 op->o_tag = LDAP_REQ_ADD;
1444 op->o_callback = &cb;
1445 cb.sc_response = null_callback;
1446 cb.sc_private = NULL;
1451 /* count RDNs in suffix */
1452 if ( be->be_nsuffix[0].bv_len ) {
1453 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1463 /* Start with BE suffix */
1464 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1465 comma = strrchr(dn.bv_val, ',');
1466 if ( ptr ) *ptr = ',';
1467 if ( comma ) *comma = '\0';
1472 dn.bv_len -= ptr - dn.bv_val;
1475 /* the normalizedDNs are always the same length, no counting
1478 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1479 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1480 ndn.bv_len = be->be_nsuffix[0].bv_len;
1483 while ( ndn.bv_val > e->e_nname.bv_val ) {
1484 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1485 ber_dupbv( &glue->e_name, &dn );
1486 ber_dupbv( &glue->e_nname, &ndn );
1488 a = ch_calloc( 1, sizeof( Attribute ));
1489 a->a_desc = slap_schema.si_ad_objectClass;
1491 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1492 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1493 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1494 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1496 a->a_nvals = a->a_vals;
1498 a->a_next = glue->e_attrs;
1501 a = ch_calloc( 1, sizeof( Attribute ));
1502 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1504 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1505 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1506 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1508 a->a_nvals = a->a_vals;
1510 a->a_next = glue->e_attrs;
1513 op->o_req_dn = glue->e_name;
1514 op->o_req_ndn = glue->e_nname;
1516 rc = be->be_add ( op, &rs );
1517 if ( rc == LDAP_SUCCESS ) {
1518 be_entry_release_w( op, glue );
1520 /* incl. ALREADY EXIST */
1524 /* Move to next child */
1525 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1528 if ( ptr == e->e_name.bv_val ) break;
1530 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1531 for( ptr = ndn.bv_val-2;
1532 ptr > e->e_nname.bv_val && *ptr != ',';
1538 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1541 op->o_req_dn = e->e_name;
1542 op->o_req_ndn = e->e_nname;
1544 rc = be->be_add ( op, &rs );
1545 if ( rc == LDAP_SUCCESS ) {
1546 be_entry_release_w( op, e );
1554 static struct berval ocbva[] = {
1556 BER_BVC("subentry"),
1557 BER_BVC("syncConsumerSubentry"),
1561 static struct berval cnbva[] = {
1566 static struct berval ssbva[] = {
1571 static struct berval scbva[] = {
1577 syncrepl_updateCookie(
1581 struct sync_cookie *syncCookie
1584 Backend *be = op->o_bd;
1586 Modifications *mlnext;
1588 Modifications *modlist = NULL;
1589 Modifications **modtail = &modlist;
1592 char txtbuf[SLAP_TEXT_BUFLEN];
1593 size_t textlen = sizeof txtbuf;
1598 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1599 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1600 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1602 slap_callback cb = { NULL };
1603 SlapReply rs = {REP_RESULT};
1605 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1606 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1608 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1609 mod->sml_op = LDAP_MOD_REPLACE;
1610 mod->sml_desc = slap_schema.si_ad_objectClass;
1611 mod->sml_type = mod->sml_desc->ad_cname;
1612 mod->sml_bvalues = ocbva;
1614 modtail = &mod->sml_next;
1616 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1617 assert( si->si_rid < 1000 );
1618 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1619 slap_syncrepl_bvc.bv_len,
1620 "syncrepl%ld", si->si_rid );
1621 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1622 mod->sml_op = LDAP_MOD_REPLACE;
1623 mod->sml_desc = slap_schema.si_ad_cn;
1624 mod->sml_type = mod->sml_desc->ad_cname;
1625 mod->sml_bvalues = cnbva;
1627 modtail = &mod->sml_next;
1629 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1630 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1631 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1632 mod->sml_op = LDAP_MOD_REPLACE;
1633 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1634 mod->sml_type = mod->sml_desc->ad_cname;
1635 mod->sml_bvalues = scbva;
1637 modtail = &mod->sml_next;
1639 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1640 mod->sml_op = LDAP_MOD_REPLACE;
1641 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1642 mod->sml_type = mod->sml_desc->ad_cname;
1643 mod->sml_bvalues = ssbva;
1645 modtail = &mod->sml_next;
1649 op->o_tag = LDAP_REQ_ADD;
1650 rc = slap_mods_opattrs( op, modlist, modtail,
1651 &text,txtbuf, textlen );
1653 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1654 ml->sml_op = LDAP_MOD_REPLACE;
1657 if( rc != LDAP_SUCCESS ) {
1659 LDAP_LOG( OPERATION, ERR,
1660 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1662 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1667 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1669 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1670 assert( si->si_rid < 1000 );
1671 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1672 slap_syncrepl_cn_bvc.bv_len,
1673 "cn=syncrepl%ld", si->si_rid );
1675 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1677 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1678 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1680 if ( slap_syncrepl_dn_bv.bv_val ) {
1681 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1686 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1688 if( rc != LDAP_SUCCESS ) {
1690 LDAP_LOG( OPERATION, ERR,
1691 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1693 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1698 cb.sc_response = null_callback;
1701 op->o_callback = &cb;
1702 op->o_req_dn = e->e_name;
1703 op->o_req_ndn = e->e_nname;
1705 /* update persistent cookie */
1706 update_cookie_retry:
1707 op->o_tag = LDAP_REQ_MODIFY;
1708 op->orm_modlist = modlist;
1709 rc = be->be_modify( op, &rs );
1711 if ( rc != LDAP_SUCCESS ) {
1712 if ( rc == LDAP_REFERRAL ||
1713 rc == LDAP_NO_SUCH_OBJECT ) {
1714 op->o_tag = LDAP_REQ_ADD;
1716 rc = be->be_add( op, &rs );
1717 if ( rc != LDAP_SUCCESS ) {
1718 if ( rc == LDAP_ALREADY_EXISTS ) {
1719 goto update_cookie_retry;
1720 } else if ( rc == LDAP_REFERRAL ||
1721 rc == LDAP_NO_SUCH_OBJECT ) {
1723 LDAP_LOG( OPERATION, ERR,
1724 "cookie will be non-persistent\n",
1727 Debug( LDAP_DEBUG_ANY,
1728 "cookie will be non-persistent\n",
1733 LDAP_LOG( OPERATION, ERR,
1734 "be_add failed (%d)\n",
1737 Debug( LDAP_DEBUG_ANY,
1738 "be_add failed (%d)\n",
1743 be_entry_release_w( op, e );
1748 LDAP_LOG( OPERATION, ERR,
1749 "be_modify failed (%d)\n", rc, 0, 0 );
1751 Debug( LDAP_DEBUG_ANY,
1752 "be_modify failed (%d)\n", rc, 0, 0 );
1763 if ( cnbva[0].bv_val ) {
1764 ch_free( cnbva[0].bv_val );
1765 cnbva[0].bv_val = NULL;
1767 if ( scbva[0].bv_val ) {
1768 ch_free( scbva[0].bv_val );
1769 scbva[0].bv_val = NULL;
1772 if ( mlnext->sml_next ) {
1773 slap_mods_free( mlnext->sml_next );
1774 mlnext->sml_next = NULL;
1777 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1778 mlnext = ml->sml_next;
1791 syncinfo_t *si = op->o_callback->sc_private;
1793 if ( rs->sr_type == REP_SEARCH ) {
1794 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1796 LDAP_LOG( OPERATION, ERR,
1797 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1799 Debug( LDAP_DEBUG_ANY,
1800 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1803 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1807 return LDAP_SUCCESS;
1811 nonpresent_callback(
1816 syncinfo_t *si = op->o_callback->sc_private;
1819 struct berval* present_uuid = NULL;
1820 struct nonpresent_entry *np_entry;
1822 if ( rs->sr_type == REP_RESULT ) {
1823 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1824 si->si_presentlist = NULL;
1826 } else if ( rs->sr_type == REP_SEARCH ) {
1827 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1829 if ( a == NULL ) return 0;
1831 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1834 if ( present_uuid == NULL ) {
1835 np_entry = (struct nonpresent_entry *)
1836 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1837 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1838 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1839 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1842 avl_delete( &si->si_presentlist,
1843 &a->a_vals[0], syncuuid_cmp );
1844 ch_free( present_uuid->bv_val );
1845 ch_free( present_uuid );
1848 return LDAP_SUCCESS;
1856 if ( rs->sr_err != LDAP_SUCCESS &&
1857 rs->sr_err != LDAP_REFERRAL &&
1858 rs->sr_err != LDAP_ALREADY_EXISTS &&
1859 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1860 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1863 LDAP_LOG( OPERATION, ERR,
1864 "null_callback : error code 0x%x\n",
1867 Debug( LDAP_DEBUG_ANY,
1868 "null_callback : error code 0x%x\n",
1872 return LDAP_SUCCESS;
1876 slap_create_syncrepl_entry(
1878 struct berval *context_csn,
1886 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1888 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1890 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1893 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1895 if ( context_csn ) {
1896 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1897 context_csn, NULL );
1901 bv.bv_len = sizeof("{}")-1;
1902 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1904 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1905 ber_dupbv( &e->e_nname, &e->e_name );
1911 slap_uuidstr_from_normalized(
1912 struct berval* uuidstr,
1913 struct berval* normalized,
1917 unsigned char nibble;
1920 if ( normalized == NULL )
1923 if ( normalized->bv_len != 16 ) {
1930 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1935 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1937 sl_free( new, ctx );
1941 for ( i = 0; i < 16; i++ ) {
1942 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1943 new->bv_val[(i<<1)+d] = '-';
1947 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1948 if ( nibble < 10 ) {
1949 new->bv_val[(i<<1)+d] = nibble + '0';
1951 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1954 nibble = (normalized->bv_val[i]) & 0xF;
1955 if ( nibble < 10 ) {
1956 new->bv_val[(i<<1)+d+1] = nibble + '0';
1958 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1962 new->bv_val[new->bv_len] = '\0';
1968 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1970 const struct berval *uuid1 = v_uuid1;
1971 const struct berval *uuid2 = v_uuid2;
1972 int rc = uuid1->bv_len - uuid2->bv_len;
1973 if ( rc ) return rc;
1974 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1978 avl_ber_bvfree( void *bv )
1983 if ( ((struct berval *)bv)->bv_val != NULL ) {
1984 ch_free ( ((struct berval *)bv)->bv_val );
1986 ch_free ( (char *) bv );