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 );
168 static const Listener dummy_list = { {0, ""}, {0, ""} };
176 int cmdline_cookie_found = 0;
178 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
179 struct berval syncrepl_cn_bv;
180 struct sync_cookie *sc = NULL;
181 struct sync_cookie syncCookie = { NULL, -1, NULL };
184 psub = &si->si_be->be_nsuffix[0];
186 /* Init connection to master */
188 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
189 if ( rc != LDAP_SUCCESS ) {
191 LDAP_LOG( OPERATION, ERR,
192 "do_syncrep1: ldap_initialize failed (%s)\n",
193 si->si_provideruri, 0, 0 );
195 Debug( LDAP_DEBUG_ANY,
196 "do_syncrep1: ldap_initialize failed (%s)\n",
197 si->si_provideruri, 0, 0 );
202 op->o_protocol = LDAP_VERSION3;
203 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
208 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
209 if( rc != LDAP_SUCCESS ) {
211 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
212 "%s: ldap_start_tls failed (%d)\n",
213 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
216 Debug( LDAP_DEBUG_ANY,
217 "%s: ldap_start_tls failed (%d)\n",
218 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
221 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
225 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
226 #ifdef HAVE_CYRUS_SASL
229 if ( si->si_secprops != NULL ) {
230 rc = ldap_set_option( si->si_ld,
231 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
233 if( rc != LDAP_OPT_SUCCESS ) {
235 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
236 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
237 si->si_provideruri, si->si_secprops, 0 );
239 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
240 "(%s,SECPROPS,\"%s\") failed!\n",
241 si->si_provideruri, si->si_secprops, 0 );
247 defaults = lutil_sasl_defaults( si->si_ld,
248 si->si_saslmech, si->si_realm,
249 si->si_authcId, si->si_passwd, si->si_authzId );
251 rc = ldap_sasl_interactive_bind_s( si->si_ld,
259 lutil_sasl_freedefs( defaults );
261 /* FIXME : different error behaviors according to
263 * 2) on err policy : exit, retry, backoff ...
265 if ( rc != LDAP_SUCCESS ) {
267 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
268 "ldap_sasl_interactive_bind_s failed (%d)\n",
271 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
272 "ldap_sasl_interactive_bind_s failed (%d)\n",
277 #else /* HAVE_CYRUS_SASL */
278 /* Should never get here, we trapped this at config time */
279 fprintf( stderr, "not compiled with SASL support\n" );
284 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
285 if ( rc != LDAP_SUCCESS ) {
287 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
288 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
290 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
291 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
297 /* get syncrepl cookie of shadow replica from subentry */
299 assert( si->si_rid < 1000 );
300 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
301 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
302 CN_STR "syncrepl%ld", si->si_rid );
303 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
304 op->o_req_dn = op->o_req_ndn;
306 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
307 if ( si->si_rid == sc->rid ) {
308 cmdline_cookie_found = 1;
313 if ( cmdline_cookie_found ) {
314 /* cookie is supplied in the command line */
315 BerVarray cookie = NULL;
316 struct berval cookie_bv;
318 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
319 slap_sync_cookie_free( &si->si_syncCookie, 0 );
321 /* read stored cookie if it exists */
322 backend_attribute( op, NULL, &op->o_req_ndn,
323 slap_schema.si_ad_syncreplCookie, &cookie );
326 /* no stored cookie */
327 if ( sc->ctxcsn == NULL ||
328 sc->ctxcsn->bv_val == NULL ) {
329 /* if cmdline cookie does not have ctxcsn */
330 /* component, set it to an initial value */
331 slap_init_sync_cookie_ctxcsn( sc );
333 slap_dup_sync_cookie( &si->si_syncCookie, sc );
334 slap_sync_cookie_free( sc, 1 );
338 struct berval newcookie = { 0, NULL };
339 ber_dupbv( &cookie_bv, &cookie[0] );
340 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
341 slap_parse_sync_cookie( &si->si_syncCookie );
342 ber_bvarray_free( si->si_syncCookie.octet_str );
343 si->si_syncCookie.octet_str = NULL;
344 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
345 if ( sc->sid != -1 ) {
346 /* command line cookie wins */
347 si->si_syncCookie.sid = sc->sid;
349 if ( sc->ctxcsn != NULL ) {
350 /* command line cookie wins */
351 if ( si->si_syncCookie.ctxcsn ) {
352 ber_bvarray_free( si->si_syncCookie.ctxcsn );
353 si->si_syncCookie.ctxcsn = NULL;
355 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
356 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
358 if ( sc->rid != -1 ) {
359 /* command line cookie wins */
360 si->si_syncCookie.rid = sc->rid;
362 slap_sync_cookie_free( sc, 1 );
364 slap_compose_sync_cookie( NULL, &newcookie,
365 &si->si_syncCookie.ctxcsn[0],
366 si->si_syncCookie.sid, si->si_syncCookie.rid );
367 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
370 /* no command line cookie is specified */
371 if ( si->si_syncCookie.octet_str == NULL ) {
372 BerVarray cookie = NULL;
373 struct berval cookie_bv;
374 /* try to read stored cookie */
375 backend_attribute( op, NULL, &op->o_req_ndn,
376 slap_schema.si_ad_syncreplCookie, &cookie );
378 ber_dupbv( &cookie_bv, &cookie[0] );
379 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
380 slap_parse_sync_cookie( &si->si_syncCookie );
381 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
386 rc = ldap_sync_search( si, op->o_tmpmemctx );
388 if( rc != LDAP_SUCCESS ) {
390 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
391 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
393 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
394 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
401 ldap_unbind( si->si_ld );
414 LDAPControl **rctrls = NULL;
417 BerElementBuffer berbuf;
418 BerElement *ber = (BerElement *)&berbuf;
420 LDAPMessage *res = NULL;
421 LDAPMessage *msg = NULL;
424 struct berval *retdata = NULL;
429 struct berval syncUUID = { 0, NULL };
430 struct sync_cookie syncCookie = { NULL, -1, NULL };
431 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
432 struct berval cookie = { 0, NULL };
440 Modifications *modlist = NULL;
445 struct timeval *tout_p = NULL;
446 struct timeval tout = { 0, 0 };
448 int refreshDeletes = 0;
450 BerVarray syncUUIDs = NULL;
453 if ( slapd_shutdown ) {
458 ber_init2( ber, NULL, LBER_USE_DER );
459 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
462 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
464 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
467 psub = &si->si_be->be_nsuffix[0];
469 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
471 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
477 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
480 if ( slapd_shutdown ) {
484 for( msg = ldap_first_message( si->si_ld, res );
486 msg = ldap_next_message( si->si_ld, msg ) )
488 switch( ldap_msgtype( msg ) ) {
489 case LDAP_RES_SEARCH_ENTRY:
490 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
491 /* we can't work without the control */
497 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
498 ber_scanf( ber, "{em", &syncstate, &syncUUID );
499 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
500 ber_scanf( ber, "m}", &cookie );
501 if ( cookie.bv_val ) {
502 struct berval tmp_bv;
503 ber_dupbv( &tmp_bv, &cookie );
504 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
506 if ( syncCookie.octet_str &&
507 syncCookie.octet_str[0].bv_val )
508 slap_parse_sync_cookie( &syncCookie );
510 if ( syncrepl_message_to_entry( si, op, msg,
511 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
512 rc_efree = syncrepl_entry( si, op, entry, modlist,
513 syncstate, &syncUUID, &syncCookie_req );
514 if ( syncCookie.octet_str &&
515 syncCookie.octet_str[0].bv_val ) {
516 syncrepl_updateCookie( si, op, psub, &syncCookie );
519 ldap_controls_free( rctrls );
521 slap_mods_free( modlist );
523 if ( rc_efree && entry ) {
528 case LDAP_RES_SEARCH_REFERENCE:
530 LDAP_LOG( OPERATION, ERR,
531 "do_syncrep2 : reference received\n", 0, 0, 0 );
533 Debug( LDAP_DEBUG_ANY,
534 "do_syncrep2 : reference received\n", 0, 0, 0 );
538 case LDAP_RES_SEARCH_RESULT:
539 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
543 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
545 ber_scanf( ber, "{" /*"}"*/);
546 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
548 ber_scanf( ber, "m", &cookie );
549 if ( cookie.bv_val ) {
550 struct berval tmp_bv;
551 ber_dupbv( &tmp_bv, &cookie );
552 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
554 if ( syncCookie.octet_str &&
555 syncCookie.octet_str[0].bv_val )
556 slap_parse_sync_cookie( &syncCookie );
558 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
560 ber_scanf( ber, "b", &refreshDeletes );
562 ber_scanf( ber, "}" );
564 if ( syncCookie_req.ctxcsn == NULL ) {
566 } else if ( syncCookie.ctxcsn == NULL ) {
569 value_match( &match, slap_schema.si_ad_entryCSN,
570 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
571 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
572 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
574 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
575 && match < 0 && err == LDAP_SUCCESS ) {
576 syncrepl_updateCookie( si, op, psub, &syncCookie );
579 ldap_controls_free( rctrls );
581 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
582 /* FIXME : different error behaviors according to
583 * 1) err code : LDAP_BUSY ...
584 * 2) on err policy : stop service, stop sync, retry
586 if ( refreshDeletes == 0 && match < 0 &&
587 err == LDAP_SUCCESS ) {
588 syncrepl_del_nonpresent( op, si );
590 avl_free( si->si_presentlist, avl_ber_bvfree );
591 si->si_presentlist = NULL;
598 case LDAP_RES_INTERMEDIATE:
599 rc = ldap_parse_intermediate( si->si_ld, msg,
600 &retoid, &retdata, NULL, 0 );
601 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
602 int si_refreshDelete = 0;
603 int si_refreshPresent = 0;
604 ber_init2( ber, retdata, LBER_USE_DER );
606 switch ( si_tag = ber_peek_tag( ber, &len )) {
608 case LDAP_TAG_SYNC_NEW_COOKIE:
609 ber_scanf( ber, "tm", &tag, &cookie );
611 case LDAP_TAG_SYNC_REFRESH_DELETE:
612 si_refreshDelete = 1;
613 case LDAP_TAG_SYNC_REFRESH_PRESENT:
614 si_refreshPresent = 1;
615 ber_scanf( ber, "t{", &tag );
616 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
618 ber_scanf( ber, "m", &cookie );
619 if ( cookie.bv_val ) {
620 struct berval tmp_bv;
621 ber_dupbv( &tmp_bv, &cookie );
622 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
624 if ( syncCookie.octet_str &&
625 syncCookie.octet_str[0].bv_val )
626 slap_parse_sync_cookie( &syncCookie );
628 if ( ber_peek_tag( ber, &len ) ==
629 LDAP_TAG_REFRESHDONE )
631 ber_scanf( ber, "b", &refreshDone );
633 ber_scanf( ber, "}" );
635 case LDAP_TAG_SYNC_ID_SET:
636 ber_scanf( ber, "t{", &tag );
637 if ( ber_peek_tag( ber, &len ) ==
638 LDAP_TAG_SYNC_COOKIE ) {
639 ber_scanf( ber, "m", &cookie );
640 if ( cookie.bv_val ) {
641 struct berval tmp_bv;
642 ber_dupbv( &tmp_bv, &cookie );
643 ber_bvarray_add( &syncCookie.octet_str,
646 if ( syncCookie.octet_str &&
647 syncCookie.octet_str[0].bv_val )
648 slap_parse_sync_cookie( &syncCookie );
650 if ( ber_peek_tag( ber, &len ) ==
651 LDAP_TAG_REFRESHDELETES )
653 ber_scanf( ber, "b", &refreshDeletes );
655 ber_scanf( ber, "[W]", &syncUUIDs );
656 ber_scanf( ber, "}" );
657 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
658 struct berval *syncuuid_bv;
659 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
660 avl_insert( &si->si_presentlist,
661 (caddr_t) syncuuid_bv,
662 syncuuid_cmp, avl_dup_error );
664 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
668 LDAP_LOG( OPERATION, ERR,
669 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
670 (long) si_tag, 0, 0 );
672 Debug( LDAP_DEBUG_ANY,
673 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
674 (long) si_tag, 0, 0 );
676 ldap_memfree( retoid );
677 ber_bvfree( retdata );
681 if ( syncCookie_req.ctxcsn == NULL ) {
683 } else if ( syncCookie.ctxcsn == NULL ) {
686 value_match( &match, slap_schema.si_ad_entryCSN,
687 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
688 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
689 &syncCookie_req.ctxcsn[0],
690 &syncCookie.ctxcsn[0], &text );
693 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
695 syncrepl_updateCookie( si, op, psub, &syncCookie);
698 if ( si_refreshPresent == 1 ) {
700 syncrepl_del_nonpresent( op, si );
704 ldap_memfree( retoid );
705 ber_bvfree( retdata );
709 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
710 " unknown intermediate "
711 "response\n", 0, 0, 0 );
713 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
714 "unknown intermediate response (%d)\n",
717 ldap_memfree( retoid );
718 ber_bvfree( retdata );
724 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
725 "unknown message\n", 0, 0, 0 );
727 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
728 "unknown message\n", 0, 0, 0 );
733 if ( syncCookie.octet_str ) {
734 slap_sync_cookie_free( &syncCookie_req, 0 );
735 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
736 slap_sync_cookie_free( &syncCookie, 0 );
746 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
747 errstr = ldap_err2string( rc );
750 LDAP_LOG( OPERATION, ERR,
751 "do_syncrep2 : %s\n", errstr, 0, 0 );
753 Debug( LDAP_DEBUG_ANY,
754 "do_syncrep2 : %s\n", errstr, 0, 0 );
759 slap_sync_cookie_free( &syncCookie, 0 );
760 slap_sync_cookie_free( &syncCookie_req, 0 );
762 if ( res ) ldap_msgfree( res );
764 if ( rc && si->si_ld ) {
765 ldap_unbind( si->si_ld );
777 struct re_s* rtask = arg;
778 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
779 Connection conn = {0};
781 int rc = LDAP_SUCCESS;
787 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
789 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
795 switch( abs( si->si_type )) {
796 case LDAP_SYNC_REFRESH_ONLY:
797 case LDAP_SYNC_REFRESH_AND_PERSIST:
803 if ( slapd_shutdown && si->si_ld ) {
804 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
805 connection_client_stop( s );
806 ldap_unbind( si->si_ld );
812 conn.c_send_ldap_result = slap_send_ldap_result;
813 conn.c_send_search_entry = slap_send_search_entry;
814 conn.c_send_search_reference = slap_send_search_reference;
815 conn.c_listener = (Listener *)&dummy_list;
816 conn.c_peer_name = slap_empty_bv;
818 /* set memory context */
819 op.o_tmpmemctx = sl_mem_create( SLMALLOC_SLAB_SIZE, ctx );
820 op.o_tmpmfuncs = &sl_mfuncs;
822 op.o_dn = si->si_updatedn;
823 op.o_ndn = si->si_updatedn;
824 op.o_time = slap_get_time();
825 op.o_threadctx = ctx;
826 op.o_managedsait = 1;
829 op.o_connid = op.o_conn->c_connid;
831 op.o_sync_state.ctxcsn = NULL;
832 op.o_sync_state.sid = -1;
833 op.o_sync_state.octet_str = NULL;
834 op.o_sync_slog_size = -1;
835 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
836 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
838 /* Establish session, do search */
841 rc = do_syncrep1( &op, si );
844 /* Process results */
845 if ( rc == LDAP_SUCCESS ) {
846 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
848 rc = do_syncrep2( &op, si );
850 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
851 /* If we succeeded, enable the connection for further listening.
852 * If we failed, tear down the connection and reschedule.
854 if ( rc == LDAP_SUCCESS ) {
856 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
859 connection_client_enable( s );
861 } else if ( !first ) {
865 if ( rc == -2 ) rc = 0;
869 /* At this point, we have 4 cases:
870 * 1) for any hard failure, give up and remove this task
871 * 2) for ServerDown, reschedule this task to run
872 * 3) for Refresh and Success, reschedule to run
873 * 4) for Persist and Success, reschedule to defer
875 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
876 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
877 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
881 connection_client_stop( s );
884 if ( rc && rc != LDAP_SERVER_DOWN ) {
885 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
887 if ( rc == LDAP_SERVER_DOWN ||
888 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
893 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
895 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
901 syncrepl_message_to_entry(
905 Modifications **modlist,
911 BerElement *ber = NULL;
914 Modifications **modtail = modlist;
917 char txtbuf[SLAP_TEXT_BUFLEN];
918 size_t textlen = sizeof txtbuf;
920 struct berval bdn = {0, NULL}, dn, ndn;
925 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
927 LDAP_LOG( OPERATION, ERR,
928 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
930 Debug( LDAP_DEBUG_ANY,
931 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
936 op->o_tag = LDAP_REQ_ADD;
938 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
940 if ( rc != LDAP_SUCCESS ) {
942 LDAP_LOG( OPERATION, ERR,
943 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
945 Debug( LDAP_DEBUG_ANY,
946 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
951 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
952 ber_dupbv( &op->o_req_dn, &dn );
953 ber_dupbv( &op->o_req_ndn, &ndn );
954 sl_free( ndn.bv_val, op->o_tmpmemctx );
955 sl_free( dn.bv_val, op->o_tmpmemctx );
957 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
962 if ( entry == NULL ) {
966 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
968 e->e_name = op->o_req_dn;
969 e->e_nname = op->o_req_ndn;
971 while ( ber_remaining( ber ) ) {
972 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
973 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
978 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
980 mod->sml_op = LDAP_MOD_REPLACE;
981 mod->sml_next = NULL;
982 mod->sml_desc = NULL;
983 mod->sml_type = tmp.sml_type;
984 mod->sml_bvalues = tmp.sml_bvalues;
985 mod->sml_nvalues = NULL;
988 modtail = &mod->sml_next;
991 if ( *modlist == NULL ) {
993 LDAP_LOG( OPERATION, ERR,
994 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
996 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1001 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1003 if ( rc != LDAP_SUCCESS ) {
1005 LDAP_LOG( OPERATION, ERR,
1006 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1008 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1014 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1015 if( rc != LDAP_SUCCESS ) {
1017 LDAP_LOG( OPERATION, ERR,
1018 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1020 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1026 ber_free ( ber, 0 );
1027 if ( rc != LDAP_SUCCESS ) {
1040 Modifications* modlist,
1042 struct berval* syncUUID,
1043 struct sync_cookie* syncCookie_req
1046 Backend *be = op->o_bd;
1047 slap_callback cb = { NULL };
1048 struct berval *syncuuid_bv = NULL;
1049 struct berval syncUUID_strrep = { 0, NULL };
1051 SlapReply rs = {REP_RESULT};
1053 AttributeAssertion ava = {0};
1054 int rc = LDAP_SUCCESS;
1055 int ret = LDAP_SUCCESS;
1058 struct berval pdn = { 0, NULL };
1059 struct berval org_req_dn = { 0, NULL };
1060 struct berval org_req_ndn = { 0, NULL };
1061 struct berval org_dn = { 0, NULL };
1062 struct berval org_ndn = { 0, NULL };
1063 int org_managedsait;
1065 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1067 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1068 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1069 syncuuid_cmp, avl_dup_error );
1072 if ( syncstate == LDAP_SYNC_PRESENT ) {
1076 f.f_choice = LDAP_FILTER_EQUALITY;
1078 ava.aa_desc = slap_schema.si_ad_entryUUID;
1079 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1080 ava.aa_value = *syncUUID;
1081 op->ors_filter = &f;
1083 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1084 op->ors_filterstr.bv_val = (char *) sl_malloc(
1085 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1086 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1087 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1088 syncUUID->bv_val, syncUUID->bv_len );
1089 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1091 op->ors_scope = LDAP_SCOPE_SUBTREE;
1093 /* get syncrepl cookie of shadow replica from subentry */
1094 op->o_req_dn = si->si_base;
1095 op->o_req_ndn = si->si_base;
1097 /* set callback function */
1098 op->o_callback = &cb;
1099 cb.sc_response = dn_callback;
1102 si->si_syncUUID_ndn.bv_val = NULL;
1104 rc = be->be_search( op, &rs );
1106 if ( op->ors_filterstr.bv_val ) {
1107 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1110 cb.sc_response = null_callback;
1113 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1117 if ( syncstate != LDAP_SYNC_DELETE ) {
1118 op->o_no_psearch = 1;
1121 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1122 if ( op->o_sync_csn.bv_val ) {
1123 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1128 op->o_req_dn = si->si_syncUUID_ndn;
1129 op->o_req_ndn = si->si_syncUUID_ndn;
1130 op->o_tag = LDAP_REQ_DELETE;
1131 rc = be->be_delete( op, &rs );
1133 org_req_dn = op->o_req_dn;
1134 org_req_ndn = op->o_req_ndn;
1136 org_ndn = op->o_ndn;
1137 org_managedsait = get_manageDSAit( op );
1138 op->o_dn = op->o_bd->be_rootdn;
1139 op->o_ndn = op->o_bd->be_rootndn;
1140 op->o_managedsait = 1;
1142 while ( rs.sr_err == LDAP_SUCCESS &&
1143 op->o_delete_glue_parent ) {
1144 op->o_delete_glue_parent = 0;
1145 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1146 slap_callback cb = { NULL };
1147 cb.sc_response = slap_null_cb;
1148 dnParent( &op->o_req_ndn, &pdn );
1150 op->o_req_ndn = pdn;
1151 op->o_callback = &cb;
1152 op->o_bd->be_delete( op, &rs );
1158 op->o_managedsait = org_managedsait;
1160 op->o_ndn = org_ndn;
1161 op->o_req_dn = org_req_dn;
1162 op->o_req_ndn = org_req_ndn;
1163 op->o_delete_glue_parent = 0;
1165 op->o_no_psearch = 0;
1168 switch ( syncstate ) {
1170 case LDAP_SYNC_MODIFY:
1171 if ( rc == LDAP_SUCCESS ||
1172 rc == LDAP_REFERRAL ||
1173 rc == LDAP_NO_SUCH_OBJECT ||
1174 rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1176 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1177 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1178 syncUUID, &ava.aa_value );
1180 op->o_tag = LDAP_REQ_ADD;
1182 op->o_req_dn = e->e_name;
1183 op->o_req_ndn = e->e_nname;
1184 rc = be->be_add( op, &rs );
1186 if ( rc != LDAP_SUCCESS ) {
1187 if ( rc == LDAP_ALREADY_EXISTS ) {
1188 op->o_tag = LDAP_REQ_MODIFY;
1189 op->orm_modlist = modlist;
1190 op->o_req_dn = e->e_name;
1191 op->o_req_ndn = e->e_nname;
1192 rc = be->be_modify( op, &rs );
1193 if ( rc != LDAP_SUCCESS ) {
1195 LDAP_LOG( OPERATION, ERR,
1196 "syncrepl_entry : be_modify failed (%d)\n",
1199 Debug( LDAP_DEBUG_ANY,
1200 "syncrepl_entry : be_modify failed (%d)\n",
1206 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1207 syncrepl_add_glue( op, e );
1212 LDAP_LOG( OPERATION, ERR,
1213 "syncrepl_entry : be_add failed (%d)\n",
1216 Debug( LDAP_DEBUG_ANY,
1217 "syncrepl_entry : be_add failed (%d)\n",
1224 be_entry_release_w( op, e );
1230 LDAP_LOG( OPERATION, ERR,
1231 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1233 Debug( LDAP_DEBUG_ANY,
1234 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1240 case LDAP_SYNC_DELETE :
1241 /* Already deleted */
1247 LDAP_LOG( OPERATION, ERR,
1248 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1250 Debug( LDAP_DEBUG_ANY,
1251 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1259 if ( syncUUID_strrep.bv_val ) {
1260 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1262 if ( si->si_syncUUID_ndn.bv_val ) {
1263 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1268 static struct berval gcbva[] = {
1275 syncrepl_del_nonpresent(
1280 Backend* be = op->o_bd;
1281 slap_callback cb = { NULL };
1282 SlapReply rs = {REP_RESULT};
1283 struct nonpresent_entry *np_list, *np_prev;
1286 Modifications *mlnext;
1288 Modifications *modlist = NULL;
1289 Modifications **modtail = &modlist;
1292 struct berval pdn = { 0, NULL };
1293 struct berval org_req_dn = { 0, NULL };
1294 struct berval org_req_ndn = { 0, NULL };
1295 struct berval org_dn = { 0, NULL };
1296 struct berval org_ndn = { 0, NULL };
1297 int org_managedsait;
1299 op->o_req_dn = si->si_base;
1300 op->o_req_ndn = si->si_base;
1302 cb.sc_response = nonpresent_callback;
1305 op->o_callback = &cb;
1306 op->o_tag = LDAP_REQ_SEARCH;
1307 op->ors_scope = si->si_scope;
1308 op->ors_deref = LDAP_DEREF_NEVER;
1311 op->ors_attrsonly = 0;
1312 op->ors_attrs = NULL;
1313 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1314 op->ors_filterstr = si->si_filterstr;
1316 op->o_nocaching = 1;
1317 op->o_managedsait = 0;
1318 be->be_search( op, &rs );
1319 op->o_managedsait = 1;
1320 op->o_nocaching = 0;
1322 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1324 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1325 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1326 while ( np_list != NULL ) {
1327 LDAP_LIST_REMOVE( np_list, npe_link );
1329 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1330 op->o_tag = LDAP_REQ_DELETE;
1331 op->o_callback = &cb;
1332 cb.sc_response = null_callback;
1334 op->o_req_dn = *np_prev->npe_name;
1335 op->o_req_ndn = *np_prev->npe_nname;
1336 rc = op->o_bd->be_delete( op, &rs );
1338 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1339 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1340 mod->sml_op = LDAP_MOD_REPLACE;
1341 mod->sml_desc = slap_schema.si_ad_objectClass;
1342 mod->sml_type = mod->sml_desc->ad_cname;
1343 mod->sml_bvalues = &gcbva[0];
1345 modtail = &mod->sml_next;
1347 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1348 mod->sml_op = LDAP_MOD_REPLACE;
1349 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1350 mod->sml_type = mod->sml_desc->ad_cname;
1351 mod->sml_bvalues = &gcbva[1];
1353 modtail = &mod->sml_next;
1355 op->o_tag = LDAP_REQ_MODIFY;
1356 op->orm_modlist = modlist;
1358 rc = be->be_modify( op, &rs );
1360 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1361 mlnext = ml->sml_next;
1366 org_req_dn = op->o_req_dn;
1367 org_req_ndn = op->o_req_ndn;
1369 org_ndn = op->o_ndn;
1370 org_managedsait = get_manageDSAit( op );
1371 op->o_dn = op->o_bd->be_rootdn;
1372 op->o_ndn = op->o_bd->be_rootndn;
1373 op->o_managedsait = 1;
1375 while ( rs.sr_err == LDAP_SUCCESS &&
1376 op->o_delete_glue_parent ) {
1377 op->o_delete_glue_parent = 0;
1378 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1379 slap_callback cb = { NULL };
1380 cb.sc_response = slap_null_cb;
1381 dnParent( &op->o_req_ndn, &pdn );
1383 op->o_req_ndn = pdn;
1384 op->o_callback = &cb;
1385 /* give it a root privil ? */
1386 op->o_bd->be_delete( op, &rs );
1392 op->o_managedsait = org_managedsait;
1394 op->o_ndn = org_ndn;
1395 op->o_req_dn = org_req_dn;
1396 op->o_req_ndn = org_req_ndn;
1397 op->o_delete_glue_parent = 0;
1399 ber_bvfree( np_prev->npe_name );
1400 ber_bvfree( np_prev->npe_nname );
1401 op->o_req_dn.bv_val = NULL;
1402 op->o_req_ndn.bv_val = NULL;
1417 Backend *be = op->o_bd;
1418 slap_callback cb = { NULL };
1423 struct berval dn = {0, NULL};
1424 struct berval ndn = {0, NULL};
1426 SlapReply rs = {REP_RESULT};
1429 op->o_tag = LDAP_REQ_ADD;
1430 op->o_callback = &cb;
1431 cb.sc_response = null_callback;
1432 cb.sc_private = NULL;
1437 /* count RDNs in suffix */
1438 if ( be->be_nsuffix[0].bv_len ) {
1439 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1449 /* Start with BE suffix */
1450 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1451 comma = strrchr(dn.bv_val, ',');
1452 if ( ptr ) *ptr = ',';
1453 if ( comma ) *comma = '\0';
1458 dn.bv_len -= ptr - dn.bv_val;
1461 /* the normalizedDNs are always the same length, no counting
1464 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1465 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1466 ndn.bv_len = be->be_nsuffix[0].bv_len;
1469 while ( ndn.bv_val > e->e_nname.bv_val ) {
1470 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1471 ber_dupbv( &glue->e_name, &dn );
1472 ber_dupbv( &glue->e_nname, &ndn );
1474 a = ch_calloc( 1, sizeof( Attribute ));
1475 a->a_desc = slap_schema.si_ad_objectClass;
1477 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1478 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1479 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1480 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1482 a->a_nvals = a->a_vals;
1484 a->a_next = glue->e_attrs;
1487 a = ch_calloc( 1, sizeof( Attribute ));
1488 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1490 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1491 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1492 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1494 a->a_nvals = a->a_vals;
1496 a->a_next = glue->e_attrs;
1499 op->o_req_dn = glue->e_name;
1500 op->o_req_ndn = glue->e_nname;
1502 rc = be->be_add ( op, &rs );
1503 if ( rc == LDAP_SUCCESS ) {
1504 be_entry_release_w( op, glue );
1506 /* incl. ALREADY EXIST */
1510 /* Move to next child */
1511 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1514 if ( ptr == e->e_name.bv_val ) break;
1516 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1517 for( ptr = ndn.bv_val-2;
1518 ptr > e->e_nname.bv_val && *ptr != ',';
1524 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1527 op->o_req_dn = e->e_name;
1528 op->o_req_ndn = e->e_nname;
1530 rc = be->be_add ( op, &rs );
1531 if ( rc == LDAP_SUCCESS ) {
1532 be_entry_release_w( op, e );
1540 static struct berval ocbva[] = {
1542 BER_BVC("subentry"),
1543 BER_BVC("syncConsumerSubentry"),
1547 static struct berval cnbva[] = {
1552 static struct berval ssbva[] = {
1557 static struct berval scbva[] = {
1563 syncrepl_updateCookie(
1567 struct sync_cookie *syncCookie
1570 Backend *be = op->o_bd;
1572 Modifications *mlnext;
1574 Modifications *modlist = NULL;
1575 Modifications **modtail = &modlist;
1578 char txtbuf[SLAP_TEXT_BUFLEN];
1579 size_t textlen = sizeof txtbuf;
1584 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1585 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1586 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1588 slap_callback cb = { NULL };
1589 SlapReply rs = {REP_RESULT};
1591 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1592 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1594 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1595 mod->sml_op = LDAP_MOD_REPLACE;
1596 mod->sml_desc = slap_schema.si_ad_objectClass;
1597 mod->sml_type = mod->sml_desc->ad_cname;
1598 mod->sml_bvalues = ocbva;
1600 modtail = &mod->sml_next;
1602 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1603 assert( si->si_rid < 1000 );
1604 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1605 slap_syncrepl_bvc.bv_len,
1606 "syncrepl%ld", si->si_rid );
1607 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1608 mod->sml_op = LDAP_MOD_REPLACE;
1609 mod->sml_desc = slap_schema.si_ad_cn;
1610 mod->sml_type = mod->sml_desc->ad_cname;
1611 mod->sml_bvalues = cnbva;
1613 modtail = &mod->sml_next;
1615 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1616 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1617 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1618 mod->sml_op = LDAP_MOD_REPLACE;
1619 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1620 mod->sml_type = mod->sml_desc->ad_cname;
1621 mod->sml_bvalues = scbva;
1623 modtail = &mod->sml_next;
1625 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1626 mod->sml_op = LDAP_MOD_REPLACE;
1627 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1628 mod->sml_type = mod->sml_desc->ad_cname;
1629 mod->sml_bvalues = ssbva;
1631 modtail = &mod->sml_next;
1635 op->o_tag = LDAP_REQ_ADD;
1636 rc = slap_mods_opattrs( op, modlist, modtail,
1637 &text,txtbuf, textlen );
1639 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1640 ml->sml_op = LDAP_MOD_REPLACE;
1643 if( rc != LDAP_SUCCESS ) {
1645 LDAP_LOG( OPERATION, ERR,
1646 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1648 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1653 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1655 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1656 assert( si->si_rid < 1000 );
1657 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1658 slap_syncrepl_cn_bvc.bv_len,
1659 "cn=syncrepl%ld", si->si_rid );
1661 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1663 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1664 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1666 if ( slap_syncrepl_dn_bv.bv_val ) {
1667 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1672 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1674 if( rc != LDAP_SUCCESS ) {
1676 LDAP_LOG( OPERATION, ERR,
1677 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1679 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1684 cb.sc_response = null_callback;
1687 op->o_callback = &cb;
1688 op->o_req_dn = e->e_name;
1689 op->o_req_ndn = e->e_nname;
1691 /* update persistent cookie */
1692 update_cookie_retry:
1693 op->o_tag = LDAP_REQ_MODIFY;
1694 op->orm_modlist = modlist;
1695 rc = be->be_modify( op, &rs );
1697 if ( rc != LDAP_SUCCESS ) {
1698 if ( rc == LDAP_REFERRAL ||
1699 rc == LDAP_NO_SUCH_OBJECT ) {
1700 op->o_tag = LDAP_REQ_ADD;
1702 rc = be->be_add( op, &rs );
1703 if ( rc != LDAP_SUCCESS ) {
1704 if ( rc == LDAP_ALREADY_EXISTS ) {
1705 goto update_cookie_retry;
1706 } else if ( rc == LDAP_REFERRAL ||
1707 rc == LDAP_NO_SUCH_OBJECT ) {
1709 LDAP_LOG( OPERATION, ERR,
1710 "cookie will be non-persistent\n",
1713 Debug( LDAP_DEBUG_ANY,
1714 "cookie will be non-persistent\n",
1719 LDAP_LOG( OPERATION, ERR,
1720 "be_add failed (%d)\n",
1723 Debug( LDAP_DEBUG_ANY,
1724 "be_add failed (%d)\n",
1729 be_entry_release_w( op, e );
1734 LDAP_LOG( OPERATION, ERR,
1735 "be_modify failed (%d)\n", rc, 0, 0 );
1737 Debug( LDAP_DEBUG_ANY,
1738 "be_modify failed (%d)\n", rc, 0, 0 );
1749 if ( cnbva[0].bv_val ) {
1750 ch_free( cnbva[0].bv_val );
1751 cnbva[0].bv_val = NULL;
1753 if ( scbva[0].bv_val ) {
1754 ch_free( scbva[0].bv_val );
1755 scbva[0].bv_val = NULL;
1758 if ( mlnext->sml_next ) {
1759 slap_mods_free( mlnext->sml_next );
1760 mlnext->sml_next = NULL;
1763 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1764 mlnext = ml->sml_next;
1777 syncinfo_t *si = op->o_callback->sc_private;
1779 if ( rs->sr_type == REP_SEARCH ) {
1780 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1782 LDAP_LOG( OPERATION, ERR,
1783 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1785 Debug( LDAP_DEBUG_ANY,
1786 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1789 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1793 return LDAP_SUCCESS;
1797 nonpresent_callback(
1802 syncinfo_t *si = op->o_callback->sc_private;
1805 struct berval* present_uuid = NULL;
1806 struct nonpresent_entry *np_entry;
1808 if ( rs->sr_type == REP_RESULT ) {
1809 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1810 si->si_presentlist = NULL;
1812 } else if ( rs->sr_type == REP_SEARCH ) {
1813 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1815 if ( a == NULL ) return 0;
1817 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1820 if ( present_uuid == NULL ) {
1821 np_entry = (struct nonpresent_entry *)
1822 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1823 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1824 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1825 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1828 avl_delete( &si->si_presentlist,
1829 &a->a_vals[0], syncuuid_cmp );
1830 ch_free( present_uuid->bv_val );
1831 ch_free( present_uuid );
1834 return LDAP_SUCCESS;
1842 if ( rs->sr_err != LDAP_SUCCESS &&
1843 rs->sr_err != LDAP_REFERRAL &&
1844 rs->sr_err != LDAP_ALREADY_EXISTS &&
1845 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1848 LDAP_LOG( OPERATION, ERR,
1849 "null_callback : error code 0x%x\n",
1852 Debug( LDAP_DEBUG_ANY,
1853 "null_callback : error code 0x%x\n",
1857 return LDAP_SUCCESS;
1861 slap_create_syncrepl_entry(
1863 struct berval *context_csn,
1871 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1873 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1875 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1878 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1880 if ( context_csn ) {
1881 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1882 context_csn, NULL );
1886 bv.bv_len = sizeof("{}")-1;
1887 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1889 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1890 ber_dupbv( &e->e_nname, &e->e_name );
1896 slap_uuidstr_from_normalized(
1897 struct berval* uuidstr,
1898 struct berval* normalized,
1902 unsigned char nibble;
1905 if ( normalized == NULL )
1908 if ( normalized->bv_len != 16 ) {
1915 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1920 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1922 sl_free( new, ctx );
1926 for ( i = 0; i < 16; i++ ) {
1927 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1928 new->bv_val[(i<<1)+d] = '-';
1932 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1933 if ( nibble < 10 ) {
1934 new->bv_val[(i<<1)+d] = nibble + '0';
1936 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1939 nibble = (normalized->bv_val[i]) & 0xF;
1940 if ( nibble < 10 ) {
1941 new->bv_val[(i<<1)+d+1] = nibble + '0';
1943 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1947 new->bv_val[new->bv_len] = '\0';
1953 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1955 const struct berval *uuid1 = v_uuid1;
1956 const struct berval *uuid2 = v_uuid2;
1957 int rc = uuid1->bv_len - uuid2->bv_len;
1958 if ( rc ) return rc;
1959 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1963 avl_ber_bvfree( void *bv )
1968 if ( ((struct berval *)bv)->bv_val != NULL ) {
1969 ch_free ( ((struct berval *)bv)->bv_val );
1971 ch_free ( (char *) bv );