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 );
92 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
93 tmp[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
98 tmp = ( char ** ) ch_realloc( si->si_attrs, 3 * sizeof( char * ));
101 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
103 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
106 tmp[ n++ ] = ch_strdup( "*" );
107 tmp[ n++ ] = ch_strdup( "+" );
113 for ( n = 0; si->si_exattrs[ n ] != NULL; n++ ) /* empty */;
115 /* Delete Attributes from exattrs list */
116 for ( i = 0; sync_descs[i] != NULL; i++ ) {
117 for ( j = 0; si->si_exattrs[j] != NULL; j++ ) {
118 if ( strcmp( si->si_exattrs[j], sync_descs[i]->ad_cname.bv_val )
121 ch_free( si->si_exattrs[j] );
122 for ( k = j; si->si_exattrs[k] != NULL; k++ ) {
123 si->si_exattrs[k] = si->si_exattrs[k+1];
136 BerElementBuffer berbuf;
137 BerElement *ber = (BerElement *)&berbuf;
138 LDAPControl c[2], *ctrls[3];
139 struct timeval timeout;
143 /* setup LDAP SYNC control */
144 ber_init2( ber, NULL, LBER_USE_DER );
145 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
147 if ( si->si_syncCookie.octet_str &&
148 si->si_syncCookie.octet_str[0].bv_val )
150 ber_printf( ber, "{eO}",
152 &si->si_syncCookie.octet_str[0] );
154 ber_printf( ber, "{e}",
158 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
163 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
164 c[0].ldctl_iscritical = si->si_type < 0;
167 if ( si->si_authzId ) {
168 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
169 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
170 c[1].ldctl_iscritical = 1;
177 timeout.tv_sec = si->si_tlimit;
180 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
181 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
182 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
183 si->si_slimit, &msgid );
194 int cmdline_cookie_found = 0;
196 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
197 struct berval syncrepl_cn_bv;
198 struct sync_cookie *sc = NULL;
199 struct sync_cookie syncCookie = { NULL, -1, NULL };
205 psub = &si->si_be->be_nsuffix[0];
207 /* Init connection to master */
208 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
209 if ( rc != LDAP_SUCCESS ) {
211 LDAP_LOG( OPERATION, ERR,
212 "do_syncrep1: ldap_initialize failed (%s)\n",
213 si->si_provideruri, 0, 0 );
215 Debug( LDAP_DEBUG_ANY,
216 "do_syncrep1: ldap_initialize failed (%s)\n",
217 si->si_provideruri, 0, 0 );
222 op->o_protocol = LDAP_VERSION3;
223 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
228 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
229 if( rc != LDAP_SUCCESS ) {
231 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
232 "%s: ldap_start_tls failed (%d)\n",
233 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
236 Debug( LDAP_DEBUG_ANY,
237 "%s: ldap_start_tls failed (%d)\n",
238 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
241 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
245 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
246 #ifdef HAVE_CYRUS_SASL
249 if ( si->si_secprops != NULL ) {
250 rc = ldap_set_option( si->si_ld,
251 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
253 if( rc != LDAP_OPT_SUCCESS ) {
255 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
256 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
257 si->si_provideruri, si->si_secprops, 0 );
259 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
260 "(%s,SECPROPS,\"%s\") failed!\n",
261 si->si_provideruri, si->si_secprops, 0 );
267 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
268 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
270 rc = ldap_sasl_interactive_bind_s( si->si_ld,
278 lutil_sasl_freedefs( defaults );
280 /* FIXME: different error behaviors according to
282 * 2) on err policy : exit, retry, backoff ...
284 if ( rc != LDAP_SUCCESS ) {
286 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
287 "ldap_sasl_interactive_bind_s failed (%d)\n",
290 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
291 "ldap_sasl_interactive_bind_s failed (%d)\n",
295 /* FIXME (see above comment) */
296 /* if Kerberos credentials cache is not active, retry */
297 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
298 rc == LDAP_LOCAL_ERROR )
300 rc = LDAP_SERVER_DOWN;
305 #else /* HAVE_CYRUS_SASL */
306 /* Should never get here, we trapped this at config time */
308 fprintf( stderr, "not compiled with SASL support\n" );
314 rc = ldap_bind_s( si->si_ld,
315 si->si_binddn, si->si_passwd, si->si_bindmethod );
316 if ( rc != LDAP_SUCCESS ) {
318 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
319 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
321 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
322 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
328 /* Set SSF to strongest of TLS, SASL SSFs */
331 op->o_transport_ssf = 0;
333 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
334 == LDAP_SUCCESS && ssl != NULL )
336 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
338 #endif /* HAVE_TLS */
339 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
340 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
341 ? op->o_sasl_ssf : op->o_tls_ssf;
343 /* get syncrepl cookie of shadow replica from subentry */
344 assert( si->si_rid < 1000 );
345 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
346 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
347 CN_STR "syncrepl%ld", si->si_rid );
348 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
349 op->o_req_dn = op->o_req_ndn;
351 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
352 if ( si->si_rid == sc->rid ) {
353 cmdline_cookie_found = 1;
358 if ( cmdline_cookie_found ) {
359 /* cookie is supplied in the command line */
360 BerVarray cookie = NULL;
361 struct berval cookie_bv;
363 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
364 slap_sync_cookie_free( &si->si_syncCookie, 0 );
366 /* read stored cookie if it exists */
367 backend_attribute( op, NULL, &op->o_req_ndn,
368 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
371 /* no stored cookie */
372 if ( sc->ctxcsn == NULL ||
373 sc->ctxcsn->bv_val == NULL ) {
374 /* if cmdline cookie does not have ctxcsn */
375 /* component, set it to an initial value */
376 slap_init_sync_cookie_ctxcsn( sc );
378 slap_dup_sync_cookie( &si->si_syncCookie, sc );
379 slap_sync_cookie_free( sc, 1 );
384 struct berval newcookie = BER_BVNULL;
385 ber_dupbv( &cookie_bv, &cookie[0] );
386 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
387 slap_parse_sync_cookie( &si->si_syncCookie );
388 ber_bvarray_free( si->si_syncCookie.octet_str );
389 si->si_syncCookie.octet_str = NULL;
390 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
391 if ( sc->sid != -1 ) {
392 /* command line cookie wins */
393 si->si_syncCookie.sid = sc->sid;
395 if ( sc->ctxcsn != NULL ) {
396 /* command line cookie wins */
397 if ( si->si_syncCookie.ctxcsn ) {
398 ber_bvarray_free( si->si_syncCookie.ctxcsn );
399 si->si_syncCookie.ctxcsn = NULL;
401 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
402 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
404 if ( sc->rid != -1 ) {
405 /* command line cookie wins */
406 si->si_syncCookie.rid = sc->rid;
408 slap_sync_cookie_free( sc, 1 );
410 slap_compose_sync_cookie( NULL, &newcookie,
411 &si->si_syncCookie.ctxcsn[0],
412 si->si_syncCookie.sid, si->si_syncCookie.rid );
413 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
417 /* no command line cookie is specified */
418 if ( si->si_syncCookie.octet_str == NULL ) {
419 BerVarray cookie = NULL;
420 struct berval cookie_bv;
421 /* try to read stored cookie */
422 backend_attribute( op, NULL, &op->o_req_ndn,
423 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
425 ber_dupbv( &cookie_bv, &cookie[0] );
426 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
427 slap_parse_sync_cookie( &si->si_syncCookie );
428 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
433 rc = ldap_sync_search( si, op->o_tmpmemctx );
435 if( rc != LDAP_SUCCESS ) {
437 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
438 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
440 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
441 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
448 ldap_unbind( si->si_ld );
461 LDAPControl **rctrls = NULL;
464 BerElementBuffer berbuf;
465 BerElement *ber = (BerElement *)&berbuf;
467 LDAPMessage *res = NULL;
468 LDAPMessage *msg = NULL;
471 struct berval *retdata = NULL;
476 struct berval syncUUID = BER_BVNULL;
477 struct sync_cookie syncCookie = { NULL, -1, NULL };
478 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
479 struct berval cookie = BER_BVNULL;
487 Modifications *modlist = NULL;
492 struct timeval *tout_p = NULL;
493 struct timeval tout = { 0, 0 };
495 int refreshDeletes = 0;
497 BerVarray syncUUIDs = NULL;
500 if ( slapd_shutdown ) {
505 ber_init2( ber, NULL, LBER_USE_DER );
506 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
509 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
514 psub = &si->si_be->be_nsuffix[0];
516 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
518 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
524 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
525 tout_p, &res )) > 0 )
527 if ( slapd_shutdown ) {
531 for( msg = ldap_first_message( si->si_ld, res );
533 msg = ldap_next_message( si->si_ld, msg ) )
535 switch( ldap_msgtype( msg ) ) {
536 case LDAP_RES_SEARCH_ENTRY:
537 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
538 /* we can't work without the control */
544 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
545 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
546 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
547 ber_scanf( ber, /*"{"*/ "m}", &cookie );
548 if ( cookie.bv_val ) {
549 struct berval tmp_bv;
550 ber_dupbv( &tmp_bv, &cookie );
551 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
553 if ( syncCookie.octet_str &&
554 syncCookie.octet_str[0].bv_val )
555 slap_parse_sync_cookie( &syncCookie );
557 if ( syncrepl_message_to_entry( si, op, msg,
558 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
559 rc_efree = syncrepl_entry( si, op, entry, modlist,
560 syncstate, &syncUUID, &syncCookie_req );
561 if ( syncCookie.octet_str &&
562 syncCookie.octet_str[0].bv_val )
564 syncrepl_updateCookie( si, op, psub, &syncCookie );
567 ldap_controls_free( rctrls );
569 slap_mods_free( modlist );
571 if ( rc_efree && entry ) {
577 case LDAP_RES_SEARCH_REFERENCE:
579 LDAP_LOG( OPERATION, ERR,
580 "do_syncrep2 : reference received\n", 0, 0, 0 );
582 Debug( LDAP_DEBUG_ANY,
583 "do_syncrep2 : reference received\n", 0, 0, 0 );
587 case LDAP_RES_SEARCH_RESULT:
588 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
592 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
594 ber_scanf( ber, "{" /*"}"*/);
595 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
596 ber_scanf( ber, "m", &cookie );
597 if ( cookie.bv_val ) {
598 struct berval tmp_bv;
599 ber_dupbv( &tmp_bv, &cookie );
600 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
602 if ( syncCookie.octet_str &&
603 syncCookie.octet_str[0].bv_val )
605 slap_parse_sync_cookie( &syncCookie );
608 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
610 ber_scanf( ber, "b", &refreshDeletes );
612 ber_scanf( ber, /*"{"*/ "}" );
614 if ( syncCookie_req.ctxcsn == NULL ) {
616 } else if ( syncCookie.ctxcsn == NULL ) {
619 value_match( &match, slap_schema.si_ad_entryCSN,
620 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
621 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
622 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
625 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
626 match < 0 && err == LDAP_SUCCESS )
628 syncrepl_updateCookie( si, op, psub, &syncCookie );
631 ldap_controls_free( rctrls );
633 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
634 /* FIXME : different error behaviors according to
635 * 1) err code : LDAP_BUSY ...
636 * 2) on err policy : stop service, stop sync, retry
638 if ( refreshDeletes == 0 && match < 0 &&
639 err == LDAP_SUCCESS )
641 syncrepl_del_nonpresent( op, si );
643 avl_free( si->si_presentlist, avl_ber_bvfree );
644 si->si_presentlist = NULL;
651 case LDAP_RES_INTERMEDIATE:
652 rc = ldap_parse_intermediate( si->si_ld, msg,
653 &retoid, &retdata, NULL, 0 );
654 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
655 int si_refreshDelete = 0;
656 int si_refreshPresent = 0;
657 ber_init2( ber, retdata, LBER_USE_DER );
659 switch ( si_tag = ber_peek_tag( ber, &len )) {
661 case LDAP_TAG_SYNC_NEW_COOKIE:
662 ber_scanf( ber, "tm", &tag, &cookie );
664 case LDAP_TAG_SYNC_REFRESH_DELETE:
665 si_refreshDelete = 1;
666 case LDAP_TAG_SYNC_REFRESH_PRESENT:
667 si_refreshPresent = 1;
668 ber_scanf( ber, "t{" /*"}"*/, &tag );
669 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
671 ber_scanf( ber, "m", &cookie );
672 if ( cookie.bv_val ) {
673 struct berval tmp_bv;
674 ber_dupbv( &tmp_bv, &cookie );
675 ber_bvarray_add( &syncCookie.octet_str,
678 if ( syncCookie.octet_str &&
679 syncCookie.octet_str[0].bv_val )
681 slap_parse_sync_cookie( &syncCookie );
684 if ( ber_peek_tag( ber, &len ) ==
685 LDAP_TAG_REFRESHDONE )
687 ber_scanf( ber, "b", &refreshDone );
689 ber_scanf( ber, /*"{"*/ "}" );
691 case LDAP_TAG_SYNC_ID_SET:
692 ber_scanf( ber, "t{" /*"}"*/, &tag );
693 if ( ber_peek_tag( ber, &len ) ==
694 LDAP_TAG_SYNC_COOKIE )
696 ber_scanf( ber, "m", &cookie );
697 if ( cookie.bv_val ) {
698 struct berval tmp_bv;
699 ber_dupbv( &tmp_bv, &cookie );
700 ber_bvarray_add( &syncCookie.octet_str,
703 if ( syncCookie.octet_str &&
704 syncCookie.octet_str[0].bv_val )
706 slap_parse_sync_cookie( &syncCookie );
709 if ( ber_peek_tag( ber, &len ) ==
710 LDAP_TAG_REFRESHDELETES )
712 ber_scanf( ber, "b", &refreshDeletes );
714 ber_scanf( ber, "[W]", &syncUUIDs );
715 ber_scanf( ber, /*"{"*/ "}" );
716 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
717 struct berval *syncuuid_bv;
718 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
719 avl_insert( &si->si_presentlist,
720 (caddr_t) syncuuid_bv,
721 syncuuid_cmp, avl_dup_error );
723 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
727 LDAP_LOG( OPERATION, ERR,
728 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
729 (long) si_tag, 0, 0 );
731 Debug( LDAP_DEBUG_ANY,
732 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
733 (long) si_tag, 0, 0 );
735 ldap_memfree( retoid );
736 ber_bvfree( retdata );
740 if ( syncCookie_req.ctxcsn == NULL ) {
742 } else if ( syncCookie.ctxcsn == NULL ) {
745 value_match( &match, slap_schema.si_ad_entryCSN,
746 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
747 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
748 &syncCookie_req.ctxcsn[0],
749 &syncCookie.ctxcsn[0], &text );
752 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
755 syncrepl_updateCookie( si, op, psub, &syncCookie);
758 if ( si_refreshPresent == 1 ) {
760 syncrepl_del_nonpresent( op, si );
764 ldap_memfree( retoid );
765 ber_bvfree( retdata );
770 LDAP_LOG( OPERATION, ERR, "do_syncrep2 :"
771 " unknown intermediate "
772 "response\n", 0, 0, 0 );
774 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
775 "unknown intermediate response (%d)\n",
778 ldap_memfree( retoid );
779 ber_bvfree( retdata );
786 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
787 "unknown message\n", 0, 0, 0 );
789 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
790 "unknown message\n", 0, 0, 0 );
795 if ( syncCookie.octet_str ) {
796 slap_sync_cookie_free( &syncCookie_req, 0 );
797 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
798 slap_sync_cookie_free( &syncCookie, 0 );
808 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
809 errstr = ldap_err2string( rc );
812 LDAP_LOG( OPERATION, ERR,
813 "do_syncrep2 : %s\n", errstr, 0, 0 );
815 Debug( LDAP_DEBUG_ANY,
816 "do_syncrep2 : %s\n", errstr, 0, 0 );
821 slap_sync_cookie_free( &syncCookie, 0 );
822 slap_sync_cookie_free( &syncCookie_req, 0 );
824 if ( res ) ldap_msgfree( res );
826 if ( rc && si->si_ld ) {
827 ldap_unbind( si->si_ld );
839 struct re_s* rtask = arg;
840 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
841 Connection conn = {0};
843 int rc = LDAP_SUCCESS;
850 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
852 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
858 switch( abs( si->si_type )) {
859 case LDAP_SYNC_REFRESH_ONLY:
860 case LDAP_SYNC_REFRESH_AND_PERSIST:
866 if ( slapd_shutdown && si->si_ld ) {
867 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
868 connection_client_stop( s );
869 ldap_unbind( si->si_ld );
874 connection_fake_init( &conn, &op, ctx );
876 /* use global malloc for now */
877 op.o_tmpmemctx = NULL;
878 op.o_tmpmfuncs = &ch_mfuncs;
880 op.o_dn = si->si_updatedn;
881 op.o_ndn = si->si_updatedn;
882 op.o_managedsait = 1;
885 op.o_sync_state.ctxcsn = NULL;
886 op.o_sync_state.sid = -1;
887 op.o_sync_state.octet_str = NULL;
888 op.o_sync_slog_size = -1;
889 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
890 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
892 /* Establish session, do search */
895 rc = do_syncrep1( &op, si );
898 /* Process results */
899 if ( rc == LDAP_SUCCESS ) {
900 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
902 rc = do_syncrep2( &op, si );
904 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
905 /* If we succeeded, enable the connection for further listening.
906 * If we failed, tear down the connection and reschedule.
908 if ( rc == LDAP_SUCCESS ) {
910 rc = connection_client_setup( s, do_syncrepl,
913 connection_client_enable( s );
915 } else if ( !first ) {
919 if ( rc == -2 ) rc = 0;
923 /* At this point, we have 4 cases:
924 * 1) for any hard failure, give up and remove this task
925 * 2) for ServerDown, reschedule this task to run
926 * 3) for Refresh and Success, reschedule to run
927 * 4) for Persist and Success, reschedule to defer
929 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
931 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
932 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
936 connection_client_stop( s );
939 if ( rc == LDAP_SUCCESS ) {
940 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
943 rtask->interval.tv_sec = si->si_interval;
944 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
945 if ( si->si_retrynum ) {
946 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
947 si->si_retrynum[i] = si->si_retrynum_init[i];
949 si->si_retrynum[i] = -2;
952 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
953 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
957 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
958 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
959 } else if ( si->si_retrynum[i] >= -1 ) {
960 if ( si->si_retrynum[i] > 0 )
961 si->si_retrynum[i]--;
962 rtask->interval.tv_sec = si->si_retryinterval[i];
963 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
964 slap_wake_listener();
968 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
974 syncrepl_message_to_entry(
978 Modifications **modlist,
984 BerElement *ber = NULL;
987 Modifications **modtail = modlist;
990 char txtbuf[SLAP_TEXT_BUFLEN];
991 size_t textlen = sizeof txtbuf;
993 struct berval bdn = {0, NULL}, dn, ndn;
998 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1000 LDAP_LOG( OPERATION, ERR,
1001 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1003 Debug( LDAP_DEBUG_ANY,
1004 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1009 op->o_tag = LDAP_REQ_ADD;
1011 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1013 if ( rc != LDAP_SUCCESS ) {
1015 LDAP_LOG( OPERATION, ERR,
1016 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1018 Debug( LDAP_DEBUG_ANY,
1019 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1024 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1025 ber_dupbv( &op->o_req_dn, &dn );
1026 ber_dupbv( &op->o_req_ndn, &ndn );
1027 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1028 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1030 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1033 return LDAP_SUCCESS;
1036 if ( entry == NULL ) {
1040 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1042 e->e_name = op->o_req_dn;
1043 e->e_nname = op->o_req_ndn;
1045 while ( ber_remaining( ber ) ) {
1046 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1047 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1052 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1054 mod->sml_op = LDAP_MOD_REPLACE;
1055 mod->sml_next = NULL;
1056 mod->sml_desc = NULL;
1057 mod->sml_type = tmp.sml_type;
1058 mod->sml_values = tmp.sml_values;
1059 mod->sml_nvalues = NULL;
1062 modtail = &mod->sml_next;
1065 if ( *modlist == NULL ) {
1067 LDAP_LOG( OPERATION, ERR,
1068 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1070 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1077 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1079 if ( rc != LDAP_SUCCESS ) {
1081 LDAP_LOG( OPERATION, ERR,
1082 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1084 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1090 /* Strip out dynamically generated attrs */
1091 for ( modtail = modlist; *modtail ; ) {
1093 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1094 *modtail = mod->sml_next;
1095 slap_mod_free( &mod->sml_mod, 0 );
1098 modtail = &mod->sml_next;
1102 /* Strip out attrs in exattrs list */
1103 for ( modtail = modlist; *modtail ; ) {
1105 if ( ldap_charray_inlist( si->si_exattrs,
1106 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1107 *modtail = mod->sml_next;
1108 slap_mod_free( &mod->sml_mod, 0 );
1111 modtail = &mod->sml_next;
1115 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1116 if( rc != LDAP_SUCCESS ) {
1118 LDAP_LOG( OPERATION, ERR,
1119 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1121 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1127 ber_free ( ber, 0 );
1128 if ( rc != LDAP_SUCCESS ) {
1143 Modifications* modlist,
1145 struct berval* syncUUID,
1146 struct sync_cookie* syncCookie_req )
1148 Backend *be = op->o_bd;
1149 slap_callback cb = { NULL };
1150 struct berval *syncuuid_bv = NULL;
1151 struct berval syncUUID_strrep = BER_BVNULL;
1152 struct berval uuid_bv = BER_BVNULL;
1154 SlapReply rs_search = {REP_RESULT};
1155 SlapReply rs_delete = {REP_RESULT};
1156 SlapReply rs_add = {REP_RESULT};
1157 SlapReply rs_modify = {REP_RESULT};
1159 AttributeAssertion ava = {0};
1160 int rc = LDAP_SUCCESS;
1161 int ret = LDAP_SUCCESS;
1164 struct berval pdn = BER_BVNULL;
1165 struct berval org_req_dn = BER_BVNULL;
1166 struct berval org_req_ndn = BER_BVNULL;
1167 struct berval org_dn = BER_BVNULL;
1168 struct berval org_ndn = BER_BVNULL;
1169 int org_managedsait;
1171 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1172 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1173 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1174 syncuuid_cmp, avl_dup_error );
1177 if ( syncstate == LDAP_SYNC_PRESENT ) {
1179 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1180 if ( entry == NULL ) {
1185 f.f_choice = LDAP_FILTER_EQUALITY;
1187 ava.aa_desc = slap_schema.si_ad_entryUUID;
1188 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1189 ava.aa_value = *syncUUID;
1190 op->ors_filter = &f;
1192 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1193 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1194 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1195 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1196 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1197 syncUUID->bv_val, syncUUID->bv_len );
1198 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1200 op->o_tag = LDAP_REQ_SEARCH;
1201 op->ors_scope = LDAP_SCOPE_SUBTREE;
1203 /* get syncrepl cookie of shadow replica from subentry */
1204 op->o_req_dn = si->si_base;
1205 op->o_req_ndn = si->si_base;
1207 op->o_time = slap_get_time();
1208 op->ors_tlimit = SLAP_NO_LIMIT;
1211 op->ors_attrs = slap_anlist_no_attrs;
1212 op->ors_attrsonly = 1;
1214 /* set callback function */
1215 op->o_callback = &cb;
1216 cb.sc_response = dn_callback;
1219 si->si_syncUUID_ndn.bv_val = NULL;
1221 if ( limits_check( op, &rs_search ) == 0 ) {
1222 rc = be->be_search( op, &rs_search );
1225 if ( op->ors_filterstr.bv_val ) {
1226 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1229 cb.sc_response = null_callback;
1232 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1235 if ( syncstate != LDAP_SYNC_DELETE ) {
1236 op->o_no_psearch = 1;
1239 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1240 if ( op->o_sync_csn.bv_val ) {
1241 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1246 op->o_req_dn = si->si_syncUUID_ndn;
1247 op->o_req_ndn = si->si_syncUUID_ndn;
1248 op->o_tag = LDAP_REQ_DELETE;
1249 rc = be->be_delete( op, &rs_delete );
1251 org_req_dn = op->o_req_dn;
1252 org_req_ndn = op->o_req_ndn;
1254 org_ndn = op->o_ndn;
1255 org_managedsait = get_manageDSAit( op );
1256 op->o_dn = op->o_bd->be_rootdn;
1257 op->o_ndn = op->o_bd->be_rootndn;
1258 op->o_managedsait = 1;
1260 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1261 op->o_delete_glue_parent = 0;
1262 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1263 slap_callback cb = { NULL };
1264 cb.sc_response = slap_null_cb;
1265 dnParent( &op->o_req_ndn, &pdn );
1267 op->o_req_ndn = pdn;
1268 op->o_callback = &cb;
1269 op->o_bd->be_delete( op, &rs_delete );
1275 op->o_managedsait = org_managedsait;
1277 op->o_ndn = org_ndn;
1278 op->o_req_dn = org_req_dn;
1279 op->o_req_ndn = org_req_ndn;
1280 op->o_delete_glue_parent = 0;
1282 op->o_no_psearch = 0;
1285 switch ( syncstate ) {
1287 case LDAP_SYNC_MODIFY:
1288 if ( rs_search.sr_err == LDAP_SUCCESS ||
1289 rs_search.sr_err == LDAP_REFERRAL ||
1290 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1291 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1293 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1294 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1295 &syncUUID_strrep, syncUUID );
1297 op->o_tag = LDAP_REQ_ADD;
1299 op->o_req_dn = entry->e_name;
1300 op->o_req_ndn = entry->e_nname;
1302 rc = be->be_add( op, &rs_add );
1304 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1305 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1306 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1308 Modifications *modtail = modlist;
1312 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1316 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1317 ber_dupbv( &uuid_bv, syncUUID );
1318 mod->sml_op = LDAP_MOD_REPLACE;
1319 mod->sml_desc = slap_schema.si_ad_entryUUID;
1320 mod->sml_type = mod->sml_desc->ad_cname;
1321 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1322 modtail->sml_next = mod;
1324 op->o_tag = LDAP_REQ_MODIFY;
1325 op->orm_modlist = modlist;
1326 op->o_req_dn = entry->e_name;
1327 op->o_req_ndn = entry->e_nname;
1329 rc = be->be_modify( op, &rs_modify );
1330 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1332 LDAP_LOG( OPERATION, ERR,
1333 "syncrepl_entry : be_modify failed (%d)\n",
1334 rs_modify.sr_err, 0, 0 );
1336 Debug( LDAP_DEBUG_ANY,
1337 "syncrepl_entry : be_modify failed (%d)\n",
1338 rs_modify.sr_err, 0, 0 );
1343 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1344 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1345 syncrepl_add_glue( op, entry );
1350 LDAP_LOG( OPERATION, ERR,
1351 "syncrepl_entry : be_add failed (%d)\n",
1352 rs_add.sr_err, 0, 0 );
1354 Debug( LDAP_DEBUG_ANY,
1355 "syncrepl_entry : be_add failed (%d)\n",
1356 rs_add.sr_err, 0, 0 );
1362 be_entry_release_w( op, entry );
1368 LDAP_LOG( OPERATION, ERR,
1369 "syncrepl_entry : be_search failed (%d)\n",
1370 rs_search.sr_err, 0, 0 );
1372 Debug( LDAP_DEBUG_ANY,
1373 "syncrepl_entry : be_search failed (%d)\n",
1374 rs_search.sr_err, 0, 0 );
1380 case LDAP_SYNC_DELETE :
1381 /* Already deleted */
1387 LDAP_LOG( OPERATION, ERR,
1388 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1390 Debug( LDAP_DEBUG_ANY,
1391 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1399 if ( syncUUID_strrep.bv_val ) {
1400 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1402 if ( si->si_syncUUID_ndn.bv_val ) {
1403 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1408 static struct berval gcbva[] = {
1415 syncrepl_del_nonpresent(
1419 Backend* be = op->o_bd;
1420 slap_callback cb = { NULL };
1421 SlapReply rs_search = {REP_RESULT};
1422 SlapReply rs_delete = {REP_RESULT};
1423 SlapReply rs_modify = {REP_RESULT};
1424 struct nonpresent_entry *np_list, *np_prev;
1427 Modifications *mlnext;
1429 Modifications *modlist = NULL;
1430 Modifications **modtail = &modlist;
1432 AttributeName an[2];
1434 struct berval pdn = BER_BVNULL;
1435 struct berval org_req_dn = BER_BVNULL;
1436 struct berval org_req_ndn = BER_BVNULL;
1437 struct berval org_dn = BER_BVNULL;
1438 struct berval org_ndn = BER_BVNULL;
1439 int org_managedsait;
1441 op->o_req_dn = si->si_base;
1442 op->o_req_ndn = si->si_base;
1444 cb.sc_response = nonpresent_callback;
1447 op->o_callback = &cb;
1448 op->o_tag = LDAP_REQ_SEARCH;
1449 op->ors_scope = si->si_scope;
1450 op->ors_deref = LDAP_DEREF_NEVER;
1451 op->o_time = slap_get_time();
1452 op->ors_tlimit = SLAP_NO_LIMIT;
1453 op->ors_slimit = SLAP_NO_LIMIT;
1455 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1456 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1457 an[0].an_desc = slap_schema.si_ad_entryUUID;
1460 op->ors_attrsonly = 0;
1461 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1462 op->ors_filterstr = si->si_filterstr;
1464 op->o_nocaching = 1;
1465 op->o_managedsait = 0;
1467 if ( limits_check( op, &rs_search ) == 0 ) {
1468 rc = be->be_search( op, &rs_search );
1471 op->o_managedsait = 1;
1472 op->o_nocaching = 0;
1474 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1476 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1477 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1478 while ( np_list != NULL ) {
1479 LDAP_LIST_REMOVE( np_list, npe_link );
1481 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1482 op->o_tag = LDAP_REQ_DELETE;
1483 op->o_callback = &cb;
1484 cb.sc_response = null_callback;
1486 op->o_req_dn = *np_prev->npe_name;
1487 op->o_req_ndn = *np_prev->npe_nname;
1488 rc = op->o_bd->be_delete( op, &rs_delete );
1490 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1491 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1492 mod->sml_op = LDAP_MOD_REPLACE;
1493 mod->sml_desc = slap_schema.si_ad_objectClass;
1494 mod->sml_type = mod->sml_desc->ad_cname;
1495 mod->sml_values = &gcbva[0];
1497 modtail = &mod->sml_next;
1499 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1500 mod->sml_op = LDAP_MOD_REPLACE;
1501 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1502 mod->sml_type = mod->sml_desc->ad_cname;
1503 mod->sml_values = &gcbva[1];
1505 modtail = &mod->sml_next;
1507 op->o_tag = LDAP_REQ_MODIFY;
1508 op->orm_modlist = modlist;
1510 rc = be->be_modify( op, &rs_modify );
1512 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1513 mlnext = ml->sml_next;
1518 org_req_dn = op->o_req_dn;
1519 org_req_ndn = op->o_req_ndn;
1521 org_ndn = op->o_ndn;
1522 org_managedsait = get_manageDSAit( op );
1523 op->o_dn = op->o_bd->be_rootdn;
1524 op->o_ndn = op->o_bd->be_rootndn;
1525 op->o_managedsait = 1;
1527 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1528 op->o_delete_glue_parent ) {
1529 op->o_delete_glue_parent = 0;
1530 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1531 slap_callback cb = { NULL };
1532 cb.sc_response = slap_null_cb;
1533 dnParent( &op->o_req_ndn, &pdn );
1535 op->o_req_ndn = pdn;
1536 op->o_callback = &cb;
1537 /* give it a root privil ? */
1538 op->o_bd->be_delete( op, &rs_delete );
1544 op->o_managedsait = org_managedsait;
1546 op->o_ndn = org_ndn;
1547 op->o_req_dn = org_req_dn;
1548 op->o_req_ndn = org_req_ndn;
1549 op->o_delete_glue_parent = 0;
1551 ber_bvfree( np_prev->npe_name );
1552 ber_bvfree( np_prev->npe_nname );
1553 op->o_req_dn.bv_val = NULL;
1554 op->o_req_ndn.bv_val = NULL;
1567 Backend *be = op->o_bd;
1568 slap_callback cb = { NULL };
1573 struct berval dn = {0, NULL};
1574 struct berval ndn = {0, NULL};
1576 SlapReply rs_add = {REP_RESULT};
1579 op->o_tag = LDAP_REQ_ADD;
1580 op->o_callback = &cb;
1581 cb.sc_response = null_callback;
1582 cb.sc_private = NULL;
1587 /* count RDNs in suffix */
1588 if ( be->be_nsuffix[0].bv_len ) {
1589 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1599 /* Start with BE suffix */
1600 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1601 comma = strrchr(dn.bv_val, ',');
1602 if ( ptr ) *ptr = ',';
1603 if ( comma ) *comma = '\0';
1608 dn.bv_len -= ptr - dn.bv_val;
1611 /* the normalizedDNs are always the same length, no counting
1614 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1615 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1616 ndn.bv_len = be->be_nsuffix[0].bv_len;
1619 while ( ndn.bv_val > e->e_nname.bv_val ) {
1620 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1621 ber_dupbv( &glue->e_name, &dn );
1622 ber_dupbv( &glue->e_nname, &ndn );
1624 a = ch_calloc( 1, sizeof( Attribute ));
1625 a->a_desc = slap_schema.si_ad_objectClass;
1627 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1628 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1629 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1630 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1632 a->a_nvals = a->a_vals;
1634 a->a_next = glue->e_attrs;
1637 a = ch_calloc( 1, sizeof( Attribute ));
1638 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1640 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1641 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1642 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1644 a->a_nvals = a->a_vals;
1646 a->a_next = glue->e_attrs;
1649 op->o_req_dn = glue->e_name;
1650 op->o_req_ndn = glue->e_nname;
1652 rc = be->be_add ( op, &rs_add );
1653 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1654 be_entry_release_w( op, glue );
1656 /* incl. ALREADY EXIST */
1660 /* Move to next child */
1661 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1664 if ( ptr == e->e_name.bv_val ) break;
1666 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1667 for( ptr = ndn.bv_val-2;
1668 ptr > e->e_nname.bv_val && *ptr != ',';
1674 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1677 op->o_req_dn = e->e_name;
1678 op->o_req_ndn = e->e_nname;
1680 rc = be->be_add ( op, &rs_add );
1681 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1682 be_entry_release_w( op, e );
1690 static struct berval ocbva[] = {
1692 BER_BVC("subentry"),
1693 BER_BVC("syncConsumerSubentry"),
1697 static struct berval cnbva[] = {
1702 static struct berval ssbva[] = {
1707 static struct berval scbva[] = {
1713 syncrepl_updateCookie(
1717 struct sync_cookie *syncCookie )
1719 Backend *be = op->o_bd;
1721 Modifications *mlnext;
1723 Modifications *modlist = NULL;
1724 Modifications **modtail = &modlist;
1727 char txtbuf[SLAP_TEXT_BUFLEN];
1728 size_t textlen = sizeof txtbuf;
1733 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1734 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1735 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1737 slap_callback cb = { NULL };
1738 SlapReply rs_add = {REP_RESULT};
1739 SlapReply rs_modify = {REP_RESULT};
1741 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1742 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1744 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1745 mod->sml_op = LDAP_MOD_REPLACE;
1746 mod->sml_desc = slap_schema.si_ad_objectClass;
1747 mod->sml_type = mod->sml_desc->ad_cname;
1748 mod->sml_values = ocbva;
1750 modtail = &mod->sml_next;
1752 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1753 assert( si->si_rid < 1000 );
1754 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1755 slap_syncrepl_bvc.bv_len + 1,
1756 "syncrepl%ld", si->si_rid );
1757 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1758 mod->sml_op = LDAP_MOD_REPLACE;
1759 mod->sml_desc = slap_schema.si_ad_cn;
1760 mod->sml_type = mod->sml_desc->ad_cname;
1761 mod->sml_values = cnbva;
1763 modtail = &mod->sml_next;
1765 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1766 mod->sml_op = LDAP_MOD_REPLACE;
1767 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1768 mod->sml_type = mod->sml_desc->ad_cname;
1769 mod->sml_values = ssbva;
1771 modtail = &mod->sml_next;
1773 /* Keep this last, so we can avoid touching the previous
1774 * attributes unnecessarily.
1776 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1777 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1778 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1779 mod->sml_op = LDAP_MOD_REPLACE;
1780 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1781 mod->sml_type = mod->sml_desc->ad_cname;
1782 mod->sml_values = scbva;
1784 modtail = &mod->sml_next;
1788 op->o_tag = LDAP_REQ_ADD;
1789 rc = slap_mods_opattrs( op, modlist, modtail,
1790 &text, txtbuf, textlen );
1792 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1793 ml->sml_op = LDAP_MOD_REPLACE;
1796 if( rc != LDAP_SUCCESS ) {
1798 LDAP_LOG( OPERATION, ERR,
1799 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1801 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1806 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1808 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1809 assert( si->si_rid < 1000 );
1810 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1811 slap_syncrepl_cn_bvc.bv_len + 1,
1812 "cn=syncrepl%ld", si->si_rid );
1814 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1816 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1817 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1819 if ( slap_syncrepl_dn_bv.bv_val ) {
1820 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1825 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1827 if( rc != LDAP_SUCCESS ) {
1829 LDAP_LOG( OPERATION, ERR,
1830 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1832 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1837 cb.sc_response = null_callback;
1840 op->o_callback = &cb;
1841 op->o_req_dn = e->e_name;
1842 op->o_req_ndn = e->e_nname;
1844 /* update persistent cookie */
1845 update_cookie_retry:
1846 op->o_tag = LDAP_REQ_MODIFY;
1847 /* Just modify the cookie value, not the entire entry */
1848 op->orm_modlist = mod;
1849 rc = be->be_modify( op, &rs_modify );
1851 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1852 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1853 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1854 op->o_tag = LDAP_REQ_ADD;
1856 rc = be->be_add( op, &rs_add );
1857 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1858 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1859 goto update_cookie_retry;
1860 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1861 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1863 LDAP_LOG( OPERATION, ERR,
1864 "cookie will be non-persistent\n",
1867 Debug( LDAP_DEBUG_ANY,
1868 "cookie will be non-persistent\n",
1873 LDAP_LOG( OPERATION, ERR,
1874 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1876 Debug( LDAP_DEBUG_ANY,
1877 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1881 be_entry_release_w( op, e );
1886 LDAP_LOG( OPERATION, ERR,
1887 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1889 Debug( LDAP_DEBUG_ANY,
1890 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1901 if ( cnbva[0].bv_val ) {
1902 ch_free( cnbva[0].bv_val );
1903 cnbva[0].bv_val = NULL;
1905 if ( scbva[0].bv_val ) {
1906 ch_free( scbva[0].bv_val );
1907 scbva[0].bv_val = NULL;
1910 if ( mlnext->sml_next ) {
1911 slap_mods_free( mlnext->sml_next );
1912 mlnext->sml_next = NULL;
1915 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1916 mlnext = ml->sml_next;
1924 syncrepl_isupdate( Operation *op )
1926 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1930 syncrepl_isupdate_dn(
1932 struct berval* ndn )
1937 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1938 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1939 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1952 syncinfo_t *si = op->o_callback->sc_private;
1954 if ( rs->sr_type == REP_SEARCH ) {
1955 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1957 LDAP_LOG( OPERATION, ERR,
1958 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1960 Debug( LDAP_DEBUG_ANY,
1961 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1964 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1966 } else if ( rs->sr_type == REP_RESULT ) {
1967 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1969 LDAP_LOG( OPERATION, ERR,
1970 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1972 Debug( LDAP_DEBUG_ANY,
1973 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1978 return LDAP_SUCCESS;
1982 nonpresent_callback(
1986 syncinfo_t *si = op->o_callback->sc_private;
1989 struct berval* present_uuid = NULL;
1990 struct nonpresent_entry *np_entry;
1992 if ( rs->sr_type == REP_RESULT ) {
1993 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1994 si->si_presentlist = NULL;
1996 } else if ( rs->sr_type == REP_SEARCH ) {
1997 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1999 if ( a == NULL ) return 0;
2001 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2004 if ( present_uuid == NULL ) {
2005 np_entry = (struct nonpresent_entry *)
2006 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2007 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2008 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2009 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2012 avl_delete( &si->si_presentlist,
2013 &a->a_nvals[0], syncuuid_cmp );
2014 ch_free( present_uuid->bv_val );
2015 ch_free( present_uuid );
2018 return LDAP_SUCCESS;
2026 if ( rs->sr_err != LDAP_SUCCESS &&
2027 rs->sr_err != LDAP_REFERRAL &&
2028 rs->sr_err != LDAP_ALREADY_EXISTS &&
2029 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2030 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2033 LDAP_LOG( OPERATION, ERR,
2034 "null_callback : error code 0x%x\n",
2037 Debug( LDAP_DEBUG_ANY,
2038 "null_callback : error code 0x%x\n",
2042 return LDAP_SUCCESS;
2046 slap_create_syncrepl_entry(
2048 struct berval *context_csn,
2056 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2058 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2060 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2063 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2065 if ( context_csn ) {
2066 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2067 context_csn, NULL );
2071 bv.bv_len = sizeof("{}")-1;
2072 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2074 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2075 ber_dupbv( &e->e_nname, &e->e_name );
2081 slap_uuidstr_from_normalized(
2082 struct berval* uuidstr,
2083 struct berval* normalized,
2087 unsigned char nibble;
2090 if ( normalized == NULL ) return NULL;
2091 if ( normalized->bv_len != 16 ) return NULL;
2096 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2101 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2102 if ( !uuidstr ) slap_sl_free( new, ctx );
2106 for ( i = 0; i < 16; i++ ) {
2107 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2108 new->bv_val[(i<<1)+d] = '-';
2112 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2113 if ( nibble < 10 ) {
2114 new->bv_val[(i<<1)+d] = nibble + '0';
2116 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2119 nibble = (normalized->bv_val[i]) & 0xF;
2120 if ( nibble < 10 ) {
2121 new->bv_val[(i<<1)+d+1] = nibble + '0';
2123 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2127 new->bv_val[new->bv_len] = '\0';
2132 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2134 const struct berval *uuid1 = v_uuid1;
2135 const struct berval *uuid2 = v_uuid2;
2136 int rc = uuid1->bv_len - uuid2->bv_len;
2137 if ( rc ) return rc;
2138 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2142 avl_ber_bvfree( void *bv )
2144 if( bv == NULL ) return;
2145 if ( ((struct berval *)bv)->bv_val != NULL ) {
2146 ch_free( ((struct berval *)bv)->bv_val );
2148 ch_free( (char *) bv );