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 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
95 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
97 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
100 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
117 BerElementBuffer berbuf;
118 BerElement *ber = (BerElement *)&berbuf;
119 LDAPControl c[2], *ctrls[3];
120 struct timeval timeout;
124 /* setup LDAP SYNC control */
125 ber_init2( ber, NULL, LBER_USE_DER );
126 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
128 if ( si->si_syncCookie.octet_str &&
129 si->si_syncCookie.octet_str[0].bv_val )
131 ber_printf( ber, "{eO}",
133 &si->si_syncCookie.octet_str[0] );
135 ber_printf( ber, "{e}",
139 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
144 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
145 c[0].ldctl_iscritical = si->si_type < 0;
148 if ( si->si_authzId ) {
149 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
150 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
151 c[1].ldctl_iscritical = 1;
158 timeout.tv_sec = si->si_tlimit;
161 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
162 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
163 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
164 si->si_slimit, &msgid );
175 int cmdline_cookie_found = 0;
177 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
178 struct berval syncrepl_cn_bv;
179 struct sync_cookie *sc = NULL;
180 struct sync_cookie syncCookie = { NULL, -1, NULL };
186 psub = &si->si_be->be_nsuffix[0];
188 /* Init connection to master */
189 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
190 if ( rc != LDAP_SUCCESS ) {
192 LDAP_LOG( OPERATION, ERR,
193 "do_syncrep1: ldap_initialize failed (%s)\n",
194 si->si_provideruri, 0, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "do_syncrep1: ldap_initialize failed (%s)\n",
198 si->si_provideruri, 0, 0 );
203 op->o_protocol = LDAP_VERSION3;
204 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
209 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
210 if( rc != LDAP_SUCCESS ) {
212 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
213 "%s: ldap_start_tls failed (%d)\n",
214 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
217 Debug( LDAP_DEBUG_ANY,
218 "%s: ldap_start_tls failed (%d)\n",
219 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
222 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
226 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
227 #ifdef HAVE_CYRUS_SASL
230 if ( si->si_secprops != NULL ) {
231 rc = ldap_set_option( si->si_ld,
232 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
234 if( rc != LDAP_OPT_SUCCESS ) {
236 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
237 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
238 si->si_provideruri, si->si_secprops, 0 );
240 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
241 "(%s,SECPROPS,\"%s\") failed!\n",
242 si->si_provideruri, si->si_secprops, 0 );
248 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
249 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
251 rc = ldap_sasl_interactive_bind_s( si->si_ld,
259 lutil_sasl_freedefs( defaults );
261 /* FIXME: different error behaviors according to
263 * 2) on err policy : exit, retry, backoff ...
265 if ( rc != LDAP_SUCCESS ) {
267 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
268 "ldap_sasl_interactive_bind_s failed (%d)\n",
271 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
272 "ldap_sasl_interactive_bind_s failed (%d)\n",
276 /* FIXME (see above comment) */
277 /* if Kerberos credentials cache is not active, retry */
278 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
279 rc == LDAP_LOCAL_ERROR )
281 rc = LDAP_SERVER_DOWN;
286 #else /* HAVE_CYRUS_SASL */
287 /* Should never get here, we trapped this at config time */
289 fprintf( stderr, "not compiled with SASL support\n" );
295 rc = ldap_bind_s( si->si_ld,
296 si->si_binddn, si->si_passwd, si->si_bindmethod );
297 if ( rc != LDAP_SUCCESS ) {
299 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
300 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
302 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
303 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
309 /* Set SSF to strongest of TLS, SASL SSFs */
313 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl ) == LDAP_SUCCESS &&
315 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
317 #endif /* HAVE_TLS */
318 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
319 op->o_transport_ssf = op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf ) ?
320 op->o_sasl_ssf : op->o_tls_ssf;
322 /* get syncrepl cookie of shadow replica from subentry */
323 assert( si->si_rid < 1000 );
324 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
325 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
326 CN_STR "syncrepl%ld", si->si_rid );
327 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
328 op->o_req_dn = op->o_req_ndn;
330 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
331 if ( si->si_rid == sc->rid ) {
332 cmdline_cookie_found = 1;
337 if ( cmdline_cookie_found ) {
338 /* cookie is supplied in the command line */
339 BerVarray cookie = NULL;
340 struct berval cookie_bv;
342 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
343 slap_sync_cookie_free( &si->si_syncCookie, 0 );
345 /* read stored cookie if it exists */
346 backend_attribute( op, NULL, &op->o_req_ndn,
347 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
350 /* no stored cookie */
351 if ( sc->ctxcsn == NULL ||
352 sc->ctxcsn->bv_val == NULL ) {
353 /* if cmdline cookie does not have ctxcsn */
354 /* component, set it to an initial value */
355 slap_init_sync_cookie_ctxcsn( sc );
357 slap_dup_sync_cookie( &si->si_syncCookie, sc );
358 slap_sync_cookie_free( sc, 1 );
363 struct berval newcookie = BER_BVNULL;
364 ber_dupbv( &cookie_bv, &cookie[0] );
365 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
366 slap_parse_sync_cookie( &si->si_syncCookie );
367 ber_bvarray_free( si->si_syncCookie.octet_str );
368 si->si_syncCookie.octet_str = NULL;
369 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
370 if ( sc->sid != -1 ) {
371 /* command line cookie wins */
372 si->si_syncCookie.sid = sc->sid;
374 if ( sc->ctxcsn != NULL ) {
375 /* command line cookie wins */
376 if ( si->si_syncCookie.ctxcsn ) {
377 ber_bvarray_free( si->si_syncCookie.ctxcsn );
378 si->si_syncCookie.ctxcsn = NULL;
380 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
381 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
383 if ( sc->rid != -1 ) {
384 /* command line cookie wins */
385 si->si_syncCookie.rid = sc->rid;
387 slap_sync_cookie_free( sc, 1 );
389 slap_compose_sync_cookie( NULL, &newcookie,
390 &si->si_syncCookie.ctxcsn[0],
391 si->si_syncCookie.sid, si->si_syncCookie.rid );
392 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
396 /* no command line cookie is specified */
397 if ( si->si_syncCookie.octet_str == NULL ) {
398 BerVarray cookie = NULL;
399 struct berval cookie_bv;
400 /* try to read stored cookie */
401 backend_attribute( op, NULL, &op->o_req_ndn,
402 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
404 ber_dupbv( &cookie_bv, &cookie[0] );
405 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
406 slap_parse_sync_cookie( &si->si_syncCookie );
407 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
412 rc = ldap_sync_search( si, op->o_tmpmemctx );
414 if( rc != LDAP_SUCCESS ) {
416 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
417 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
419 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
420 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
427 ldap_unbind( si->si_ld );
440 LDAPControl **rctrls = NULL;
443 BerElementBuffer berbuf;
444 BerElement *ber = (BerElement *)&berbuf;
446 LDAPMessage *res = NULL;
447 LDAPMessage *msg = NULL;
450 struct berval *retdata = NULL;
455 struct berval syncUUID = BER_BVNULL;
456 struct sync_cookie syncCookie = { NULL, -1, NULL };
457 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
458 struct berval cookie = BER_BVNULL;
466 Modifications *modlist = NULL;
471 struct timeval *tout_p = NULL;
472 struct timeval tout = { 0, 0 };
474 int refreshDeletes = 0;
476 BerVarray syncUUIDs = NULL;
479 if ( slapd_shutdown ) {
484 ber_init2( ber, NULL, LBER_USE_DER );
485 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
488 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
490 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
493 psub = &si->si_be->be_nsuffix[0];
495 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
497 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
503 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
504 tout_p, &res )) > 0 )
506 if ( slapd_shutdown ) {
510 for( msg = ldap_first_message( si->si_ld, res );
512 msg = ldap_next_message( si->si_ld, msg ) )
514 switch( ldap_msgtype( msg ) ) {
515 case LDAP_RES_SEARCH_ENTRY:
516 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
517 /* we can't work without the control */
523 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
524 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
525 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
526 ber_scanf( ber, /*"{"*/ "m}", &cookie );
527 if ( cookie.bv_val ) {
528 struct berval tmp_bv;
529 ber_dupbv( &tmp_bv, &cookie );
530 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
532 if ( syncCookie.octet_str &&
533 syncCookie.octet_str[0].bv_val )
534 slap_parse_sync_cookie( &syncCookie );
536 if ( syncrepl_message_to_entry( si, op, msg,
537 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
538 rc_efree = syncrepl_entry( si, op, entry, modlist,
539 syncstate, &syncUUID, &syncCookie_req );
540 if ( syncCookie.octet_str &&
541 syncCookie.octet_str[0].bv_val )
543 syncrepl_updateCookie( si, op, psub, &syncCookie );
546 ldap_controls_free( rctrls );
548 slap_mods_free( modlist );
550 if ( rc_efree && entry ) {
556 case LDAP_RES_SEARCH_REFERENCE:
558 LDAP_LOG( OPERATION, ERR,
559 "do_syncrep2 : reference received\n", 0, 0, 0 );
561 Debug( LDAP_DEBUG_ANY,
562 "do_syncrep2 : reference received\n", 0, 0, 0 );
566 case LDAP_RES_SEARCH_RESULT:
567 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
571 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
573 ber_scanf( ber, "{" /*"}"*/);
574 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
575 ber_scanf( ber, "m", &cookie );
576 if ( cookie.bv_val ) {
577 struct berval tmp_bv;
578 ber_dupbv( &tmp_bv, &cookie );
579 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
581 if ( syncCookie.octet_str &&
582 syncCookie.octet_str[0].bv_val )
584 slap_parse_sync_cookie( &syncCookie );
587 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
589 ber_scanf( ber, "b", &refreshDeletes );
591 ber_scanf( ber, /*"{"*/ "}" );
593 if ( syncCookie_req.ctxcsn == NULL ) {
595 } else if ( syncCookie.ctxcsn == NULL ) {
598 value_match( &match, slap_schema.si_ad_entryCSN,
599 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
600 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
601 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
604 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
605 match < 0 && err == LDAP_SUCCESS )
607 syncrepl_updateCookie( si, op, psub, &syncCookie );
610 ldap_controls_free( rctrls );
612 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
613 /* FIXME : different error behaviors according to
614 * 1) err code : LDAP_BUSY ...
615 * 2) on err policy : stop service, stop sync, retry
617 if ( refreshDeletes == 0 && match < 0 &&
618 err == LDAP_SUCCESS )
620 syncrepl_del_nonpresent( op, si );
622 avl_free( si->si_presentlist, avl_ber_bvfree );
623 si->si_presentlist = NULL;
630 case LDAP_RES_INTERMEDIATE:
631 rc = ldap_parse_intermediate( si->si_ld, msg,
632 &retoid, &retdata, NULL, 0 );
633 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
634 int si_refreshDelete = 0;
635 int si_refreshPresent = 0;
636 ber_init2( ber, retdata, LBER_USE_DER );
638 switch ( si_tag = ber_peek_tag( ber, &len )) {
640 case LDAP_TAG_SYNC_NEW_COOKIE:
641 ber_scanf( ber, "tm", &tag, &cookie );
643 case LDAP_TAG_SYNC_REFRESH_DELETE:
644 si_refreshDelete = 1;
645 case LDAP_TAG_SYNC_REFRESH_PRESENT:
646 si_refreshPresent = 1;
647 ber_scanf( ber, "t{" /*"}"*/, &tag );
648 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
650 ber_scanf( ber, "m", &cookie );
651 if ( cookie.bv_val ) {
652 struct berval tmp_bv;
653 ber_dupbv( &tmp_bv, &cookie );
654 ber_bvarray_add( &syncCookie.octet_str,
657 if ( syncCookie.octet_str &&
658 syncCookie.octet_str[0].bv_val )
660 slap_parse_sync_cookie( &syncCookie );
663 if ( ber_peek_tag( ber, &len ) ==
664 LDAP_TAG_REFRESHDONE )
666 ber_scanf( ber, "b", &refreshDone );
668 ber_scanf( ber, /*"{"*/ "}" );
670 case LDAP_TAG_SYNC_ID_SET:
671 ber_scanf( ber, "t{" /*"}"*/, &tag );
672 if ( ber_peek_tag( ber, &len ) ==
673 LDAP_TAG_SYNC_COOKIE )
675 ber_scanf( ber, "m", &cookie );
676 if ( cookie.bv_val ) {
677 struct berval tmp_bv;
678 ber_dupbv( &tmp_bv, &cookie );
679 ber_bvarray_add( &syncCookie.octet_str,
682 if ( syncCookie.octet_str &&
683 syncCookie.octet_str[0].bv_val )
685 slap_parse_sync_cookie( &syncCookie );
688 if ( ber_peek_tag( ber, &len ) ==
689 LDAP_TAG_REFRESHDELETES )
691 ber_scanf( ber, "b", &refreshDeletes );
693 ber_scanf( ber, "[W]", &syncUUIDs );
694 ber_scanf( ber, /*"{"*/ "}" );
695 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
696 struct berval *syncuuid_bv;
697 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
698 avl_insert( &si->si_presentlist,
699 (caddr_t) syncuuid_bv,
700 syncuuid_cmp, avl_dup_error );
702 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
706 LDAP_LOG( OPERATION, ERR,
707 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
708 (long) si_tag, 0, 0 );
710 Debug( LDAP_DEBUG_ANY,
711 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
712 (long) si_tag, 0, 0 );
714 ldap_memfree( retoid );
715 ber_bvfree( retdata );
719 if ( syncCookie_req.ctxcsn == NULL ) {
721 } else if ( syncCookie.ctxcsn == NULL ) {
724 value_match( &match, slap_schema.si_ad_entryCSN,
725 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
726 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
727 &syncCookie_req.ctxcsn[0],
728 &syncCookie.ctxcsn[0], &text );
731 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
734 syncrepl_updateCookie( si, op, psub, &syncCookie);
737 if ( si_refreshPresent == 1 ) {
739 syncrepl_del_nonpresent( op, si );
743 ldap_memfree( retoid );
744 ber_bvfree( retdata );
749 LDAP_LOG( OPERATION, ERR, "do_syncrep2 :"
750 " unknown intermediate "
751 "response\n", 0, 0, 0 );
753 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
754 "unknown intermediate response (%d)\n",
757 ldap_memfree( retoid );
758 ber_bvfree( retdata );
765 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
766 "unknown message\n", 0, 0, 0 );
768 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
769 "unknown message\n", 0, 0, 0 );
774 if ( syncCookie.octet_str ) {
775 slap_sync_cookie_free( &syncCookie_req, 0 );
776 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
777 slap_sync_cookie_free( &syncCookie, 0 );
787 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
788 errstr = ldap_err2string( rc );
791 LDAP_LOG( OPERATION, ERR,
792 "do_syncrep2 : %s\n", errstr, 0, 0 );
794 Debug( LDAP_DEBUG_ANY,
795 "do_syncrep2 : %s\n", errstr, 0, 0 );
800 slap_sync_cookie_free( &syncCookie, 0 );
801 slap_sync_cookie_free( &syncCookie_req, 0 );
803 if ( res ) ldap_msgfree( res );
805 if ( rc && si->si_ld ) {
806 ldap_unbind( si->si_ld );
818 struct re_s* rtask = arg;
819 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
820 Connection conn = {0};
822 int rc = LDAP_SUCCESS;
829 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
831 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
837 switch( abs( si->si_type )) {
838 case LDAP_SYNC_REFRESH_ONLY:
839 case LDAP_SYNC_REFRESH_AND_PERSIST:
845 if ( slapd_shutdown && si->si_ld ) {
846 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
847 connection_client_stop( s );
848 ldap_unbind( si->si_ld );
853 connection_fake_init( &conn, &op, ctx );
855 op.o_dn = si->si_updatedn;
856 op.o_ndn = si->si_updatedn;
857 op.o_managedsait = 1;
860 op.o_sync_state.ctxcsn = NULL;
861 op.o_sync_state.sid = -1;
862 op.o_sync_state.octet_str = NULL;
863 op.o_sync_slog_size = -1;
864 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
865 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
867 /* Establish session, do search */
870 rc = do_syncrep1( &op, si );
873 /* Process results */
874 if ( rc == LDAP_SUCCESS ) {
875 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
877 rc = do_syncrep2( &op, si );
879 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
880 /* If we succeeded, enable the connection for further listening.
881 * If we failed, tear down the connection and reschedule.
883 if ( rc == LDAP_SUCCESS ) {
885 rc = connection_client_setup( s, do_syncrepl,
888 connection_client_enable( s );
890 } else if ( !first ) {
894 if ( rc == -2 ) rc = 0;
898 /* At this point, we have 4 cases:
899 * 1) for any hard failure, give up and remove this task
900 * 2) for ServerDown, reschedule this task to run
901 * 3) for Refresh and Success, reschedule to run
902 * 4) for Persist and Success, reschedule to defer
904 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
906 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
907 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
911 connection_client_stop( s );
914 if ( rc == LDAP_SUCCESS ) {
915 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
918 rtask->interval.tv_sec = si->si_interval;
919 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
920 if ( si->si_retrynum ) {
921 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
922 si->si_retrynum[i] = si->si_retrynum_init[i];
924 si->si_retrynum[i] = -2;
927 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
928 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
932 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
933 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
934 } else if ( si->si_retrynum[i] >= -1 ) {
935 if ( si->si_retrynum[i] > 0 )
936 si->si_retrynum[i]--;
937 rtask->interval.tv_sec = si->si_retryinterval[i];
938 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
939 slap_wake_listener();
943 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
949 syncrepl_message_to_entry(
953 Modifications **modlist,
959 BerElement *ber = NULL;
962 Modifications **modtail = modlist;
965 char txtbuf[SLAP_TEXT_BUFLEN];
966 size_t textlen = sizeof txtbuf;
968 struct berval bdn = {0, NULL}, dn, ndn;
973 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
975 LDAP_LOG( OPERATION, ERR,
976 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
978 Debug( LDAP_DEBUG_ANY,
979 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
984 op->o_tag = LDAP_REQ_ADD;
986 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
988 if ( rc != LDAP_SUCCESS ) {
990 LDAP_LOG( OPERATION, ERR,
991 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
993 Debug( LDAP_DEBUG_ANY,
994 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
999 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1000 ber_dupbv( &op->o_req_dn, &dn );
1001 ber_dupbv( &op->o_req_ndn, &ndn );
1002 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1003 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1005 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1006 return LDAP_SUCCESS;
1009 if ( entry == NULL ) {
1013 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1015 e->e_name = op->o_req_dn;
1016 e->e_nname = op->o_req_ndn;
1018 while ( ber_remaining( ber ) ) {
1019 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1020 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1025 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1027 mod->sml_op = LDAP_MOD_REPLACE;
1028 mod->sml_next = NULL;
1029 mod->sml_desc = NULL;
1030 mod->sml_type = tmp.sml_type;
1031 mod->sml_values = tmp.sml_values;
1032 mod->sml_nvalues = NULL;
1035 modtail = &mod->sml_next;
1038 if ( *modlist == NULL ) {
1040 LDAP_LOG( OPERATION, ERR,
1041 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1043 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1048 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1050 if ( rc != LDAP_SUCCESS ) {
1052 LDAP_LOG( OPERATION, ERR,
1053 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1055 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1061 /* Strip out dynamically generated attrs */
1062 for ( modtail = modlist; *modtail ; ) {
1064 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1065 *modtail = mod->sml_next;
1066 slap_mod_free( &mod->sml_mod, 0 );
1069 modtail = &mod->sml_next;
1073 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1074 if( rc != LDAP_SUCCESS ) {
1076 LDAP_LOG( OPERATION, ERR,
1077 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1079 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1085 ber_free ( ber, 0 );
1086 if ( rc != LDAP_SUCCESS ) {
1099 Modifications* modlist,
1101 struct berval* syncUUID,
1102 struct sync_cookie* syncCookie_req )
1104 Backend *be = op->o_bd;
1105 slap_callback cb = { NULL };
1106 struct berval *syncuuid_bv = NULL;
1107 struct berval syncUUID_strrep = BER_BVNULL;
1108 struct berval uuid_bv = BER_BVNULL;
1110 SlapReply rs_search = {REP_RESULT};
1111 SlapReply rs_delete = {REP_RESULT};
1112 SlapReply rs_add = {REP_RESULT};
1113 SlapReply rs_modify = {REP_RESULT};
1115 AttributeAssertion ava = {0};
1116 int rc = LDAP_SUCCESS;
1117 int ret = LDAP_SUCCESS;
1120 struct berval pdn = BER_BVNULL;
1121 struct berval org_req_dn = BER_BVNULL;
1122 struct berval org_req_ndn = BER_BVNULL;
1123 struct berval org_dn = BER_BVNULL;
1124 struct berval org_ndn = BER_BVNULL;
1125 int org_managedsait;
1127 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1128 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1129 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1130 syncuuid_cmp, avl_dup_error );
1133 if ( syncstate == LDAP_SYNC_PRESENT ) {
1137 f.f_choice = LDAP_FILTER_EQUALITY;
1139 ava.aa_desc = slap_schema.si_ad_entryUUID;
1140 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1141 ava.aa_value = *syncUUID;
1142 op->ors_filter = &f;
1144 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1145 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1146 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1147 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1148 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1149 syncUUID->bv_val, syncUUID->bv_len );
1150 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1152 op->o_tag = LDAP_REQ_SEARCH;
1153 op->ors_scope = LDAP_SCOPE_SUBTREE;
1155 /* get syncrepl cookie of shadow replica from subentry */
1156 op->o_req_dn = si->si_base;
1157 op->o_req_ndn = si->si_base;
1159 op->o_time = slap_get_time();
1160 op->ors_tlimit = SLAP_NO_LIMIT;
1163 op->ors_attrs = slap_anlist_no_attrs;
1164 op->ors_attrsonly = 1;
1166 /* set callback function */
1167 op->o_callback = &cb;
1168 cb.sc_response = dn_callback;
1171 si->si_syncUUID_ndn.bv_val = NULL;
1173 if ( limits_check( op, &rs_search ) == 0 ) {
1174 rc = be->be_search( op, &rs_search );
1177 if ( op->ors_filterstr.bv_val ) {
1178 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1181 cb.sc_response = null_callback;
1184 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1187 if ( syncstate != LDAP_SYNC_DELETE ) {
1188 op->o_no_psearch = 1;
1191 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1192 if ( op->o_sync_csn.bv_val ) {
1193 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1198 op->o_req_dn = si->si_syncUUID_ndn;
1199 op->o_req_ndn = si->si_syncUUID_ndn;
1200 op->o_tag = LDAP_REQ_DELETE;
1201 rc = be->be_delete( op, &rs_delete );
1203 org_req_dn = op->o_req_dn;
1204 org_req_ndn = op->o_req_ndn;
1206 org_ndn = op->o_ndn;
1207 org_managedsait = get_manageDSAit( op );
1208 op->o_dn = op->o_bd->be_rootdn;
1209 op->o_ndn = op->o_bd->be_rootndn;
1210 op->o_managedsait = 1;
1212 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1213 op->o_delete_glue_parent = 0;
1214 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1215 slap_callback cb = { NULL };
1216 cb.sc_response = slap_null_cb;
1217 dnParent( &op->o_req_ndn, &pdn );
1219 op->o_req_ndn = pdn;
1220 op->o_callback = &cb;
1221 op->o_bd->be_delete( op, &rs_delete );
1227 op->o_managedsait = org_managedsait;
1229 op->o_ndn = org_ndn;
1230 op->o_req_dn = org_req_dn;
1231 op->o_req_ndn = org_req_ndn;
1232 op->o_delete_glue_parent = 0;
1234 op->o_no_psearch = 0;
1237 switch ( syncstate ) {
1239 case LDAP_SYNC_MODIFY:
1240 if ( rs_search.sr_err == LDAP_SUCCESS ||
1241 rs_search.sr_err == LDAP_REFERRAL ||
1242 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1243 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1245 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1246 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1247 &syncUUID_strrep, syncUUID );
1249 op->o_tag = LDAP_REQ_ADD;
1251 op->o_req_dn = e->e_name;
1252 op->o_req_ndn = e->e_nname;
1254 rc = be->be_add( op, &rs_add );
1256 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1257 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1258 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1260 Modifications *modtail = modlist;
1264 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1268 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1269 ber_dupbv( &uuid_bv, syncUUID );
1270 mod->sml_op = LDAP_MOD_REPLACE;
1271 mod->sml_desc = slap_schema.si_ad_entryUUID;
1272 mod->sml_type = mod->sml_desc->ad_cname;
1273 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1274 modtail->sml_next = mod;
1276 op->o_tag = LDAP_REQ_MODIFY;
1277 op->orm_modlist = modlist;
1278 op->o_req_dn = e->e_name;
1279 op->o_req_ndn = e->e_nname;
1281 rc = be->be_modify( op, &rs_modify );
1282 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1284 LDAP_LOG( OPERATION, ERR,
1285 "syncrepl_entry : be_modify failed (%d)\n",
1286 rs_modify.sr_err, 0, 0 );
1288 Debug( LDAP_DEBUG_ANY,
1289 "syncrepl_entry : be_modify failed (%d)\n",
1290 rs_modify.sr_err, 0, 0 );
1295 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1296 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1297 syncrepl_add_glue( op, e );
1302 LDAP_LOG( OPERATION, ERR,
1303 "syncrepl_entry : be_add failed (%d)\n",
1304 rs_add.sr_err, 0, 0 );
1306 Debug( LDAP_DEBUG_ANY,
1307 "syncrepl_entry : be_add failed (%d)\n",
1308 rs_add.sr_err, 0, 0 );
1314 be_entry_release_w( op, e );
1320 LDAP_LOG( OPERATION, ERR,
1321 "syncrepl_entry : be_search failed (%d)\n",
1322 rs_search.sr_err, 0, 0 );
1324 Debug( LDAP_DEBUG_ANY,
1325 "syncrepl_entry : be_search failed (%d)\n",
1326 rs_search.sr_err, 0, 0 );
1332 case LDAP_SYNC_DELETE :
1333 /* Already deleted */
1339 LDAP_LOG( OPERATION, ERR,
1340 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1342 Debug( LDAP_DEBUG_ANY,
1343 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1351 if ( syncUUID_strrep.bv_val ) {
1352 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1354 if ( si->si_syncUUID_ndn.bv_val ) {
1355 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1360 static struct berval gcbva[] = {
1367 syncrepl_del_nonpresent(
1371 Backend* be = op->o_bd;
1372 slap_callback cb = { NULL };
1373 SlapReply rs_search = {REP_RESULT};
1374 SlapReply rs_delete = {REP_RESULT};
1375 SlapReply rs_modify = {REP_RESULT};
1376 struct nonpresent_entry *np_list, *np_prev;
1379 Modifications *mlnext;
1381 Modifications *modlist = NULL;
1382 Modifications **modtail = &modlist;
1384 AttributeName an[2];
1386 struct berval pdn = BER_BVNULL;
1387 struct berval org_req_dn = BER_BVNULL;
1388 struct berval org_req_ndn = BER_BVNULL;
1389 struct berval org_dn = BER_BVNULL;
1390 struct berval org_ndn = BER_BVNULL;
1391 int org_managedsait;
1393 op->o_req_dn = si->si_base;
1394 op->o_req_ndn = si->si_base;
1396 cb.sc_response = nonpresent_callback;
1399 op->o_callback = &cb;
1400 op->o_tag = LDAP_REQ_SEARCH;
1401 op->ors_scope = si->si_scope;
1402 op->ors_deref = LDAP_DEREF_NEVER;
1403 op->o_time = slap_get_time();
1404 op->ors_tlimit = SLAP_NO_LIMIT;
1405 op->ors_slimit = SLAP_NO_LIMIT;
1407 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1408 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1409 an[0].an_desc = slap_schema.si_ad_entryUUID;
1412 op->ors_attrsonly = 0;
1413 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1414 op->ors_filterstr = si->si_filterstr;
1416 op->o_nocaching = 1;
1417 op->o_managedsait = 0;
1419 if ( limits_check( op, &rs_search ) == 0 ) {
1420 rc = be->be_search( op, &rs_search );
1423 op->o_managedsait = 1;
1424 op->o_nocaching = 0;
1426 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1428 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1429 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1430 while ( np_list != NULL ) {
1431 LDAP_LIST_REMOVE( np_list, npe_link );
1433 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1434 op->o_tag = LDAP_REQ_DELETE;
1435 op->o_callback = &cb;
1436 cb.sc_response = null_callback;
1438 op->o_req_dn = *np_prev->npe_name;
1439 op->o_req_ndn = *np_prev->npe_nname;
1440 rc = op->o_bd->be_delete( op, &rs_delete );
1442 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1443 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1444 mod->sml_op = LDAP_MOD_REPLACE;
1445 mod->sml_desc = slap_schema.si_ad_objectClass;
1446 mod->sml_type = mod->sml_desc->ad_cname;
1447 mod->sml_values = &gcbva[0];
1449 modtail = &mod->sml_next;
1451 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1452 mod->sml_op = LDAP_MOD_REPLACE;
1453 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1454 mod->sml_type = mod->sml_desc->ad_cname;
1455 mod->sml_values = &gcbva[1];
1457 modtail = &mod->sml_next;
1459 op->o_tag = LDAP_REQ_MODIFY;
1460 op->orm_modlist = modlist;
1462 rc = be->be_modify( op, &rs_modify );
1464 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1465 mlnext = ml->sml_next;
1470 org_req_dn = op->o_req_dn;
1471 org_req_ndn = op->o_req_ndn;
1473 org_ndn = op->o_ndn;
1474 org_managedsait = get_manageDSAit( op );
1475 op->o_dn = op->o_bd->be_rootdn;
1476 op->o_ndn = op->o_bd->be_rootndn;
1477 op->o_managedsait = 1;
1479 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1480 op->o_delete_glue_parent ) {
1481 op->o_delete_glue_parent = 0;
1482 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1483 slap_callback cb = { NULL };
1484 cb.sc_response = slap_null_cb;
1485 dnParent( &op->o_req_ndn, &pdn );
1487 op->o_req_ndn = pdn;
1488 op->o_callback = &cb;
1489 /* give it a root privil ? */
1490 op->o_bd->be_delete( op, &rs_delete );
1496 op->o_managedsait = org_managedsait;
1498 op->o_ndn = org_ndn;
1499 op->o_req_dn = org_req_dn;
1500 op->o_req_ndn = org_req_ndn;
1501 op->o_delete_glue_parent = 0;
1503 ber_bvfree( np_prev->npe_name );
1504 ber_bvfree( np_prev->npe_nname );
1505 op->o_req_dn.bv_val = NULL;
1506 op->o_req_ndn.bv_val = NULL;
1519 Backend *be = op->o_bd;
1520 slap_callback cb = { NULL };
1525 struct berval dn = {0, NULL};
1526 struct berval ndn = {0, NULL};
1528 SlapReply rs_add = {REP_RESULT};
1531 op->o_tag = LDAP_REQ_ADD;
1532 op->o_callback = &cb;
1533 cb.sc_response = null_callback;
1534 cb.sc_private = NULL;
1539 /* count RDNs in suffix */
1540 if ( be->be_nsuffix[0].bv_len ) {
1541 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1551 /* Start with BE suffix */
1552 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1553 comma = strrchr(dn.bv_val, ',');
1554 if ( ptr ) *ptr = ',';
1555 if ( comma ) *comma = '\0';
1560 dn.bv_len -= ptr - dn.bv_val;
1563 /* the normalizedDNs are always the same length, no counting
1566 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1567 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1568 ndn.bv_len = be->be_nsuffix[0].bv_len;
1571 while ( ndn.bv_val > e->e_nname.bv_val ) {
1572 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1573 ber_dupbv( &glue->e_name, &dn );
1574 ber_dupbv( &glue->e_nname, &ndn );
1576 a = ch_calloc( 1, sizeof( Attribute ));
1577 a->a_desc = slap_schema.si_ad_objectClass;
1579 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1580 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1581 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1582 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1584 a->a_nvals = a->a_vals;
1586 a->a_next = glue->e_attrs;
1589 a = ch_calloc( 1, sizeof( Attribute ));
1590 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1592 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1593 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1594 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1596 a->a_nvals = a->a_vals;
1598 a->a_next = glue->e_attrs;
1601 op->o_req_dn = glue->e_name;
1602 op->o_req_ndn = glue->e_nname;
1604 rc = be->be_add ( op, &rs_add );
1605 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1606 be_entry_release_w( op, glue );
1608 /* incl. ALREADY EXIST */
1612 /* Move to next child */
1613 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1616 if ( ptr == e->e_name.bv_val ) break;
1618 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1619 for( ptr = ndn.bv_val-2;
1620 ptr > e->e_nname.bv_val && *ptr != ',';
1626 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1629 op->o_req_dn = e->e_name;
1630 op->o_req_ndn = e->e_nname;
1632 rc = be->be_add ( op, &rs_add );
1633 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1634 be_entry_release_w( op, e );
1642 static struct berval ocbva[] = {
1644 BER_BVC("subentry"),
1645 BER_BVC("syncConsumerSubentry"),
1649 static struct berval cnbva[] = {
1654 static struct berval ssbva[] = {
1659 static struct berval scbva[] = {
1665 syncrepl_updateCookie(
1669 struct sync_cookie *syncCookie )
1671 Backend *be = op->o_bd;
1673 Modifications *mlnext;
1675 Modifications *modlist = NULL;
1676 Modifications **modtail = &modlist;
1679 char txtbuf[SLAP_TEXT_BUFLEN];
1680 size_t textlen = sizeof txtbuf;
1685 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1686 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1687 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1689 slap_callback cb = { NULL };
1690 SlapReply rs_add = {REP_RESULT};
1691 SlapReply rs_modify = {REP_RESULT};
1693 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1694 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1696 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1697 mod->sml_op = LDAP_MOD_REPLACE;
1698 mod->sml_desc = slap_schema.si_ad_objectClass;
1699 mod->sml_type = mod->sml_desc->ad_cname;
1700 mod->sml_values = ocbva;
1702 modtail = &mod->sml_next;
1704 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1705 assert( si->si_rid < 1000 );
1706 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1707 slap_syncrepl_bvc.bv_len + 1,
1708 "syncrepl%ld", si->si_rid );
1709 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1710 mod->sml_op = LDAP_MOD_REPLACE;
1711 mod->sml_desc = slap_schema.si_ad_cn;
1712 mod->sml_type = mod->sml_desc->ad_cname;
1713 mod->sml_values = cnbva;
1715 modtail = &mod->sml_next;
1717 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1718 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1719 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1720 mod->sml_op = LDAP_MOD_REPLACE;
1721 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1722 mod->sml_type = mod->sml_desc->ad_cname;
1723 mod->sml_values = scbva;
1725 modtail = &mod->sml_next;
1727 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1728 mod->sml_op = LDAP_MOD_REPLACE;
1729 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1730 mod->sml_type = mod->sml_desc->ad_cname;
1731 mod->sml_values = ssbva;
1733 modtail = &mod->sml_next;
1737 op->o_tag = LDAP_REQ_ADD;
1738 rc = slap_mods_opattrs( op, modlist, modtail,
1739 &text, txtbuf, textlen );
1741 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1742 ml->sml_op = LDAP_MOD_REPLACE;
1745 if( rc != LDAP_SUCCESS ) {
1747 LDAP_LOG( OPERATION, ERR,
1748 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1750 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1755 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1757 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1758 assert( si->si_rid < 1000 );
1759 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1760 slap_syncrepl_cn_bvc.bv_len + 1,
1761 "cn=syncrepl%ld", si->si_rid );
1763 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1765 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1766 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1768 if ( slap_syncrepl_dn_bv.bv_val ) {
1769 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1774 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1776 if( rc != LDAP_SUCCESS ) {
1778 LDAP_LOG( OPERATION, ERR,
1779 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1781 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1786 cb.sc_response = null_callback;
1789 op->o_callback = &cb;
1790 op->o_req_dn = e->e_name;
1791 op->o_req_ndn = e->e_nname;
1793 /* update persistent cookie */
1794 update_cookie_retry:
1795 op->o_tag = LDAP_REQ_MODIFY;
1796 op->orm_modlist = modlist;
1797 rc = be->be_modify( op, &rs_modify );
1799 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1800 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1801 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1802 op->o_tag = LDAP_REQ_ADD;
1804 rc = be->be_add( op, &rs_add );
1805 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1806 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1807 goto update_cookie_retry;
1808 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1809 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1811 LDAP_LOG( OPERATION, ERR,
1812 "cookie will be non-persistent\n",
1815 Debug( LDAP_DEBUG_ANY,
1816 "cookie will be non-persistent\n",
1821 LDAP_LOG( OPERATION, ERR,
1822 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1824 Debug( LDAP_DEBUG_ANY,
1825 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1829 be_entry_release_w( op, e );
1834 LDAP_LOG( OPERATION, ERR,
1835 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1837 Debug( LDAP_DEBUG_ANY,
1838 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1849 if ( cnbva[0].bv_val ) {
1850 ch_free( cnbva[0].bv_val );
1851 cnbva[0].bv_val = NULL;
1853 if ( scbva[0].bv_val ) {
1854 ch_free( scbva[0].bv_val );
1855 scbva[0].bv_val = NULL;
1858 if ( mlnext->sml_next ) {
1859 slap_mods_free( mlnext->sml_next );
1860 mlnext->sml_next = NULL;
1863 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1864 mlnext = ml->sml_next;
1872 syncrepl_isupdate( Operation *op )
1874 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1878 syncrepl_isupdate_dn(
1880 struct berval* ndn )
1885 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1886 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1887 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1900 syncinfo_t *si = op->o_callback->sc_private;
1902 if ( rs->sr_type == REP_SEARCH ) {
1903 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1905 LDAP_LOG( OPERATION, ERR,
1906 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1908 Debug( LDAP_DEBUG_ANY,
1909 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1912 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1914 } else if ( rs->sr_type == REP_RESULT ) {
1915 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1917 LDAP_LOG( OPERATION, ERR,
1918 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1920 Debug( LDAP_DEBUG_ANY,
1921 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1926 return LDAP_SUCCESS;
1930 nonpresent_callback(
1934 syncinfo_t *si = op->o_callback->sc_private;
1937 struct berval* present_uuid = NULL;
1938 struct nonpresent_entry *np_entry;
1940 if ( rs->sr_type == REP_RESULT ) {
1941 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1942 si->si_presentlist = NULL;
1944 } else if ( rs->sr_type == REP_SEARCH ) {
1945 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1947 if ( a == NULL ) return 0;
1949 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1952 if ( present_uuid == NULL ) {
1953 np_entry = (struct nonpresent_entry *)
1954 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1955 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1956 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1957 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1960 avl_delete( &si->si_presentlist,
1961 &a->a_nvals[0], syncuuid_cmp );
1962 ch_free( present_uuid->bv_val );
1963 ch_free( present_uuid );
1966 return LDAP_SUCCESS;
1974 if ( rs->sr_err != LDAP_SUCCESS &&
1975 rs->sr_err != LDAP_REFERRAL &&
1976 rs->sr_err != LDAP_ALREADY_EXISTS &&
1977 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1978 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1981 LDAP_LOG( OPERATION, ERR,
1982 "null_callback : error code 0x%x\n",
1985 Debug( LDAP_DEBUG_ANY,
1986 "null_callback : error code 0x%x\n",
1990 return LDAP_SUCCESS;
1994 slap_create_syncrepl_entry(
1996 struct berval *context_csn,
2004 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2006 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2008 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2011 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2013 if ( context_csn ) {
2014 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2015 context_csn, NULL );
2019 bv.bv_len = sizeof("{}")-1;
2020 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2022 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2023 ber_dupbv( &e->e_nname, &e->e_name );
2029 slap_uuidstr_from_normalized(
2030 struct berval* uuidstr,
2031 struct berval* normalized,
2035 unsigned char nibble;
2038 if ( normalized == NULL ) return NULL;
2039 if ( normalized->bv_len != 16 ) return NULL;
2044 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2049 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2050 if ( !uuidstr ) slap_sl_free( new, ctx );
2054 for ( i = 0; i < 16; i++ ) {
2055 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2056 new->bv_val[(i<<1)+d] = '-';
2060 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2061 if ( nibble < 10 ) {
2062 new->bv_val[(i<<1)+d] = nibble + '0';
2064 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2067 nibble = (normalized->bv_val[i]) & 0xF;
2068 if ( nibble < 10 ) {
2069 new->bv_val[(i<<1)+d+1] = nibble + '0';
2071 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2075 new->bv_val[new->bv_len] = '\0';
2080 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2082 const struct berval *uuid1 = v_uuid1;
2083 const struct berval *uuid2 = v_uuid2;
2084 int rc = uuid1->bv_len - uuid2->bv_len;
2085 if ( rc ) return rc;
2086 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2090 avl_ber_bvfree( void *bv )
2092 if( bv == NULL ) return;
2093 if ( ((struct berval *)bv)->bv_val != NULL ) {
2094 ch_free( ((struct berval *)bv)->bv_val );
2096 ch_free( (char *) bv );