1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
29 #include "lutil_ldap.h"
33 #define SYNCREPL_STR "syncreplxxx"
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
48 static AttributeDescription *sync_descs[4];
50 struct runqueue_s syncrepl_rq;
53 init_syncrepl(syncinfo_t *si)
58 if ( !sync_descs[0] ) {
59 sync_descs[0] = slap_schema.si_ad_objectClass;
60 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61 sync_descs[2] = slap_schema.si_ad_entryCSN;
65 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
68 /* Delete Attributes */
69 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
74 ch_free( si->si_attrs[j] );
75 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 si->si_attrs[k] = si->si_attrs[k+1];
81 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
85 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
87 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
91 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
94 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
96 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
99 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
118 BerElementBuffer berbuf;
119 BerElement *ber = (BerElement *)&berbuf;
120 LDAPControl c[2], *ctrls[3];
121 struct timeval timeout;
125 /* setup LDAP SYNC control */
126 ber_init2( ber, NULL, LBER_USE_DER );
127 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
129 if ( si->si_syncCookie.octet_str &&
130 si->si_syncCookie.octet_str[0].bv_val ) {
131 ber_printf( ber, "{eO}", abs(si->si_type),
132 &si->si_syncCookie.octet_str[0] );
134 ber_printf( ber, "{e}", abs(si->si_type) );
137 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
142 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143 c[0].ldctl_iscritical = si->si_type < 0;
146 if ( si->si_authzId ) {
147 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149 c[1].ldctl_iscritical = 1;
156 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
159 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
162 si->si_slimit, &msgid );
174 int cmdline_cookie_found = 0;
176 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
177 struct berval syncrepl_cn_bv;
178 struct sync_cookie *sc = NULL;
179 struct sync_cookie syncCookie = { NULL, -1, NULL };
182 psub = &si->si_be->be_nsuffix[0];
184 /* Init connection to master */
186 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
187 if ( rc != LDAP_SUCCESS ) {
189 LDAP_LOG( OPERATION, ERR,
190 "do_syncrep1: ldap_initialize failed (%s)\n",
191 si->si_provideruri, 0, 0 );
193 Debug( LDAP_DEBUG_ANY,
194 "do_syncrep1: ldap_initialize failed (%s)\n",
195 si->si_provideruri, 0, 0 );
200 op->o_protocol = LDAP_VERSION3;
201 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
206 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
207 if( rc != LDAP_SUCCESS ) {
209 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
210 "%s: ldap_start_tls failed (%d)\n",
211 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
214 Debug( LDAP_DEBUG_ANY,
215 "%s: ldap_start_tls failed (%d)\n",
216 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
219 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
223 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
224 #ifdef HAVE_CYRUS_SASL
227 if ( si->si_secprops != NULL ) {
228 rc = ldap_set_option( si->si_ld,
229 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
231 if( rc != LDAP_OPT_SUCCESS ) {
233 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
234 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
235 si->si_provideruri, si->si_secprops, 0 );
237 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
238 "(%s,SECPROPS,\"%s\") failed!\n",
239 si->si_provideruri, si->si_secprops, 0 );
245 defaults = lutil_sasl_defaults( si->si_ld,
246 si->si_saslmech, si->si_realm,
247 si->si_authcId, si->si_passwd, si->si_authzId );
249 rc = ldap_sasl_interactive_bind_s( si->si_ld,
257 lutil_sasl_freedefs( defaults );
259 /* FIXME: different error behaviors according to
261 * 2) on err policy : exit, retry, backoff ...
263 if ( rc != LDAP_SUCCESS ) {
265 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
266 "ldap_sasl_interactive_bind_s failed (%d)\n",
269 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
270 "ldap_sasl_interactive_bind_s failed (%d)\n",
274 /* FIXME (see above comment) */
275 /* if Kerberos credentials cache is not active, retry */
276 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
277 rc == LDAP_LOCAL_ERROR )
279 rc = LDAP_SERVER_DOWN;
284 #else /* HAVE_CYRUS_SASL */
285 /* Should never get here, we trapped this at config time */
286 fprintf( stderr, "not compiled with SASL support\n" );
291 rc = ldap_bind_s( si->si_ld,
292 si->si_binddn, si->si_passwd, si->si_bindmethod );
293 if ( rc != LDAP_SUCCESS ) {
295 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
296 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
298 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
299 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
305 /* get syncrepl cookie of shadow replica from subentry */
307 assert( si->si_rid < 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%ld", si->si_rid );
311 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
312 op->o_req_dn = op->o_req_ndn;
314 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
315 if ( si->si_rid == sc->rid ) {
316 cmdline_cookie_found = 1;
321 if ( cmdline_cookie_found ) {
322 /* cookie is supplied in the command line */
323 BerVarray cookie = NULL;
324 struct berval cookie_bv;
326 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
327 slap_sync_cookie_free( &si->si_syncCookie, 0 );
329 /* read stored cookie if it exists */
330 backend_attribute( op, NULL, &op->o_req_ndn,
331 slap_schema.si_ad_syncreplCookie, &cookie );
334 /* no stored cookie */
335 if ( sc->ctxcsn == NULL ||
336 sc->ctxcsn->bv_val == NULL ) {
337 /* if cmdline cookie does not have ctxcsn */
338 /* component, set it to an initial value */
339 slap_init_sync_cookie_ctxcsn( sc );
341 slap_dup_sync_cookie( &si->si_syncCookie, sc );
342 slap_sync_cookie_free( sc, 1 );
346 struct berval newcookie = BER_BVNULL;
347 ber_dupbv( &cookie_bv, &cookie[0] );
348 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
349 slap_parse_sync_cookie( &si->si_syncCookie );
350 ber_bvarray_free( si->si_syncCookie.octet_str );
351 si->si_syncCookie.octet_str = NULL;
352 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
353 if ( sc->sid != -1 ) {
354 /* command line cookie wins */
355 si->si_syncCookie.sid = sc->sid;
357 if ( sc->ctxcsn != NULL ) {
358 /* command line cookie wins */
359 if ( si->si_syncCookie.ctxcsn ) {
360 ber_bvarray_free( si->si_syncCookie.ctxcsn );
361 si->si_syncCookie.ctxcsn = NULL;
363 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
364 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
366 if ( sc->rid != -1 ) {
367 /* command line cookie wins */
368 si->si_syncCookie.rid = sc->rid;
370 slap_sync_cookie_free( sc, 1 );
372 slap_compose_sync_cookie( NULL, &newcookie,
373 &si->si_syncCookie.ctxcsn[0],
374 si->si_syncCookie.sid, si->si_syncCookie.rid );
375 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
378 /* no command line cookie is specified */
379 if ( si->si_syncCookie.octet_str == NULL ) {
380 BerVarray cookie = NULL;
381 struct berval cookie_bv;
382 /* try to read stored cookie */
383 backend_attribute( op, NULL, &op->o_req_ndn,
384 slap_schema.si_ad_syncreplCookie, &cookie );
386 ber_dupbv( &cookie_bv, &cookie[0] );
387 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
388 slap_parse_sync_cookie( &si->si_syncCookie );
389 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
394 rc = ldap_sync_search( si, op->o_tmpmemctx );
396 if( rc != LDAP_SUCCESS ) {
398 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
399 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
401 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
402 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
409 ldap_unbind( si->si_ld );
422 LDAPControl **rctrls = NULL;
425 BerElementBuffer berbuf;
426 BerElement *ber = (BerElement *)&berbuf;
428 LDAPMessage *res = NULL;
429 LDAPMessage *msg = NULL;
432 struct berval *retdata = NULL;
437 struct berval syncUUID = BER_BVNULL;
438 struct sync_cookie syncCookie = { NULL, -1, NULL };
439 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
440 struct berval cookie = BER_BVNULL;
448 Modifications *modlist = NULL;
453 struct timeval *tout_p = NULL;
454 struct timeval tout = { 0, 0 };
456 int refreshDeletes = 0;
458 BerVarray syncUUIDs = NULL;
461 if ( slapd_shutdown ) {
466 ber_init2( ber, NULL, LBER_USE_DER );
467 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
470 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
472 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
475 psub = &si->si_be->be_nsuffix[0];
477 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
479 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
485 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
488 if ( slapd_shutdown ) {
492 for( msg = ldap_first_message( si->si_ld, res );
494 msg = ldap_next_message( si->si_ld, msg ) )
496 switch( ldap_msgtype( msg ) ) {
497 case LDAP_RES_SEARCH_ENTRY:
498 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
499 /* we can't work without the control */
505 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
506 ber_scanf( ber, "{em", &syncstate, &syncUUID );
507 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
508 ber_scanf( ber, "m}", &cookie );
509 if ( cookie.bv_val ) {
510 struct berval tmp_bv;
511 ber_dupbv( &tmp_bv, &cookie );
512 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
514 if ( syncCookie.octet_str &&
515 syncCookie.octet_str[0].bv_val )
516 slap_parse_sync_cookie( &syncCookie );
518 if ( syncrepl_message_to_entry( si, op, msg,
519 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
520 rc_efree = syncrepl_entry( si, op, entry, modlist,
521 syncstate, &syncUUID, &syncCookie_req );
522 if ( syncCookie.octet_str &&
523 syncCookie.octet_str[0].bv_val ) {
524 syncrepl_updateCookie( si, op, psub, &syncCookie );
527 ldap_controls_free( rctrls );
529 slap_mods_free( modlist );
531 if ( rc_efree && entry ) {
537 case LDAP_RES_SEARCH_REFERENCE:
539 LDAP_LOG( OPERATION, ERR,
540 "do_syncrep2 : reference received\n", 0, 0, 0 );
542 Debug( LDAP_DEBUG_ANY,
543 "do_syncrep2 : reference received\n", 0, 0, 0 );
547 case LDAP_RES_SEARCH_RESULT:
548 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
552 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
554 ber_scanf( ber, "{" /*"}"*/);
555 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
557 ber_scanf( ber, "m", &cookie );
558 if ( cookie.bv_val ) {
559 struct berval tmp_bv;
560 ber_dupbv( &tmp_bv, &cookie );
561 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
563 if ( syncCookie.octet_str &&
564 syncCookie.octet_str[0].bv_val )
565 slap_parse_sync_cookie( &syncCookie );
567 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
569 ber_scanf( ber, "b", &refreshDeletes );
571 ber_scanf( ber, "}" );
573 if ( syncCookie_req.ctxcsn == NULL ) {
575 } else if ( syncCookie.ctxcsn == NULL ) {
578 value_match( &match, slap_schema.si_ad_entryCSN,
579 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
580 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
581 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
583 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
584 && match < 0 && err == LDAP_SUCCESS ) {
585 syncrepl_updateCookie( si, op, psub, &syncCookie );
588 ldap_controls_free( rctrls );
590 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
591 /* FIXME : different error behaviors according to
592 * 1) err code : LDAP_BUSY ...
593 * 2) on err policy : stop service, stop sync, retry
595 if ( refreshDeletes == 0 && match < 0 &&
596 err == LDAP_SUCCESS ) {
597 syncrepl_del_nonpresent( op, si );
599 avl_free( si->si_presentlist, avl_ber_bvfree );
600 si->si_presentlist = NULL;
607 case LDAP_RES_INTERMEDIATE:
608 rc = ldap_parse_intermediate( si->si_ld, msg,
609 &retoid, &retdata, NULL, 0 );
610 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
611 int si_refreshDelete = 0;
612 int si_refreshPresent = 0;
613 ber_init2( ber, retdata, LBER_USE_DER );
615 switch ( si_tag = ber_peek_tag( ber, &len )) {
617 case LDAP_TAG_SYNC_NEW_COOKIE:
618 ber_scanf( ber, "tm", &tag, &cookie );
620 case LDAP_TAG_SYNC_REFRESH_DELETE:
621 si_refreshDelete = 1;
622 case LDAP_TAG_SYNC_REFRESH_PRESENT:
623 si_refreshPresent = 1;
624 ber_scanf( ber, "t{", &tag );
625 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
627 ber_scanf( ber, "m", &cookie );
628 if ( cookie.bv_val ) {
629 struct berval tmp_bv;
630 ber_dupbv( &tmp_bv, &cookie );
631 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
633 if ( syncCookie.octet_str &&
634 syncCookie.octet_str[0].bv_val )
635 slap_parse_sync_cookie( &syncCookie );
637 if ( ber_peek_tag( ber, &len ) ==
638 LDAP_TAG_REFRESHDONE )
640 ber_scanf( ber, "b", &refreshDone );
642 ber_scanf( ber, "}" );
644 case LDAP_TAG_SYNC_ID_SET:
645 ber_scanf( ber, "t{", &tag );
646 if ( ber_peek_tag( ber, &len ) ==
647 LDAP_TAG_SYNC_COOKIE ) {
648 ber_scanf( ber, "m", &cookie );
649 if ( cookie.bv_val ) {
650 struct berval tmp_bv;
651 ber_dupbv( &tmp_bv, &cookie );
652 ber_bvarray_add( &syncCookie.octet_str,
655 if ( syncCookie.octet_str &&
656 syncCookie.octet_str[0].bv_val )
657 slap_parse_sync_cookie( &syncCookie );
659 if ( ber_peek_tag( ber, &len ) ==
660 LDAP_TAG_REFRESHDELETES )
662 ber_scanf( ber, "b", &refreshDeletes );
664 ber_scanf( ber, "[W]", &syncUUIDs );
665 ber_scanf( ber, "}" );
666 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
667 struct berval *syncuuid_bv;
668 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
669 avl_insert( &si->si_presentlist,
670 (caddr_t) syncuuid_bv,
671 syncuuid_cmp, avl_dup_error );
673 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
677 LDAP_LOG( OPERATION, ERR,
678 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
679 (long) si_tag, 0, 0 );
681 Debug( LDAP_DEBUG_ANY,
682 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
683 (long) si_tag, 0, 0 );
685 ldap_memfree( retoid );
686 ber_bvfree( retdata );
690 if ( syncCookie_req.ctxcsn == NULL ) {
692 } else if ( syncCookie.ctxcsn == NULL ) {
695 value_match( &match, slap_schema.si_ad_entryCSN,
696 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
697 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
698 &syncCookie_req.ctxcsn[0],
699 &syncCookie.ctxcsn[0], &text );
702 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
704 syncrepl_updateCookie( si, op, psub, &syncCookie);
707 if ( si_refreshPresent == 1 ) {
709 syncrepl_del_nonpresent( op, si );
713 ldap_memfree( retoid );
714 ber_bvfree( retdata );
718 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
719 " unknown intermediate "
720 "response\n", 0, 0, 0 );
722 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
723 "unknown intermediate response (%d)\n",
726 ldap_memfree( retoid );
727 ber_bvfree( retdata );
733 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
734 "unknown message\n", 0, 0, 0 );
736 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
737 "unknown message\n", 0, 0, 0 );
742 if ( syncCookie.octet_str ) {
743 slap_sync_cookie_free( &syncCookie_req, 0 );
744 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
745 slap_sync_cookie_free( &syncCookie, 0 );
755 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
756 errstr = ldap_err2string( rc );
759 LDAP_LOG( OPERATION, ERR,
760 "do_syncrep2 : %s\n", errstr, 0, 0 );
762 Debug( LDAP_DEBUG_ANY,
763 "do_syncrep2 : %s\n", errstr, 0, 0 );
768 slap_sync_cookie_free( &syncCookie, 0 );
769 slap_sync_cookie_free( &syncCookie_req, 0 );
771 if ( res ) ldap_msgfree( res );
773 if ( rc && si->si_ld ) {
774 ldap_unbind( si->si_ld );
786 struct re_s* rtask = arg;
787 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
788 Connection conn = {0};
790 int rc = LDAP_SUCCESS;
796 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
798 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
804 switch( abs( si->si_type )) {
805 case LDAP_SYNC_REFRESH_ONLY:
806 case LDAP_SYNC_REFRESH_AND_PERSIST:
812 if ( slapd_shutdown && si->si_ld ) {
813 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
814 connection_client_stop( s );
815 ldap_unbind( si->si_ld );
820 connection_fake_init( &conn, &op, ctx );
822 op.o_dn = si->si_updatedn;
823 op.o_ndn = si->si_updatedn;
824 op.o_managedsait = 1;
827 op.o_sync_state.ctxcsn = NULL;
828 op.o_sync_state.sid = -1;
829 op.o_sync_state.octet_str = NULL;
830 op.o_sync_slog_size = -1;
831 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
832 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
834 /* Establish session, do search */
837 rc = do_syncrep1( &op, si );
840 /* Process results */
841 if ( rc == LDAP_SUCCESS ) {
842 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
844 rc = do_syncrep2( &op, si );
846 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
847 /* If we succeeded, enable the connection for further listening.
848 * If we failed, tear down the connection and reschedule.
850 if ( rc == LDAP_SUCCESS ) {
852 rc = connection_client_setup( s, do_syncrepl,
855 connection_client_enable( s );
857 } else if ( !first ) {
861 if ( rc == -2 ) rc = 0;
865 /* At this point, we have 4 cases:
866 * 1) for any hard failure, give up and remove this task
867 * 2) for ServerDown, reschedule this task to run
868 * 3) for Refresh and Success, reschedule to run
869 * 4) for Persist and Success, reschedule to defer
871 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
872 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
873 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
877 connection_client_stop( s );
880 if ( rc && rc != LDAP_SERVER_DOWN ) {
881 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
883 if ( rc == LDAP_SERVER_DOWN ||
884 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
889 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
891 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
897 syncrepl_message_to_entry(
901 Modifications **modlist,
907 BerElement *ber = NULL;
910 Modifications **modtail = modlist;
913 char txtbuf[SLAP_TEXT_BUFLEN];
914 size_t textlen = sizeof txtbuf;
916 struct berval bdn = {0, NULL}, dn, ndn;
921 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
923 LDAP_LOG( OPERATION, ERR,
924 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
926 Debug( LDAP_DEBUG_ANY,
927 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
932 op->o_tag = LDAP_REQ_ADD;
934 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
936 if ( rc != LDAP_SUCCESS ) {
938 LDAP_LOG( OPERATION, ERR,
939 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
941 Debug( LDAP_DEBUG_ANY,
942 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
947 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
948 ber_dupbv( &op->o_req_dn, &dn );
949 ber_dupbv( &op->o_req_ndn, &ndn );
950 sl_free( ndn.bv_val, op->o_tmpmemctx );
951 sl_free( dn.bv_val, op->o_tmpmemctx );
953 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
958 if ( entry == NULL ) {
962 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
964 e->e_name = op->o_req_dn;
965 e->e_nname = op->o_req_ndn;
967 while ( ber_remaining( ber ) ) {
968 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
969 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
974 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
976 mod->sml_op = LDAP_MOD_REPLACE;
977 mod->sml_next = NULL;
978 mod->sml_desc = NULL;
979 mod->sml_type = tmp.sml_type;
980 mod->sml_bvalues = tmp.sml_bvalues;
981 mod->sml_nvalues = NULL;
984 modtail = &mod->sml_next;
987 if ( *modlist == NULL ) {
989 LDAP_LOG( OPERATION, ERR,
990 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
992 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
997 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
999 if ( rc != LDAP_SUCCESS ) {
1001 LDAP_LOG( OPERATION, ERR,
1002 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1004 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1010 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1011 if( rc != LDAP_SUCCESS ) {
1013 LDAP_LOG( OPERATION, ERR,
1014 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1016 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1022 ber_free ( ber, 0 );
1023 if ( rc != LDAP_SUCCESS ) {
1036 Modifications* modlist,
1038 struct berval* syncUUID,
1039 struct sync_cookie* syncCookie_req
1042 Backend *be = op->o_bd;
1043 slap_callback cb = { NULL };
1044 struct berval *syncuuid_bv = NULL;
1045 struct berval syncUUID_strrep = BER_BVNULL;
1046 struct berval uuid_bv = BER_BVNULL;
1048 SlapReply rs = {REP_RESULT};
1050 AttributeAssertion ava = {0};
1051 int rc = LDAP_SUCCESS;
1052 int ret = LDAP_SUCCESS;
1055 struct berval pdn = BER_BVNULL;
1056 struct berval org_req_dn = BER_BVNULL;
1057 struct berval org_req_ndn = BER_BVNULL;
1058 struct berval org_dn = BER_BVNULL;
1059 struct berval org_ndn = BER_BVNULL;
1060 int org_managedsait;
1062 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1064 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1065 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1066 syncuuid_cmp, avl_dup_error );
1069 if ( syncstate == LDAP_SYNC_PRESENT ) {
1073 f.f_choice = LDAP_FILTER_EQUALITY;
1075 ava.aa_desc = slap_schema.si_ad_entryUUID;
1076 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1077 ava.aa_value = *syncUUID;
1078 op->ors_filter = &f;
1080 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1081 op->ors_filterstr.bv_val = (char *) sl_malloc(
1082 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1083 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1084 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1085 syncUUID->bv_val, syncUUID->bv_len );
1086 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1088 op->o_tag = LDAP_REQ_SEARCH;
1089 op->ors_scope = LDAP_SCOPE_SUBTREE;
1091 /* get syncrepl cookie of shadow replica from subentry */
1092 op->o_req_dn = si->si_base;
1093 op->o_req_ndn = si->si_base;
1095 /* set callback function */
1096 op->o_callback = &cb;
1097 cb.sc_response = dn_callback;
1100 si->si_syncUUID_ndn.bv_val = NULL;
1102 if ( limits_check( op, &rs ) == 0 ) {
1103 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 ) {
1189 Modifications *modtail;
1191 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1195 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1196 ber_dupbv( &uuid_bv, syncUUID );
1197 mod->sml_op = LDAP_MOD_REPLACE;
1198 mod->sml_desc = slap_schema.si_ad_entryUUID;
1199 mod->sml_type = mod->sml_desc->ad_cname;
1200 ber_bvarray_add( &mod->sml_bvalues, &uuid_bv );
1201 modtail->sml_next = mod;
1203 op->o_tag = LDAP_REQ_MODIFY;
1204 op->orm_modlist = modlist;
1205 op->o_req_dn = e->e_name;
1206 op->o_req_ndn = e->e_nname;
1208 rc = be->be_modify( op, &rs );
1209 if ( rc != LDAP_SUCCESS ) {
1211 LDAP_LOG( OPERATION, ERR,
1212 "syncrepl_entry : be_modify failed (%d)\n",
1215 Debug( LDAP_DEBUG_ANY,
1216 "syncrepl_entry : be_modify failed (%d)\n",
1222 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1223 syncrepl_add_glue( op, e );
1228 LDAP_LOG( OPERATION, ERR,
1229 "syncrepl_entry : be_add failed (%d)\n",
1232 Debug( LDAP_DEBUG_ANY,
1233 "syncrepl_entry : be_add failed (%d)\n",
1240 be_entry_release_w( op, e );
1246 LDAP_LOG( OPERATION, ERR,
1247 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1249 Debug( LDAP_DEBUG_ANY,
1250 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1256 case LDAP_SYNC_DELETE :
1257 /* Already deleted */
1263 LDAP_LOG( OPERATION, ERR,
1264 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1266 Debug( LDAP_DEBUG_ANY,
1267 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1275 if ( syncUUID_strrep.bv_val ) {
1276 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1278 if ( si->si_syncUUID_ndn.bv_val ) {
1279 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1284 static struct berval gcbva[] = {
1291 syncrepl_del_nonpresent(
1296 Backend* be = op->o_bd;
1297 slap_callback cb = { NULL };
1298 SlapReply rs = {REP_RESULT};
1299 struct nonpresent_entry *np_list, *np_prev;
1302 Modifications *mlnext;
1304 Modifications *modlist = NULL;
1305 Modifications **modtail = &modlist;
1308 struct berval pdn = BER_BVNULL;
1309 struct berval org_req_dn = BER_BVNULL;
1310 struct berval org_req_ndn = BER_BVNULL;
1311 struct berval org_dn = BER_BVNULL;
1312 struct berval org_ndn = BER_BVNULL;
1313 int org_managedsait;
1315 op->o_req_dn = si->si_base;
1316 op->o_req_ndn = si->si_base;
1318 cb.sc_response = nonpresent_callback;
1321 op->o_callback = &cb;
1322 op->o_tag = LDAP_REQ_SEARCH;
1323 op->ors_scope = si->si_scope;
1324 op->ors_deref = LDAP_DEREF_NEVER;
1327 op->ors_attrsonly = 0;
1328 op->ors_attrs = NULL;
1329 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1330 op->ors_filterstr = si->si_filterstr;
1332 op->o_nocaching = 1;
1333 op->o_managedsait = 0;
1335 if ( limits_check( op, &rs ) == 0 ) {
1336 be->be_search( op, &rs );
1339 op->o_managedsait = 1;
1340 op->o_nocaching = 0;
1342 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1344 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1345 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1346 while ( np_list != NULL ) {
1347 LDAP_LIST_REMOVE( np_list, npe_link );
1349 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1350 op->o_tag = LDAP_REQ_DELETE;
1351 op->o_callback = &cb;
1352 cb.sc_response = null_callback;
1354 op->o_req_dn = *np_prev->npe_name;
1355 op->o_req_ndn = *np_prev->npe_nname;
1356 rc = op->o_bd->be_delete( op, &rs );
1358 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1359 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1360 mod->sml_op = LDAP_MOD_REPLACE;
1361 mod->sml_desc = slap_schema.si_ad_objectClass;
1362 mod->sml_type = mod->sml_desc->ad_cname;
1363 mod->sml_bvalues = &gcbva[0];
1365 modtail = &mod->sml_next;
1367 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1368 mod->sml_op = LDAP_MOD_REPLACE;
1369 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1370 mod->sml_type = mod->sml_desc->ad_cname;
1371 mod->sml_bvalues = &gcbva[1];
1373 modtail = &mod->sml_next;
1375 op->o_tag = LDAP_REQ_MODIFY;
1376 op->orm_modlist = modlist;
1378 rc = be->be_modify( op, &rs );
1380 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1381 mlnext = ml->sml_next;
1386 org_req_dn = op->o_req_dn;
1387 org_req_ndn = op->o_req_ndn;
1389 org_ndn = op->o_ndn;
1390 org_managedsait = get_manageDSAit( op );
1391 op->o_dn = op->o_bd->be_rootdn;
1392 op->o_ndn = op->o_bd->be_rootndn;
1393 op->o_managedsait = 1;
1395 while ( rs.sr_err == LDAP_SUCCESS &&
1396 op->o_delete_glue_parent ) {
1397 op->o_delete_glue_parent = 0;
1398 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1399 slap_callback cb = { NULL };
1400 cb.sc_response = slap_null_cb;
1401 dnParent( &op->o_req_ndn, &pdn );
1403 op->o_req_ndn = pdn;
1404 op->o_callback = &cb;
1405 /* give it a root privil ? */
1406 op->o_bd->be_delete( op, &rs );
1412 op->o_managedsait = org_managedsait;
1414 op->o_ndn = org_ndn;
1415 op->o_req_dn = org_req_dn;
1416 op->o_req_ndn = org_req_ndn;
1417 op->o_delete_glue_parent = 0;
1419 ber_bvfree( np_prev->npe_name );
1420 ber_bvfree( np_prev->npe_nname );
1421 op->o_req_dn.bv_val = NULL;
1422 op->o_req_ndn.bv_val = NULL;
1437 Backend *be = op->o_bd;
1438 slap_callback cb = { NULL };
1443 struct berval dn = {0, NULL};
1444 struct berval ndn = {0, NULL};
1446 SlapReply rs = {REP_RESULT};
1449 op->o_tag = LDAP_REQ_ADD;
1450 op->o_callback = &cb;
1451 cb.sc_response = null_callback;
1452 cb.sc_private = NULL;
1457 /* count RDNs in suffix */
1458 if ( be->be_nsuffix[0].bv_len ) {
1459 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1469 /* Start with BE suffix */
1470 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1471 comma = strrchr(dn.bv_val, ',');
1472 if ( ptr ) *ptr = ',';
1473 if ( comma ) *comma = '\0';
1478 dn.bv_len -= ptr - dn.bv_val;
1481 /* the normalizedDNs are always the same length, no counting
1484 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1485 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1486 ndn.bv_len = be->be_nsuffix[0].bv_len;
1489 while ( ndn.bv_val > e->e_nname.bv_val ) {
1490 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1491 ber_dupbv( &glue->e_name, &dn );
1492 ber_dupbv( &glue->e_nname, &ndn );
1494 a = ch_calloc( 1, sizeof( Attribute ));
1495 a->a_desc = slap_schema.si_ad_objectClass;
1497 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1498 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1499 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1500 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1502 a->a_nvals = a->a_vals;
1504 a->a_next = glue->e_attrs;
1507 a = ch_calloc( 1, sizeof( Attribute ));
1508 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1510 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1511 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1512 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1514 a->a_nvals = a->a_vals;
1516 a->a_next = glue->e_attrs;
1519 op->o_req_dn = glue->e_name;
1520 op->o_req_ndn = glue->e_nname;
1522 rc = be->be_add ( op, &rs );
1523 if ( rc == LDAP_SUCCESS ) {
1524 be_entry_release_w( op, glue );
1526 /* incl. ALREADY EXIST */
1530 /* Move to next child */
1531 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1534 if ( ptr == e->e_name.bv_val ) break;
1536 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1537 for( ptr = ndn.bv_val-2;
1538 ptr > e->e_nname.bv_val && *ptr != ',';
1544 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1547 op->o_req_dn = e->e_name;
1548 op->o_req_ndn = e->e_nname;
1550 rc = be->be_add ( op, &rs );
1551 if ( rc == LDAP_SUCCESS ) {
1552 be_entry_release_w( op, e );
1560 static struct berval ocbva[] = {
1562 BER_BVC("subentry"),
1563 BER_BVC("syncConsumerSubentry"),
1567 static struct berval cnbva[] = {
1572 static struct berval ssbva[] = {
1577 static struct berval scbva[] = {
1583 syncrepl_updateCookie(
1587 struct sync_cookie *syncCookie
1590 Backend *be = op->o_bd;
1592 Modifications *mlnext;
1594 Modifications *modlist = NULL;
1595 Modifications **modtail = &modlist;
1598 char txtbuf[SLAP_TEXT_BUFLEN];
1599 size_t textlen = sizeof txtbuf;
1604 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1605 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1606 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1608 slap_callback cb = { NULL };
1609 SlapReply rs = {REP_RESULT};
1611 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1612 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1614 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1615 mod->sml_op = LDAP_MOD_REPLACE;
1616 mod->sml_desc = slap_schema.si_ad_objectClass;
1617 mod->sml_type = mod->sml_desc->ad_cname;
1618 mod->sml_bvalues = ocbva;
1620 modtail = &mod->sml_next;
1622 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1623 assert( si->si_rid < 1000 );
1624 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1625 slap_syncrepl_bvc.bv_len + 1,
1626 "syncrepl%ld", si->si_rid );
1627 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1628 mod->sml_op = LDAP_MOD_REPLACE;
1629 mod->sml_desc = slap_schema.si_ad_cn;
1630 mod->sml_type = mod->sml_desc->ad_cname;
1631 mod->sml_bvalues = cnbva;
1633 modtail = &mod->sml_next;
1635 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1636 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1637 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1638 mod->sml_op = LDAP_MOD_REPLACE;
1639 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1640 mod->sml_type = mod->sml_desc->ad_cname;
1641 mod->sml_bvalues = scbva;
1643 modtail = &mod->sml_next;
1645 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1646 mod->sml_op = LDAP_MOD_REPLACE;
1647 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1648 mod->sml_type = mod->sml_desc->ad_cname;
1649 mod->sml_bvalues = ssbva;
1651 modtail = &mod->sml_next;
1655 op->o_tag = LDAP_REQ_ADD;
1656 rc = slap_mods_opattrs( op, modlist, modtail,
1657 &text,txtbuf, textlen );
1659 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1660 ml->sml_op = LDAP_MOD_REPLACE;
1663 if( rc != LDAP_SUCCESS ) {
1665 LDAP_LOG( OPERATION, ERR,
1666 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1668 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1673 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1675 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1676 assert( si->si_rid < 1000 );
1677 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1678 slap_syncrepl_cn_bvc.bv_len + 1,
1679 "cn=syncrepl%ld", si->si_rid );
1681 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1683 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1684 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1686 if ( slap_syncrepl_dn_bv.bv_val ) {
1687 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1692 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1694 if( rc != LDAP_SUCCESS ) {
1696 LDAP_LOG( OPERATION, ERR,
1697 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1699 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1704 cb.sc_response = null_callback;
1707 op->o_callback = &cb;
1708 op->o_req_dn = e->e_name;
1709 op->o_req_ndn = e->e_nname;
1711 /* update persistent cookie */
1712 update_cookie_retry:
1713 op->o_tag = LDAP_REQ_MODIFY;
1714 op->orm_modlist = modlist;
1715 rc = be->be_modify( op, &rs );
1717 if ( rc != LDAP_SUCCESS ) {
1718 if ( rc == LDAP_REFERRAL ||
1719 rc == LDAP_NO_SUCH_OBJECT ) {
1720 op->o_tag = LDAP_REQ_ADD;
1722 rc = be->be_add( op, &rs );
1723 if ( rc != LDAP_SUCCESS ) {
1724 if ( rc == LDAP_ALREADY_EXISTS ) {
1725 goto update_cookie_retry;
1726 } else if ( rc == LDAP_REFERRAL ||
1727 rc == LDAP_NO_SUCH_OBJECT ) {
1729 LDAP_LOG( OPERATION, ERR,
1730 "cookie will be non-persistent\n",
1733 Debug( LDAP_DEBUG_ANY,
1734 "cookie will be non-persistent\n",
1739 LDAP_LOG( OPERATION, ERR,
1740 "be_add failed (%d)\n",
1743 Debug( LDAP_DEBUG_ANY,
1744 "be_add failed (%d)\n",
1749 be_entry_release_w( op, e );
1754 LDAP_LOG( OPERATION, ERR,
1755 "be_modify failed (%d)\n", rc, 0, 0 );
1757 Debug( LDAP_DEBUG_ANY,
1758 "be_modify failed (%d)\n", rc, 0, 0 );
1769 if ( cnbva[0].bv_val ) {
1770 ch_free( cnbva[0].bv_val );
1771 cnbva[0].bv_val = NULL;
1773 if ( scbva[0].bv_val ) {
1774 ch_free( scbva[0].bv_val );
1775 scbva[0].bv_val = NULL;
1778 if ( mlnext->sml_next ) {
1779 slap_mods_free( mlnext->sml_next );
1780 mlnext->sml_next = NULL;
1783 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1784 mlnext = ml->sml_next;
1797 syncinfo_t *si = op->o_callback->sc_private;
1799 if ( rs->sr_type == REP_SEARCH ) {
1800 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1802 LDAP_LOG( OPERATION, ERR,
1803 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1805 Debug( LDAP_DEBUG_ANY,
1806 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1809 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1813 return LDAP_SUCCESS;
1817 nonpresent_callback(
1822 syncinfo_t *si = op->o_callback->sc_private;
1825 struct berval* present_uuid = NULL;
1826 struct nonpresent_entry *np_entry;
1828 if ( rs->sr_type == REP_RESULT ) {
1829 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1830 si->si_presentlist = NULL;
1832 } else if ( rs->sr_type == REP_SEARCH ) {
1833 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1835 if ( a == NULL ) return 0;
1837 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1840 if ( present_uuid == NULL ) {
1841 np_entry = (struct nonpresent_entry *)
1842 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1843 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1844 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1845 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1848 avl_delete( &si->si_presentlist,
1849 &a->a_nvals[0], syncuuid_cmp );
1850 ch_free( present_uuid->bv_val );
1851 ch_free( present_uuid );
1854 return LDAP_SUCCESS;
1862 if ( rs->sr_err != LDAP_SUCCESS &&
1863 rs->sr_err != LDAP_REFERRAL &&
1864 rs->sr_err != LDAP_ALREADY_EXISTS &&
1865 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1866 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1869 LDAP_LOG( OPERATION, ERR,
1870 "null_callback : error code 0x%x\n",
1873 Debug( LDAP_DEBUG_ANY,
1874 "null_callback : error code 0x%x\n",
1878 return LDAP_SUCCESS;
1882 slap_create_syncrepl_entry(
1884 struct berval *context_csn,
1892 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1894 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1896 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1899 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1901 if ( context_csn ) {
1902 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1903 context_csn, NULL );
1907 bv.bv_len = sizeof("{}")-1;
1908 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1910 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1911 ber_dupbv( &e->e_nname, &e->e_name );
1917 slap_uuidstr_from_normalized(
1918 struct berval* uuidstr,
1919 struct berval* normalized,
1923 unsigned char nibble;
1926 if ( normalized == NULL )
1929 if ( normalized->bv_len != 16 ) {
1936 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1941 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1943 sl_free( new, ctx );
1947 for ( i = 0; i < 16; i++ ) {
1948 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1949 new->bv_val[(i<<1)+d] = '-';
1953 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1954 if ( nibble < 10 ) {
1955 new->bv_val[(i<<1)+d] = nibble + '0';
1957 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1960 nibble = (normalized->bv_val[i]) & 0xF;
1961 if ( nibble < 10 ) {
1962 new->bv_val[(i<<1)+d+1] = nibble + '0';
1964 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1968 new->bv_val[new->bv_len] = '\0';
1974 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1976 const struct berval *uuid1 = v_uuid1;
1977 const struct berval *uuid2 = v_uuid2;
1978 int rc = uuid1->bv_len - uuid2->bv_len;
1979 if ( rc ) return rc;
1980 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
1984 avl_ber_bvfree( void *bv )
1989 if ( ((struct berval *)bv)->bv_val != NULL ) {
1990 ch_free ( ((struct berval *)bv)->bv_val );
1992 ch_free ( (char *) bv );