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);
54 syncrepl_del_nonpresent( Operation *, syncinfo_t * );
56 /* callback functions */
57 static int dn_callback( struct slap_op *, struct slap_rep * );
58 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
59 static int null_callback( struct slap_op *, struct slap_rep * );
61 static AttributeDescription *sync_descs[4];
63 struct runqueue_s syncrepl_rq;
66 init_syncrepl(syncinfo_t *si)
71 if ( !sync_descs[0] ) {
72 sync_descs[0] = slap_schema.si_ad_objectClass;
73 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
74 sync_descs[2] = slap_schema.si_ad_entryCSN;
78 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
81 /* Delete Attributes */
82 for ( i = 0; sync_descs[i] != NULL; i++ ) {
83 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
84 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
87 ch_free( si->si_attrs[j] );
88 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
89 si->si_attrs[k] = si->si_attrs[k+1];
94 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
95 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
98 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
100 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
104 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
107 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
109 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
112 tmp[ n++ ] = ch_strdup( "*" );
119 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
120 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
121 si->si_attrs[ n ] = NULL;
129 struct berval *syncCookie )
131 BerElementBuffer berbuf;
132 BerElement *ber = (BerElement *)&berbuf;
133 LDAPControl c[2], *ctrls[3];
134 struct timeval timeout;
138 /* setup LDAP SYNC control */
139 ber_init2( ber, NULL, LBER_USE_DER );
140 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
143 ber_printf( ber, "{eO}", abs(si->si_type), syncCookie );
145 ber_printf( ber, "{e}", abs(si->si_type) );
148 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
153 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
154 c[0].ldctl_iscritical = si->si_type < 0;
157 if ( si->si_authzId ) {
158 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
159 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
160 c[1].ldctl_iscritical = 1;
167 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
170 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
171 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
172 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
173 si->si_slimit, &msgid );
179 static const Listener dummy_list = { {0, ""}, {0, ""} };
188 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
189 struct berval syncrepl_cn_bv;
190 BerVarray syncCookie;
192 si->si_sync_mode = LDAP_SYNC_STATE_MODE;
194 /* Init connection to master */
196 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
197 if ( rc != LDAP_SUCCESS ) {
199 LDAP_LOG( OPERATION, ERR,
200 "do_syncrepl: ldap_initialize failed (%s)\n",
201 si->si_provideruri, 0, 0 );
203 Debug( LDAP_DEBUG_ANY,
204 "do_syncrepl: ldap_initialize failed (%s)\n",
205 si->si_provideruri, 0, 0 );
210 op->o_protocol = LDAP_VERSION3;
211 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
216 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
217 if( rc != LDAP_SUCCESS ) {
219 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
220 "%s: ldap_start_tls failed (%d)\n",
221 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
224 Debug( LDAP_DEBUG_ANY,
225 "%s: ldap_start_tls failed (%d)\n",
226 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
229 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
233 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
234 #ifdef HAVE_CYRUS_SASL
237 if ( si->si_secprops != NULL ) {
238 rc = ldap_set_option( si->si_ld,
239 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
241 if( rc != LDAP_OPT_SUCCESS ) {
243 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
244 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
245 si->si_provideruri, si->si_secprops, 0 );
247 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
248 "(%s,SECPROPS,\"%s\") failed!\n",
249 si->si_provideruri, si->si_secprops, 0 );
255 defaults = lutil_sasl_defaults( si->si_ld,
256 si->si_saslmech, si->si_realm,
257 si->si_authcId, si->si_passwd, si->si_authzId );
259 rc = ldap_sasl_interactive_bind_s( si->si_ld,
267 lutil_sasl_freedefs( defaults );
269 /* FIXME : different error behaviors according to
271 * 2) on err policy : exit, retry, backoff ...
273 if ( rc != LDAP_SUCCESS ) {
275 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
276 "ldap_sasl_interactive_bind_s failed (%d)\n",
279 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
280 "ldap_sasl_interactive_bind_s failed (%d)\n",
285 #else /* HAVE_CYRUS_SASL */
286 /* Should never get here, we trapped this at config time */
287 fprintf( stderr, "not compiled with SASL support\n" );
292 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
293 if ( rc != LDAP_SUCCESS ) {
295 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
296 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
298 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
299 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
305 /* get syncrepl cookie of shadow replica from subentry */
307 assert( si->si_id < 1000 );
308 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
309 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
310 CN_STR "syncrepl%d", si->si_id );
311 build_new_dn( &op->o_req_ndn, &si->si_base, &syncrepl_cn_bv,
313 op->o_req_dn = op->o_req_ndn;
316 backend_attribute( op, NULL, &op->o_req_ndn,
317 slap_schema.si_ad_syncreplCookie, &syncCookie );
319 rc = ldap_sync_search( si, op->o_tmpmemctx, syncCookie );
320 if( rc != LDAP_SUCCESS ) {
322 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
323 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
325 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
326 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
330 ber_dupbv( &si->si_syncCookie, syncCookie );
331 ber_bvarray_free_x( syncCookie, op->o_tmpmemctx );
337 ldap_unbind( si->si_ld );
350 LDAPControl **rctrls = NULL;
353 BerElementBuffer berbuf;
354 BerElement *ber = (BerElement *)&berbuf;
356 LDAPMessage *res = NULL;
357 LDAPMessage *msg = NULL;
360 struct berval *retdata = NULL;
362 int sync_info_arrived = 0;
366 struct berval syncUUID = { 0, NULL };
367 struct berval syncCookie = { 0, NULL };
368 struct berval syncCookie_req;
373 int syncinfo_arrived = 0;
380 Modifications *modlist = NULL;
385 struct timeval *tout_p = NULL;
386 struct timeval tout = { 0, 0 };
388 if ( slapd_abrupt_shutdown ) {
394 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
396 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
399 op->o_callback = &cb;
401 psub = &si->si_be->be_nsuffix[0];
403 syncCookie_req = si->si_syncCookie;
405 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
411 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
414 if ( slapd_abrupt_shutdown ) {
418 for( msg = ldap_first_message( si->si_ld, res );
420 msg = ldap_next_message( si->si_ld, msg ) )
422 syncCookie.bv_len = 0; syncCookie.bv_val = NULL;
423 switch( ldap_msgtype( msg ) ) {
424 case LDAP_RES_SEARCH_ENTRY:
425 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
428 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
429 ber_scanf( ber, "{em", &syncstate, &syncUUID );
430 if ( ber_peek_tag( ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
431 ber_scanf( ber, "m}", &syncCookie );
434 entry = syncrepl_message_to_entry( si, op, msg,
435 &modlist, syncstate );
436 rc_efree = syncrepl_entry( si, op, entry, modlist,
437 syncstate, &syncUUID, !syncinfo_arrived );
438 if ( syncCookie.bv_len ) {
439 syncrepl_updateCookie( si, op, psub, &syncCookie );
442 ldap_controls_free( rctrls );
445 slap_mods_free( modlist );
447 if ( rc_efree && entry ) {
452 case LDAP_RES_SEARCH_REFERENCE:
454 LDAP_LOG( OPERATION, ERR,
455 "do_syncrepl : reference received\n", 0, 0, 0 );
457 Debug( LDAP_DEBUG_ANY,
458 "do_syncrepl : reference received\n", 0, 0, 0 );
462 case LDAP_RES_SEARCH_RESULT:
463 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
467 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
469 ber_scanf( ber, "{" /*"}"*/);
470 if ( ber_peek_tag( ber, &len ) == LDAP_SYNC_TAG_COOKIE )
472 ber_scanf( ber, "m", &syncCookie );
475 value_match( &match, slap_schema.si_ad_entryCSN,
476 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
477 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
478 &syncCookie_req, &syncCookie, &text );
479 if ( syncCookie.bv_len && match < 0) {
480 syncrepl_updateCookie( si, op,
484 ldap_controls_free( rctrls );
486 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
487 /* FIXME : different error behaviors according to
488 * 1) err code : LDAP_BUSY ...
489 * 2) on err policy : stop service, stop sync, retry
491 if ( si->si_sync_mode == LDAP_SYNC_STATE_MODE && match
494 syncrepl_del_nonpresent( op, si );
501 case LDAP_RES_INTERMEDIATE:
502 rc = ldap_parse_intermediate( si->si_ld, msg,
503 &retoid, &retdata, NULL, 0 );
504 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
505 sync_info_arrived = 1;
506 ber_init2( ber, retdata, LBER_USE_DER );
507 ber_scanf( ber, "{e" /*"}"*/, &syncstate );
509 if ( ber_peek_tag( ber, &len )
510 == LDAP_SYNC_TAG_COOKIE ) {
511 ber_scanf( ber, /*"{"*/ "m}", &syncCookie );
513 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
515 LDAP_LOG( OPERATION, ERR,
516 "do_syncrepl : cookie required\n", 0, 0, 0 );
518 Debug( LDAP_DEBUG_ANY,
519 "do_syncrepl : cookie required\n", 0, 0, 0 );
524 value_match( &match, slap_schema.si_ad_entryCSN,
525 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
526 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
527 &syncCookie_req, &syncCookie, &text );
529 if ( syncCookie.bv_len && match < 0 ) {
530 syncrepl_updateCookie( si, op, psub, &syncCookie);
533 if ( syncstate == LDAP_SYNC_STATE_MODE_DONE ) {
535 syncrepl_del_nonpresent( op, si );
537 si->si_sync_mode = LDAP_SYNC_LOG_MODE;
538 } else if ( syncstate == LDAP_SYNC_LOG_MODE_DONE ) {
539 si->si_sync_mode = LDAP_SYNC_PERSIST_MODE;
540 } else if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
541 si->si_sync_mode = LDAP_SYNC_PERSIST_MODE;
542 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ||
543 syncstate != LDAP_SYNC_LOG_MODE_DONE )
546 LDAP_LOG( OPERATION, ERR,
547 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
549 Debug( LDAP_DEBUG_ANY,
550 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
554 ldap_memfree( retoid );
555 ber_bvfree( retdata );
559 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
560 " unknown intermediate "
561 "response\n", 0, 0, 0 );
563 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
564 "unknown intermediate response (%d)\n",
567 ldap_memfree( retoid );
568 ber_bvfree( retdata );
574 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
575 "unknown message\n", 0, 0, 0 );
577 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
578 "unknown message\n", 0, 0, 0 );
591 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
592 errstr = ldap_err2string( rc );
595 LDAP_LOG( OPERATION, ERR,
596 "do_syncrepl : %s\n", errstr, 0, 0 );
598 Debug( LDAP_DEBUG_ANY,
599 "do_syncrepl : %s\n", errstr, 0, 0 );
604 if ( res ) ldap_msgfree( res );
606 if ( rc && si->si_ld ) {
607 ldap_unbind( si->si_ld );
619 struct re_s* rtask = arg;
620 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
621 Connection conn = {0};
623 int rc = LDAP_SUCCESS;
629 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
631 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
637 switch( abs( si->si_type )) {
638 case LDAP_SYNC_REFRESH_ONLY:
639 case LDAP_SYNC_REFRESH_AND_PERSIST:
645 if ( slapd_abrupt_shutdown && si->si_ld ) {
646 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
647 connection_client_stop( s );
648 ldap_unbind( si->si_ld );
654 conn.c_send_ldap_result = slap_send_ldap_result;
655 conn.c_send_search_entry = slap_send_search_entry;
656 conn.c_send_search_reference = slap_send_search_reference;
657 conn.c_listener = (Listener *)&dummy_list;
658 conn.c_peer_name = slap_empty_bv;
660 /* set memory context */
661 #define SLAB_SIZE 1048576
662 op.o_tmpmemctx = sl_mem_create( SLAB_SIZE, ctx );
663 op.o_tmpmfuncs = &sl_mfuncs;
665 op.o_dn = si->si_updatedn;
666 op.o_ndn = si->si_updatedn;
667 op.o_time = slap_get_time();
668 op.o_threadctx = ctx;
669 op.o_managedsait = 1;
672 op.o_connid = op.o_conn->c_connid;
674 /* Establish session, do search */
677 rc = do_syncrep1( &op, si );
680 /* Process results */
681 if ( rc == LDAP_SUCCESS ) {
682 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
684 rc = do_syncrep2( &op, si );
686 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
687 /* If we succeeded, enable the connection for further listening.
688 * If we failed, tear down the connection and reschedule.
690 if ( rc == LDAP_SUCCESS ) {
692 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
695 connection_client_enable( s );
701 ch_free( si->si_syncCookie.bv_val );
702 si->si_syncCookie.bv_val = NULL;
703 si->si_syncCookie.bv_len = 0;
707 /* At this point, we have 4 cases:
708 * 1) for any hard failure, give up and remove this task
709 * 2) for ServerDown, reschedule this task to run
710 * 3) for Refresh and Success, reschedule to run
711 * 4) for Persist and Success, reschedule to defer
713 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
714 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
715 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
719 connection_client_stop( s );
722 if ( rc && rc != LDAP_SERVER_DOWN ) {
723 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
725 if ( rc == LDAP_SERVER_DOWN ||
726 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
731 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
733 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
739 syncrepl_message_to_entry(
743 Modifications **modlist,
748 BerElement *ber = NULL;
751 Modifications **modtail = modlist;
754 char txtbuf[SLAP_TEXT_BUFLEN];
755 size_t textlen = sizeof txtbuf;
757 struct berval bdn = {0, NULL}, dn, ndn;
762 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
764 LDAP_LOG( OPERATION, ERR,
765 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
767 Debug( LDAP_DEBUG_ANY,
768 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
773 op->o_tag = LDAP_REQ_ADD;
775 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
777 if ( rc != LDAP_SUCCESS ) {
779 LDAP_LOG( OPERATION, ERR,
780 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
782 Debug( LDAP_DEBUG_ANY,
783 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
788 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
789 ber_dupbv( &op->o_req_dn, &dn );
790 ber_dupbv( &op->o_req_ndn, &ndn );
791 sl_free( ndn.bv_val, op->o_tmpmemctx );
792 sl_free( dn.bv_val, op->o_tmpmemctx );
794 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
798 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
799 e->e_name = op->o_req_dn;
800 e->e_nname = op->o_req_ndn;
802 while ( ber_remaining( ber ) ) {
803 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
804 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
809 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
811 mod->sml_op = LDAP_MOD_REPLACE;
812 mod->sml_next = NULL;
813 mod->sml_desc = NULL;
814 mod->sml_type = tmp.sml_type;
815 mod->sml_bvalues = tmp.sml_bvalues;
816 mod->sml_nvalues = NULL;
819 modtail = &mod->sml_next;
822 if ( *modlist == NULL ) {
824 LDAP_LOG( OPERATION, ERR,
825 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
827 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
832 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
834 if ( rc != LDAP_SUCCESS ) {
836 LDAP_LOG( OPERATION, ERR,
837 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
839 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
845 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
846 if( rc != LDAP_SUCCESS ) {
848 LDAP_LOG( OPERATION, ERR,
849 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
851 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
858 if ( rc != LDAP_SUCCESS ) {
867 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
869 const struct berval *uuid1 = v_uuid1;
870 const struct berval *uuid2 = v_uuid2;
871 int rc = uuid1->bv_len - uuid2->bv_len;
873 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
881 Modifications* modlist,
883 struct berval* syncUUID,
887 Backend *be = op->o_bd;
889 struct berval *syncuuid_bv = NULL;
891 SlapReply rs = {REP_RESULT};
893 AttributeAssertion ava = {0};
894 int rc = LDAP_SUCCESS;
895 int ret = LDAP_SUCCESS;
898 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
900 syncuuid_bv = ber_dupbv( NULL, syncUUID );
901 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
902 syncuuid_cmp, avl_dup_error );
905 if ( syncstate == LDAP_SYNC_PRESENT ) {
909 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
910 op->ors_filterstr.bv_val = (char *) sl_malloc(
911 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
912 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
913 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
914 syncUUID->bv_val, syncUUID->bv_len );
915 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
917 f.f_choice = LDAP_FILTER_EQUALITY;
919 ava.aa_desc = slap_schema.si_ad_entryUUID;
920 ava.aa_value = *syncUUID;
922 op->ors_scope = LDAP_SCOPE_SUBTREE;
924 /* get syncrepl cookie of shadow replica from subentry */
925 op->o_req_dn = si->si_base;
926 op->o_req_ndn = si->si_base;
928 /* set callback function */
929 op->o_callback = &cb;
930 cb.sc_response = dn_callback;
933 si->si_syncUUID_ndn.bv_val = NULL;
935 rc = be->be_search( op, &rs );
937 if ( op->ors_filterstr.bv_val ) {
938 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
941 cb.sc_response = null_callback;
944 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val &&
945 si->si_sync_mode != LDAP_SYNC_LOG_MODE )
947 op->o_req_dn = si->si_syncUUID_ndn;
948 op->o_req_ndn = si->si_syncUUID_ndn;
949 op->o_tag = LDAP_REQ_DELETE;
950 rc = be->be_delete( op, &rs );
953 switch ( syncstate ) {
955 case LDAP_SYNC_MODIFY:
956 if ( rc == LDAP_SUCCESS ||
957 rc == LDAP_REFERRAL ||
958 rc == LDAP_NO_SUCH_OBJECT )
960 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
961 attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID,
962 syncUUID, op->o_tmpmemctx );
964 op->o_tag = LDAP_REQ_ADD;
966 op->o_req_dn = e->e_name;
967 op->o_req_ndn = e->e_nname;
968 rc = be->be_add( op, &rs );
970 if ( rc != LDAP_SUCCESS ) {
971 if ( rc == LDAP_ALREADY_EXISTS ) {
972 op->o_tag = LDAP_REQ_MODIFY;
973 op->orm_modlist = modlist;
974 op->o_req_dn = e->e_name;
975 op->o_req_ndn = e->e_nname;
976 rc = be->be_modify( op, &rs );
977 if ( rc != LDAP_SUCCESS ) {
979 LDAP_LOG( OPERATION, ERR,
980 "syncrepl_entry : be_modify failed (%d)\n",
983 Debug( LDAP_DEBUG_ANY,
984 "syncrepl_entry : be_modify failed (%d)\n",
990 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
991 syncrepl_add_glue( op, e );
996 LDAP_LOG( OPERATION, ERR,
997 "syncrepl_entry : be_add failed (%d)\n",
1000 Debug( LDAP_DEBUG_ANY,
1001 "syncrepl_entry : be_add failed (%d)\n",
1008 be_entry_release_w( op, e );
1014 LDAP_LOG( OPERATION, ERR,
1015 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1017 Debug( LDAP_DEBUG_ANY,
1018 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1024 case LDAP_SYNC_DELETE :
1025 if ( si->si_sync_mode == LDAP_SYNC_LOG_MODE ) {
1026 if ( si->si_syncUUID_ndn.bv_val ) {
1027 op->o_req_dn = si->si_syncUUID_ndn;
1028 op->o_req_ndn = si->si_syncUUID_ndn;
1029 op->o_tag = LDAP_REQ_DELETE;
1030 rc = be->be_delete( op, &rs );
1033 /* Already deleted otherwise */
1039 LDAP_LOG( OPERATION, ERR,
1040 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1042 Debug( LDAP_DEBUG_ANY,
1043 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1051 if ( si->si_syncUUID_ndn.bv_val ) {
1052 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1058 syncrepl_del_nonpresent(
1063 Backend* be = op->o_bd;
1065 SlapReply rs = {REP_RESULT};
1066 struct nonpresent_entry *np_list, *np_prev;
1068 op->o_req_dn = si->si_base;
1069 op->o_req_ndn = si->si_base;
1071 cb.sc_response = nonpresent_callback;
1074 op->o_callback = &cb;
1075 op->o_tag = LDAP_REQ_SEARCH;
1076 op->ors_scope = si->si_scope;
1077 op->ors_deref = LDAP_DEREF_NEVER;
1080 op->ors_attrsonly = 0;
1081 op->ors_attrs = NULL;
1082 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1083 op->ors_filterstr = si->si_filterstr;
1085 op->o_nocaching = 1;
1086 be->be_search( op, &rs );
1087 op->o_nocaching = 0;
1089 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1091 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1092 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1093 while ( np_list != NULL ) {
1094 LDAP_LIST_REMOVE( np_list, npe_link );
1096 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1097 op->o_tag = LDAP_REQ_DELETE;
1098 op->o_callback = &cb;
1099 cb.sc_response = null_callback;
1101 op->o_req_dn = *np_prev->npe_name;
1102 op->o_req_ndn = *np_prev->npe_nname;
1103 op->o_bd->be_delete( op, &rs );
1104 ber_bvfree( np_prev->npe_name );
1105 ber_bvfree( np_prev->npe_nname );
1106 op->o_req_dn.bv_val = NULL;
1107 op->o_req_ndn.bv_val = NULL;
1116 static struct berval gcbva[] = {
1127 Backend *be = op->o_bd;
1133 struct berval dn = {0, NULL};
1134 struct berval ndn = {0, NULL};
1136 SlapReply rs = {REP_RESULT};
1139 op->o_tag = LDAP_REQ_ADD;
1140 op->o_callback = &cb;
1141 cb.sc_response = null_callback;
1142 cb.sc_private = NULL;
1147 /* count RDNs in suffix */
1148 if ( be->be_nsuffix[0].bv_len ) {
1149 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1159 /* Start with BE suffix */
1160 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1161 comma = strrchr(dn.bv_val, ',');
1162 if ( ptr ) *ptr = ',';
1163 if ( comma ) *comma = '\0';
1168 dn.bv_len -= ptr - dn.bv_val;
1171 /* the normalizedDNs are always the same length, no counting
1174 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1175 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1176 ndn.bv_len = be->be_nsuffix[0].bv_len;
1179 while ( ndn.bv_val > e->e_nname.bv_val ) {
1180 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1181 ber_dupbv( &glue->e_name, &dn );
1182 ber_dupbv( &glue->e_nname, &ndn );
1184 a = ch_calloc( 1, sizeof( Attribute ));
1185 a->a_desc = slap_schema.si_ad_objectClass;
1187 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1188 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1189 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1190 a->a_vals[2].bv_len = 0;
1191 a->a_vals[2].bv_val = NULL;
1193 a->a_nvals = a->a_vals;
1195 a->a_next = glue->e_attrs;
1198 a = ch_calloc( 1, sizeof( Attribute ));
1199 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1201 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1202 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1203 a->a_vals[1].bv_len = 0;
1204 a->a_vals[1].bv_val = NULL;
1206 a->a_nvals = a->a_vals;
1208 a->a_next = glue->e_attrs;
1211 op->o_req_dn = glue->e_name;
1212 op->o_req_ndn = glue->e_nname;
1214 rc = be->be_add ( op, &rs );
1215 if ( rc == LDAP_SUCCESS ) {
1216 be_entry_release_w( op, glue );
1218 /* incl. ALREADY EXIST */
1222 /* Move to next child */
1223 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1226 if ( ptr == e->e_name.bv_val ) break;
1228 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1229 for( ptr = ndn.bv_val-2;
1230 ptr > e->e_nname.bv_val && *ptr != ',';
1236 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1239 op->o_req_dn = e->e_name;
1240 op->o_req_ndn = e->e_nname;
1242 rc = be->be_add ( op, &rs );
1243 if ( rc == LDAP_SUCCESS ) {
1244 be_entry_release_w( op, e );
1252 static struct berval ocbva[] = {
1254 BER_BVC("subentry"),
1255 BER_BVC("syncConsumerSubentry"),
1259 static struct berval cnbva[] = {
1264 static struct berval ssbva[] = {
1269 static struct berval scbva[] = {
1275 syncrepl_updateCookie(
1279 struct berval *syncCookie
1282 Backend *be = op->o_bd;
1284 Modifications *mlnext;
1286 Modifications *modlist = NULL;
1287 Modifications **modtail = &modlist;
1290 char txtbuf[SLAP_TEXT_BUFLEN];
1291 size_t textlen = sizeof txtbuf;
1296 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1297 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1298 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1301 SlapReply rs = {REP_RESULT};
1303 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1304 mod->sml_op = LDAP_MOD_REPLACE;
1305 mod->sml_desc = slap_schema.si_ad_objectClass;
1306 mod->sml_type = mod->sml_desc->ad_cname;
1307 mod->sml_bvalues = ocbva;
1309 modtail = &mod->sml_next;
1311 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1312 assert( si->si_id < 1000 );
1313 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1314 slap_syncrepl_bvc.bv_len,
1315 "syncrepl%d", si->si_id );
1316 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1317 mod->sml_op = LDAP_MOD_REPLACE;
1318 mod->sml_desc = slap_schema.si_ad_cn;
1319 mod->sml_type = mod->sml_desc->ad_cname;
1320 mod->sml_bvalues = cnbva;
1322 modtail = &mod->sml_next;
1324 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1325 ber_dupbv( &scbva[0], syncCookie );
1326 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1327 mod->sml_op = LDAP_MOD_REPLACE;
1328 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1329 mod->sml_type = mod->sml_desc->ad_cname;
1330 mod->sml_bvalues = scbva;
1332 modtail = &mod->sml_next;
1334 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1335 mod->sml_op = LDAP_MOD_REPLACE;
1336 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1337 mod->sml_type = mod->sml_desc->ad_cname;
1338 mod->sml_bvalues = ssbva;
1340 modtail = &mod->sml_next;
1344 op->o_tag = LDAP_REQ_ADD;
1345 rc = slap_mods_opattrs( op, modlist, modtail,
1346 &text,txtbuf, textlen );
1348 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1349 ml->sml_op = LDAP_MOD_REPLACE;
1352 if( rc != LDAP_SUCCESS ) {
1354 LDAP_LOG( OPERATION, ERR,
1355 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1357 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1362 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1364 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1365 assert( si->si_id < 1000 );
1366 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1367 slap_syncrepl_cn_bvc.bv_len,
1368 "cn=syncrepl%d", si->si_id );
1370 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1372 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1373 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1375 if ( slap_syncrepl_dn_bv.bv_val ) {
1376 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1381 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1383 if( rc != LDAP_SUCCESS ) {
1385 LDAP_LOG( OPERATION, ERR,
1386 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1388 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1393 cb.sc_response = null_callback;
1396 op->o_callback = &cb;
1397 op->o_req_dn = e->e_name;
1398 op->o_req_ndn = e->e_nname;
1400 /* update persistent cookie */
1401 update_cookie_retry:
1402 op->o_tag = LDAP_REQ_MODIFY;
1403 op->orm_modlist = modlist;
1404 rc = be->be_modify( op, &rs );
1406 if ( rc != LDAP_SUCCESS ) {
1407 if ( rc == LDAP_REFERRAL ||
1408 rc == LDAP_NO_SUCH_OBJECT ) {
1409 op->o_tag = LDAP_REQ_ADD;
1411 rc = be->be_add( op, &rs );
1412 if ( rc != LDAP_SUCCESS ) {
1413 if ( rc == LDAP_ALREADY_EXISTS ) {
1414 goto update_cookie_retry;
1415 } else if ( rc == LDAP_REFERRAL ||
1416 rc == LDAP_NO_SUCH_OBJECT ) {
1418 LDAP_LOG( OPERATION, ERR,
1419 "cookie will be non-persistent\n",
1422 Debug( LDAP_DEBUG_ANY,
1423 "cookie will be non-persistent\n",
1428 LDAP_LOG( OPERATION, ERR,
1429 "be_add failed (%d)\n",
1432 Debug( LDAP_DEBUG_ANY,
1433 "be_add failed (%d)\n",
1438 be_entry_release_w( op, e );
1443 LDAP_LOG( OPERATION, ERR,
1444 "be_modify failed (%d)\n", rc, 0, 0 );
1446 Debug( LDAP_DEBUG_ANY,
1447 "be_modify failed (%d)\n", rc, 0, 0 );
1458 if ( cnbva[0].bv_val ) {
1459 ch_free( cnbva[0].bv_val );
1460 cnbva[0].bv_val = NULL;
1462 if ( scbva[0].bv_val ) {
1463 ch_free( scbva[0].bv_val );
1464 scbva[0].bv_val = NULL;
1467 if ( mlnext->sml_next ) {
1468 slap_mods_free( mlnext->sml_next );
1469 mlnext->sml_next = NULL;
1472 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1473 mlnext = ml->sml_next;
1481 avl_ber_bvfree( void *bv )
1486 if ( ((struct berval *)bv)->bv_val != NULL ) {
1487 ch_free ( ((struct berval *)bv)->bv_val );
1489 ch_free ( (char *) bv );
1498 syncinfo_t *si = op->o_callback->sc_private;
1500 if ( rs->sr_type == REP_SEARCH ) {
1501 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1503 LDAP_LOG( OPERATION, ERR,
1504 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1506 Debug( LDAP_DEBUG_ANY,
1507 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1510 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1514 return LDAP_SUCCESS;
1518 nonpresent_callback(
1523 syncinfo_t *si = op->o_callback->sc_private;
1526 struct berval* present_uuid = NULL;
1527 struct nonpresent_entry *np_entry;
1529 if ( rs->sr_type == REP_RESULT ) {
1530 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1531 si->si_presentlist = NULL;
1533 } else if ( rs->sr_type == REP_SEARCH ) {
1534 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1536 if ( a == NULL ) return 0;
1538 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1541 if ( present_uuid == NULL ) {
1542 np_entry = (struct nonpresent_entry *)
1543 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1544 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1545 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1546 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1549 avl_delete( &si->si_presentlist,
1550 &a->a_vals[0], syncuuid_cmp );
1551 ch_free( present_uuid->bv_val );
1552 ch_free( present_uuid );
1555 return LDAP_SUCCESS;
1564 if ( rs->sr_err != LDAP_SUCCESS &&
1565 rs->sr_err != LDAP_REFERRAL &&
1566 rs->sr_err != LDAP_ALREADY_EXISTS &&
1567 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1570 LDAP_LOG( OPERATION, ERR,
1571 "null_callback : error code 0x%x\n",
1574 Debug( LDAP_DEBUG_ANY,
1575 "null_callback : error code 0x%x\n",
1579 return LDAP_SUCCESS;
1583 slap_create_syncrepl_entry(
1585 struct berval *context_csn,
1594 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1596 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1598 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1601 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1603 if ( context_csn ) {
1604 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1605 context_csn, NULL );
1609 bv.bv_len = sizeof("{}")-1;
1610 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1612 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1613 ber_dupbv( &e->e_nname, &e->e_name );