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 */
316 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl ) == LDAP_SUCCESS &&
318 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
320 #endif /* HAVE_TLS */
321 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
322 op->o_transport_ssf = op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf ) ?
323 op->o_sasl_ssf : op->o_tls_ssf;
325 /* get syncrepl cookie of shadow replica from subentry */
326 assert( si->si_rid < 1000 );
327 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
328 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
329 CN_STR "syncrepl%ld", si->si_rid );
330 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
331 op->o_req_dn = op->o_req_ndn;
333 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
334 if ( si->si_rid == sc->rid ) {
335 cmdline_cookie_found = 1;
340 if ( cmdline_cookie_found ) {
341 /* cookie is supplied in the command line */
342 BerVarray cookie = NULL;
343 struct berval cookie_bv;
345 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
346 slap_sync_cookie_free( &si->si_syncCookie, 0 );
348 /* read stored cookie if it exists */
349 backend_attribute( op, NULL, &op->o_req_ndn,
350 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
353 /* no stored cookie */
354 if ( sc->ctxcsn == NULL ||
355 sc->ctxcsn->bv_val == NULL ) {
356 /* if cmdline cookie does not have ctxcsn */
357 /* component, set it to an initial value */
358 slap_init_sync_cookie_ctxcsn( sc );
360 slap_dup_sync_cookie( &si->si_syncCookie, sc );
361 slap_sync_cookie_free( sc, 1 );
366 struct berval newcookie = BER_BVNULL;
367 ber_dupbv( &cookie_bv, &cookie[0] );
368 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
369 slap_parse_sync_cookie( &si->si_syncCookie );
370 ber_bvarray_free( si->si_syncCookie.octet_str );
371 si->si_syncCookie.octet_str = NULL;
372 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
373 if ( sc->sid != -1 ) {
374 /* command line cookie wins */
375 si->si_syncCookie.sid = sc->sid;
377 if ( sc->ctxcsn != NULL ) {
378 /* command line cookie wins */
379 if ( si->si_syncCookie.ctxcsn ) {
380 ber_bvarray_free( si->si_syncCookie.ctxcsn );
381 si->si_syncCookie.ctxcsn = NULL;
383 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
384 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
386 if ( sc->rid != -1 ) {
387 /* command line cookie wins */
388 si->si_syncCookie.rid = sc->rid;
390 slap_sync_cookie_free( sc, 1 );
392 slap_compose_sync_cookie( NULL, &newcookie,
393 &si->si_syncCookie.ctxcsn[0],
394 si->si_syncCookie.sid, si->si_syncCookie.rid );
395 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
399 /* no command line cookie is specified */
400 if ( si->si_syncCookie.octet_str == NULL ) {
401 BerVarray cookie = NULL;
402 struct berval cookie_bv;
403 /* try to read stored cookie */
404 backend_attribute( op, NULL, &op->o_req_ndn,
405 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
407 ber_dupbv( &cookie_bv, &cookie[0] );
408 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
409 slap_parse_sync_cookie( &si->si_syncCookie );
410 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
415 rc = ldap_sync_search( si, op->o_tmpmemctx );
417 if( rc != LDAP_SUCCESS ) {
419 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
420 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
422 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
423 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
430 ldap_unbind( si->si_ld );
443 LDAPControl **rctrls = NULL;
446 BerElementBuffer berbuf;
447 BerElement *ber = (BerElement *)&berbuf;
449 LDAPMessage *res = NULL;
450 LDAPMessage *msg = NULL;
453 struct berval *retdata = NULL;
458 struct berval syncUUID = BER_BVNULL;
459 struct sync_cookie syncCookie = { NULL, -1, NULL };
460 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
461 struct berval cookie = BER_BVNULL;
469 Modifications *modlist = NULL;
474 struct timeval *tout_p = NULL;
475 struct timeval tout = { 0, 0 };
477 int refreshDeletes = 0;
479 BerVarray syncUUIDs = NULL;
482 if ( slapd_shutdown ) {
487 ber_init2( ber, NULL, LBER_USE_DER );
488 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
491 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
493 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
496 psub = &si->si_be->be_nsuffix[0];
498 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
500 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
506 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
507 tout_p, &res )) > 0 )
509 if ( slapd_shutdown ) {
513 for( msg = ldap_first_message( si->si_ld, res );
515 msg = ldap_next_message( si->si_ld, msg ) )
517 switch( ldap_msgtype( msg ) ) {
518 case LDAP_RES_SEARCH_ENTRY:
519 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
520 /* we can't work without the control */
526 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
527 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
528 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
529 ber_scanf( ber, /*"{"*/ "m}", &cookie );
530 if ( cookie.bv_val ) {
531 struct berval tmp_bv;
532 ber_dupbv( &tmp_bv, &cookie );
533 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
535 if ( syncCookie.octet_str &&
536 syncCookie.octet_str[0].bv_val )
537 slap_parse_sync_cookie( &syncCookie );
539 if ( syncrepl_message_to_entry( si, op, msg,
540 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
541 rc_efree = syncrepl_entry( si, op, entry, modlist,
542 syncstate, &syncUUID, &syncCookie_req );
543 if ( syncCookie.octet_str &&
544 syncCookie.octet_str[0].bv_val )
546 syncrepl_updateCookie( si, op, psub, &syncCookie );
549 ldap_controls_free( rctrls );
551 slap_mods_free( modlist );
553 if ( rc_efree && entry ) {
559 case LDAP_RES_SEARCH_REFERENCE:
561 LDAP_LOG( OPERATION, ERR,
562 "do_syncrep2 : reference received\n", 0, 0, 0 );
564 Debug( LDAP_DEBUG_ANY,
565 "do_syncrep2 : reference received\n", 0, 0, 0 );
569 case LDAP_RES_SEARCH_RESULT:
570 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
574 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
576 ber_scanf( ber, "{" /*"}"*/);
577 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
578 ber_scanf( ber, "m", &cookie );
579 if ( cookie.bv_val ) {
580 struct berval tmp_bv;
581 ber_dupbv( &tmp_bv, &cookie );
582 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
584 if ( syncCookie.octet_str &&
585 syncCookie.octet_str[0].bv_val )
587 slap_parse_sync_cookie( &syncCookie );
590 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
592 ber_scanf( ber, "b", &refreshDeletes );
594 ber_scanf( ber, /*"{"*/ "}" );
596 if ( syncCookie_req.ctxcsn == NULL ) {
598 } else if ( syncCookie.ctxcsn == NULL ) {
601 value_match( &match, slap_schema.si_ad_entryCSN,
602 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
603 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
604 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
607 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
608 match < 0 && err == LDAP_SUCCESS )
610 syncrepl_updateCookie( si, op, psub, &syncCookie );
613 ldap_controls_free( rctrls );
615 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
616 /* FIXME : different error behaviors according to
617 * 1) err code : LDAP_BUSY ...
618 * 2) on err policy : stop service, stop sync, retry
620 if ( refreshDeletes == 0 && match < 0 &&
621 err == LDAP_SUCCESS )
623 syncrepl_del_nonpresent( op, si );
625 avl_free( si->si_presentlist, avl_ber_bvfree );
626 si->si_presentlist = NULL;
633 case LDAP_RES_INTERMEDIATE:
634 rc = ldap_parse_intermediate( si->si_ld, msg,
635 &retoid, &retdata, NULL, 0 );
636 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
637 int si_refreshDelete = 0;
638 int si_refreshPresent = 0;
639 ber_init2( ber, retdata, LBER_USE_DER );
641 switch ( si_tag = ber_peek_tag( ber, &len )) {
643 case LDAP_TAG_SYNC_NEW_COOKIE:
644 ber_scanf( ber, "tm", &tag, &cookie );
646 case LDAP_TAG_SYNC_REFRESH_DELETE:
647 si_refreshDelete = 1;
648 case LDAP_TAG_SYNC_REFRESH_PRESENT:
649 si_refreshPresent = 1;
650 ber_scanf( ber, "t{" /*"}"*/, &tag );
651 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
653 ber_scanf( ber, "m", &cookie );
654 if ( cookie.bv_val ) {
655 struct berval tmp_bv;
656 ber_dupbv( &tmp_bv, &cookie );
657 ber_bvarray_add( &syncCookie.octet_str,
660 if ( syncCookie.octet_str &&
661 syncCookie.octet_str[0].bv_val )
663 slap_parse_sync_cookie( &syncCookie );
666 if ( ber_peek_tag( ber, &len ) ==
667 LDAP_TAG_REFRESHDONE )
669 ber_scanf( ber, "b", &refreshDone );
671 ber_scanf( ber, /*"{"*/ "}" );
673 case LDAP_TAG_SYNC_ID_SET:
674 ber_scanf( ber, "t{" /*"}"*/, &tag );
675 if ( ber_peek_tag( ber, &len ) ==
676 LDAP_TAG_SYNC_COOKIE )
678 ber_scanf( ber, "m", &cookie );
679 if ( cookie.bv_val ) {
680 struct berval tmp_bv;
681 ber_dupbv( &tmp_bv, &cookie );
682 ber_bvarray_add( &syncCookie.octet_str,
685 if ( syncCookie.octet_str &&
686 syncCookie.octet_str[0].bv_val )
688 slap_parse_sync_cookie( &syncCookie );
691 if ( ber_peek_tag( ber, &len ) ==
692 LDAP_TAG_REFRESHDELETES )
694 ber_scanf( ber, "b", &refreshDeletes );
696 ber_scanf( ber, "[W]", &syncUUIDs );
697 ber_scanf( ber, /*"{"*/ "}" );
698 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
699 struct berval *syncuuid_bv;
700 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
701 avl_insert( &si->si_presentlist,
702 (caddr_t) syncuuid_bv,
703 syncuuid_cmp, avl_dup_error );
705 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
709 LDAP_LOG( OPERATION, ERR,
710 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
711 (long) si_tag, 0, 0 );
713 Debug( LDAP_DEBUG_ANY,
714 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
715 (long) si_tag, 0, 0 );
717 ldap_memfree( retoid );
718 ber_bvfree( retdata );
722 if ( syncCookie_req.ctxcsn == NULL ) {
724 } else if ( syncCookie.ctxcsn == NULL ) {
727 value_match( &match, slap_schema.si_ad_entryCSN,
728 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
729 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
730 &syncCookie_req.ctxcsn[0],
731 &syncCookie.ctxcsn[0], &text );
734 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
737 syncrepl_updateCookie( si, op, psub, &syncCookie);
740 if ( si_refreshPresent == 1 ) {
742 syncrepl_del_nonpresent( op, si );
746 ldap_memfree( retoid );
747 ber_bvfree( retdata );
752 LDAP_LOG( OPERATION, ERR, "do_syncrep2 :"
753 " unknown intermediate "
754 "response\n", 0, 0, 0 );
756 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
757 "unknown intermediate response (%d)\n",
760 ldap_memfree( retoid );
761 ber_bvfree( retdata );
768 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
769 "unknown message\n", 0, 0, 0 );
771 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
772 "unknown message\n", 0, 0, 0 );
777 if ( syncCookie.octet_str ) {
778 slap_sync_cookie_free( &syncCookie_req, 0 );
779 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
780 slap_sync_cookie_free( &syncCookie, 0 );
790 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
791 errstr = ldap_err2string( rc );
794 LDAP_LOG( OPERATION, ERR,
795 "do_syncrep2 : %s\n", errstr, 0, 0 );
797 Debug( LDAP_DEBUG_ANY,
798 "do_syncrep2 : %s\n", errstr, 0, 0 );
803 slap_sync_cookie_free( &syncCookie, 0 );
804 slap_sync_cookie_free( &syncCookie_req, 0 );
806 if ( res ) ldap_msgfree( res );
808 if ( rc && si->si_ld ) {
809 ldap_unbind( si->si_ld );
821 struct re_s* rtask = arg;
822 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
823 Connection conn = {0};
825 int rc = LDAP_SUCCESS;
832 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
834 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
840 switch( abs( si->si_type )) {
841 case LDAP_SYNC_REFRESH_ONLY:
842 case LDAP_SYNC_REFRESH_AND_PERSIST:
848 if ( slapd_shutdown && si->si_ld ) {
849 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
850 connection_client_stop( s );
851 ldap_unbind( si->si_ld );
856 connection_fake_init( &conn, &op, ctx );
858 op.o_dn = si->si_updatedn;
859 op.o_ndn = si->si_updatedn;
860 op.o_managedsait = 1;
863 op.o_sync_state.ctxcsn = NULL;
864 op.o_sync_state.sid = -1;
865 op.o_sync_state.octet_str = NULL;
866 op.o_sync_slog_size = -1;
867 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
868 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
870 /* Establish session, do search */
873 rc = do_syncrep1( &op, si );
876 /* Process results */
877 if ( rc == LDAP_SUCCESS ) {
878 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
880 rc = do_syncrep2( &op, si );
882 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
883 /* If we succeeded, enable the connection for further listening.
884 * If we failed, tear down the connection and reschedule.
886 if ( rc == LDAP_SUCCESS ) {
888 rc = connection_client_setup( s, do_syncrepl,
891 connection_client_enable( s );
893 } else if ( !first ) {
897 if ( rc == -2 ) rc = 0;
901 /* At this point, we have 4 cases:
902 * 1) for any hard failure, give up and remove this task
903 * 2) for ServerDown, reschedule this task to run
904 * 3) for Refresh and Success, reschedule to run
905 * 4) for Persist and Success, reschedule to defer
907 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
909 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
910 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
914 connection_client_stop( s );
917 if ( rc == LDAP_SUCCESS ) {
918 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
921 rtask->interval.tv_sec = si->si_interval;
922 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
923 if ( si->si_retrynum ) {
924 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
925 si->si_retrynum[i] = si->si_retrynum_init[i];
927 si->si_retrynum[i] = -2;
930 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
931 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
935 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
936 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
937 } else if ( si->si_retrynum[i] >= -1 ) {
938 if ( si->si_retrynum[i] > 0 )
939 si->si_retrynum[i]--;
940 rtask->interval.tv_sec = si->si_retryinterval[i];
941 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
942 slap_wake_listener();
946 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
952 syncrepl_message_to_entry(
956 Modifications **modlist,
962 BerElement *ber = NULL;
965 Modifications **modtail = modlist;
968 char txtbuf[SLAP_TEXT_BUFLEN];
969 size_t textlen = sizeof txtbuf;
971 struct berval bdn = {0, NULL}, dn, ndn;
976 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
978 LDAP_LOG( OPERATION, ERR,
979 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
981 Debug( LDAP_DEBUG_ANY,
982 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
987 op->o_tag = LDAP_REQ_ADD;
989 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
991 if ( rc != LDAP_SUCCESS ) {
993 LDAP_LOG( OPERATION, ERR,
994 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
996 Debug( LDAP_DEBUG_ANY,
997 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1002 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1003 ber_dupbv( &op->o_req_dn, &dn );
1004 ber_dupbv( &op->o_req_ndn, &ndn );
1005 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1006 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1008 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1009 return LDAP_SUCCESS;
1012 if ( entry == NULL ) {
1016 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1018 e->e_name = op->o_req_dn;
1019 e->e_nname = op->o_req_ndn;
1021 while ( ber_remaining( ber ) ) {
1022 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1023 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1028 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1030 mod->sml_op = LDAP_MOD_REPLACE;
1031 mod->sml_next = NULL;
1032 mod->sml_desc = NULL;
1033 mod->sml_type = tmp.sml_type;
1034 mod->sml_values = tmp.sml_values;
1035 mod->sml_nvalues = NULL;
1038 modtail = &mod->sml_next;
1041 if ( *modlist == NULL ) {
1043 LDAP_LOG( OPERATION, ERR,
1044 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1046 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1051 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1053 if ( rc != LDAP_SUCCESS ) {
1055 LDAP_LOG( OPERATION, ERR,
1056 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1058 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1064 /* Strip out dynamically generated attrs */
1065 for ( modtail = modlist; *modtail ; ) {
1067 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1068 *modtail = mod->sml_next;
1069 slap_mod_free( &mod->sml_mod, 0 );
1072 modtail = &mod->sml_next;
1076 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1077 if( rc != LDAP_SUCCESS ) {
1079 LDAP_LOG( OPERATION, ERR,
1080 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1082 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1088 ber_free ( ber, 0 );
1089 if ( rc != LDAP_SUCCESS ) {
1102 Modifications* modlist,
1104 struct berval* syncUUID,
1105 struct sync_cookie* syncCookie_req )
1107 Backend *be = op->o_bd;
1108 slap_callback cb = { NULL };
1109 struct berval *syncuuid_bv = NULL;
1110 struct berval syncUUID_strrep = BER_BVNULL;
1111 struct berval uuid_bv = BER_BVNULL;
1113 SlapReply rs_search = {REP_RESULT};
1114 SlapReply rs_delete = {REP_RESULT};
1115 SlapReply rs_add = {REP_RESULT};
1116 SlapReply rs_modify = {REP_RESULT};
1118 AttributeAssertion ava = {0};
1119 int rc = LDAP_SUCCESS;
1120 int ret = LDAP_SUCCESS;
1123 struct berval pdn = BER_BVNULL;
1124 struct berval org_req_dn = BER_BVNULL;
1125 struct berval org_req_ndn = BER_BVNULL;
1126 struct berval org_dn = BER_BVNULL;
1127 struct berval org_ndn = BER_BVNULL;
1128 int org_managedsait;
1130 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1131 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1132 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1133 syncuuid_cmp, avl_dup_error );
1136 if ( syncstate == LDAP_SYNC_PRESENT ) {
1140 f.f_choice = LDAP_FILTER_EQUALITY;
1142 ava.aa_desc = slap_schema.si_ad_entryUUID;
1143 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1144 ava.aa_value = *syncUUID;
1145 op->ors_filter = &f;
1147 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1148 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1149 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1150 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1151 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1152 syncUUID->bv_val, syncUUID->bv_len );
1153 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1155 op->o_tag = LDAP_REQ_SEARCH;
1156 op->ors_scope = LDAP_SCOPE_SUBTREE;
1158 /* get syncrepl cookie of shadow replica from subentry */
1159 op->o_req_dn = si->si_base;
1160 op->o_req_ndn = si->si_base;
1162 op->o_time = slap_get_time();
1163 op->ors_tlimit = SLAP_NO_LIMIT;
1166 op->ors_attrs = slap_anlist_no_attrs;
1167 op->ors_attrsonly = 1;
1169 /* set callback function */
1170 op->o_callback = &cb;
1171 cb.sc_response = dn_callback;
1174 si->si_syncUUID_ndn.bv_val = NULL;
1176 if ( limits_check( op, &rs_search ) == 0 ) {
1177 rc = be->be_search( op, &rs_search );
1180 if ( op->ors_filterstr.bv_val ) {
1181 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1184 cb.sc_response = null_callback;
1187 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1190 if ( syncstate != LDAP_SYNC_DELETE ) {
1191 op->o_no_psearch = 1;
1194 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1195 if ( op->o_sync_csn.bv_val ) {
1196 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1201 op->o_req_dn = si->si_syncUUID_ndn;
1202 op->o_req_ndn = si->si_syncUUID_ndn;
1203 op->o_tag = LDAP_REQ_DELETE;
1204 rc = be->be_delete( op, &rs_delete );
1206 org_req_dn = op->o_req_dn;
1207 org_req_ndn = op->o_req_ndn;
1209 org_ndn = op->o_ndn;
1210 org_managedsait = get_manageDSAit( op );
1211 op->o_dn = op->o_bd->be_rootdn;
1212 op->o_ndn = op->o_bd->be_rootndn;
1213 op->o_managedsait = 1;
1215 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1216 op->o_delete_glue_parent = 0;
1217 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1218 slap_callback cb = { NULL };
1219 cb.sc_response = slap_null_cb;
1220 dnParent( &op->o_req_ndn, &pdn );
1222 op->o_req_ndn = pdn;
1223 op->o_callback = &cb;
1224 op->o_bd->be_delete( op, &rs_delete );
1230 op->o_managedsait = org_managedsait;
1232 op->o_ndn = org_ndn;
1233 op->o_req_dn = org_req_dn;
1234 op->o_req_ndn = org_req_ndn;
1235 op->o_delete_glue_parent = 0;
1237 op->o_no_psearch = 0;
1240 switch ( syncstate ) {
1242 case LDAP_SYNC_MODIFY:
1243 if ( rs_search.sr_err == LDAP_SUCCESS ||
1244 rs_search.sr_err == LDAP_REFERRAL ||
1245 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1246 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1248 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1249 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1250 &syncUUID_strrep, syncUUID );
1252 op->o_tag = LDAP_REQ_ADD;
1254 op->o_req_dn = e->e_name;
1255 op->o_req_ndn = e->e_nname;
1257 rc = be->be_add( op, &rs_add );
1259 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1260 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1261 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1263 Modifications *modtail = modlist;
1267 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1271 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1272 ber_dupbv( &uuid_bv, syncUUID );
1273 mod->sml_op = LDAP_MOD_REPLACE;
1274 mod->sml_desc = slap_schema.si_ad_entryUUID;
1275 mod->sml_type = mod->sml_desc->ad_cname;
1276 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1277 modtail->sml_next = mod;
1279 op->o_tag = LDAP_REQ_MODIFY;
1280 op->orm_modlist = modlist;
1281 op->o_req_dn = e->e_name;
1282 op->o_req_ndn = e->e_nname;
1284 rc = be->be_modify( op, &rs_modify );
1285 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1287 LDAP_LOG( OPERATION, ERR,
1288 "syncrepl_entry : be_modify failed (%d)\n",
1289 rs_modify.sr_err, 0, 0 );
1291 Debug( LDAP_DEBUG_ANY,
1292 "syncrepl_entry : be_modify failed (%d)\n",
1293 rs_modify.sr_err, 0, 0 );
1298 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1299 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1300 syncrepl_add_glue( op, e );
1305 LDAP_LOG( OPERATION, ERR,
1306 "syncrepl_entry : be_add failed (%d)\n",
1307 rs_add.sr_err, 0, 0 );
1309 Debug( LDAP_DEBUG_ANY,
1310 "syncrepl_entry : be_add failed (%d)\n",
1311 rs_add.sr_err, 0, 0 );
1317 be_entry_release_w( op, e );
1323 LDAP_LOG( OPERATION, ERR,
1324 "syncrepl_entry : be_search failed (%d)\n",
1325 rs_search.sr_err, 0, 0 );
1327 Debug( LDAP_DEBUG_ANY,
1328 "syncrepl_entry : be_search failed (%d)\n",
1329 rs_search.sr_err, 0, 0 );
1335 case LDAP_SYNC_DELETE :
1336 /* Already deleted */
1342 LDAP_LOG( OPERATION, ERR,
1343 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1345 Debug( LDAP_DEBUG_ANY,
1346 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1354 if ( syncUUID_strrep.bv_val ) {
1355 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1357 if ( si->si_syncUUID_ndn.bv_val ) {
1358 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1363 static struct berval gcbva[] = {
1370 syncrepl_del_nonpresent(
1374 Backend* be = op->o_bd;
1375 slap_callback cb = { NULL };
1376 SlapReply rs_search = {REP_RESULT};
1377 SlapReply rs_delete = {REP_RESULT};
1378 SlapReply rs_modify = {REP_RESULT};
1379 struct nonpresent_entry *np_list, *np_prev;
1382 Modifications *mlnext;
1384 Modifications *modlist = NULL;
1385 Modifications **modtail = &modlist;
1387 AttributeName an[2];
1389 struct berval pdn = BER_BVNULL;
1390 struct berval org_req_dn = BER_BVNULL;
1391 struct berval org_req_ndn = BER_BVNULL;
1392 struct berval org_dn = BER_BVNULL;
1393 struct berval org_ndn = BER_BVNULL;
1394 int org_managedsait;
1396 op->o_req_dn = si->si_base;
1397 op->o_req_ndn = si->si_base;
1399 cb.sc_response = nonpresent_callback;
1402 op->o_callback = &cb;
1403 op->o_tag = LDAP_REQ_SEARCH;
1404 op->ors_scope = si->si_scope;
1405 op->ors_deref = LDAP_DEREF_NEVER;
1406 op->o_time = slap_get_time();
1407 op->ors_tlimit = SLAP_NO_LIMIT;
1408 op->ors_slimit = SLAP_NO_LIMIT;
1410 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1411 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1412 an[0].an_desc = slap_schema.si_ad_entryUUID;
1415 op->ors_attrsonly = 0;
1416 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1417 op->ors_filterstr = si->si_filterstr;
1419 op->o_nocaching = 1;
1420 op->o_managedsait = 0;
1422 if ( limits_check( op, &rs_search ) == 0 ) {
1423 rc = be->be_search( op, &rs_search );
1426 op->o_managedsait = 1;
1427 op->o_nocaching = 0;
1429 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1431 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1432 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1433 while ( np_list != NULL ) {
1434 LDAP_LIST_REMOVE( np_list, npe_link );
1436 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1437 op->o_tag = LDAP_REQ_DELETE;
1438 op->o_callback = &cb;
1439 cb.sc_response = null_callback;
1441 op->o_req_dn = *np_prev->npe_name;
1442 op->o_req_ndn = *np_prev->npe_nname;
1443 rc = op->o_bd->be_delete( op, &rs_delete );
1445 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1446 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1447 mod->sml_op = LDAP_MOD_REPLACE;
1448 mod->sml_desc = slap_schema.si_ad_objectClass;
1449 mod->sml_type = mod->sml_desc->ad_cname;
1450 mod->sml_values = &gcbva[0];
1452 modtail = &mod->sml_next;
1454 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1455 mod->sml_op = LDAP_MOD_REPLACE;
1456 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1457 mod->sml_type = mod->sml_desc->ad_cname;
1458 mod->sml_values = &gcbva[1];
1460 modtail = &mod->sml_next;
1462 op->o_tag = LDAP_REQ_MODIFY;
1463 op->orm_modlist = modlist;
1465 rc = be->be_modify( op, &rs_modify );
1467 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1468 mlnext = ml->sml_next;
1473 org_req_dn = op->o_req_dn;
1474 org_req_ndn = op->o_req_ndn;
1476 org_ndn = op->o_ndn;
1477 org_managedsait = get_manageDSAit( op );
1478 op->o_dn = op->o_bd->be_rootdn;
1479 op->o_ndn = op->o_bd->be_rootndn;
1480 op->o_managedsait = 1;
1482 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1483 op->o_delete_glue_parent ) {
1484 op->o_delete_glue_parent = 0;
1485 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1486 slap_callback cb = { NULL };
1487 cb.sc_response = slap_null_cb;
1488 dnParent( &op->o_req_ndn, &pdn );
1490 op->o_req_ndn = pdn;
1491 op->o_callback = &cb;
1492 /* give it a root privil ? */
1493 op->o_bd->be_delete( op, &rs_delete );
1499 op->o_managedsait = org_managedsait;
1501 op->o_ndn = org_ndn;
1502 op->o_req_dn = org_req_dn;
1503 op->o_req_ndn = org_req_ndn;
1504 op->o_delete_glue_parent = 0;
1506 ber_bvfree( np_prev->npe_name );
1507 ber_bvfree( np_prev->npe_nname );
1508 op->o_req_dn.bv_val = NULL;
1509 op->o_req_ndn.bv_val = NULL;
1522 Backend *be = op->o_bd;
1523 slap_callback cb = { NULL };
1528 struct berval dn = {0, NULL};
1529 struct berval ndn = {0, NULL};
1531 SlapReply rs_add = {REP_RESULT};
1534 op->o_tag = LDAP_REQ_ADD;
1535 op->o_callback = &cb;
1536 cb.sc_response = null_callback;
1537 cb.sc_private = NULL;
1542 /* count RDNs in suffix */
1543 if ( be->be_nsuffix[0].bv_len ) {
1544 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1554 /* Start with BE suffix */
1555 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1556 comma = strrchr(dn.bv_val, ',');
1557 if ( ptr ) *ptr = ',';
1558 if ( comma ) *comma = '\0';
1563 dn.bv_len -= ptr - dn.bv_val;
1566 /* the normalizedDNs are always the same length, no counting
1569 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1570 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1571 ndn.bv_len = be->be_nsuffix[0].bv_len;
1574 while ( ndn.bv_val > e->e_nname.bv_val ) {
1575 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1576 ber_dupbv( &glue->e_name, &dn );
1577 ber_dupbv( &glue->e_nname, &ndn );
1579 a = ch_calloc( 1, sizeof( Attribute ));
1580 a->a_desc = slap_schema.si_ad_objectClass;
1582 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1583 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1584 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1585 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1587 a->a_nvals = a->a_vals;
1589 a->a_next = glue->e_attrs;
1592 a = ch_calloc( 1, sizeof( Attribute ));
1593 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1595 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1596 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1597 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1599 a->a_nvals = a->a_vals;
1601 a->a_next = glue->e_attrs;
1604 op->o_req_dn = glue->e_name;
1605 op->o_req_ndn = glue->e_nname;
1607 rc = be->be_add ( op, &rs_add );
1608 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1609 be_entry_release_w( op, glue );
1611 /* incl. ALREADY EXIST */
1615 /* Move to next child */
1616 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1619 if ( ptr == e->e_name.bv_val ) break;
1621 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1622 for( ptr = ndn.bv_val-2;
1623 ptr > e->e_nname.bv_val && *ptr != ',';
1629 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1632 op->o_req_dn = e->e_name;
1633 op->o_req_ndn = e->e_nname;
1635 rc = be->be_add ( op, &rs_add );
1636 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1637 be_entry_release_w( op, e );
1645 static struct berval ocbva[] = {
1647 BER_BVC("subentry"),
1648 BER_BVC("syncConsumerSubentry"),
1652 static struct berval cnbva[] = {
1657 static struct berval ssbva[] = {
1662 static struct berval scbva[] = {
1668 syncrepl_updateCookie(
1672 struct sync_cookie *syncCookie )
1674 Backend *be = op->o_bd;
1676 Modifications *mlnext;
1678 Modifications *modlist = NULL;
1679 Modifications **modtail = &modlist;
1682 char txtbuf[SLAP_TEXT_BUFLEN];
1683 size_t textlen = sizeof txtbuf;
1688 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1689 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1690 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1692 slap_callback cb = { NULL };
1693 SlapReply rs_add = {REP_RESULT};
1694 SlapReply rs_modify = {REP_RESULT};
1696 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1697 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1699 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1700 mod->sml_op = LDAP_MOD_REPLACE;
1701 mod->sml_desc = slap_schema.si_ad_objectClass;
1702 mod->sml_type = mod->sml_desc->ad_cname;
1703 mod->sml_values = ocbva;
1705 modtail = &mod->sml_next;
1707 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1708 assert( si->si_rid < 1000 );
1709 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1710 slap_syncrepl_bvc.bv_len + 1,
1711 "syncrepl%ld", si->si_rid );
1712 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1713 mod->sml_op = LDAP_MOD_REPLACE;
1714 mod->sml_desc = slap_schema.si_ad_cn;
1715 mod->sml_type = mod->sml_desc->ad_cname;
1716 mod->sml_values = cnbva;
1718 modtail = &mod->sml_next;
1720 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1721 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1722 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1723 mod->sml_op = LDAP_MOD_REPLACE;
1724 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1725 mod->sml_type = mod->sml_desc->ad_cname;
1726 mod->sml_values = scbva;
1728 modtail = &mod->sml_next;
1730 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1731 mod->sml_op = LDAP_MOD_REPLACE;
1732 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1733 mod->sml_type = mod->sml_desc->ad_cname;
1734 mod->sml_values = ssbva;
1736 modtail = &mod->sml_next;
1740 op->o_tag = LDAP_REQ_ADD;
1741 rc = slap_mods_opattrs( op, modlist, modtail,
1742 &text, txtbuf, textlen );
1744 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1745 ml->sml_op = LDAP_MOD_REPLACE;
1748 if( rc != LDAP_SUCCESS ) {
1750 LDAP_LOG( OPERATION, ERR,
1751 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1753 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1758 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1760 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1761 assert( si->si_rid < 1000 );
1762 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1763 slap_syncrepl_cn_bvc.bv_len + 1,
1764 "cn=syncrepl%ld", si->si_rid );
1766 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1768 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1769 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1771 if ( slap_syncrepl_dn_bv.bv_val ) {
1772 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1777 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1779 if( rc != LDAP_SUCCESS ) {
1781 LDAP_LOG( OPERATION, ERR,
1782 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1784 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1789 cb.sc_response = null_callback;
1792 op->o_callback = &cb;
1793 op->o_req_dn = e->e_name;
1794 op->o_req_ndn = e->e_nname;
1796 /* update persistent cookie */
1797 update_cookie_retry:
1798 op->o_tag = LDAP_REQ_MODIFY;
1799 op->orm_modlist = modlist;
1800 rc = be->be_modify( op, &rs_modify );
1802 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1803 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1804 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1805 op->o_tag = LDAP_REQ_ADD;
1807 rc = be->be_add( op, &rs_add );
1808 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1809 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1810 goto update_cookie_retry;
1811 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1812 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1814 LDAP_LOG( OPERATION, ERR,
1815 "cookie will be non-persistent\n",
1818 Debug( LDAP_DEBUG_ANY,
1819 "cookie will be non-persistent\n",
1824 LDAP_LOG( OPERATION, ERR,
1825 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1827 Debug( LDAP_DEBUG_ANY,
1828 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1832 be_entry_release_w( op, e );
1837 LDAP_LOG( OPERATION, ERR,
1838 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1840 Debug( LDAP_DEBUG_ANY,
1841 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1852 if ( cnbva[0].bv_val ) {
1853 ch_free( cnbva[0].bv_val );
1854 cnbva[0].bv_val = NULL;
1856 if ( scbva[0].bv_val ) {
1857 ch_free( scbva[0].bv_val );
1858 scbva[0].bv_val = NULL;
1861 if ( mlnext->sml_next ) {
1862 slap_mods_free( mlnext->sml_next );
1863 mlnext->sml_next = NULL;
1866 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1867 mlnext = ml->sml_next;
1875 syncrepl_isupdate( Operation *op )
1877 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1881 syncrepl_isupdate_dn(
1883 struct berval* ndn )
1888 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1889 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1890 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1903 syncinfo_t *si = op->o_callback->sc_private;
1905 if ( rs->sr_type == REP_SEARCH ) {
1906 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1908 LDAP_LOG( OPERATION, ERR,
1909 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1911 Debug( LDAP_DEBUG_ANY,
1912 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1915 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1917 } else if ( rs->sr_type == REP_RESULT ) {
1918 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1920 LDAP_LOG( OPERATION, ERR,
1921 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1923 Debug( LDAP_DEBUG_ANY,
1924 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1929 return LDAP_SUCCESS;
1933 nonpresent_callback(
1937 syncinfo_t *si = op->o_callback->sc_private;
1940 struct berval* present_uuid = NULL;
1941 struct nonpresent_entry *np_entry;
1943 if ( rs->sr_type == REP_RESULT ) {
1944 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1945 si->si_presentlist = NULL;
1947 } else if ( rs->sr_type == REP_SEARCH ) {
1948 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1950 if ( a == NULL ) return 0;
1952 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1955 if ( present_uuid == NULL ) {
1956 np_entry = (struct nonpresent_entry *)
1957 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1958 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1959 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1960 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1963 avl_delete( &si->si_presentlist,
1964 &a->a_nvals[0], syncuuid_cmp );
1965 ch_free( present_uuid->bv_val );
1966 ch_free( present_uuid );
1969 return LDAP_SUCCESS;
1977 if ( rs->sr_err != LDAP_SUCCESS &&
1978 rs->sr_err != LDAP_REFERRAL &&
1979 rs->sr_err != LDAP_ALREADY_EXISTS &&
1980 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1981 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1984 LDAP_LOG( OPERATION, ERR,
1985 "null_callback : error code 0x%x\n",
1988 Debug( LDAP_DEBUG_ANY,
1989 "null_callback : error code 0x%x\n",
1993 return LDAP_SUCCESS;
1997 slap_create_syncrepl_entry(
1999 struct berval *context_csn,
2007 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2009 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2011 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2014 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2016 if ( context_csn ) {
2017 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2018 context_csn, NULL );
2022 bv.bv_len = sizeof("{}")-1;
2023 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2025 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2026 ber_dupbv( &e->e_nname, &e->e_name );
2032 slap_uuidstr_from_normalized(
2033 struct berval* uuidstr,
2034 struct berval* normalized,
2038 unsigned char nibble;
2041 if ( normalized == NULL ) return NULL;
2042 if ( normalized->bv_len != 16 ) return NULL;
2047 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2052 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2053 if ( !uuidstr ) slap_sl_free( new, ctx );
2057 for ( i = 0; i < 16; i++ ) {
2058 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2059 new->bv_val[(i<<1)+d] = '-';
2063 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2064 if ( nibble < 10 ) {
2065 new->bv_val[(i<<1)+d] = nibble + '0';
2067 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2070 nibble = (normalized->bv_val[i]) & 0xF;
2071 if ( nibble < 10 ) {
2072 new->bv_val[(i<<1)+d+1] = nibble + '0';
2074 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2078 new->bv_val[new->bv_len] = '\0';
2083 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2085 const struct berval *uuid1 = v_uuid1;
2086 const struct berval *uuid2 = v_uuid2;
2087 int rc = uuid1->bv_len - uuid2->bv_len;
2088 if ( rc ) return rc;
2089 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2093 avl_ber_bvfree( void *bv )
2095 if( bv == NULL ) return;
2096 if ( ((struct berval *)bv)->bv_val != NULL ) {
2097 ch_free( ((struct berval *)bv)->bv_val );
2099 ch_free( (char *) bv );