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( "+" );
120 BerElementBuffer berbuf;
121 BerElement *ber = (BerElement *)&berbuf;
122 LDAPControl c[2], *ctrls[3];
123 struct timeval timeout;
127 /* setup LDAP SYNC control */
128 ber_init2( ber, NULL, LBER_USE_DER );
129 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
131 if ( si->si_syncCookie.octet_str &&
132 si->si_syncCookie.octet_str[0].bv_val )
134 ber_printf( ber, "{eO}",
136 &si->si_syncCookie.octet_str[0] );
138 ber_printf( ber, "{e}",
142 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
147 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
148 c[0].ldctl_iscritical = si->si_type < 0;
151 if ( si->si_authzId ) {
152 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
153 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
154 c[1].ldctl_iscritical = 1;
161 timeout.tv_sec = si->si_tlimit;
164 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
165 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
166 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
167 si->si_slimit, &msgid );
178 int cmdline_cookie_found = 0;
180 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
181 struct berval syncrepl_cn_bv;
182 struct sync_cookie *sc = NULL;
183 struct sync_cookie syncCookie = { NULL, -1, NULL };
189 psub = &si->si_be->be_nsuffix[0];
191 /* Init connection to master */
192 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
193 if ( rc != LDAP_SUCCESS ) {
195 LDAP_LOG( OPERATION, ERR,
196 "do_syncrep1: ldap_initialize failed (%s)\n",
197 si->si_provideruri, 0, 0 );
199 Debug( LDAP_DEBUG_ANY,
200 "do_syncrep1: ldap_initialize failed (%s)\n",
201 si->si_provideruri, 0, 0 );
206 op->o_protocol = LDAP_VERSION3;
207 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
212 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
213 if( rc != LDAP_SUCCESS ) {
215 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
216 "%s: ldap_start_tls failed (%d)\n",
217 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
220 Debug( LDAP_DEBUG_ANY,
221 "%s: ldap_start_tls failed (%d)\n",
222 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
225 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
229 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
230 #ifdef HAVE_CYRUS_SASL
233 if ( si->si_secprops != NULL ) {
234 rc = ldap_set_option( si->si_ld,
235 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
237 if( rc != LDAP_OPT_SUCCESS ) {
239 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
240 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
241 si->si_provideruri, si->si_secprops, 0 );
243 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
244 "(%s,SECPROPS,\"%s\") failed!\n",
245 si->si_provideruri, si->si_secprops, 0 );
251 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
252 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
254 rc = ldap_sasl_interactive_bind_s( si->si_ld,
262 lutil_sasl_freedefs( defaults );
264 /* FIXME: different error behaviors according to
266 * 2) on err policy : exit, retry, backoff ...
268 if ( rc != LDAP_SUCCESS ) {
270 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
271 "ldap_sasl_interactive_bind_s failed (%d)\n",
274 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
275 "ldap_sasl_interactive_bind_s failed (%d)\n",
279 /* FIXME (see above comment) */
280 /* if Kerberos credentials cache is not active, retry */
281 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
282 rc == LDAP_LOCAL_ERROR )
284 rc = LDAP_SERVER_DOWN;
289 #else /* HAVE_CYRUS_SASL */
290 /* Should never get here, we trapped this at config time */
292 fprintf( stderr, "not compiled with SASL support\n" );
298 rc = ldap_bind_s( si->si_ld,
299 si->si_binddn, si->si_passwd, si->si_bindmethod );
300 if ( rc != LDAP_SUCCESS ) {
302 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
303 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
305 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
306 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
312 /* Set SSF to strongest of TLS, SASL SSFs */
315 op->o_transport_ssf = 0;
317 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
318 == LDAP_SUCCESS && ssl != NULL )
320 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
322 #endif /* HAVE_TLS */
323 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
324 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
325 ? op->o_sasl_ssf : op->o_tls_ssf;
327 /* get syncrepl cookie of shadow replica from subentry */
328 assert( si->si_rid < 1000 );
329 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
330 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
331 CN_STR "syncrepl%ld", si->si_rid );
332 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
333 op->o_req_dn = op->o_req_ndn;
335 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
336 if ( si->si_rid == sc->rid ) {
337 cmdline_cookie_found = 1;
342 if ( cmdline_cookie_found ) {
343 /* cookie is supplied in the command line */
344 BerVarray cookie = NULL;
345 struct berval cookie_bv;
347 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
348 slap_sync_cookie_free( &si->si_syncCookie, 0 );
350 /* read stored cookie if it exists */
351 backend_attribute( op, NULL, &op->o_req_ndn,
352 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
355 /* no stored cookie */
356 if ( sc->ctxcsn == NULL ||
357 sc->ctxcsn->bv_val == NULL ) {
358 /* if cmdline cookie does not have ctxcsn */
359 /* component, set it to an initial value */
360 slap_init_sync_cookie_ctxcsn( sc );
362 slap_dup_sync_cookie( &si->si_syncCookie, sc );
363 slap_sync_cookie_free( sc, 1 );
368 struct berval newcookie = BER_BVNULL;
369 ber_dupbv( &cookie_bv, &cookie[0] );
370 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
371 slap_parse_sync_cookie( &si->si_syncCookie );
372 ber_bvarray_free( si->si_syncCookie.octet_str );
373 si->si_syncCookie.octet_str = NULL;
374 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
375 if ( sc->sid != -1 ) {
376 /* command line cookie wins */
377 si->si_syncCookie.sid = sc->sid;
379 if ( sc->ctxcsn != NULL ) {
380 /* command line cookie wins */
381 if ( si->si_syncCookie.ctxcsn ) {
382 ber_bvarray_free( si->si_syncCookie.ctxcsn );
383 si->si_syncCookie.ctxcsn = NULL;
385 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
386 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
388 if ( sc->rid != -1 ) {
389 /* command line cookie wins */
390 si->si_syncCookie.rid = sc->rid;
392 slap_sync_cookie_free( sc, 1 );
394 slap_compose_sync_cookie( NULL, &newcookie,
395 &si->si_syncCookie.ctxcsn[0],
396 si->si_syncCookie.sid, si->si_syncCookie.rid );
397 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
401 /* no command line cookie is specified */
402 if ( si->si_syncCookie.octet_str == NULL ) {
403 BerVarray cookie = NULL;
404 struct berval cookie_bv;
405 /* try to read stored cookie */
406 backend_attribute( op, NULL, &op->o_req_ndn,
407 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
409 ber_dupbv( &cookie_bv, &cookie[0] );
410 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
411 slap_parse_sync_cookie( &si->si_syncCookie );
412 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
417 rc = ldap_sync_search( si, op->o_tmpmemctx );
419 if( rc != LDAP_SUCCESS ) {
421 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
422 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
424 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
425 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
432 ldap_unbind( si->si_ld );
445 LDAPControl **rctrls = NULL;
448 BerElementBuffer berbuf;
449 BerElement *ber = (BerElement *)&berbuf;
451 LDAPMessage *res = NULL;
452 LDAPMessage *msg = NULL;
455 struct berval *retdata = NULL;
460 struct berval syncUUID = BER_BVNULL;
461 struct sync_cookie syncCookie = { NULL, -1, NULL };
462 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
463 struct berval cookie = BER_BVNULL;
471 Modifications *modlist = NULL;
476 struct timeval *tout_p = NULL;
477 struct timeval tout = { 0, 0 };
479 int refreshDeletes = 0;
481 BerVarray syncUUIDs = NULL;
484 if ( slapd_shutdown ) {
489 ber_init2( ber, NULL, LBER_USE_DER );
490 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
493 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
495 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
498 psub = &si->si_be->be_nsuffix[0];
500 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
502 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
508 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
509 tout_p, &res )) > 0 )
511 if ( slapd_shutdown ) {
515 for( msg = ldap_first_message( si->si_ld, res );
517 msg = ldap_next_message( si->si_ld, msg ) )
519 switch( ldap_msgtype( msg ) ) {
520 case LDAP_RES_SEARCH_ENTRY:
521 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
522 /* we can't work without the control */
528 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
529 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
530 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
531 ber_scanf( ber, /*"{"*/ "m}", &cookie );
532 if ( cookie.bv_val ) {
533 struct berval tmp_bv;
534 ber_dupbv( &tmp_bv, &cookie );
535 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
537 if ( syncCookie.octet_str &&
538 syncCookie.octet_str[0].bv_val )
539 slap_parse_sync_cookie( &syncCookie );
541 if ( syncrepl_message_to_entry( si, op, msg,
542 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
543 rc_efree = syncrepl_entry( si, op, entry, modlist,
544 syncstate, &syncUUID, &syncCookie_req );
545 if ( syncCookie.octet_str &&
546 syncCookie.octet_str[0].bv_val )
548 syncrepl_updateCookie( si, op, psub, &syncCookie );
551 ldap_controls_free( rctrls );
553 slap_mods_free( modlist );
555 if ( rc_efree && entry ) {
561 case LDAP_RES_SEARCH_REFERENCE:
563 LDAP_LOG( OPERATION, ERR,
564 "do_syncrep2 : reference received\n", 0, 0, 0 );
566 Debug( LDAP_DEBUG_ANY,
567 "do_syncrep2 : reference received\n", 0, 0, 0 );
571 case LDAP_RES_SEARCH_RESULT:
572 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
576 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
578 ber_scanf( ber, "{" /*"}"*/);
579 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
580 ber_scanf( ber, "m", &cookie );
581 if ( cookie.bv_val ) {
582 struct berval tmp_bv;
583 ber_dupbv( &tmp_bv, &cookie );
584 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
586 if ( syncCookie.octet_str &&
587 syncCookie.octet_str[0].bv_val )
589 slap_parse_sync_cookie( &syncCookie );
592 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
594 ber_scanf( ber, "b", &refreshDeletes );
596 ber_scanf( ber, /*"{"*/ "}" );
598 if ( syncCookie_req.ctxcsn == NULL ) {
600 } else if ( syncCookie.ctxcsn == NULL ) {
603 value_match( &match, slap_schema.si_ad_entryCSN,
604 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
605 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
606 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
609 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
610 match < 0 && err == LDAP_SUCCESS )
612 syncrepl_updateCookie( si, op, psub, &syncCookie );
615 ldap_controls_free( rctrls );
617 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
618 /* FIXME : different error behaviors according to
619 * 1) err code : LDAP_BUSY ...
620 * 2) on err policy : stop service, stop sync, retry
622 if ( refreshDeletes == 0 && match < 0 &&
623 err == LDAP_SUCCESS )
625 syncrepl_del_nonpresent( op, si );
627 avl_free( si->si_presentlist, avl_ber_bvfree );
628 si->si_presentlist = NULL;
635 case LDAP_RES_INTERMEDIATE:
636 rc = ldap_parse_intermediate( si->si_ld, msg,
637 &retoid, &retdata, NULL, 0 );
638 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
639 int si_refreshDelete = 0;
640 int si_refreshPresent = 0;
641 ber_init2( ber, retdata, LBER_USE_DER );
643 switch ( si_tag = ber_peek_tag( ber, &len )) {
645 case LDAP_TAG_SYNC_NEW_COOKIE:
646 ber_scanf( ber, "tm", &tag, &cookie );
648 case LDAP_TAG_SYNC_REFRESH_DELETE:
649 si_refreshDelete = 1;
650 case LDAP_TAG_SYNC_REFRESH_PRESENT:
651 si_refreshPresent = 1;
652 ber_scanf( ber, "t{" /*"}"*/, &tag );
653 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
655 ber_scanf( ber, "m", &cookie );
656 if ( cookie.bv_val ) {
657 struct berval tmp_bv;
658 ber_dupbv( &tmp_bv, &cookie );
659 ber_bvarray_add( &syncCookie.octet_str,
662 if ( syncCookie.octet_str &&
663 syncCookie.octet_str[0].bv_val )
665 slap_parse_sync_cookie( &syncCookie );
668 if ( ber_peek_tag( ber, &len ) ==
669 LDAP_TAG_REFRESHDONE )
671 ber_scanf( ber, "b", &refreshDone );
673 ber_scanf( ber, /*"{"*/ "}" );
675 case LDAP_TAG_SYNC_ID_SET:
676 ber_scanf( ber, "t{" /*"}"*/, &tag );
677 if ( ber_peek_tag( ber, &len ) ==
678 LDAP_TAG_SYNC_COOKIE )
680 ber_scanf( ber, "m", &cookie );
681 if ( cookie.bv_val ) {
682 struct berval tmp_bv;
683 ber_dupbv( &tmp_bv, &cookie );
684 ber_bvarray_add( &syncCookie.octet_str,
687 if ( syncCookie.octet_str &&
688 syncCookie.octet_str[0].bv_val )
690 slap_parse_sync_cookie( &syncCookie );
693 if ( ber_peek_tag( ber, &len ) ==
694 LDAP_TAG_REFRESHDELETES )
696 ber_scanf( ber, "b", &refreshDeletes );
698 ber_scanf( ber, "[W]", &syncUUIDs );
699 ber_scanf( ber, /*"{"*/ "}" );
700 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
701 struct berval *syncuuid_bv;
702 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
703 avl_insert( &si->si_presentlist,
704 (caddr_t) syncuuid_bv,
705 syncuuid_cmp, avl_dup_error );
707 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
711 LDAP_LOG( OPERATION, ERR,
712 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
713 (long) si_tag, 0, 0 );
715 Debug( LDAP_DEBUG_ANY,
716 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
717 (long) si_tag, 0, 0 );
719 ldap_memfree( retoid );
720 ber_bvfree( retdata );
724 if ( syncCookie_req.ctxcsn == NULL ) {
726 } else if ( syncCookie.ctxcsn == NULL ) {
729 value_match( &match, slap_schema.si_ad_entryCSN,
730 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
731 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
732 &syncCookie_req.ctxcsn[0],
733 &syncCookie.ctxcsn[0], &text );
736 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
739 syncrepl_updateCookie( si, op, psub, &syncCookie);
742 if ( si_refreshPresent == 1 ) {
744 syncrepl_del_nonpresent( op, si );
748 ldap_memfree( retoid );
749 ber_bvfree( retdata );
754 LDAP_LOG( OPERATION, ERR, "do_syncrep2 :"
755 " unknown intermediate "
756 "response\n", 0, 0, 0 );
758 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
759 "unknown intermediate response (%d)\n",
762 ldap_memfree( retoid );
763 ber_bvfree( retdata );
770 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
771 "unknown message\n", 0, 0, 0 );
773 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
774 "unknown message\n", 0, 0, 0 );
779 if ( syncCookie.octet_str ) {
780 slap_sync_cookie_free( &syncCookie_req, 0 );
781 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
782 slap_sync_cookie_free( &syncCookie, 0 );
792 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
793 errstr = ldap_err2string( rc );
796 LDAP_LOG( OPERATION, ERR,
797 "do_syncrep2 : %s\n", errstr, 0, 0 );
799 Debug( LDAP_DEBUG_ANY,
800 "do_syncrep2 : %s\n", errstr, 0, 0 );
805 slap_sync_cookie_free( &syncCookie, 0 );
806 slap_sync_cookie_free( &syncCookie_req, 0 );
808 if ( res ) ldap_msgfree( res );
810 if ( rc && si->si_ld ) {
811 ldap_unbind( si->si_ld );
823 struct re_s* rtask = arg;
824 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
825 Connection conn = {0};
827 int rc = LDAP_SUCCESS;
834 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
836 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
842 switch( abs( si->si_type )) {
843 case LDAP_SYNC_REFRESH_ONLY:
844 case LDAP_SYNC_REFRESH_AND_PERSIST:
850 if ( slapd_shutdown && si->si_ld ) {
851 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
852 connection_client_stop( s );
853 ldap_unbind( si->si_ld );
858 connection_fake_init( &conn, &op, ctx );
860 /* use global malloc for now */
861 op.o_tmpmemctx = NULL;
862 op.o_tmpmfuncs = &ch_mfuncs;
864 op.o_dn = si->si_updatedn;
865 op.o_ndn = si->si_updatedn;
866 op.o_managedsait = 1;
869 op.o_sync_state.ctxcsn = NULL;
870 op.o_sync_state.sid = -1;
871 op.o_sync_state.octet_str = NULL;
872 op.o_sync_slog_size = -1;
873 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
874 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
876 /* Establish session, do search */
879 rc = do_syncrep1( &op, si );
882 /* Process results */
883 if ( rc == LDAP_SUCCESS ) {
884 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
886 rc = do_syncrep2( &op, si );
888 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
889 /* If we succeeded, enable the connection for further listening.
890 * If we failed, tear down the connection and reschedule.
892 if ( rc == LDAP_SUCCESS ) {
894 rc = connection_client_setup( s, do_syncrepl,
897 connection_client_enable( s );
899 } else if ( !first ) {
903 if ( rc == -2 ) rc = 0;
907 /* At this point, we have 4 cases:
908 * 1) for any hard failure, give up and remove this task
909 * 2) for ServerDown, reschedule this task to run
910 * 3) for Refresh and Success, reschedule to run
911 * 4) for Persist and Success, reschedule to defer
913 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
915 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
916 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
920 connection_client_stop( s );
923 if ( rc == LDAP_SUCCESS ) {
924 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
927 rtask->interval.tv_sec = si->si_interval;
928 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
929 if ( si->si_retrynum ) {
930 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
931 si->si_retrynum[i] = si->si_retrynum_init[i];
933 si->si_retrynum[i] = -2;
936 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
937 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
941 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
942 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
943 } else if ( si->si_retrynum[i] >= -1 ) {
944 if ( si->si_retrynum[i] > 0 )
945 si->si_retrynum[i]--;
946 rtask->interval.tv_sec = si->si_retryinterval[i];
947 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
948 slap_wake_listener();
952 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
958 syncrepl_message_to_entry(
962 Modifications **modlist,
968 BerElement *ber = NULL;
971 Modifications **modtail = modlist;
974 char txtbuf[SLAP_TEXT_BUFLEN];
975 size_t textlen = sizeof txtbuf;
977 struct berval bdn = {0, NULL}, dn, ndn;
982 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
984 LDAP_LOG( OPERATION, ERR,
985 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
987 Debug( LDAP_DEBUG_ANY,
988 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
993 op->o_tag = LDAP_REQ_ADD;
995 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
997 if ( rc != LDAP_SUCCESS ) {
999 LDAP_LOG( OPERATION, ERR,
1000 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1002 Debug( LDAP_DEBUG_ANY,
1003 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1008 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1009 ber_dupbv( &op->o_req_dn, &dn );
1010 ber_dupbv( &op->o_req_ndn, &ndn );
1011 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1012 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1014 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1017 return LDAP_SUCCESS;
1020 if ( entry == NULL ) {
1024 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1026 e->e_name = op->o_req_dn;
1027 e->e_nname = op->o_req_ndn;
1029 while ( ber_remaining( ber ) ) {
1030 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1031 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1036 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1038 mod->sml_op = LDAP_MOD_REPLACE;
1039 mod->sml_next = NULL;
1040 mod->sml_desc = NULL;
1041 mod->sml_type = tmp.sml_type;
1042 mod->sml_values = tmp.sml_values;
1043 mod->sml_nvalues = NULL;
1046 modtail = &mod->sml_next;
1049 if ( *modlist == NULL ) {
1051 LDAP_LOG( OPERATION, ERR,
1052 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1054 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1061 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1063 if ( rc != LDAP_SUCCESS ) {
1065 LDAP_LOG( OPERATION, ERR,
1066 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1068 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1074 /* Strip out dynamically generated attrs */
1075 for ( modtail = modlist; *modtail ; ) {
1077 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1078 *modtail = mod->sml_next;
1079 slap_mod_free( &mod->sml_mod, 0 );
1082 modtail = &mod->sml_next;
1086 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1087 if( rc != LDAP_SUCCESS ) {
1089 LDAP_LOG( OPERATION, ERR,
1090 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1092 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1098 ber_free ( ber, 0 );
1099 if ( rc != LDAP_SUCCESS ) {
1114 Modifications* modlist,
1116 struct berval* syncUUID,
1117 struct sync_cookie* syncCookie_req )
1119 Backend *be = op->o_bd;
1120 slap_callback cb = { NULL };
1121 struct berval *syncuuid_bv = NULL;
1122 struct berval syncUUID_strrep = BER_BVNULL;
1123 struct berval uuid_bv = BER_BVNULL;
1125 SlapReply rs_search = {REP_RESULT};
1126 SlapReply rs_delete = {REP_RESULT};
1127 SlapReply rs_add = {REP_RESULT};
1128 SlapReply rs_modify = {REP_RESULT};
1130 AttributeAssertion ava = {0};
1131 int rc = LDAP_SUCCESS;
1132 int ret = LDAP_SUCCESS;
1135 struct berval pdn = BER_BVNULL;
1136 struct berval org_req_dn = BER_BVNULL;
1137 struct berval org_req_ndn = BER_BVNULL;
1138 struct berval org_dn = BER_BVNULL;
1139 struct berval org_ndn = BER_BVNULL;
1140 int org_managedsait;
1142 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1143 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1144 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1145 syncuuid_cmp, avl_dup_error );
1148 if ( syncstate == LDAP_SYNC_PRESENT ) {
1150 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1151 if ( entry == NULL ) {
1156 f.f_choice = LDAP_FILTER_EQUALITY;
1158 ava.aa_desc = slap_schema.si_ad_entryUUID;
1159 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1160 ava.aa_value = *syncUUID;
1161 op->ors_filter = &f;
1163 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1164 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1165 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1166 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1167 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1168 syncUUID->bv_val, syncUUID->bv_len );
1169 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1171 op->o_tag = LDAP_REQ_SEARCH;
1172 op->ors_scope = LDAP_SCOPE_SUBTREE;
1174 /* get syncrepl cookie of shadow replica from subentry */
1175 op->o_req_dn = si->si_base;
1176 op->o_req_ndn = si->si_base;
1178 op->o_time = slap_get_time();
1179 op->ors_tlimit = SLAP_NO_LIMIT;
1182 op->ors_attrs = slap_anlist_no_attrs;
1183 op->ors_attrsonly = 1;
1185 /* set callback function */
1186 op->o_callback = &cb;
1187 cb.sc_response = dn_callback;
1190 si->si_syncUUID_ndn.bv_val = NULL;
1192 if ( limits_check( op, &rs_search ) == 0 ) {
1193 rc = be->be_search( op, &rs_search );
1196 if ( op->ors_filterstr.bv_val ) {
1197 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1200 cb.sc_response = null_callback;
1203 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1206 if ( syncstate != LDAP_SYNC_DELETE ) {
1207 op->o_no_psearch = 1;
1210 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1211 if ( op->o_sync_csn.bv_val ) {
1212 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1217 op->o_req_dn = si->si_syncUUID_ndn;
1218 op->o_req_ndn = si->si_syncUUID_ndn;
1219 op->o_tag = LDAP_REQ_DELETE;
1220 rc = be->be_delete( op, &rs_delete );
1222 org_req_dn = op->o_req_dn;
1223 org_req_ndn = op->o_req_ndn;
1225 org_ndn = op->o_ndn;
1226 org_managedsait = get_manageDSAit( op );
1227 op->o_dn = op->o_bd->be_rootdn;
1228 op->o_ndn = op->o_bd->be_rootndn;
1229 op->o_managedsait = 1;
1231 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1232 op->o_delete_glue_parent = 0;
1233 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1234 slap_callback cb = { NULL };
1235 cb.sc_response = slap_null_cb;
1236 dnParent( &op->o_req_ndn, &pdn );
1238 op->o_req_ndn = pdn;
1239 op->o_callback = &cb;
1240 op->o_bd->be_delete( op, &rs_delete );
1246 op->o_managedsait = org_managedsait;
1248 op->o_ndn = org_ndn;
1249 op->o_req_dn = org_req_dn;
1250 op->o_req_ndn = org_req_ndn;
1251 op->o_delete_glue_parent = 0;
1253 op->o_no_psearch = 0;
1256 switch ( syncstate ) {
1258 case LDAP_SYNC_MODIFY:
1259 if ( rs_search.sr_err == LDAP_SUCCESS ||
1260 rs_search.sr_err == LDAP_REFERRAL ||
1261 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1262 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1264 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1265 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1266 &syncUUID_strrep, syncUUID );
1268 op->o_tag = LDAP_REQ_ADD;
1270 op->o_req_dn = entry->e_name;
1271 op->o_req_ndn = entry->e_nname;
1273 rc = be->be_add( op, &rs_add );
1275 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1276 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1277 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1279 Modifications *modtail = modlist;
1283 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1287 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1288 ber_dupbv( &uuid_bv, syncUUID );
1289 mod->sml_op = LDAP_MOD_REPLACE;
1290 mod->sml_desc = slap_schema.si_ad_entryUUID;
1291 mod->sml_type = mod->sml_desc->ad_cname;
1292 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1293 modtail->sml_next = mod;
1295 op->o_tag = LDAP_REQ_MODIFY;
1296 op->orm_modlist = modlist;
1297 op->o_req_dn = entry->e_name;
1298 op->o_req_ndn = entry->e_nname;
1300 rc = be->be_modify( op, &rs_modify );
1301 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1303 LDAP_LOG( OPERATION, ERR,
1304 "syncrepl_entry : be_modify failed (%d)\n",
1305 rs_modify.sr_err, 0, 0 );
1307 Debug( LDAP_DEBUG_ANY,
1308 "syncrepl_entry : be_modify failed (%d)\n",
1309 rs_modify.sr_err, 0, 0 );
1314 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1315 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1316 syncrepl_add_glue( op, entry );
1321 LDAP_LOG( OPERATION, ERR,
1322 "syncrepl_entry : be_add failed (%d)\n",
1323 rs_add.sr_err, 0, 0 );
1325 Debug( LDAP_DEBUG_ANY,
1326 "syncrepl_entry : be_add failed (%d)\n",
1327 rs_add.sr_err, 0, 0 );
1333 be_entry_release_w( op, entry );
1339 LDAP_LOG( OPERATION, ERR,
1340 "syncrepl_entry : be_search failed (%d)\n",
1341 rs_search.sr_err, 0, 0 );
1343 Debug( LDAP_DEBUG_ANY,
1344 "syncrepl_entry : be_search failed (%d)\n",
1345 rs_search.sr_err, 0, 0 );
1351 case LDAP_SYNC_DELETE :
1352 /* Already deleted */
1358 LDAP_LOG( OPERATION, ERR,
1359 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1361 Debug( LDAP_DEBUG_ANY,
1362 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1370 if ( syncUUID_strrep.bv_val ) {
1371 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1373 if ( si->si_syncUUID_ndn.bv_val ) {
1374 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1379 static struct berval gcbva[] = {
1386 syncrepl_del_nonpresent(
1390 Backend* be = op->o_bd;
1391 slap_callback cb = { NULL };
1392 SlapReply rs_search = {REP_RESULT};
1393 SlapReply rs_delete = {REP_RESULT};
1394 SlapReply rs_modify = {REP_RESULT};
1395 struct nonpresent_entry *np_list, *np_prev;
1398 Modifications *mlnext;
1400 Modifications *modlist = NULL;
1401 Modifications **modtail = &modlist;
1403 AttributeName an[2];
1405 struct berval pdn = BER_BVNULL;
1406 struct berval org_req_dn = BER_BVNULL;
1407 struct berval org_req_ndn = BER_BVNULL;
1408 struct berval org_dn = BER_BVNULL;
1409 struct berval org_ndn = BER_BVNULL;
1410 int org_managedsait;
1412 op->o_req_dn = si->si_base;
1413 op->o_req_ndn = si->si_base;
1415 cb.sc_response = nonpresent_callback;
1418 op->o_callback = &cb;
1419 op->o_tag = LDAP_REQ_SEARCH;
1420 op->ors_scope = si->si_scope;
1421 op->ors_deref = LDAP_DEREF_NEVER;
1422 op->o_time = slap_get_time();
1423 op->ors_tlimit = SLAP_NO_LIMIT;
1424 op->ors_slimit = SLAP_NO_LIMIT;
1426 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1427 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1428 an[0].an_desc = slap_schema.si_ad_entryUUID;
1431 op->ors_attrsonly = 0;
1432 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1433 op->ors_filterstr = si->si_filterstr;
1435 op->o_nocaching = 1;
1436 op->o_managedsait = 0;
1438 if ( limits_check( op, &rs_search ) == 0 ) {
1439 rc = be->be_search( op, &rs_search );
1442 op->o_managedsait = 1;
1443 op->o_nocaching = 0;
1445 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1447 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1448 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1449 while ( np_list != NULL ) {
1450 LDAP_LIST_REMOVE( np_list, npe_link );
1452 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1453 op->o_tag = LDAP_REQ_DELETE;
1454 op->o_callback = &cb;
1455 cb.sc_response = null_callback;
1457 op->o_req_dn = *np_prev->npe_name;
1458 op->o_req_ndn = *np_prev->npe_nname;
1459 rc = op->o_bd->be_delete( op, &rs_delete );
1461 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1462 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1463 mod->sml_op = LDAP_MOD_REPLACE;
1464 mod->sml_desc = slap_schema.si_ad_objectClass;
1465 mod->sml_type = mod->sml_desc->ad_cname;
1466 mod->sml_values = &gcbva[0];
1468 modtail = &mod->sml_next;
1470 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1471 mod->sml_op = LDAP_MOD_REPLACE;
1472 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1473 mod->sml_type = mod->sml_desc->ad_cname;
1474 mod->sml_values = &gcbva[1];
1476 modtail = &mod->sml_next;
1478 op->o_tag = LDAP_REQ_MODIFY;
1479 op->orm_modlist = modlist;
1481 rc = be->be_modify( op, &rs_modify );
1483 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1484 mlnext = ml->sml_next;
1489 org_req_dn = op->o_req_dn;
1490 org_req_ndn = op->o_req_ndn;
1492 org_ndn = op->o_ndn;
1493 org_managedsait = get_manageDSAit( op );
1494 op->o_dn = op->o_bd->be_rootdn;
1495 op->o_ndn = op->o_bd->be_rootndn;
1496 op->o_managedsait = 1;
1498 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1499 op->o_delete_glue_parent ) {
1500 op->o_delete_glue_parent = 0;
1501 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1502 slap_callback cb = { NULL };
1503 cb.sc_response = slap_null_cb;
1504 dnParent( &op->o_req_ndn, &pdn );
1506 op->o_req_ndn = pdn;
1507 op->o_callback = &cb;
1508 /* give it a root privil ? */
1509 op->o_bd->be_delete( op, &rs_delete );
1515 op->o_managedsait = org_managedsait;
1517 op->o_ndn = org_ndn;
1518 op->o_req_dn = org_req_dn;
1519 op->o_req_ndn = org_req_ndn;
1520 op->o_delete_glue_parent = 0;
1522 ber_bvfree( np_prev->npe_name );
1523 ber_bvfree( np_prev->npe_nname );
1524 op->o_req_dn.bv_val = NULL;
1525 op->o_req_ndn.bv_val = NULL;
1538 Backend *be = op->o_bd;
1539 slap_callback cb = { NULL };
1544 struct berval dn = {0, NULL};
1545 struct berval ndn = {0, NULL};
1547 SlapReply rs_add = {REP_RESULT};
1550 op->o_tag = LDAP_REQ_ADD;
1551 op->o_callback = &cb;
1552 cb.sc_response = null_callback;
1553 cb.sc_private = NULL;
1558 /* count RDNs in suffix */
1559 if ( be->be_nsuffix[0].bv_len ) {
1560 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1570 /* Start with BE suffix */
1571 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1572 comma = strrchr(dn.bv_val, ',');
1573 if ( ptr ) *ptr = ',';
1574 if ( comma ) *comma = '\0';
1579 dn.bv_len -= ptr - dn.bv_val;
1582 /* the normalizedDNs are always the same length, no counting
1585 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1586 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1587 ndn.bv_len = be->be_nsuffix[0].bv_len;
1590 while ( ndn.bv_val > e->e_nname.bv_val ) {
1591 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1592 ber_dupbv( &glue->e_name, &dn );
1593 ber_dupbv( &glue->e_nname, &ndn );
1595 a = ch_calloc( 1, sizeof( Attribute ));
1596 a->a_desc = slap_schema.si_ad_objectClass;
1598 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1599 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1600 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1601 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1603 a->a_nvals = a->a_vals;
1605 a->a_next = glue->e_attrs;
1608 a = ch_calloc( 1, sizeof( Attribute ));
1609 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1611 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1612 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1613 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1615 a->a_nvals = a->a_vals;
1617 a->a_next = glue->e_attrs;
1620 op->o_req_dn = glue->e_name;
1621 op->o_req_ndn = glue->e_nname;
1623 rc = be->be_add ( op, &rs_add );
1624 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1625 be_entry_release_w( op, glue );
1627 /* incl. ALREADY EXIST */
1631 /* Move to next child */
1632 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1635 if ( ptr == e->e_name.bv_val ) break;
1637 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1638 for( ptr = ndn.bv_val-2;
1639 ptr > e->e_nname.bv_val && *ptr != ',';
1645 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1648 op->o_req_dn = e->e_name;
1649 op->o_req_ndn = e->e_nname;
1651 rc = be->be_add ( op, &rs_add );
1652 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1653 be_entry_release_w( op, e );
1661 static struct berval ocbva[] = {
1663 BER_BVC("subentry"),
1664 BER_BVC("syncConsumerSubentry"),
1668 static struct berval cnbva[] = {
1673 static struct berval ssbva[] = {
1678 static struct berval scbva[] = {
1684 syncrepl_updateCookie(
1688 struct sync_cookie *syncCookie )
1690 Backend *be = op->o_bd;
1692 Modifications *mlnext;
1694 Modifications *modlist = NULL;
1695 Modifications **modtail = &modlist;
1698 char txtbuf[SLAP_TEXT_BUFLEN];
1699 size_t textlen = sizeof txtbuf;
1704 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1705 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1706 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1708 slap_callback cb = { NULL };
1709 SlapReply rs_add = {REP_RESULT};
1710 SlapReply rs_modify = {REP_RESULT};
1712 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1713 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1715 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1716 mod->sml_op = LDAP_MOD_REPLACE;
1717 mod->sml_desc = slap_schema.si_ad_objectClass;
1718 mod->sml_type = mod->sml_desc->ad_cname;
1719 mod->sml_values = ocbva;
1721 modtail = &mod->sml_next;
1723 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1724 assert( si->si_rid < 1000 );
1725 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1726 slap_syncrepl_bvc.bv_len + 1,
1727 "syncrepl%ld", si->si_rid );
1728 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1729 mod->sml_op = LDAP_MOD_REPLACE;
1730 mod->sml_desc = slap_schema.si_ad_cn;
1731 mod->sml_type = mod->sml_desc->ad_cname;
1732 mod->sml_values = cnbva;
1734 modtail = &mod->sml_next;
1736 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1737 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1738 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1739 mod->sml_op = LDAP_MOD_REPLACE;
1740 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1741 mod->sml_type = mod->sml_desc->ad_cname;
1742 mod->sml_values = scbva;
1744 modtail = &mod->sml_next;
1746 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1747 mod->sml_op = LDAP_MOD_REPLACE;
1748 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1749 mod->sml_type = mod->sml_desc->ad_cname;
1750 mod->sml_values = ssbva;
1752 modtail = &mod->sml_next;
1756 op->o_tag = LDAP_REQ_ADD;
1757 rc = slap_mods_opattrs( op, modlist, modtail,
1758 &text, txtbuf, textlen );
1760 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1761 ml->sml_op = LDAP_MOD_REPLACE;
1764 if( rc != LDAP_SUCCESS ) {
1766 LDAP_LOG( OPERATION, ERR,
1767 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1769 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1774 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1776 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1777 assert( si->si_rid < 1000 );
1778 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1779 slap_syncrepl_cn_bvc.bv_len + 1,
1780 "cn=syncrepl%ld", si->si_rid );
1782 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1784 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1785 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1787 if ( slap_syncrepl_dn_bv.bv_val ) {
1788 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1793 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1795 if( rc != LDAP_SUCCESS ) {
1797 LDAP_LOG( OPERATION, ERR,
1798 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1800 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1805 cb.sc_response = null_callback;
1808 op->o_callback = &cb;
1809 op->o_req_dn = e->e_name;
1810 op->o_req_ndn = e->e_nname;
1812 /* update persistent cookie */
1813 update_cookie_retry:
1814 op->o_tag = LDAP_REQ_MODIFY;
1815 op->orm_modlist = modlist;
1816 rc = be->be_modify( op, &rs_modify );
1818 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1819 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1820 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1821 op->o_tag = LDAP_REQ_ADD;
1823 rc = be->be_add( op, &rs_add );
1824 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1825 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1826 goto update_cookie_retry;
1827 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1828 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1830 LDAP_LOG( OPERATION, ERR,
1831 "cookie will be non-persistent\n",
1834 Debug( LDAP_DEBUG_ANY,
1835 "cookie will be non-persistent\n",
1840 LDAP_LOG( OPERATION, ERR,
1841 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1843 Debug( LDAP_DEBUG_ANY,
1844 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1848 be_entry_release_w( op, e );
1853 LDAP_LOG( OPERATION, ERR,
1854 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1856 Debug( LDAP_DEBUG_ANY,
1857 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1868 if ( cnbva[0].bv_val ) {
1869 ch_free( cnbva[0].bv_val );
1870 cnbva[0].bv_val = NULL;
1872 if ( scbva[0].bv_val ) {
1873 ch_free( scbva[0].bv_val );
1874 scbva[0].bv_val = NULL;
1877 if ( mlnext->sml_next ) {
1878 slap_mods_free( mlnext->sml_next );
1879 mlnext->sml_next = NULL;
1882 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1883 mlnext = ml->sml_next;
1891 syncrepl_isupdate( Operation *op )
1893 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1897 syncrepl_isupdate_dn(
1899 struct berval* ndn )
1904 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1905 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1906 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1919 syncinfo_t *si = op->o_callback->sc_private;
1921 if ( rs->sr_type == REP_SEARCH ) {
1922 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1924 LDAP_LOG( OPERATION, ERR,
1925 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1927 Debug( LDAP_DEBUG_ANY,
1928 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1931 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1933 } else if ( rs->sr_type == REP_RESULT ) {
1934 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1936 LDAP_LOG( OPERATION, ERR,
1937 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1939 Debug( LDAP_DEBUG_ANY,
1940 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1945 return LDAP_SUCCESS;
1949 nonpresent_callback(
1953 syncinfo_t *si = op->o_callback->sc_private;
1956 struct berval* present_uuid = NULL;
1957 struct nonpresent_entry *np_entry;
1959 if ( rs->sr_type == REP_RESULT ) {
1960 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1961 si->si_presentlist = NULL;
1963 } else if ( rs->sr_type == REP_SEARCH ) {
1964 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1966 if ( a == NULL ) return 0;
1968 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1971 if ( present_uuid == NULL ) {
1972 np_entry = (struct nonpresent_entry *)
1973 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1974 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1975 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1976 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1979 avl_delete( &si->si_presentlist,
1980 &a->a_nvals[0], syncuuid_cmp );
1981 ch_free( present_uuid->bv_val );
1982 ch_free( present_uuid );
1985 return LDAP_SUCCESS;
1993 if ( rs->sr_err != LDAP_SUCCESS &&
1994 rs->sr_err != LDAP_REFERRAL &&
1995 rs->sr_err != LDAP_ALREADY_EXISTS &&
1996 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1997 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2000 LDAP_LOG( OPERATION, ERR,
2001 "null_callback : error code 0x%x\n",
2004 Debug( LDAP_DEBUG_ANY,
2005 "null_callback : error code 0x%x\n",
2009 return LDAP_SUCCESS;
2013 slap_create_syncrepl_entry(
2015 struct berval *context_csn,
2023 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2025 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2027 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2030 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2032 if ( context_csn ) {
2033 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2034 context_csn, NULL );
2038 bv.bv_len = sizeof("{}")-1;
2039 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2041 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2042 ber_dupbv( &e->e_nname, &e->e_name );
2048 slap_uuidstr_from_normalized(
2049 struct berval* uuidstr,
2050 struct berval* normalized,
2054 unsigned char nibble;
2057 if ( normalized == NULL ) return NULL;
2058 if ( normalized->bv_len != 16 ) return NULL;
2063 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2068 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2069 if ( !uuidstr ) slap_sl_free( new, ctx );
2073 for ( i = 0; i < 16; i++ ) {
2074 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2075 new->bv_val[(i<<1)+d] = '-';
2079 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2080 if ( nibble < 10 ) {
2081 new->bv_val[(i<<1)+d] = nibble + '0';
2083 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2086 nibble = (normalized->bv_val[i]) & 0xF;
2087 if ( nibble < 10 ) {
2088 new->bv_val[(i<<1)+d+1] = nibble + '0';
2090 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2094 new->bv_val[new->bv_len] = '\0';
2099 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2101 const struct berval *uuid1 = v_uuid1;
2102 const struct berval *uuid2 = v_uuid2;
2103 int rc = uuid1->bv_len - uuid2->bv_len;
2104 if ( rc ) return rc;
2105 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2109 avl_ber_bvfree( void *bv )
2111 if( bv == NULL ) return;
2112 if ( ((struct berval *)bv)->bv_val != NULL ) {
2113 ch_free( ((struct berval *)bv)->bv_val );
2115 ch_free( (char *) bv );