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 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 };
442 Modifications *modlist = NULL;
447 struct timeval *tout_p = NULL;
448 struct timeval tout = { 0, 0 };
450 int refreshDeletes = 0;
452 BerVarray syncUUIDs = NULL;
455 if ( slapd_abrupt_shutdown ) {
460 ber_init2( ber, NULL, LBER_USE_DER );
461 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
464 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
466 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
469 op->o_callback = &cb;
471 psub = &si->si_be->be_nsuffix[0];
473 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
475 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
481 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
484 if ( slapd_abrupt_shutdown ) {
488 for( msg = ldap_first_message( si->si_ld, res );
490 msg = ldap_next_message( si->si_ld, msg ) )
492 switch( ldap_msgtype( msg ) ) {
493 case LDAP_RES_SEARCH_ENTRY:
494 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
495 /* we can't work without the control */
501 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
502 ber_scanf( ber, "{em", &syncstate, &syncUUID );
503 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
504 ber_scanf( ber, "m}", &cookie );
505 if ( cookie.bv_val ) {
506 struct berval tmp_bv;
507 ber_dupbv( &tmp_bv, &cookie );
508 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
510 if ( syncCookie.octet_str &&
511 syncCookie.octet_str[0].bv_val )
512 slap_parse_sync_cookie( &syncCookie );
514 entry = syncrepl_message_to_entry( si, op, msg,
515 &modlist, syncstate );
516 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
517 &syncUUID, &syncCookie_req );
518 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
519 syncrepl_updateCookie( si, op, psub, &syncCookie );
521 ldap_controls_free( rctrls );
523 slap_mods_free( modlist );
525 if ( rc_efree && entry ) {
530 case LDAP_RES_SEARCH_REFERENCE:
532 LDAP_LOG( OPERATION, ERR,
533 "do_syncrep2 : reference received\n", 0, 0, 0 );
535 Debug( LDAP_DEBUG_ANY,
536 "do_syncrep2 : reference received\n", 0, 0, 0 );
540 case LDAP_RES_SEARCH_RESULT:
541 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
545 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
547 ber_scanf( ber, "{" /*"}"*/);
548 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
550 ber_scanf( ber, "m", &cookie );
551 if ( cookie.bv_val ) {
552 struct berval tmp_bv;
553 ber_dupbv( &tmp_bv, &cookie );
554 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
556 if ( syncCookie.octet_str &&
557 syncCookie.octet_str[0].bv_val )
558 slap_parse_sync_cookie( &syncCookie );
560 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
562 ber_scanf( ber, "b", &refreshDeletes );
564 ber_scanf( ber, "}" );
566 if ( syncCookie_req.ctxcsn == NULL ) {
568 } else if ( syncCookie.ctxcsn == NULL ) {
571 value_match( &match, slap_schema.si_ad_entryCSN,
572 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
573 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
574 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
576 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
578 syncrepl_updateCookie( si, op, psub, &syncCookie );
581 ldap_controls_free( rctrls );
583 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
584 /* FIXME : different error behaviors according to
585 * 1) err code : LDAP_BUSY ...
586 * 2) on err policy : stop service, stop sync, retry
588 if ( refreshDeletes == 0 && match < 0 ) {
589 syncrepl_del_nonpresent( op, si );
591 avl_free( si->si_presentlist, avl_ber_bvfree );
592 si->si_presentlist = NULL;
599 case LDAP_RES_INTERMEDIATE:
600 rc = ldap_parse_intermediate( si->si_ld, msg,
601 &retoid, &retdata, NULL, 0 );
602 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
603 int si_refreshDelete = 0;
604 int si_refreshPresent = 0;
605 ber_init2( ber, retdata, LBER_USE_DER );
607 switch ( si_tag = ber_peek_tag( ber, &len )) {
609 case LDAP_TAG_SYNC_NEW_COOKIE:
610 ber_scanf( ber, "tm", &tag, &cookie );
612 case LDAP_TAG_SYNC_REFRESH_DELETE:
613 si_refreshDelete = 1;
614 case LDAP_TAG_SYNC_REFRESH_PRESENT:
615 si_refreshPresent = 1;
616 ber_scanf( ber, "t{", &tag );
617 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
619 ber_scanf( ber, "m", &cookie );
620 if ( cookie.bv_val ) {
621 struct berval tmp_bv;
622 ber_dupbv( &tmp_bv, &cookie );
623 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
625 if ( syncCookie.octet_str &&
626 syncCookie.octet_str[0].bv_val )
627 slap_parse_sync_cookie( &syncCookie );
629 if ( ber_peek_tag( ber, &len ) ==
630 LDAP_TAG_REFRESHDONE )
632 ber_scanf( ber, "b", &refreshDone );
634 ber_scanf( ber, "}" );
636 case LDAP_TAG_SYNC_ID_SET:
637 ber_scanf( ber, "t{", &tag );
638 if ( ber_peek_tag( ber, &len ) ==
639 LDAP_TAG_SYNC_COOKIE ) {
640 ber_scanf( ber, "m", &cookie );
641 if ( cookie.bv_val ) {
642 struct berval tmp_bv;
643 ber_dupbv( &tmp_bv, &cookie );
644 ber_bvarray_add( &syncCookie.octet_str,
647 if ( syncCookie.octet_str &&
648 syncCookie.octet_str[0].bv_val )
649 slap_parse_sync_cookie( &syncCookie );
651 if ( ber_peek_tag( ber, &len ) ==
652 LDAP_TAG_REFRESHDELETES )
654 ber_scanf( ber, "b", &refreshDeletes );
656 ber_scanf( ber, "[W]", &syncUUIDs );
657 ber_scanf( ber, "}" );
658 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
659 struct berval *syncuuid_bv;
660 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
661 avl_insert( &si->si_presentlist,
662 (caddr_t) syncuuid_bv,
663 syncuuid_cmp, avl_dup_error );
665 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
669 LDAP_LOG( OPERATION, ERR,
670 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
671 (long) si_tag, 0, 0 );
673 Debug( LDAP_DEBUG_ANY,
674 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
675 (long) si_tag, 0, 0 );
677 ldap_memfree( retoid );
678 ber_bvfree( retdata );
682 if ( syncCookie_req.ctxcsn == NULL ) {
684 } else if ( syncCookie.ctxcsn == NULL ) {
687 value_match( &match, slap_schema.si_ad_entryCSN,
688 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
689 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
690 &syncCookie_req.ctxcsn[0],
691 &syncCookie.ctxcsn[0], &text );
694 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
696 syncrepl_updateCookie( si, op, psub, &syncCookie);
699 if ( si_refreshPresent == 1 ) {
701 syncrepl_del_nonpresent( op, si );
705 ldap_memfree( retoid );
706 ber_bvfree( retdata );
710 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
711 " unknown intermediate "
712 "response\n", 0, 0, 0 );
714 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
715 "unknown intermediate response (%d)\n",
718 ldap_memfree( retoid );
719 ber_bvfree( retdata );
725 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
726 "unknown message\n", 0, 0, 0 );
728 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
729 "unknown message\n", 0, 0, 0 );
734 if ( syncCookie.octet_str ) {
735 slap_sync_cookie_free( &syncCookie_req, 0 );
736 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
737 slap_sync_cookie_free( &syncCookie, 0 );
747 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
748 errstr = ldap_err2string( rc );
751 LDAP_LOG( OPERATION, ERR,
752 "do_syncrep2 : %s\n", errstr, 0, 0 );
754 Debug( LDAP_DEBUG_ANY,
755 "do_syncrep2 : %s\n", errstr, 0, 0 );
760 slap_sync_cookie_free( &syncCookie, 0 );
761 slap_sync_cookie_free( &syncCookie_req, 0 );
763 if ( res ) ldap_msgfree( res );
765 if ( rc && si->si_ld ) {
766 ldap_unbind( si->si_ld );
778 struct re_s* rtask = arg;
779 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
780 Connection conn = {0};
782 int rc = LDAP_SUCCESS;
788 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
790 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
796 switch( abs( si->si_type )) {
797 case LDAP_SYNC_REFRESH_ONLY:
798 case LDAP_SYNC_REFRESH_AND_PERSIST:
804 if ( slapd_abrupt_shutdown && si->si_ld ) {
805 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
806 connection_client_stop( s );
807 ldap_unbind( si->si_ld );
813 conn.c_send_ldap_result = slap_send_ldap_result;
814 conn.c_send_search_entry = slap_send_search_entry;
815 conn.c_send_search_reference = slap_send_search_reference;
816 conn.c_listener = (Listener *)&dummy_list;
817 conn.c_peer_name = slap_empty_bv;
819 /* set memory context */
820 op.o_tmpmemctx = sl_mem_create( SLMALLOC_SLAB_SIZE, ctx );
821 op.o_tmpmfuncs = &sl_mfuncs;
823 op.o_dn = si->si_updatedn;
824 op.o_ndn = si->si_updatedn;
825 op.o_time = slap_get_time();
826 op.o_threadctx = ctx;
827 op.o_managedsait = 1;
830 op.o_connid = op.o_conn->c_connid;
832 op.o_sync_state.ctxcsn = NULL;
833 op.o_sync_state.sid = -1;
834 op.o_sync_state.octet_str = NULL;
835 op.o_sync_slog_size = -1;
836 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
837 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
839 /* Establish session, do search */
842 rc = do_syncrep1( &op, si );
845 /* Process results */
846 if ( rc == LDAP_SUCCESS ) {
847 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
849 rc = do_syncrep2( &op, si );
851 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
852 /* If we succeeded, enable the connection for further listening.
853 * If we failed, tear down the connection and reschedule.
855 if ( rc == LDAP_SUCCESS ) {
857 rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
860 connection_client_enable( s );
862 } else if ( !first ) {
866 if ( rc == -2 ) rc = 0;
870 /* At this point, we have 4 cases:
871 * 1) for any hard failure, give up and remove this task
872 * 2) for ServerDown, reschedule this task to run
873 * 3) for Refresh and Success, reschedule to run
874 * 4) for Persist and Success, reschedule to defer
876 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
877 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
878 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
882 connection_client_stop( s );
885 if ( rc && rc != LDAP_SERVER_DOWN ) {
886 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
888 if ( rc == LDAP_SERVER_DOWN ||
889 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
894 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
896 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
902 syncrepl_message_to_entry(
906 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 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
963 e->e_name = op->o_req_dn;
964 e->e_nname = op->o_req_ndn;
966 while ( ber_remaining( ber ) ) {
967 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
968 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
973 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
975 mod->sml_op = LDAP_MOD_REPLACE;
976 mod->sml_next = NULL;
977 mod->sml_desc = NULL;
978 mod->sml_type = tmp.sml_type;
979 mod->sml_bvalues = tmp.sml_bvalues;
980 mod->sml_nvalues = NULL;
983 modtail = &mod->sml_next;
986 if ( *modlist == NULL ) {
988 LDAP_LOG( OPERATION, ERR,
989 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
991 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
996 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
998 if ( rc != LDAP_SUCCESS ) {
1000 LDAP_LOG( OPERATION, ERR,
1001 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1003 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1009 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1010 if( rc != LDAP_SUCCESS ) {
1012 LDAP_LOG( OPERATION, ERR,
1013 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1015 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1021 ber_free ( ber, 0 );
1022 if ( rc != LDAP_SUCCESS ) {
1035 Modifications* modlist,
1037 struct berval* syncUUID,
1038 struct sync_cookie* syncCookie_req
1041 Backend *be = op->o_bd;
1043 struct berval *syncuuid_bv = NULL;
1044 struct berval syncUUID_strrep = { 0, NULL };
1046 SlapReply rs = {REP_RESULT};
1048 AttributeAssertion ava = {0};
1049 int rc = LDAP_SUCCESS;
1050 int ret = LDAP_SUCCESS;
1053 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1055 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1056 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1057 syncuuid_cmp, avl_dup_error );
1060 if ( syncstate == LDAP_SYNC_PRESENT ) {
1064 f.f_choice = LDAP_FILTER_EQUALITY;
1066 ava.aa_desc = slap_schema.si_ad_entryUUID;
1067 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1068 ava.aa_value = *syncUUID;
1069 op->ors_filter = &f;
1071 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1072 op->ors_filterstr.bv_val = (char *) sl_malloc(
1073 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1074 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1075 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1076 syncUUID->bv_val, syncUUID->bv_len );
1077 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1079 op->ors_scope = LDAP_SCOPE_SUBTREE;
1081 /* get syncrepl cookie of shadow replica from subentry */
1082 op->o_req_dn = si->si_base;
1083 op->o_req_ndn = si->si_base;
1085 /* set callback function */
1086 op->o_callback = &cb;
1087 cb.sc_response = dn_callback;
1090 si->si_syncUUID_ndn.bv_val = NULL;
1092 rc = be->be_search( op, &rs );
1094 if ( op->ors_filterstr.bv_val ) {
1095 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1098 cb.sc_response = null_callback;
1101 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1105 if ( syncstate != LDAP_SYNC_DELETE ) {
1106 op->o_no_psearch = 1;
1109 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1110 if ( op->o_sync_csn.bv_val ) {
1111 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1116 op->o_req_dn = si->si_syncUUID_ndn;
1117 op->o_req_ndn = si->si_syncUUID_ndn;
1118 op->o_tag = LDAP_REQ_DELETE;
1119 rc = be->be_delete( op, &rs );
1120 op->o_no_psearch = 0;
1123 switch ( syncstate ) {
1125 case LDAP_SYNC_MODIFY:
1126 if ( rc == LDAP_SUCCESS ||
1127 rc == LDAP_REFERRAL ||
1128 rc == LDAP_NO_SUCH_OBJECT )
1130 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1131 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1132 syncUUID, &ava.aa_value );
1134 op->o_tag = LDAP_REQ_ADD;
1136 op->o_req_dn = e->e_name;
1137 op->o_req_ndn = e->e_nname;
1138 rc = be->be_add( op, &rs );
1140 if ( rc != LDAP_SUCCESS ) {
1141 if ( rc == LDAP_ALREADY_EXISTS ) {
1142 op->o_tag = LDAP_REQ_MODIFY;
1143 op->orm_modlist = modlist;
1144 op->o_req_dn = e->e_name;
1145 op->o_req_ndn = e->e_nname;
1146 rc = be->be_modify( op, &rs );
1147 if ( rc != LDAP_SUCCESS ) {
1149 LDAP_LOG( OPERATION, ERR,
1150 "syncrepl_entry : be_modify failed (%d)\n",
1153 Debug( LDAP_DEBUG_ANY,
1154 "syncrepl_entry : be_modify failed (%d)\n",
1160 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1161 syncrepl_add_glue( op, e );
1166 LDAP_LOG( OPERATION, ERR,
1167 "syncrepl_entry : be_add failed (%d)\n",
1170 Debug( LDAP_DEBUG_ANY,
1171 "syncrepl_entry : be_add failed (%d)\n",
1178 be_entry_release_w( op, e );
1184 LDAP_LOG( OPERATION, ERR,
1185 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1187 Debug( LDAP_DEBUG_ANY,
1188 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1194 case LDAP_SYNC_DELETE :
1195 /* Already deleted */
1201 LDAP_LOG( OPERATION, ERR,
1202 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1204 Debug( LDAP_DEBUG_ANY,
1205 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1213 if ( syncUUID_strrep.bv_val ) {
1214 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1216 if ( si->si_syncUUID_ndn.bv_val ) {
1217 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1223 syncrepl_del_nonpresent(
1228 Backend* be = op->o_bd;
1230 SlapReply rs = {REP_RESULT};
1231 struct nonpresent_entry *np_list, *np_prev;
1233 op->o_req_dn = si->si_base;
1234 op->o_req_ndn = si->si_base;
1236 cb.sc_response = nonpresent_callback;
1239 op->o_callback = &cb;
1240 op->o_tag = LDAP_REQ_SEARCH;
1241 op->ors_scope = si->si_scope;
1242 op->ors_deref = LDAP_DEREF_NEVER;
1245 op->ors_attrsonly = 0;
1246 op->ors_attrs = NULL;
1247 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1248 op->ors_filterstr = si->si_filterstr;
1250 op->o_nocaching = 1;
1251 be->be_search( op, &rs );
1252 op->o_nocaching = 0;
1254 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1256 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1257 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1258 while ( np_list != NULL ) {
1259 LDAP_LIST_REMOVE( np_list, npe_link );
1261 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1262 op->o_tag = LDAP_REQ_DELETE;
1263 op->o_callback = &cb;
1264 cb.sc_response = null_callback;
1266 op->o_req_dn = *np_prev->npe_name;
1267 op->o_req_ndn = *np_prev->npe_nname;
1268 op->o_bd->be_delete( op, &rs );
1269 ber_bvfree( np_prev->npe_name );
1270 ber_bvfree( np_prev->npe_nname );
1271 op->o_req_dn.bv_val = NULL;
1272 op->o_req_ndn.bv_val = NULL;
1281 static struct berval gcbva[] = {
1292 Backend *be = op->o_bd;
1298 struct berval dn = {0, NULL};
1299 struct berval ndn = {0, NULL};
1301 SlapReply rs = {REP_RESULT};
1304 op->o_tag = LDAP_REQ_ADD;
1305 op->o_callback = &cb;
1306 cb.sc_response = null_callback;
1307 cb.sc_private = NULL;
1312 /* count RDNs in suffix */
1313 if ( be->be_nsuffix[0].bv_len ) {
1314 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1324 /* Start with BE suffix */
1325 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1326 comma = strrchr(dn.bv_val, ',');
1327 if ( ptr ) *ptr = ',';
1328 if ( comma ) *comma = '\0';
1333 dn.bv_len -= ptr - dn.bv_val;
1336 /* the normalizedDNs are always the same length, no counting
1339 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1340 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1341 ndn.bv_len = be->be_nsuffix[0].bv_len;
1344 while ( ndn.bv_val > e->e_nname.bv_val ) {
1345 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1346 ber_dupbv( &glue->e_name, &dn );
1347 ber_dupbv( &glue->e_nname, &ndn );
1349 a = ch_calloc( 1, sizeof( Attribute ));
1350 a->a_desc = slap_schema.si_ad_objectClass;
1352 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1353 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1354 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1355 a->a_vals[2].bv_len = 0;
1356 a->a_vals[2].bv_val = NULL;
1358 a->a_nvals = a->a_vals;
1360 a->a_next = glue->e_attrs;
1363 a = ch_calloc( 1, sizeof( Attribute ));
1364 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1366 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1367 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1368 a->a_vals[1].bv_len = 0;
1369 a->a_vals[1].bv_val = NULL;
1371 a->a_nvals = a->a_vals;
1373 a->a_next = glue->e_attrs;
1376 op->o_req_dn = glue->e_name;
1377 op->o_req_ndn = glue->e_nname;
1379 rc = be->be_add ( op, &rs );
1380 if ( rc == LDAP_SUCCESS ) {
1381 be_entry_release_w( op, glue );
1383 /* incl. ALREADY EXIST */
1387 /* Move to next child */
1388 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1391 if ( ptr == e->e_name.bv_val ) break;
1393 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1394 for( ptr = ndn.bv_val-2;
1395 ptr > e->e_nname.bv_val && *ptr != ',';
1401 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1404 op->o_req_dn = e->e_name;
1405 op->o_req_ndn = e->e_nname;
1407 rc = be->be_add ( op, &rs );
1408 if ( rc == LDAP_SUCCESS ) {
1409 be_entry_release_w( op, e );
1417 static struct berval ocbva[] = {
1419 BER_BVC("subentry"),
1420 BER_BVC("syncConsumerSubentry"),
1424 static struct berval cnbva[] = {
1429 static struct berval ssbva[] = {
1434 static struct berval scbva[] = {
1440 syncrepl_updateCookie(
1444 struct sync_cookie *syncCookie
1447 Backend *be = op->o_bd;
1449 Modifications *mlnext;
1451 Modifications *modlist = NULL;
1452 Modifications **modtail = &modlist;
1455 char txtbuf[SLAP_TEXT_BUFLEN];
1456 size_t textlen = sizeof txtbuf;
1461 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1462 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1463 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1466 SlapReply rs = {REP_RESULT};
1468 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1469 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1471 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1472 mod->sml_op = LDAP_MOD_REPLACE;
1473 mod->sml_desc = slap_schema.si_ad_objectClass;
1474 mod->sml_type = mod->sml_desc->ad_cname;
1475 mod->sml_bvalues = ocbva;
1477 modtail = &mod->sml_next;
1479 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1480 assert( si->si_rid < 1000 );
1481 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1482 slap_syncrepl_bvc.bv_len,
1483 "syncrepl%ld", si->si_rid );
1484 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1485 mod->sml_op = LDAP_MOD_REPLACE;
1486 mod->sml_desc = slap_schema.si_ad_cn;
1487 mod->sml_type = mod->sml_desc->ad_cname;
1488 mod->sml_bvalues = cnbva;
1490 modtail = &mod->sml_next;
1492 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1493 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1494 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1495 mod->sml_op = LDAP_MOD_REPLACE;
1496 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1497 mod->sml_type = mod->sml_desc->ad_cname;
1498 mod->sml_bvalues = scbva;
1500 modtail = &mod->sml_next;
1502 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1503 mod->sml_op = LDAP_MOD_REPLACE;
1504 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1505 mod->sml_type = mod->sml_desc->ad_cname;
1506 mod->sml_bvalues = ssbva;
1508 modtail = &mod->sml_next;
1512 op->o_tag = LDAP_REQ_ADD;
1513 rc = slap_mods_opattrs( op, modlist, modtail,
1514 &text,txtbuf, textlen );
1516 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1517 ml->sml_op = LDAP_MOD_REPLACE;
1520 if( rc != LDAP_SUCCESS ) {
1522 LDAP_LOG( OPERATION, ERR,
1523 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1525 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1530 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1532 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1533 assert( si->si_rid < 1000 );
1534 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1535 slap_syncrepl_cn_bvc.bv_len,
1536 "cn=syncrepl%ld", si->si_rid );
1538 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1540 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1541 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1543 if ( slap_syncrepl_dn_bv.bv_val ) {
1544 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1549 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1551 if( rc != LDAP_SUCCESS ) {
1553 LDAP_LOG( OPERATION, ERR,
1554 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1556 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1561 cb.sc_response = null_callback;
1564 op->o_callback = &cb;
1565 op->o_req_dn = e->e_name;
1566 op->o_req_ndn = e->e_nname;
1568 /* update persistent cookie */
1569 update_cookie_retry:
1570 op->o_tag = LDAP_REQ_MODIFY;
1571 op->orm_modlist = modlist;
1572 rc = be->be_modify( op, &rs );
1574 if ( rc != LDAP_SUCCESS ) {
1575 if ( rc == LDAP_REFERRAL ||
1576 rc == LDAP_NO_SUCH_OBJECT ) {
1577 op->o_tag = LDAP_REQ_ADD;
1579 rc = be->be_add( op, &rs );
1580 if ( rc != LDAP_SUCCESS ) {
1581 if ( rc == LDAP_ALREADY_EXISTS ) {
1582 goto update_cookie_retry;
1583 } else if ( rc == LDAP_REFERRAL ||
1584 rc == LDAP_NO_SUCH_OBJECT ) {
1586 LDAP_LOG( OPERATION, ERR,
1587 "cookie will be non-persistent\n",
1590 Debug( LDAP_DEBUG_ANY,
1591 "cookie will be non-persistent\n",
1596 LDAP_LOG( OPERATION, ERR,
1597 "be_add failed (%d)\n",
1600 Debug( LDAP_DEBUG_ANY,
1601 "be_add failed (%d)\n",
1606 be_entry_release_w( op, e );
1611 LDAP_LOG( OPERATION, ERR,
1612 "be_modify failed (%d)\n", rc, 0, 0 );
1614 Debug( LDAP_DEBUG_ANY,
1615 "be_modify failed (%d)\n", rc, 0, 0 );
1626 if ( cnbva[0].bv_val ) {
1627 ch_free( cnbva[0].bv_val );
1628 cnbva[0].bv_val = NULL;
1630 if ( scbva[0].bv_val ) {
1631 ch_free( scbva[0].bv_val );
1632 scbva[0].bv_val = NULL;
1635 if ( mlnext->sml_next ) {
1636 slap_mods_free( mlnext->sml_next );
1637 mlnext->sml_next = NULL;
1640 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1641 mlnext = ml->sml_next;
1654 syncinfo_t *si = op->o_callback->sc_private;
1656 if ( rs->sr_type == REP_SEARCH ) {
1657 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1659 LDAP_LOG( OPERATION, ERR,
1660 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1662 Debug( LDAP_DEBUG_ANY,
1663 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1666 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1670 return LDAP_SUCCESS;
1674 nonpresent_callback(
1679 syncinfo_t *si = op->o_callback->sc_private;
1682 struct berval* present_uuid = NULL;
1683 struct nonpresent_entry *np_entry;
1685 if ( rs->sr_type == REP_RESULT ) {
1686 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1687 si->si_presentlist = NULL;
1689 } else if ( rs->sr_type == REP_SEARCH ) {
1690 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1692 if ( a == NULL ) return 0;
1694 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1697 if ( present_uuid == NULL ) {
1698 np_entry = (struct nonpresent_entry *)
1699 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1700 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1701 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1702 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1705 avl_delete( &si->si_presentlist,
1706 &a->a_vals[0], syncuuid_cmp );
1707 ch_free( present_uuid->bv_val );
1708 ch_free( present_uuid );
1711 return LDAP_SUCCESS;
1720 if ( rs->sr_err != LDAP_SUCCESS &&
1721 rs->sr_err != LDAP_REFERRAL &&
1722 rs->sr_err != LDAP_ALREADY_EXISTS &&
1723 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1726 LDAP_LOG( OPERATION, ERR,
1727 "null_callback : error code 0x%x\n",
1730 Debug( LDAP_DEBUG_ANY,
1731 "null_callback : error code 0x%x\n",
1735 return LDAP_SUCCESS;
1739 slap_create_syncrepl_entry(
1741 struct berval *context_csn,
1750 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1752 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1754 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1757 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1759 if ( context_csn ) {
1760 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1761 context_csn, NULL );
1765 bv.bv_len = sizeof("{}")-1;
1766 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1768 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1769 ber_dupbv( &e->e_nname, &e->e_name );
1775 slap_uuidstr_from_normalized(
1776 struct berval* uuidstr,
1777 struct berval* normalized,
1781 unsigned char nibble;
1784 if ( normalized == NULL )
1787 if ( normalized->bv_len != 16 ) {
1794 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1799 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1801 sl_free( new, ctx );
1805 for ( i = 0; i < 16; i++ ) {
1806 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1807 new->bv_val[(i<<1)+d] = '-';
1811 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1812 if ( nibble < 10 ) {
1813 new->bv_val[(i<<1)+d] = nibble + '0';
1815 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1818 nibble = (normalized->bv_val[i]) & 0xF;
1819 if ( nibble < 10 ) {
1820 new->bv_val[(i<<1)+d+1] = nibble + '0';
1822 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1826 new->bv_val[new->bv_len] = '\0';
1832 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1834 const struct berval *uuid1 = v_uuid1;
1835 const struct berval *uuid2 = v_uuid2;
1836 int rc = uuid1->bv_len - uuid2->bv_len;
1837 if ( rc ) return rc;
1838 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1842 avl_ber_bvfree( void *bv )
1847 if ( ((struct berval *)bv)->bv_val != NULL ) {
1848 ch_free ( ((struct berval *)bv)->bv_val );
1850 ch_free ( (char *) bv );