1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
29 #include "lutil_ldap.h"
33 #define SYNCREPL_STR "syncreplxxx"
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
48 static AttributeDescription *sync_descs[4];
50 struct runqueue_s syncrepl_rq;
53 init_syncrepl(syncinfo_t *si)
58 if ( !sync_descs[0] ) {
59 sync_descs[0] = slap_schema.si_ad_objectClass;
60 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61 sync_descs[2] = slap_schema.si_ad_entryCSN;
65 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
68 /* Delete Attributes */
69 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
74 ch_free( si->si_attrs[j] );
75 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 si->si_attrs[k] = si->si_attrs[k+1];
81 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
85 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
87 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
91 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
94 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
96 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
99 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
118 BerElementBuffer berbuf;
119 BerElement *ber = (BerElement *)&berbuf;
120 LDAPControl c[2], *ctrls[3];
121 struct timeval timeout;
125 /* setup LDAP SYNC control */
126 ber_init2( ber, NULL, LBER_USE_DER );
127 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
129 if ( si->si_syncCookie.octet_str &&
130 si->si_syncCookie.octet_str[0].bv_val ) {
131 ber_printf( ber, "{eO}", abs(si->si_type),
132 &si->si_syncCookie.octet_str[0] );
134 ber_printf( ber, "{e}", abs(si->si_type) );
137 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
142 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143 c[0].ldctl_iscritical = si->si_type < 0;
146 if ( si->si_authzId ) {
147 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149 c[1].ldctl_iscritical = 1;
156 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
159 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
162 si->si_slimit, &msgid );
174 int cmdline_cookie_found = 0;
176 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
177 struct berval syncrepl_cn_bv;
178 struct sync_cookie *sc = NULL;
179 struct sync_cookie syncCookie = { NULL, -1, NULL };
185 psub = &si->si_be->be_nsuffix[0];
187 /* 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,
249 si->si_saslmech, si->si_realm,
250 si->si_authcId, si->si_passwd, si->si_authzId );
252 rc = ldap_sasl_interactive_bind_s( si->si_ld,
260 lutil_sasl_freedefs( defaults );
262 /* FIXME: different error behaviors according to
264 * 2) on err policy : exit, retry, backoff ...
266 if ( rc != LDAP_SUCCESS ) {
268 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
269 "ldap_sasl_interactive_bind_s failed (%d)\n",
272 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
273 "ldap_sasl_interactive_bind_s failed (%d)\n",
277 /* FIXME (see above comment) */
278 /* if Kerberos credentials cache is not active, retry */
279 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
280 rc == LDAP_LOCAL_ERROR )
282 rc = LDAP_SERVER_DOWN;
287 #else /* HAVE_CYRUS_SASL */
288 /* Should never get here, we trapped this at config time */
289 fprintf( stderr, "not compiled with SASL support\n" );
294 rc = ldap_bind_s( si->si_ld,
295 si->si_binddn, si->si_passwd, si->si_bindmethod );
296 if ( rc != LDAP_SUCCESS ) {
298 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
299 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
301 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
302 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
308 /* Set SSF to strongest of TLS, SASL SSFs */
312 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl ) == LDAP_SUCCESS &&
314 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
316 #endif /* HAVE_TLS */
317 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
318 op->o_transport_ssf = op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf ) ?
319 op->o_sasl_ssf : op->o_tls_ssf;
321 /* 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 );
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 );
362 struct berval newcookie = BER_BVNULL;
363 ber_dupbv( &cookie_bv, &cookie[0] );
364 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
365 slap_parse_sync_cookie( &si->si_syncCookie );
366 ber_bvarray_free( si->si_syncCookie.octet_str );
367 si->si_syncCookie.octet_str = NULL;
368 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
369 if ( sc->sid != -1 ) {
370 /* command line cookie wins */
371 si->si_syncCookie.sid = sc->sid;
373 if ( sc->ctxcsn != NULL ) {
374 /* command line cookie wins */
375 if ( si->si_syncCookie.ctxcsn ) {
376 ber_bvarray_free( si->si_syncCookie.ctxcsn );
377 si->si_syncCookie.ctxcsn = NULL;
379 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
380 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
382 if ( sc->rid != -1 ) {
383 /* command line cookie wins */
384 si->si_syncCookie.rid = sc->rid;
386 slap_sync_cookie_free( sc, 1 );
388 slap_compose_sync_cookie( NULL, &newcookie,
389 &si->si_syncCookie.ctxcsn[0],
390 si->si_syncCookie.sid, si->si_syncCookie.rid );
391 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
394 /* no command line cookie is specified */
395 if ( si->si_syncCookie.octet_str == NULL ) {
396 BerVarray cookie = NULL;
397 struct berval cookie_bv;
398 /* try to read stored cookie */
399 backend_attribute( op, NULL, &op->o_req_ndn,
400 slap_schema.si_ad_syncreplCookie, &cookie );
402 ber_dupbv( &cookie_bv, &cookie[0] );
403 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
404 slap_parse_sync_cookie( &si->si_syncCookie );
405 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
410 rc = ldap_sync_search( si, op->o_tmpmemctx );
412 if( rc != LDAP_SUCCESS ) {
414 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
415 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
417 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
418 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
425 ldap_unbind( si->si_ld );
438 LDAPControl **rctrls = NULL;
441 BerElementBuffer berbuf;
442 BerElement *ber = (BerElement *)&berbuf;
444 LDAPMessage *res = NULL;
445 LDAPMessage *msg = NULL;
448 struct berval *retdata = NULL;
453 struct berval syncUUID = BER_BVNULL;
454 struct sync_cookie syncCookie = { NULL, -1, NULL };
455 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
456 struct berval cookie = BER_BVNULL;
464 Modifications *modlist = NULL;
469 struct timeval *tout_p = NULL;
470 struct timeval tout = { 0, 0 };
472 int refreshDeletes = 0;
474 BerVarray syncUUIDs = NULL;
477 if ( slapd_shutdown ) {
482 ber_init2( ber, NULL, LBER_USE_DER );
483 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
486 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
488 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
491 psub = &si->si_be->be_nsuffix[0];
493 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
495 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
501 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
504 if ( slapd_shutdown ) {
508 for( msg = ldap_first_message( si->si_ld, res );
510 msg = ldap_next_message( si->si_ld, msg ) )
512 switch( ldap_msgtype( msg ) ) {
513 case LDAP_RES_SEARCH_ENTRY:
514 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
515 /* we can't work without the control */
521 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
522 ber_scanf( ber, "{em", &syncstate, &syncUUID );
523 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
524 ber_scanf( ber, "m}", &cookie );
525 if ( cookie.bv_val ) {
526 struct berval tmp_bv;
527 ber_dupbv( &tmp_bv, &cookie );
528 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
530 if ( syncCookie.octet_str &&
531 syncCookie.octet_str[0].bv_val )
532 slap_parse_sync_cookie( &syncCookie );
534 if ( syncrepl_message_to_entry( si, op, msg,
535 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
536 rc_efree = syncrepl_entry( si, op, entry, modlist,
537 syncstate, &syncUUID, &syncCookie_req );
538 if ( syncCookie.octet_str &&
539 syncCookie.octet_str[0].bv_val ) {
540 syncrepl_updateCookie( si, op, psub, &syncCookie );
543 ldap_controls_free( rctrls );
545 slap_mods_free( modlist );
547 if ( rc_efree && entry ) {
553 case LDAP_RES_SEARCH_REFERENCE:
555 LDAP_LOG( OPERATION, ERR,
556 "do_syncrep2 : reference received\n", 0, 0, 0 );
558 Debug( LDAP_DEBUG_ANY,
559 "do_syncrep2 : reference received\n", 0, 0, 0 );
563 case LDAP_RES_SEARCH_RESULT:
564 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
568 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
570 ber_scanf( ber, "{" /*"}"*/);
571 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
573 ber_scanf( ber, "m", &cookie );
574 if ( cookie.bv_val ) {
575 struct berval tmp_bv;
576 ber_dupbv( &tmp_bv, &cookie );
577 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
579 if ( syncCookie.octet_str &&
580 syncCookie.octet_str[0].bv_val )
581 slap_parse_sync_cookie( &syncCookie );
583 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
585 ber_scanf( ber, "b", &refreshDeletes );
587 ber_scanf( ber, "}" );
589 if ( syncCookie_req.ctxcsn == NULL ) {
591 } else if ( syncCookie.ctxcsn == NULL ) {
594 value_match( &match, slap_schema.si_ad_entryCSN,
595 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
596 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
597 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
599 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
600 && match < 0 && err == LDAP_SUCCESS ) {
601 syncrepl_updateCookie( si, op, psub, &syncCookie );
604 ldap_controls_free( rctrls );
606 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
607 /* FIXME : different error behaviors according to
608 * 1) err code : LDAP_BUSY ...
609 * 2) on err policy : stop service, stop sync, retry
611 if ( refreshDeletes == 0 && match < 0 &&
612 err == LDAP_SUCCESS ) {
613 syncrepl_del_nonpresent( op, si );
615 avl_free( si->si_presentlist, avl_ber_bvfree );
616 si->si_presentlist = NULL;
623 case LDAP_RES_INTERMEDIATE:
624 rc = ldap_parse_intermediate( si->si_ld, msg,
625 &retoid, &retdata, NULL, 0 );
626 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
627 int si_refreshDelete = 0;
628 int si_refreshPresent = 0;
629 ber_init2( ber, retdata, LBER_USE_DER );
631 switch ( si_tag = ber_peek_tag( ber, &len )) {
633 case LDAP_TAG_SYNC_NEW_COOKIE:
634 ber_scanf( ber, "tm", &tag, &cookie );
636 case LDAP_TAG_SYNC_REFRESH_DELETE:
637 si_refreshDelete = 1;
638 case LDAP_TAG_SYNC_REFRESH_PRESENT:
639 si_refreshPresent = 1;
640 ber_scanf( ber, "t{", &tag );
641 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
643 ber_scanf( ber, "m", &cookie );
644 if ( cookie.bv_val ) {
645 struct berval tmp_bv;
646 ber_dupbv( &tmp_bv, &cookie );
647 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
649 if ( syncCookie.octet_str &&
650 syncCookie.octet_str[0].bv_val )
651 slap_parse_sync_cookie( &syncCookie );
653 if ( ber_peek_tag( ber, &len ) ==
654 LDAP_TAG_REFRESHDONE )
656 ber_scanf( ber, "b", &refreshDone );
658 ber_scanf( ber, "}" );
660 case LDAP_TAG_SYNC_ID_SET:
661 ber_scanf( ber, "t{", &tag );
662 if ( ber_peek_tag( ber, &len ) ==
663 LDAP_TAG_SYNC_COOKIE ) {
664 ber_scanf( ber, "m", &cookie );
665 if ( cookie.bv_val ) {
666 struct berval tmp_bv;
667 ber_dupbv( &tmp_bv, &cookie );
668 ber_bvarray_add( &syncCookie.octet_str,
671 if ( syncCookie.octet_str &&
672 syncCookie.octet_str[0].bv_val )
673 slap_parse_sync_cookie( &syncCookie );
675 if ( ber_peek_tag( ber, &len ) ==
676 LDAP_TAG_REFRESHDELETES )
678 ber_scanf( ber, "b", &refreshDeletes );
680 ber_scanf( ber, "[W]", &syncUUIDs );
681 ber_scanf( ber, "}" );
682 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
683 struct berval *syncuuid_bv;
684 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
685 avl_insert( &si->si_presentlist,
686 (caddr_t) syncuuid_bv,
687 syncuuid_cmp, avl_dup_error );
689 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
693 LDAP_LOG( OPERATION, ERR,
694 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
695 (long) si_tag, 0, 0 );
697 Debug( LDAP_DEBUG_ANY,
698 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
699 (long) si_tag, 0, 0 );
701 ldap_memfree( retoid );
702 ber_bvfree( retdata );
706 if ( syncCookie_req.ctxcsn == NULL ) {
708 } else if ( syncCookie.ctxcsn == NULL ) {
711 value_match( &match, slap_schema.si_ad_entryCSN,
712 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
713 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
714 &syncCookie_req.ctxcsn[0],
715 &syncCookie.ctxcsn[0], &text );
718 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
720 syncrepl_updateCookie( si, op, psub, &syncCookie);
723 if ( si_refreshPresent == 1 ) {
725 syncrepl_del_nonpresent( op, si );
729 ldap_memfree( retoid );
730 ber_bvfree( retdata );
734 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
735 " unknown intermediate "
736 "response\n", 0, 0, 0 );
738 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
739 "unknown intermediate response (%d)\n",
742 ldap_memfree( retoid );
743 ber_bvfree( retdata );
749 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
750 "unknown message\n", 0, 0, 0 );
752 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
753 "unknown message\n", 0, 0, 0 );
758 if ( syncCookie.octet_str ) {
759 slap_sync_cookie_free( &syncCookie_req, 0 );
760 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
761 slap_sync_cookie_free( &syncCookie, 0 );
771 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
772 errstr = ldap_err2string( rc );
775 LDAP_LOG( OPERATION, ERR,
776 "do_syncrep2 : %s\n", errstr, 0, 0 );
778 Debug( LDAP_DEBUG_ANY,
779 "do_syncrep2 : %s\n", errstr, 0, 0 );
784 slap_sync_cookie_free( &syncCookie, 0 );
785 slap_sync_cookie_free( &syncCookie_req, 0 );
787 if ( res ) ldap_msgfree( res );
789 if ( rc && si->si_ld ) {
790 ldap_unbind( si->si_ld );
802 struct re_s* rtask = arg;
803 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
804 Connection conn = {0};
806 int rc = LDAP_SUCCESS;
812 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
814 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
820 switch( abs( si->si_type )) {
821 case LDAP_SYNC_REFRESH_ONLY:
822 case LDAP_SYNC_REFRESH_AND_PERSIST:
828 if ( slapd_shutdown && si->si_ld ) {
829 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
830 connection_client_stop( s );
831 ldap_unbind( si->si_ld );
836 connection_fake_init( &conn, &op, ctx );
838 op.o_dn = si->si_updatedn;
839 op.o_ndn = si->si_updatedn;
840 op.o_managedsait = 1;
843 op.o_sync_state.ctxcsn = NULL;
844 op.o_sync_state.sid = -1;
845 op.o_sync_state.octet_str = NULL;
846 op.o_sync_slog_size = -1;
847 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
848 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
850 /* Establish session, do search */
853 rc = do_syncrep1( &op, si );
856 /* Process results */
857 if ( rc == LDAP_SUCCESS ) {
858 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
860 rc = do_syncrep2( &op, si );
862 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
863 /* If we succeeded, enable the connection for further listening.
864 * If we failed, tear down the connection and reschedule.
866 if ( rc == LDAP_SUCCESS ) {
868 rc = connection_client_setup( s, do_syncrepl,
871 connection_client_enable( s );
873 } else if ( !first ) {
877 if ( rc == -2 ) rc = 0;
881 /* At this point, we have 4 cases:
882 * 1) for any hard failure, give up and remove this task
883 * 2) for ServerDown, reschedule this task to run
884 * 3) for Refresh and Success, reschedule to run
885 * 4) for Persist and Success, reschedule to defer
887 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
888 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
889 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
893 connection_client_stop( s );
896 if ( rc && rc != LDAP_SERVER_DOWN ) {
897 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
899 if ( rc == LDAP_SERVER_DOWN ||
900 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
905 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
907 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
913 syncrepl_message_to_entry(
917 Modifications **modlist,
923 BerElement *ber = NULL;
926 Modifications **modtail = modlist;
929 char txtbuf[SLAP_TEXT_BUFLEN];
930 size_t textlen = sizeof txtbuf;
932 struct berval bdn = {0, NULL}, dn, ndn;
937 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
939 LDAP_LOG( OPERATION, ERR,
940 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
942 Debug( LDAP_DEBUG_ANY,
943 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
948 op->o_tag = LDAP_REQ_ADD;
950 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
952 if ( rc != LDAP_SUCCESS ) {
954 LDAP_LOG( OPERATION, ERR,
955 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
957 Debug( LDAP_DEBUG_ANY,
958 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
963 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
964 ber_dupbv( &op->o_req_dn, &dn );
965 ber_dupbv( &op->o_req_ndn, &ndn );
966 sl_free( ndn.bv_val, op->o_tmpmemctx );
967 sl_free( dn.bv_val, op->o_tmpmemctx );
969 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
974 if ( entry == NULL ) {
978 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
980 e->e_name = op->o_req_dn;
981 e->e_nname = op->o_req_ndn;
983 while ( ber_remaining( ber ) ) {
984 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
985 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
990 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
992 mod->sml_op = LDAP_MOD_REPLACE;
993 mod->sml_next = NULL;
994 mod->sml_desc = NULL;
995 mod->sml_type = tmp.sml_type;
996 mod->sml_values = tmp.sml_values;
997 mod->sml_nvalues = NULL;
1000 modtail = &mod->sml_next;
1003 if ( *modlist == NULL ) {
1005 LDAP_LOG( OPERATION, ERR,
1006 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1008 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1013 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1015 if ( rc != LDAP_SUCCESS ) {
1017 LDAP_LOG( OPERATION, ERR,
1018 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1020 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1026 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1027 if( rc != LDAP_SUCCESS ) {
1029 LDAP_LOG( OPERATION, ERR,
1030 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1032 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1038 ber_free ( ber, 0 );
1039 if ( rc != LDAP_SUCCESS ) {
1052 Modifications* modlist,
1054 struct berval* syncUUID,
1055 struct sync_cookie* syncCookie_req
1058 Backend *be = op->o_bd;
1059 slap_callback cb = { NULL };
1060 struct berval *syncuuid_bv = NULL;
1061 struct berval syncUUID_strrep = BER_BVNULL;
1062 struct berval uuid_bv = BER_BVNULL;
1064 SlapReply rs = {REP_RESULT};
1066 AttributeAssertion ava = {0};
1067 int rc = LDAP_SUCCESS;
1068 int ret = LDAP_SUCCESS;
1071 struct berval pdn = BER_BVNULL;
1072 struct berval org_req_dn = BER_BVNULL;
1073 struct berval org_req_ndn = BER_BVNULL;
1074 struct berval org_dn = BER_BVNULL;
1075 struct berval org_ndn = BER_BVNULL;
1076 int org_managedsait;
1078 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1080 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1081 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1082 syncuuid_cmp, avl_dup_error );
1085 if ( syncstate == LDAP_SYNC_PRESENT ) {
1089 f.f_choice = LDAP_FILTER_EQUALITY;
1091 ava.aa_desc = slap_schema.si_ad_entryUUID;
1092 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1093 ava.aa_value = *syncUUID;
1094 op->ors_filter = &f;
1096 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1097 op->ors_filterstr.bv_val = (char *) sl_malloc(
1098 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1099 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1100 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1101 syncUUID->bv_val, syncUUID->bv_len );
1102 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1104 op->o_tag = LDAP_REQ_SEARCH;
1105 op->ors_scope = LDAP_SCOPE_SUBTREE;
1107 /* get syncrepl cookie of shadow replica from subentry */
1108 op->o_req_dn = si->si_base;
1109 op->o_req_ndn = si->si_base;
1111 /* set callback function */
1112 op->o_callback = &cb;
1113 cb.sc_response = dn_callback;
1116 si->si_syncUUID_ndn.bv_val = NULL;
1118 if ( limits_check( op, &rs ) == 0 ) {
1119 rc = be->be_search( op, &rs );
1122 if ( op->ors_filterstr.bv_val ) {
1123 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1126 cb.sc_response = null_callback;
1129 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1133 if ( syncstate != LDAP_SYNC_DELETE ) {
1134 op->o_no_psearch = 1;
1137 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1138 if ( op->o_sync_csn.bv_val ) {
1139 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1144 op->o_req_dn = si->si_syncUUID_ndn;
1145 op->o_req_ndn = si->si_syncUUID_ndn;
1146 op->o_tag = LDAP_REQ_DELETE;
1147 rc = be->be_delete( op, &rs );
1149 org_req_dn = op->o_req_dn;
1150 org_req_ndn = op->o_req_ndn;
1152 org_ndn = op->o_ndn;
1153 org_managedsait = get_manageDSAit( op );
1154 op->o_dn = op->o_bd->be_rootdn;
1155 op->o_ndn = op->o_bd->be_rootndn;
1156 op->o_managedsait = 1;
1158 while ( rs.sr_err == LDAP_SUCCESS &&
1159 op->o_delete_glue_parent ) {
1160 op->o_delete_glue_parent = 0;
1161 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1162 slap_callback cb = { NULL };
1163 cb.sc_response = slap_null_cb;
1164 dnParent( &op->o_req_ndn, &pdn );
1166 op->o_req_ndn = pdn;
1167 op->o_callback = &cb;
1168 op->o_bd->be_delete( op, &rs );
1174 op->o_managedsait = org_managedsait;
1176 op->o_ndn = org_ndn;
1177 op->o_req_dn = org_req_dn;
1178 op->o_req_ndn = org_req_ndn;
1179 op->o_delete_glue_parent = 0;
1181 op->o_no_psearch = 0;
1184 switch ( syncstate ) {
1186 case LDAP_SYNC_MODIFY:
1187 if ( rc == LDAP_SUCCESS ||
1188 rc == LDAP_REFERRAL ||
1189 rc == LDAP_NO_SUCH_OBJECT ||
1190 rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1192 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1193 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1194 syncUUID, &ava.aa_value );
1196 op->o_tag = LDAP_REQ_ADD;
1198 op->o_req_dn = e->e_name;
1199 op->o_req_ndn = e->e_nname;
1200 rc = be->be_add( op, &rs );
1202 if ( rc != LDAP_SUCCESS ) {
1203 if ( rc == LDAP_ALREADY_EXISTS ) {
1205 Modifications *modtail = modlist;
1209 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1213 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1214 ber_dupbv( &uuid_bv, syncUUID );
1215 mod->sml_op = LDAP_MOD_REPLACE;
1216 mod->sml_desc = slap_schema.si_ad_entryUUID;
1217 mod->sml_type = mod->sml_desc->ad_cname;
1218 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1219 modtail->sml_next = mod;
1221 op->o_tag = LDAP_REQ_MODIFY;
1222 op->orm_modlist = modlist;
1223 op->o_req_dn = e->e_name;
1224 op->o_req_ndn = e->e_nname;
1226 rc = be->be_modify( op, &rs );
1227 if ( rc != LDAP_SUCCESS ) {
1229 LDAP_LOG( OPERATION, ERR,
1230 "syncrepl_entry : be_modify failed (%d)\n",
1233 Debug( LDAP_DEBUG_ANY,
1234 "syncrepl_entry : be_modify failed (%d)\n",
1240 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1241 syncrepl_add_glue( op, e );
1246 LDAP_LOG( OPERATION, ERR,
1247 "syncrepl_entry : be_add failed (%d)\n",
1250 Debug( LDAP_DEBUG_ANY,
1251 "syncrepl_entry : be_add failed (%d)\n",
1258 be_entry_release_w( op, e );
1264 LDAP_LOG( OPERATION, ERR,
1265 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1267 Debug( LDAP_DEBUG_ANY,
1268 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1274 case LDAP_SYNC_DELETE :
1275 /* Already deleted */
1281 LDAP_LOG( OPERATION, ERR,
1282 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1284 Debug( LDAP_DEBUG_ANY,
1285 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1293 if ( syncUUID_strrep.bv_val ) {
1294 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1296 if ( si->si_syncUUID_ndn.bv_val ) {
1297 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1302 static struct berval gcbva[] = {
1309 syncrepl_del_nonpresent(
1314 Backend* be = op->o_bd;
1315 slap_callback cb = { NULL };
1316 SlapReply rs = {REP_RESULT};
1317 struct nonpresent_entry *np_list, *np_prev;
1320 Modifications *mlnext;
1322 Modifications *modlist = NULL;
1323 Modifications **modtail = &modlist;
1326 struct berval pdn = BER_BVNULL;
1327 struct berval org_req_dn = BER_BVNULL;
1328 struct berval org_req_ndn = BER_BVNULL;
1329 struct berval org_dn = BER_BVNULL;
1330 struct berval org_ndn = BER_BVNULL;
1331 int org_managedsait;
1333 op->o_req_dn = si->si_base;
1334 op->o_req_ndn = si->si_base;
1336 cb.sc_response = nonpresent_callback;
1339 op->o_callback = &cb;
1340 op->o_tag = LDAP_REQ_SEARCH;
1341 op->ors_scope = si->si_scope;
1342 op->ors_deref = LDAP_DEREF_NEVER;
1345 op->ors_attrsonly = 0;
1346 op->ors_attrs = NULL;
1347 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1348 op->ors_filterstr = si->si_filterstr;
1350 op->o_nocaching = 1;
1351 op->o_managedsait = 0;
1353 if ( limits_check( op, &rs ) == 0 ) {
1354 be->be_search( op, &rs );
1357 op->o_managedsait = 1;
1358 op->o_nocaching = 0;
1360 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1362 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1363 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1364 while ( np_list != NULL ) {
1365 LDAP_LIST_REMOVE( np_list, npe_link );
1367 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1368 op->o_tag = LDAP_REQ_DELETE;
1369 op->o_callback = &cb;
1370 cb.sc_response = null_callback;
1372 op->o_req_dn = *np_prev->npe_name;
1373 op->o_req_ndn = *np_prev->npe_nname;
1374 rc = op->o_bd->be_delete( op, &rs );
1376 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1377 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1378 mod->sml_op = LDAP_MOD_REPLACE;
1379 mod->sml_desc = slap_schema.si_ad_objectClass;
1380 mod->sml_type = mod->sml_desc->ad_cname;
1381 mod->sml_values = &gcbva[0];
1383 modtail = &mod->sml_next;
1385 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1386 mod->sml_op = LDAP_MOD_REPLACE;
1387 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1388 mod->sml_type = mod->sml_desc->ad_cname;
1389 mod->sml_values = &gcbva[1];
1391 modtail = &mod->sml_next;
1393 op->o_tag = LDAP_REQ_MODIFY;
1394 op->orm_modlist = modlist;
1396 rc = be->be_modify( op, &rs );
1398 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1399 mlnext = ml->sml_next;
1404 org_req_dn = op->o_req_dn;
1405 org_req_ndn = op->o_req_ndn;
1407 org_ndn = op->o_ndn;
1408 org_managedsait = get_manageDSAit( op );
1409 op->o_dn = op->o_bd->be_rootdn;
1410 op->o_ndn = op->o_bd->be_rootndn;
1411 op->o_managedsait = 1;
1413 while ( rs.sr_err == LDAP_SUCCESS &&
1414 op->o_delete_glue_parent ) {
1415 op->o_delete_glue_parent = 0;
1416 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1417 slap_callback cb = { NULL };
1418 cb.sc_response = slap_null_cb;
1419 dnParent( &op->o_req_ndn, &pdn );
1421 op->o_req_ndn = pdn;
1422 op->o_callback = &cb;
1423 /* give it a root privil ? */
1424 op->o_bd->be_delete( op, &rs );
1430 op->o_managedsait = org_managedsait;
1432 op->o_ndn = org_ndn;
1433 op->o_req_dn = org_req_dn;
1434 op->o_req_ndn = org_req_ndn;
1435 op->o_delete_glue_parent = 0;
1437 ber_bvfree( np_prev->npe_name );
1438 ber_bvfree( np_prev->npe_nname );
1439 op->o_req_dn.bv_val = NULL;
1440 op->o_req_ndn.bv_val = NULL;
1455 Backend *be = op->o_bd;
1456 slap_callback cb = { NULL };
1461 struct berval dn = {0, NULL};
1462 struct berval ndn = {0, NULL};
1464 SlapReply rs = {REP_RESULT};
1467 op->o_tag = LDAP_REQ_ADD;
1468 op->o_callback = &cb;
1469 cb.sc_response = null_callback;
1470 cb.sc_private = NULL;
1475 /* count RDNs in suffix */
1476 if ( be->be_nsuffix[0].bv_len ) {
1477 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1487 /* Start with BE suffix */
1488 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1489 comma = strrchr(dn.bv_val, ',');
1490 if ( ptr ) *ptr = ',';
1491 if ( comma ) *comma = '\0';
1496 dn.bv_len -= ptr - dn.bv_val;
1499 /* the normalizedDNs are always the same length, no counting
1502 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1503 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1504 ndn.bv_len = be->be_nsuffix[0].bv_len;
1507 while ( ndn.bv_val > e->e_nname.bv_val ) {
1508 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1509 ber_dupbv( &glue->e_name, &dn );
1510 ber_dupbv( &glue->e_nname, &ndn );
1512 a = ch_calloc( 1, sizeof( Attribute ));
1513 a->a_desc = slap_schema.si_ad_objectClass;
1515 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1516 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1517 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1518 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1520 a->a_nvals = a->a_vals;
1522 a->a_next = glue->e_attrs;
1525 a = ch_calloc( 1, sizeof( Attribute ));
1526 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1528 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1529 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1530 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1532 a->a_nvals = a->a_vals;
1534 a->a_next = glue->e_attrs;
1537 op->o_req_dn = glue->e_name;
1538 op->o_req_ndn = glue->e_nname;
1540 rc = be->be_add ( op, &rs );
1541 if ( rc == LDAP_SUCCESS ) {
1542 be_entry_release_w( op, glue );
1544 /* incl. ALREADY EXIST */
1548 /* Move to next child */
1549 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1552 if ( ptr == e->e_name.bv_val ) break;
1554 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1555 for( ptr = ndn.bv_val-2;
1556 ptr > e->e_nname.bv_val && *ptr != ',';
1562 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1565 op->o_req_dn = e->e_name;
1566 op->o_req_ndn = e->e_nname;
1568 rc = be->be_add ( op, &rs );
1569 if ( rc == LDAP_SUCCESS ) {
1570 be_entry_release_w( op, e );
1578 static struct berval ocbva[] = {
1580 BER_BVC("subentry"),
1581 BER_BVC("syncConsumerSubentry"),
1585 static struct berval cnbva[] = {
1590 static struct berval ssbva[] = {
1595 static struct berval scbva[] = {
1601 syncrepl_updateCookie(
1605 struct sync_cookie *syncCookie
1608 Backend *be = op->o_bd;
1610 Modifications *mlnext;
1612 Modifications *modlist = NULL;
1613 Modifications **modtail = &modlist;
1616 char txtbuf[SLAP_TEXT_BUFLEN];
1617 size_t textlen = sizeof txtbuf;
1622 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1623 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1624 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1626 slap_callback cb = { NULL };
1627 SlapReply rs = {REP_RESULT};
1629 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1630 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1632 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1633 mod->sml_op = LDAP_MOD_REPLACE;
1634 mod->sml_desc = slap_schema.si_ad_objectClass;
1635 mod->sml_type = mod->sml_desc->ad_cname;
1636 mod->sml_values = ocbva;
1638 modtail = &mod->sml_next;
1640 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1641 assert( si->si_rid < 1000 );
1642 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1643 slap_syncrepl_bvc.bv_len + 1,
1644 "syncrepl%ld", si->si_rid );
1645 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1646 mod->sml_op = LDAP_MOD_REPLACE;
1647 mod->sml_desc = slap_schema.si_ad_cn;
1648 mod->sml_type = mod->sml_desc->ad_cname;
1649 mod->sml_values = cnbva;
1651 modtail = &mod->sml_next;
1653 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1654 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1655 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1656 mod->sml_op = LDAP_MOD_REPLACE;
1657 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1658 mod->sml_type = mod->sml_desc->ad_cname;
1659 mod->sml_values = scbva;
1661 modtail = &mod->sml_next;
1663 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1664 mod->sml_op = LDAP_MOD_REPLACE;
1665 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1666 mod->sml_type = mod->sml_desc->ad_cname;
1667 mod->sml_values = ssbva;
1669 modtail = &mod->sml_next;
1673 op->o_tag = LDAP_REQ_ADD;
1674 rc = slap_mods_opattrs( op, modlist, modtail,
1675 &text,txtbuf, textlen );
1677 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1678 ml->sml_op = LDAP_MOD_REPLACE;
1681 if( rc != LDAP_SUCCESS ) {
1683 LDAP_LOG( OPERATION, ERR,
1684 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1686 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1691 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1693 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1694 assert( si->si_rid < 1000 );
1695 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1696 slap_syncrepl_cn_bvc.bv_len + 1,
1697 "cn=syncrepl%ld", si->si_rid );
1699 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1701 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1702 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1704 if ( slap_syncrepl_dn_bv.bv_val ) {
1705 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1710 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1712 if( rc != LDAP_SUCCESS ) {
1714 LDAP_LOG( OPERATION, ERR,
1715 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1717 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1722 cb.sc_response = null_callback;
1725 op->o_callback = &cb;
1726 op->o_req_dn = e->e_name;
1727 op->o_req_ndn = e->e_nname;
1729 /* update persistent cookie */
1730 update_cookie_retry:
1731 op->o_tag = LDAP_REQ_MODIFY;
1732 op->orm_modlist = modlist;
1733 rc = be->be_modify( op, &rs );
1735 if ( rc != LDAP_SUCCESS ) {
1736 if ( rc == LDAP_REFERRAL ||
1737 rc == LDAP_NO_SUCH_OBJECT ) {
1738 op->o_tag = LDAP_REQ_ADD;
1740 rc = be->be_add( op, &rs );
1741 if ( rc != LDAP_SUCCESS ) {
1742 if ( rc == LDAP_ALREADY_EXISTS ) {
1743 goto update_cookie_retry;
1744 } else if ( rc == LDAP_REFERRAL ||
1745 rc == LDAP_NO_SUCH_OBJECT ) {
1747 LDAP_LOG( OPERATION, ERR,
1748 "cookie will be non-persistent\n",
1751 Debug( LDAP_DEBUG_ANY,
1752 "cookie will be non-persistent\n",
1757 LDAP_LOG( OPERATION, ERR,
1758 "be_add failed (%d)\n",
1761 Debug( LDAP_DEBUG_ANY,
1762 "be_add failed (%d)\n",
1767 be_entry_release_w( op, e );
1772 LDAP_LOG( OPERATION, ERR,
1773 "be_modify failed (%d)\n", rc, 0, 0 );
1775 Debug( LDAP_DEBUG_ANY,
1776 "be_modify failed (%d)\n", rc, 0, 0 );
1787 if ( cnbva[0].bv_val ) {
1788 ch_free( cnbva[0].bv_val );
1789 cnbva[0].bv_val = NULL;
1791 if ( scbva[0].bv_val ) {
1792 ch_free( scbva[0].bv_val );
1793 scbva[0].bv_val = NULL;
1796 if ( mlnext->sml_next ) {
1797 slap_mods_free( mlnext->sml_next );
1798 mlnext->sml_next = NULL;
1801 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1802 mlnext = ml->sml_next;
1810 syncrepl_isupdate( Operation *op )
1812 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1816 syncrepl_isupdate_dn(
1824 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1825 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1826 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1840 syncinfo_t *si = op->o_callback->sc_private;
1842 if ( rs->sr_type == REP_SEARCH ) {
1843 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1845 LDAP_LOG( OPERATION, ERR,
1846 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1848 Debug( LDAP_DEBUG_ANY,
1849 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1852 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1856 return LDAP_SUCCESS;
1860 nonpresent_callback(
1865 syncinfo_t *si = op->o_callback->sc_private;
1868 struct berval* present_uuid = NULL;
1869 struct nonpresent_entry *np_entry;
1871 if ( rs->sr_type == REP_RESULT ) {
1872 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1873 si->si_presentlist = NULL;
1875 } else if ( rs->sr_type == REP_SEARCH ) {
1876 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1878 if ( a == NULL ) return 0;
1880 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1883 if ( present_uuid == NULL ) {
1884 np_entry = (struct nonpresent_entry *)
1885 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1886 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1887 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1888 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1891 avl_delete( &si->si_presentlist,
1892 &a->a_nvals[0], syncuuid_cmp );
1893 ch_free( present_uuid->bv_val );
1894 ch_free( present_uuid );
1897 return LDAP_SUCCESS;
1905 if ( rs->sr_err != LDAP_SUCCESS &&
1906 rs->sr_err != LDAP_REFERRAL &&
1907 rs->sr_err != LDAP_ALREADY_EXISTS &&
1908 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1909 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1912 LDAP_LOG( OPERATION, ERR,
1913 "null_callback : error code 0x%x\n",
1916 Debug( LDAP_DEBUG_ANY,
1917 "null_callback : error code 0x%x\n",
1921 return LDAP_SUCCESS;
1925 slap_create_syncrepl_entry(
1927 struct berval *context_csn,
1935 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1937 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1939 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1942 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1944 if ( context_csn ) {
1945 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1946 context_csn, NULL );
1950 bv.bv_len = sizeof("{}")-1;
1951 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1953 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1954 ber_dupbv( &e->e_nname, &e->e_name );
1960 slap_uuidstr_from_normalized(
1961 struct berval* uuidstr,
1962 struct berval* normalized,
1966 unsigned char nibble;
1969 if ( normalized == NULL )
1972 if ( normalized->bv_len != 16 ) {
1979 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1984 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1986 sl_free( new, ctx );
1990 for ( i = 0; i < 16; i++ ) {
1991 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1992 new->bv_val[(i<<1)+d] = '-';
1996 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1997 if ( nibble < 10 ) {
1998 new->bv_val[(i<<1)+d] = nibble + '0';
2000 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2003 nibble = (normalized->bv_val[i]) & 0xF;
2004 if ( nibble < 10 ) {
2005 new->bv_val[(i<<1)+d+1] = nibble + '0';
2007 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2011 new->bv_val[new->bv_len] = '\0';
2017 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2019 const struct berval *uuid1 = v_uuid1;
2020 const struct berval *uuid2 = v_uuid2;
2021 int rc = uuid1->bv_len - uuid2->bv_len;
2022 if ( rc ) return rc;
2023 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2027 avl_ber_bvfree( void *bv )
2032 if ( ((struct berval *)bv)->bv_val != NULL ) {
2033 ch_free ( ((struct berval *)bv)->bv_val );
2035 ch_free ( (char *) bv );