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 = { 0, NULL };
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 = { 0, NULL };
438 struct sync_cookie syncCookie = { NULL, -1, NULL };
439 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
440 struct berval cookie = { 0, NULL };
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 ) {
1031 static struct berval uuidbva[] = {
1041 Modifications* modlist,
1043 struct berval* syncUUID,
1044 struct sync_cookie* syncCookie_req
1047 Backend *be = op->o_bd;
1048 slap_callback cb = { NULL };
1049 struct berval *syncuuid_bv = NULL;
1050 struct berval syncUUID_strrep = { 0, NULL };
1052 SlapReply rs = {REP_RESULT};
1054 AttributeAssertion ava = {0};
1055 int rc = LDAP_SUCCESS;
1056 int ret = LDAP_SUCCESS;
1059 struct berval pdn = { 0, NULL };
1060 struct berval org_req_dn = { 0, NULL };
1061 struct berval org_req_ndn = { 0, NULL };
1062 struct berval org_dn = { 0, NULL };
1063 struct berval org_ndn = { 0, NULL };
1064 int org_managedsait;
1066 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1068 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1069 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1070 syncuuid_cmp, avl_dup_error );
1073 if ( syncstate == LDAP_SYNC_PRESENT ) {
1077 f.f_choice = LDAP_FILTER_EQUALITY;
1079 ava.aa_desc = slap_schema.si_ad_entryUUID;
1080 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1081 ava.aa_value = *syncUUID;
1082 op->ors_filter = &f;
1084 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1085 op->ors_filterstr.bv_val = (char *) sl_malloc(
1086 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1087 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1088 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1089 syncUUID->bv_val, syncUUID->bv_len );
1090 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1092 op->o_tag = LDAP_REQ_SEARCH;
1093 op->ors_scope = LDAP_SCOPE_SUBTREE;
1095 /* get syncrepl cookie of shadow replica from subentry */
1096 op->o_req_dn = si->si_base;
1097 op->o_req_ndn = si->si_base;
1099 /* set callback function */
1100 op->o_callback = &cb;
1101 cb.sc_response = dn_callback;
1104 si->si_syncUUID_ndn.bv_val = NULL;
1106 if ( limits_check( op, &rs ) == 0 ) {
1107 rc = be->be_search( op, &rs );
1110 if ( op->ors_filterstr.bv_val ) {
1111 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1114 cb.sc_response = null_callback;
1117 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1121 if ( syncstate != LDAP_SYNC_DELETE ) {
1122 op->o_no_psearch = 1;
1125 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1126 if ( op->o_sync_csn.bv_val ) {
1127 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1132 op->o_req_dn = si->si_syncUUID_ndn;
1133 op->o_req_ndn = si->si_syncUUID_ndn;
1134 op->o_tag = LDAP_REQ_DELETE;
1135 rc = be->be_delete( op, &rs );
1137 org_req_dn = op->o_req_dn;
1138 org_req_ndn = op->o_req_ndn;
1140 org_ndn = op->o_ndn;
1141 org_managedsait = get_manageDSAit( op );
1142 op->o_dn = op->o_bd->be_rootdn;
1143 op->o_ndn = op->o_bd->be_rootndn;
1144 op->o_managedsait = 1;
1146 while ( rs.sr_err == LDAP_SUCCESS &&
1147 op->o_delete_glue_parent ) {
1148 op->o_delete_glue_parent = 0;
1149 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1150 slap_callback cb = { NULL };
1151 cb.sc_response = slap_null_cb;
1152 dnParent( &op->o_req_ndn, &pdn );
1154 op->o_req_ndn = pdn;
1155 op->o_callback = &cb;
1156 op->o_bd->be_delete( op, &rs );
1162 op->o_managedsait = org_managedsait;
1164 op->o_ndn = org_ndn;
1165 op->o_req_dn = org_req_dn;
1166 op->o_req_ndn = org_req_ndn;
1167 op->o_delete_glue_parent = 0;
1169 op->o_no_psearch = 0;
1172 switch ( syncstate ) {
1174 case LDAP_SYNC_MODIFY:
1175 if ( rc == LDAP_SUCCESS ||
1176 rc == LDAP_REFERRAL ||
1177 rc == LDAP_NO_SUCH_OBJECT ||
1178 rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1180 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1181 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1182 syncUUID, &ava.aa_value );
1184 op->o_tag = LDAP_REQ_ADD;
1186 op->o_req_dn = e->e_name;
1187 op->o_req_ndn = e->e_nname;
1188 rc = be->be_add( op, &rs );
1190 if ( rc != LDAP_SUCCESS ) {
1191 if ( rc == LDAP_ALREADY_EXISTS ) {
1193 Modifications *modtail;
1195 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1199 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1200 ber_dupbv( &uuidbva[0], syncUUID );
1201 mod->sml_op = LDAP_MOD_REPLACE;
1202 mod->sml_desc = slap_schema.si_ad_entryUUID;
1203 mod->sml_type = mod->sml_desc->ad_cname;
1204 mod->sml_bvalues = uuidbva;
1205 modtail->sml_next = mod;
1207 op->o_tag = LDAP_REQ_MODIFY;
1208 op->orm_modlist = modlist;
1209 op->o_req_dn = e->e_name;
1210 op->o_req_ndn = e->e_nname;
1212 rc = be->be_modify( op, &rs );
1213 if ( rc != LDAP_SUCCESS ) {
1215 LDAP_LOG( OPERATION, ERR,
1216 "syncrepl_entry : be_modify failed (%d)\n",
1219 Debug( LDAP_DEBUG_ANY,
1220 "syncrepl_entry : be_modify failed (%d)\n",
1226 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1227 syncrepl_add_glue( op, e );
1232 LDAP_LOG( OPERATION, ERR,
1233 "syncrepl_entry : be_add failed (%d)\n",
1236 Debug( LDAP_DEBUG_ANY,
1237 "syncrepl_entry : be_add failed (%d)\n",
1244 be_entry_release_w( op, e );
1250 LDAP_LOG( OPERATION, ERR,
1251 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1253 Debug( LDAP_DEBUG_ANY,
1254 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1260 case LDAP_SYNC_DELETE :
1261 /* Already deleted */
1267 LDAP_LOG( OPERATION, ERR,
1268 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1270 Debug( LDAP_DEBUG_ANY,
1271 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1279 if ( syncUUID_strrep.bv_val ) {
1280 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1282 if ( si->si_syncUUID_ndn.bv_val ) {
1283 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1288 static struct berval gcbva[] = {
1295 syncrepl_del_nonpresent(
1300 Backend* be = op->o_bd;
1301 slap_callback cb = { NULL };
1302 SlapReply rs = {REP_RESULT};
1303 struct nonpresent_entry *np_list, *np_prev;
1306 Modifications *mlnext;
1308 Modifications *modlist = NULL;
1309 Modifications **modtail = &modlist;
1312 struct berval pdn = { 0, NULL };
1313 struct berval org_req_dn = { 0, NULL };
1314 struct berval org_req_ndn = { 0, NULL };
1315 struct berval org_dn = { 0, NULL };
1316 struct berval org_ndn = { 0, NULL };
1317 int org_managedsait;
1319 op->o_req_dn = si->si_base;
1320 op->o_req_ndn = si->si_base;
1322 cb.sc_response = nonpresent_callback;
1325 op->o_callback = &cb;
1326 op->o_tag = LDAP_REQ_SEARCH;
1327 op->ors_scope = si->si_scope;
1328 op->ors_deref = LDAP_DEREF_NEVER;
1331 op->ors_attrsonly = 0;
1332 op->ors_attrs = NULL;
1333 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1334 op->ors_filterstr = si->si_filterstr;
1336 op->o_nocaching = 1;
1337 op->o_managedsait = 0;
1339 if ( limits_check( op, &rs ) == 0 ) {
1340 be->be_search( op, &rs );
1343 op->o_managedsait = 1;
1344 op->o_nocaching = 0;
1346 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1348 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1349 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1350 while ( np_list != NULL ) {
1351 LDAP_LIST_REMOVE( np_list, npe_link );
1353 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1354 op->o_tag = LDAP_REQ_DELETE;
1355 op->o_callback = &cb;
1356 cb.sc_response = null_callback;
1358 op->o_req_dn = *np_prev->npe_name;
1359 op->o_req_ndn = *np_prev->npe_nname;
1360 rc = op->o_bd->be_delete( op, &rs );
1362 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1363 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1364 mod->sml_op = LDAP_MOD_REPLACE;
1365 mod->sml_desc = slap_schema.si_ad_objectClass;
1366 mod->sml_type = mod->sml_desc->ad_cname;
1367 mod->sml_bvalues = &gcbva[0];
1369 modtail = &mod->sml_next;
1371 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1372 mod->sml_op = LDAP_MOD_REPLACE;
1373 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1374 mod->sml_type = mod->sml_desc->ad_cname;
1375 mod->sml_bvalues = &gcbva[1];
1377 modtail = &mod->sml_next;
1379 op->o_tag = LDAP_REQ_MODIFY;
1380 op->orm_modlist = modlist;
1382 rc = be->be_modify( op, &rs );
1384 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1385 mlnext = ml->sml_next;
1390 org_req_dn = op->o_req_dn;
1391 org_req_ndn = op->o_req_ndn;
1393 org_ndn = op->o_ndn;
1394 org_managedsait = get_manageDSAit( op );
1395 op->o_dn = op->o_bd->be_rootdn;
1396 op->o_ndn = op->o_bd->be_rootndn;
1397 op->o_managedsait = 1;
1399 while ( rs.sr_err == LDAP_SUCCESS &&
1400 op->o_delete_glue_parent ) {
1401 op->o_delete_glue_parent = 0;
1402 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1403 slap_callback cb = { NULL };
1404 cb.sc_response = slap_null_cb;
1405 dnParent( &op->o_req_ndn, &pdn );
1407 op->o_req_ndn = pdn;
1408 op->o_callback = &cb;
1409 /* give it a root privil ? */
1410 op->o_bd->be_delete( op, &rs );
1416 op->o_managedsait = org_managedsait;
1418 op->o_ndn = org_ndn;
1419 op->o_req_dn = org_req_dn;
1420 op->o_req_ndn = org_req_ndn;
1421 op->o_delete_glue_parent = 0;
1423 ber_bvfree( np_prev->npe_name );
1424 ber_bvfree( np_prev->npe_nname );
1425 op->o_req_dn.bv_val = NULL;
1426 op->o_req_ndn.bv_val = NULL;
1441 Backend *be = op->o_bd;
1442 slap_callback cb = { NULL };
1447 struct berval dn = {0, NULL};
1448 struct berval ndn = {0, NULL};
1450 SlapReply rs = {REP_RESULT};
1453 op->o_tag = LDAP_REQ_ADD;
1454 op->o_callback = &cb;
1455 cb.sc_response = null_callback;
1456 cb.sc_private = NULL;
1461 /* count RDNs in suffix */
1462 if ( be->be_nsuffix[0].bv_len ) {
1463 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1473 /* Start with BE suffix */
1474 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1475 comma = strrchr(dn.bv_val, ',');
1476 if ( ptr ) *ptr = ',';
1477 if ( comma ) *comma = '\0';
1482 dn.bv_len -= ptr - dn.bv_val;
1485 /* the normalizedDNs are always the same length, no counting
1488 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1489 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1490 ndn.bv_len = be->be_nsuffix[0].bv_len;
1493 while ( ndn.bv_val > e->e_nname.bv_val ) {
1494 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1495 ber_dupbv( &glue->e_name, &dn );
1496 ber_dupbv( &glue->e_nname, &ndn );
1498 a = ch_calloc( 1, sizeof( Attribute ));
1499 a->a_desc = slap_schema.si_ad_objectClass;
1501 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1502 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1503 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1504 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1506 a->a_nvals = a->a_vals;
1508 a->a_next = glue->e_attrs;
1511 a = ch_calloc( 1, sizeof( Attribute ));
1512 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1514 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1515 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1516 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1518 a->a_nvals = a->a_vals;
1520 a->a_next = glue->e_attrs;
1523 op->o_req_dn = glue->e_name;
1524 op->o_req_ndn = glue->e_nname;
1526 rc = be->be_add ( op, &rs );
1527 if ( rc == LDAP_SUCCESS ) {
1528 be_entry_release_w( op, glue );
1530 /* incl. ALREADY EXIST */
1534 /* Move to next child */
1535 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1538 if ( ptr == e->e_name.bv_val ) break;
1540 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1541 for( ptr = ndn.bv_val-2;
1542 ptr > e->e_nname.bv_val && *ptr != ',';
1548 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1551 op->o_req_dn = e->e_name;
1552 op->o_req_ndn = e->e_nname;
1554 rc = be->be_add ( op, &rs );
1555 if ( rc == LDAP_SUCCESS ) {
1556 be_entry_release_w( op, e );
1564 static struct berval ocbva[] = {
1566 BER_BVC("subentry"),
1567 BER_BVC("syncConsumerSubentry"),
1571 static struct berval cnbva[] = {
1576 static struct berval ssbva[] = {
1581 static struct berval scbva[] = {
1587 syncrepl_updateCookie(
1591 struct sync_cookie *syncCookie
1594 Backend *be = op->o_bd;
1596 Modifications *mlnext;
1598 Modifications *modlist = NULL;
1599 Modifications **modtail = &modlist;
1602 char txtbuf[SLAP_TEXT_BUFLEN];
1603 size_t textlen = sizeof txtbuf;
1608 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1609 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1610 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1612 slap_callback cb = { NULL };
1613 SlapReply rs = {REP_RESULT};
1615 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1616 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1618 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1619 mod->sml_op = LDAP_MOD_REPLACE;
1620 mod->sml_desc = slap_schema.si_ad_objectClass;
1621 mod->sml_type = mod->sml_desc->ad_cname;
1622 mod->sml_bvalues = ocbva;
1624 modtail = &mod->sml_next;
1626 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1627 assert( si->si_rid < 1000 );
1628 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1629 slap_syncrepl_bvc.bv_len,
1630 "syncrepl%ld", si->si_rid );
1631 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1632 mod->sml_op = LDAP_MOD_REPLACE;
1633 mod->sml_desc = slap_schema.si_ad_cn;
1634 mod->sml_type = mod->sml_desc->ad_cname;
1635 mod->sml_bvalues = cnbva;
1637 modtail = &mod->sml_next;
1639 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1640 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1641 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1642 mod->sml_op = LDAP_MOD_REPLACE;
1643 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1644 mod->sml_type = mod->sml_desc->ad_cname;
1645 mod->sml_bvalues = scbva;
1647 modtail = &mod->sml_next;
1649 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1650 mod->sml_op = LDAP_MOD_REPLACE;
1651 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1652 mod->sml_type = mod->sml_desc->ad_cname;
1653 mod->sml_bvalues = ssbva;
1655 modtail = &mod->sml_next;
1659 op->o_tag = LDAP_REQ_ADD;
1660 rc = slap_mods_opattrs( op, modlist, modtail,
1661 &text,txtbuf, textlen );
1663 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1664 ml->sml_op = LDAP_MOD_REPLACE;
1667 if( rc != LDAP_SUCCESS ) {
1669 LDAP_LOG( OPERATION, ERR,
1670 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1672 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1677 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1679 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1680 assert( si->si_rid < 1000 );
1681 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1682 slap_syncrepl_cn_bvc.bv_len,
1683 "cn=syncrepl%ld", si->si_rid );
1685 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1687 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1688 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1690 if ( slap_syncrepl_dn_bv.bv_val ) {
1691 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1696 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1698 if( rc != LDAP_SUCCESS ) {
1700 LDAP_LOG( OPERATION, ERR,
1701 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1703 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1708 cb.sc_response = null_callback;
1711 op->o_callback = &cb;
1712 op->o_req_dn = e->e_name;
1713 op->o_req_ndn = e->e_nname;
1715 /* update persistent cookie */
1716 update_cookie_retry:
1717 op->o_tag = LDAP_REQ_MODIFY;
1718 op->orm_modlist = modlist;
1719 rc = be->be_modify( op, &rs );
1721 if ( rc != LDAP_SUCCESS ) {
1722 if ( rc == LDAP_REFERRAL ||
1723 rc == LDAP_NO_SUCH_OBJECT ) {
1724 op->o_tag = LDAP_REQ_ADD;
1726 rc = be->be_add( op, &rs );
1727 if ( rc != LDAP_SUCCESS ) {
1728 if ( rc == LDAP_ALREADY_EXISTS ) {
1729 goto update_cookie_retry;
1730 } else if ( rc == LDAP_REFERRAL ||
1731 rc == LDAP_NO_SUCH_OBJECT ) {
1733 LDAP_LOG( OPERATION, ERR,
1734 "cookie will be non-persistent\n",
1737 Debug( LDAP_DEBUG_ANY,
1738 "cookie will be non-persistent\n",
1743 LDAP_LOG( OPERATION, ERR,
1744 "be_add failed (%d)\n",
1747 Debug( LDAP_DEBUG_ANY,
1748 "be_add failed (%d)\n",
1753 be_entry_release_w( op, e );
1758 LDAP_LOG( OPERATION, ERR,
1759 "be_modify failed (%d)\n", rc, 0, 0 );
1761 Debug( LDAP_DEBUG_ANY,
1762 "be_modify failed (%d)\n", rc, 0, 0 );
1773 if ( cnbva[0].bv_val ) {
1774 ch_free( cnbva[0].bv_val );
1775 cnbva[0].bv_val = NULL;
1777 if ( scbva[0].bv_val ) {
1778 ch_free( scbva[0].bv_val );
1779 scbva[0].bv_val = NULL;
1782 if ( mlnext->sml_next ) {
1783 slap_mods_free( mlnext->sml_next );
1784 mlnext->sml_next = NULL;
1787 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1788 mlnext = ml->sml_next;
1801 syncinfo_t *si = op->o_callback->sc_private;
1803 if ( rs->sr_type == REP_SEARCH ) {
1804 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1806 LDAP_LOG( OPERATION, ERR,
1807 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1809 Debug( LDAP_DEBUG_ANY,
1810 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1813 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1817 return LDAP_SUCCESS;
1821 nonpresent_callback(
1826 syncinfo_t *si = op->o_callback->sc_private;
1829 struct berval* present_uuid = NULL;
1830 struct nonpresent_entry *np_entry;
1832 if ( rs->sr_type == REP_RESULT ) {
1833 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1834 si->si_presentlist = NULL;
1836 } else if ( rs->sr_type == REP_SEARCH ) {
1837 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1839 if ( a == NULL ) return 0;
1841 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1844 if ( present_uuid == NULL ) {
1845 np_entry = (struct nonpresent_entry *)
1846 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1847 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1848 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1849 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1852 avl_delete( &si->si_presentlist,
1853 &a->a_vals[0], syncuuid_cmp );
1854 ch_free( present_uuid->bv_val );
1855 ch_free( present_uuid );
1858 return LDAP_SUCCESS;
1866 if ( rs->sr_err != LDAP_SUCCESS &&
1867 rs->sr_err != LDAP_REFERRAL &&
1868 rs->sr_err != LDAP_ALREADY_EXISTS &&
1869 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1870 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1873 LDAP_LOG( OPERATION, ERR,
1874 "null_callback : error code 0x%x\n",
1877 Debug( LDAP_DEBUG_ANY,
1878 "null_callback : error code 0x%x\n",
1882 return LDAP_SUCCESS;
1886 slap_create_syncrepl_entry(
1888 struct berval *context_csn,
1896 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1898 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1900 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1903 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1905 if ( context_csn ) {
1906 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1907 context_csn, NULL );
1911 bv.bv_len = sizeof("{}")-1;
1912 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1914 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1915 ber_dupbv( &e->e_nname, &e->e_name );
1921 slap_uuidstr_from_normalized(
1922 struct berval* uuidstr,
1923 struct berval* normalized,
1927 unsigned char nibble;
1930 if ( normalized == NULL )
1933 if ( normalized->bv_len != 16 ) {
1940 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1945 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1947 sl_free( new, ctx );
1951 for ( i = 0; i < 16; i++ ) {
1952 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1953 new->bv_val[(i<<1)+d] = '-';
1957 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1958 if ( nibble < 10 ) {
1959 new->bv_val[(i<<1)+d] = nibble + '0';
1961 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1964 nibble = (normalized->bv_val[i]) & 0xF;
1965 if ( nibble < 10 ) {
1966 new->bv_val[(i<<1)+d+1] = nibble + '0';
1968 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1972 new->bv_val[new->bv_len] = '\0';
1978 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1980 const struct berval *uuid1 = v_uuid1;
1981 const struct berval *uuid2 = v_uuid2;
1982 int rc = uuid1->bv_len - uuid2->bv_len;
1983 if ( rc ) return rc;
1984 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1988 avl_ber_bvfree( void *bv )
1993 if ( ((struct berval *)bv)->bv_val != NULL ) {
1994 ch_free ( ((struct berval *)bv)->bv_val );
1996 ch_free ( (char *) bv );