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 ) {
133 ber_printf( ber, "{eO}", abs(si->si_type),
134 &si->si_syncCookie.octet_str[0] );
136 ber_printf( ber, "{e}", abs(si->si_type) );
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 );
176 int cmdline_cookie_found = 0;
178 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
179 struct berval syncrepl_cn_bv;
180 struct sync_cookie *sc = NULL;
181 struct sync_cookie syncCookie = { NULL, -1, NULL };
187 psub = &si->si_be->be_nsuffix[0];
189 /* Init connection to master */
191 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
192 if ( rc != LDAP_SUCCESS ) {
194 LDAP_LOG( OPERATION, ERR,
195 "do_syncrep1: ldap_initialize failed (%s)\n",
196 si->si_provideruri, 0, 0 );
198 Debug( LDAP_DEBUG_ANY,
199 "do_syncrep1: ldap_initialize failed (%s)\n",
200 si->si_provideruri, 0, 0 );
205 op->o_protocol = LDAP_VERSION3;
206 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
211 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
212 if( rc != LDAP_SUCCESS ) {
214 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
215 "%s: ldap_start_tls failed (%d)\n",
216 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
219 Debug( LDAP_DEBUG_ANY,
220 "%s: ldap_start_tls failed (%d)\n",
221 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
224 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
228 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
229 #ifdef HAVE_CYRUS_SASL
232 if ( si->si_secprops != NULL ) {
233 rc = ldap_set_option( si->si_ld,
234 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
236 if( rc != LDAP_OPT_SUCCESS ) {
238 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
239 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
240 si->si_provideruri, si->si_secprops, 0 );
242 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
243 "(%s,SECPROPS,\"%s\") failed!\n",
244 si->si_provideruri, si->si_secprops, 0 );
250 defaults = lutil_sasl_defaults( si->si_ld,
251 si->si_saslmech, si->si_realm,
252 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 */
291 fprintf( stderr, "not compiled with SASL support\n" );
296 rc = ldap_bind_s( si->si_ld,
297 si->si_binddn, si->si_passwd, si->si_bindmethod );
298 if ( rc != LDAP_SUCCESS ) {
300 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
301 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
303 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
304 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
310 /* Set SSF to strongest of TLS, SASL SSFs */
313 op->o_transport_ssf = 0;
315 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
316 == LDAP_SUCCESS && ssl != NULL )
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_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 */
327 assert( si->si_rid < 1000 );
328 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
329 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
330 CN_STR "syncrepl%ld", si->si_rid );
331 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
332 op->o_req_dn = op->o_req_ndn;
334 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
335 if ( si->si_rid == sc->rid ) {
336 cmdline_cookie_found = 1;
341 if ( cmdline_cookie_found ) {
342 /* cookie is supplied in the command line */
343 BerVarray cookie = NULL;
344 struct berval cookie_bv;
346 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
347 slap_sync_cookie_free( &si->si_syncCookie, 0 );
349 /* read stored cookie if it exists */
350 backend_attribute( op, NULL, &op->o_req_ndn,
351 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
354 /* no stored cookie */
355 if ( sc->ctxcsn == NULL ||
356 sc->ctxcsn->bv_val == NULL ) {
357 /* if cmdline cookie does not have ctxcsn */
358 /* component, set it to an initial value */
359 slap_init_sync_cookie_ctxcsn( sc );
361 slap_dup_sync_cookie( &si->si_syncCookie, sc );
362 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 );
398 /* no command line cookie is specified */
399 if ( si->si_syncCookie.octet_str == NULL ) {
400 BerVarray cookie = NULL;
401 struct berval cookie_bv;
402 /* try to read stored cookie */
403 backend_attribute( op, NULL, &op->o_req_ndn,
404 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
406 ber_dupbv( &cookie_bv, &cookie[0] );
407 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
408 slap_parse_sync_cookie( &si->si_syncCookie );
409 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
414 rc = ldap_sync_search( si, op->o_tmpmemctx );
416 if( rc != LDAP_SUCCESS ) {
418 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
419 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
421 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
422 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
429 ldap_unbind( si->si_ld );
442 LDAPControl **rctrls = NULL;
445 BerElementBuffer berbuf;
446 BerElement *ber = (BerElement *)&berbuf;
448 LDAPMessage *res = NULL;
449 LDAPMessage *msg = NULL;
452 struct berval *retdata = NULL;
457 struct berval syncUUID = BER_BVNULL;
458 struct sync_cookie syncCookie = { NULL, -1, NULL };
459 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
460 struct berval cookie = BER_BVNULL;
468 Modifications *modlist = NULL;
473 struct timeval *tout_p = NULL;
474 struct timeval tout = { 0, 0 };
476 int refreshDeletes = 0;
478 BerVarray syncUUIDs = NULL;
481 if ( slapd_shutdown ) {
486 ber_init2( ber, NULL, LBER_USE_DER );
487 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
490 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
492 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
495 psub = &si->si_be->be_nsuffix[0];
497 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
499 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
505 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
508 if ( slapd_shutdown ) {
512 for( msg = ldap_first_message( si->si_ld, res );
514 msg = ldap_next_message( si->si_ld, msg ) )
516 switch( ldap_msgtype( msg ) ) {
517 case LDAP_RES_SEARCH_ENTRY:
518 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
519 /* we can't work without the control */
525 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
526 ber_scanf( ber, "{em", &syncstate, &syncUUID );
527 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
528 ber_scanf( ber, "m}", &cookie );
529 if ( cookie.bv_val ) {
530 struct berval tmp_bv;
531 ber_dupbv( &tmp_bv, &cookie );
532 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
534 if ( syncCookie.octet_str &&
535 syncCookie.octet_str[0].bv_val )
536 slap_parse_sync_cookie( &syncCookie );
538 if ( syncrepl_message_to_entry( si, op, msg,
539 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
540 rc_efree = syncrepl_entry( si, op, entry, modlist,
541 syncstate, &syncUUID, &syncCookie_req );
542 if ( syncCookie.octet_str &&
543 syncCookie.octet_str[0].bv_val ) {
544 syncrepl_updateCookie( si, op, psub, &syncCookie );
547 ldap_controls_free( rctrls );
549 slap_mods_free( modlist );
551 if ( rc_efree && entry ) {
557 case LDAP_RES_SEARCH_REFERENCE:
559 LDAP_LOG( OPERATION, ERR,
560 "do_syncrep2 : reference received\n", 0, 0, 0 );
562 Debug( LDAP_DEBUG_ANY,
563 "do_syncrep2 : reference received\n", 0, 0, 0 );
567 case LDAP_RES_SEARCH_RESULT:
568 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
572 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
574 ber_scanf( ber, "{" /*"}"*/);
575 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
577 ber_scanf( ber, "m", &cookie );
578 if ( cookie.bv_val ) {
579 struct berval tmp_bv;
580 ber_dupbv( &tmp_bv, &cookie );
581 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
583 if ( syncCookie.octet_str &&
584 syncCookie.octet_str[0].bv_val )
585 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], &text );
603 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
604 && match < 0 && err == LDAP_SUCCESS ) {
605 syncrepl_updateCookie( si, op, psub, &syncCookie );
608 ldap_controls_free( rctrls );
610 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
611 /* FIXME : different error behaviors according to
612 * 1) err code : LDAP_BUSY ...
613 * 2) on err policy : stop service, stop sync, retry
615 if ( refreshDeletes == 0 && match < 0 &&
616 err == LDAP_SUCCESS ) {
617 syncrepl_del_nonpresent( op, si );
619 avl_free( si->si_presentlist, avl_ber_bvfree );
620 si->si_presentlist = NULL;
627 case LDAP_RES_INTERMEDIATE:
628 rc = ldap_parse_intermediate( si->si_ld, msg,
629 &retoid, &retdata, NULL, 0 );
630 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
631 int si_refreshDelete = 0;
632 int si_refreshPresent = 0;
633 ber_init2( ber, retdata, LBER_USE_DER );
635 switch ( si_tag = ber_peek_tag( ber, &len )) {
637 case LDAP_TAG_SYNC_NEW_COOKIE:
638 ber_scanf( ber, "tm", &tag, &cookie );
640 case LDAP_TAG_SYNC_REFRESH_DELETE:
641 si_refreshDelete = 1;
642 case LDAP_TAG_SYNC_REFRESH_PRESENT:
643 si_refreshPresent = 1;
644 ber_scanf( ber, "t{", &tag );
645 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
647 ber_scanf( ber, "m", &cookie );
648 if ( cookie.bv_val ) {
649 struct berval tmp_bv;
650 ber_dupbv( &tmp_bv, &cookie );
651 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
653 if ( syncCookie.octet_str &&
654 syncCookie.octet_str[0].bv_val )
655 slap_parse_sync_cookie( &syncCookie );
657 if ( ber_peek_tag( ber, &len ) ==
658 LDAP_TAG_REFRESHDONE )
660 ber_scanf( ber, "b", &refreshDone );
662 ber_scanf( ber, "}" );
664 case LDAP_TAG_SYNC_ID_SET:
665 ber_scanf( ber, "t{", &tag );
666 if ( ber_peek_tag( ber, &len ) ==
667 LDAP_TAG_SYNC_COOKIE ) {
668 ber_scanf( ber, "m", &cookie );
669 if ( cookie.bv_val ) {
670 struct berval tmp_bv;
671 ber_dupbv( &tmp_bv, &cookie );
672 ber_bvarray_add( &syncCookie.octet_str,
675 if ( syncCookie.octet_str &&
676 syncCookie.octet_str[0].bv_val )
677 slap_parse_sync_cookie( &syncCookie );
679 if ( ber_peek_tag( ber, &len ) ==
680 LDAP_TAG_REFRESHDELETES )
682 ber_scanf( ber, "b", &refreshDeletes );
684 ber_scanf( ber, "[W]", &syncUUIDs );
685 ber_scanf( ber, "}" );
686 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
687 struct berval *syncuuid_bv;
688 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
689 avl_insert( &si->si_presentlist,
690 (caddr_t) syncuuid_bv,
691 syncuuid_cmp, avl_dup_error );
693 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
697 LDAP_LOG( OPERATION, ERR,
698 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
699 (long) si_tag, 0, 0 );
701 Debug( LDAP_DEBUG_ANY,
702 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
703 (long) si_tag, 0, 0 );
705 ldap_memfree( retoid );
706 ber_bvfree( retdata );
710 if ( syncCookie_req.ctxcsn == NULL ) {
712 } else if ( syncCookie.ctxcsn == NULL ) {
715 value_match( &match, slap_schema.si_ad_entryCSN,
716 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
717 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
718 &syncCookie_req.ctxcsn[0],
719 &syncCookie.ctxcsn[0], &text );
722 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
724 syncrepl_updateCookie( si, op, psub, &syncCookie);
727 if ( si_refreshPresent == 1 ) {
729 syncrepl_del_nonpresent( op, si );
733 ldap_memfree( retoid );
734 ber_bvfree( retdata );
738 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
739 " unknown intermediate "
740 "response\n", 0, 0, 0 );
742 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
743 "unknown intermediate response (%d)\n",
746 ldap_memfree( retoid );
747 ber_bvfree( retdata );
753 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
754 "unknown message\n", 0, 0, 0 );
756 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
757 "unknown message\n", 0, 0, 0 );
762 if ( syncCookie.octet_str ) {
763 slap_sync_cookie_free( &syncCookie_req, 0 );
764 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
765 slap_sync_cookie_free( &syncCookie, 0 );
775 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
776 errstr = ldap_err2string( rc );
779 LDAP_LOG( OPERATION, ERR,
780 "do_syncrep2 : %s\n", errstr, 0, 0 );
782 Debug( LDAP_DEBUG_ANY,
783 "do_syncrep2 : %s\n", errstr, 0, 0 );
788 slap_sync_cookie_free( &syncCookie, 0 );
789 slap_sync_cookie_free( &syncCookie_req, 0 );
791 if ( res ) ldap_msgfree( res );
793 if ( rc && si->si_ld ) {
794 ldap_unbind( si->si_ld );
806 struct re_s* rtask = arg;
807 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
808 Connection conn = {0};
810 int rc = LDAP_SUCCESS;
817 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
819 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
825 switch( abs( si->si_type )) {
826 case LDAP_SYNC_REFRESH_ONLY:
827 case LDAP_SYNC_REFRESH_AND_PERSIST:
833 if ( slapd_shutdown && si->si_ld ) {
834 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
835 connection_client_stop( s );
836 ldap_unbind( si->si_ld );
841 connection_fake_init( &conn, &op, ctx );
843 /* use global malloc for now */
844 op.o_tmpmemctx = NULL;
845 op.o_tmpmfuncs = &ch_mfuncs;
847 op.o_dn = si->si_updatedn;
848 op.o_ndn = si->si_updatedn;
849 op.o_managedsait = 1;
852 op.o_sync_state.ctxcsn = NULL;
853 op.o_sync_state.sid = -1;
854 op.o_sync_state.octet_str = NULL;
855 op.o_sync_slog_size = -1;
856 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
857 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
859 /* Establish session, do search */
862 rc = do_syncrep1( &op, si );
865 /* Process results */
866 if ( rc == LDAP_SUCCESS ) {
867 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
869 rc = do_syncrep2( &op, si );
871 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
872 /* If we succeeded, enable the connection for further listening.
873 * If we failed, tear down the connection and reschedule.
875 if ( rc == LDAP_SUCCESS ) {
877 rc = connection_client_setup( s, do_syncrepl,
880 connection_client_enable( s );
882 } else if ( !first ) {
886 if ( rc == -2 ) rc = 0;
890 /* At this point, we have 4 cases:
891 * 1) for any hard failure, give up and remove this task
892 * 2) for ServerDown, reschedule this task to run
893 * 3) for Refresh and Success, reschedule to run
894 * 4) for Persist and Success, reschedule to defer
896 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
898 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
899 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
903 connection_client_stop( s );
906 if ( rc == LDAP_SUCCESS ) {
907 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
910 rtask->interval.tv_sec = si->si_interval;
911 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
912 if ( si->si_retrynum ) {
913 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
914 si->si_retrynum[i] = si->si_retrynum_init[i];
916 si->si_retrynum[i] = -2;
919 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
920 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
924 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
925 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
926 } else if ( si->si_retrynum[i] >= -1 ) {
927 if ( si->si_retrynum[i] > 0 )
928 si->si_retrynum[i]--;
929 rtask->interval.tv_sec = si->si_retryinterval[i];
930 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
931 slap_wake_listener();
935 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
941 syncrepl_message_to_entry(
945 Modifications **modlist,
951 BerElement *ber = NULL;
954 Modifications **modtail = modlist;
957 char txtbuf[SLAP_TEXT_BUFLEN];
958 size_t textlen = sizeof txtbuf;
960 struct berval bdn = {0, NULL}, dn, ndn;
965 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
967 LDAP_LOG( OPERATION, ERR,
968 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
970 Debug( LDAP_DEBUG_ANY,
971 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
976 op->o_tag = LDAP_REQ_ADD;
978 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
980 if ( rc != LDAP_SUCCESS ) {
982 LDAP_LOG( OPERATION, ERR,
983 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
985 Debug( LDAP_DEBUG_ANY,
986 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
991 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
992 ber_dupbv( &op->o_req_dn, &dn );
993 ber_dupbv( &op->o_req_ndn, &ndn );
994 sl_free( ndn.bv_val, op->o_tmpmemctx );
995 sl_free( dn.bv_val, op->o_tmpmemctx );
997 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1000 return LDAP_SUCCESS;
1003 if ( entry == NULL ) {
1007 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1009 e->e_name = op->o_req_dn;
1010 e->e_nname = op->o_req_ndn;
1012 while ( ber_remaining( ber ) ) {
1013 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1014 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1019 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1021 mod->sml_op = LDAP_MOD_REPLACE;
1022 mod->sml_next = NULL;
1023 mod->sml_desc = NULL;
1024 mod->sml_type = tmp.sml_type;
1025 mod->sml_values = tmp.sml_values;
1026 mod->sml_nvalues = NULL;
1029 modtail = &mod->sml_next;
1032 if ( *modlist == NULL ) {
1034 LDAP_LOG( OPERATION, ERR,
1035 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1037 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1044 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1046 if ( rc != LDAP_SUCCESS ) {
1048 LDAP_LOG( OPERATION, ERR,
1049 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1051 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1057 /* Strip out dynamically generated attrs */
1058 for ( modtail = modlist; *modtail ; ) {
1060 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1061 *modtail = mod->sml_next;
1062 slap_mod_free( &mod->sml_mod, 0 );
1065 modtail = &mod->sml_next;
1069 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1070 if( rc != LDAP_SUCCESS ) {
1072 LDAP_LOG( OPERATION, ERR,
1073 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1075 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1081 ber_free ( ber, 0 );
1082 if ( rc != LDAP_SUCCESS ) {
1097 Modifications* modlist,
1099 struct berval* syncUUID,
1100 struct sync_cookie* syncCookie_req
1103 Backend *be = op->o_bd;
1104 slap_callback cb = { NULL };
1105 struct berval *syncuuid_bv = NULL;
1106 struct berval syncUUID_strrep = BER_BVNULL;
1107 struct berval uuid_bv = BER_BVNULL;
1109 SlapReply rs_search = {REP_RESULT};
1110 SlapReply rs_delete = {REP_RESULT};
1111 SlapReply rs_add = {REP_RESULT};
1112 SlapReply rs_modify = {REP_RESULT};
1114 AttributeAssertion ava = {0};
1115 int rc = LDAP_SUCCESS;
1116 int ret = LDAP_SUCCESS;
1119 struct berval pdn = BER_BVNULL;
1120 struct berval org_req_dn = BER_BVNULL;
1121 struct berval org_req_ndn = BER_BVNULL;
1122 struct berval org_dn = BER_BVNULL;
1123 struct berval org_ndn = BER_BVNULL;
1124 int org_managedsait;
1126 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 ) {
1135 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1136 if ( entry == NULL ) {
1141 f.f_choice = LDAP_FILTER_EQUALITY;
1143 ava.aa_desc = slap_schema.si_ad_entryUUID;
1144 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1145 ava.aa_value = *syncUUID;
1146 op->ors_filter = &f;
1148 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1149 op->ors_filterstr.bv_val = (char *) sl_malloc(
1150 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1151 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1152 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1153 syncUUID->bv_val, syncUUID->bv_len );
1154 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1156 op->o_tag = LDAP_REQ_SEARCH;
1157 op->ors_scope = LDAP_SCOPE_SUBTREE;
1159 /* get syncrepl cookie of shadow replica from subentry */
1160 op->o_req_dn = si->si_base;
1161 op->o_req_ndn = si->si_base;
1163 op->o_time = slap_get_time();
1164 op->ors_tlimit = SLAP_NO_LIMIT;
1167 /* set callback function */
1168 op->o_callback = &cb;
1169 cb.sc_response = dn_callback;
1172 si->si_syncUUID_ndn.bv_val = NULL;
1174 if ( limits_check( op, &rs_search ) == 0 ) {
1175 rc = be->be_search( op, &rs_search );
1178 if ( op->ors_filterstr.bv_val ) {
1179 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1182 cb.sc_response = null_callback;
1185 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1188 if ( syncstate != LDAP_SYNC_DELETE ) {
1189 op->o_no_psearch = 1;
1192 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1193 if ( op->o_sync_csn.bv_val ) {
1194 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1199 op->o_req_dn = si->si_syncUUID_ndn;
1200 op->o_req_ndn = si->si_syncUUID_ndn;
1201 op->o_tag = LDAP_REQ_DELETE;
1202 rc = be->be_delete( op, &rs_delete );
1204 org_req_dn = op->o_req_dn;
1205 org_req_ndn = op->o_req_ndn;
1207 org_ndn = op->o_ndn;
1208 org_managedsait = get_manageDSAit( op );
1209 op->o_dn = op->o_bd->be_rootdn;
1210 op->o_ndn = op->o_bd->be_rootndn;
1211 op->o_managedsait = 1;
1213 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1214 op->o_delete_glue_parent = 0;
1215 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1216 slap_callback cb = { NULL };
1217 cb.sc_response = slap_null_cb;
1218 dnParent( &op->o_req_ndn, &pdn );
1220 op->o_req_ndn = pdn;
1221 op->o_callback = &cb;
1222 op->o_bd->be_delete( op, &rs_delete );
1228 op->o_managedsait = org_managedsait;
1230 op->o_ndn = org_ndn;
1231 op->o_req_dn = org_req_dn;
1232 op->o_req_ndn = org_req_ndn;
1233 op->o_delete_glue_parent = 0;
1235 op->o_no_psearch = 0;
1238 switch ( syncstate ) {
1240 case LDAP_SYNC_MODIFY:
1241 if ( rs_search.sr_err == LDAP_SUCCESS ||
1242 rs_search.sr_err == LDAP_REFERRAL ||
1243 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1244 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1246 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1247 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1248 &syncUUID_strrep, syncUUID );
1250 op->o_tag = LDAP_REQ_ADD;
1252 op->o_req_dn = entry->e_name;
1253 op->o_req_ndn = entry->e_nname;
1255 rc = be->be_add( op, &rs_add );
1257 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1258 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1259 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1261 Modifications *modtail = modlist;
1265 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1269 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1270 ber_dupbv( &uuid_bv, syncUUID );
1271 mod->sml_op = LDAP_MOD_REPLACE;
1272 mod->sml_desc = slap_schema.si_ad_entryUUID;
1273 mod->sml_type = mod->sml_desc->ad_cname;
1274 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1275 modtail->sml_next = mod;
1277 op->o_tag = LDAP_REQ_MODIFY;
1278 op->orm_modlist = modlist;
1279 op->o_req_dn = entry->e_name;
1280 op->o_req_ndn = entry->e_nname;
1282 rc = be->be_modify( op, &rs_modify );
1283 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1285 LDAP_LOG( OPERATION, ERR,
1286 "syncrepl_entry : be_modify failed (%d)\n",
1287 rs_modify.sr_err, 0, 0 );
1289 Debug( LDAP_DEBUG_ANY,
1290 "syncrepl_entry : be_modify failed (%d)\n",
1291 rs_modify.sr_err, 0, 0 );
1296 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1297 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1298 syncrepl_add_glue( op, entry );
1303 LDAP_LOG( OPERATION, ERR,
1304 "syncrepl_entry : be_add failed (%d)\n",
1305 rs_add.sr_err, 0, 0 );
1307 Debug( LDAP_DEBUG_ANY,
1308 "syncrepl_entry : be_add failed (%d)\n",
1309 rs_add.sr_err, 0, 0 );
1315 be_entry_release_w( op, entry );
1321 LDAP_LOG( OPERATION, ERR,
1322 "syncrepl_entry : be_search failed (%d)\n",
1323 rs_search.sr_err, 0, 0 );
1325 Debug( LDAP_DEBUG_ANY,
1326 "syncrepl_entry : be_search failed (%d)\n",
1327 rs_search.sr_err, 0, 0 );
1333 case LDAP_SYNC_DELETE :
1334 /* Already deleted */
1340 LDAP_LOG( OPERATION, ERR,
1341 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1343 Debug( LDAP_DEBUG_ANY,
1344 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1352 if ( syncUUID_strrep.bv_val ) {
1353 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1355 if ( si->si_syncUUID_ndn.bv_val ) {
1356 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1361 static struct berval gcbva[] = {
1368 syncrepl_del_nonpresent(
1373 Backend* be = op->o_bd;
1374 slap_callback cb = { NULL };
1375 SlapReply rs_search = {REP_RESULT};
1376 SlapReply rs_delete = {REP_RESULT};
1377 SlapReply rs_modify = {REP_RESULT};
1378 struct nonpresent_entry *np_list, *np_prev;
1381 Modifications *mlnext;
1383 Modifications *modlist = NULL;
1384 Modifications **modtail = &modlist;
1387 struct berval pdn = BER_BVNULL;
1388 struct berval org_req_dn = BER_BVNULL;
1389 struct berval org_req_ndn = BER_BVNULL;
1390 struct berval org_dn = BER_BVNULL;
1391 struct berval org_ndn = BER_BVNULL;
1392 int org_managedsait;
1394 op->o_req_dn = si->si_base;
1395 op->o_req_ndn = si->si_base;
1397 cb.sc_response = nonpresent_callback;
1400 op->o_callback = &cb;
1401 op->o_tag = LDAP_REQ_SEARCH;
1402 op->ors_scope = si->si_scope;
1403 op->ors_deref = LDAP_DEREF_NEVER;
1404 op->o_time = slap_get_time();
1405 op->ors_tlimit = SLAP_NO_LIMIT;
1406 op->ors_slimit = SLAP_NO_LIMIT;
1407 op->ors_attrsonly = 0;
1408 op->ors_attrs = NULL;
1409 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1410 op->ors_filterstr = si->si_filterstr;
1412 op->o_nocaching = 1;
1413 op->o_managedsait = 0;
1415 if ( limits_check( op, &rs_search ) == 0 ) {
1416 rc = be->be_search( op, &rs_search );
1419 op->o_managedsait = 1;
1420 op->o_nocaching = 0;
1422 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1424 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1425 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1426 while ( np_list != NULL ) {
1427 LDAP_LIST_REMOVE( np_list, npe_link );
1429 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1430 op->o_tag = LDAP_REQ_DELETE;
1431 op->o_callback = &cb;
1432 cb.sc_response = null_callback;
1434 op->o_req_dn = *np_prev->npe_name;
1435 op->o_req_ndn = *np_prev->npe_nname;
1436 rc = op->o_bd->be_delete( op, &rs_delete );
1438 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1439 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1440 mod->sml_op = LDAP_MOD_REPLACE;
1441 mod->sml_desc = slap_schema.si_ad_objectClass;
1442 mod->sml_type = mod->sml_desc->ad_cname;
1443 mod->sml_values = &gcbva[0];
1445 modtail = &mod->sml_next;
1447 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1448 mod->sml_op = LDAP_MOD_REPLACE;
1449 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1450 mod->sml_type = mod->sml_desc->ad_cname;
1451 mod->sml_values = &gcbva[1];
1453 modtail = &mod->sml_next;
1455 op->o_tag = LDAP_REQ_MODIFY;
1456 op->orm_modlist = modlist;
1458 rc = be->be_modify( op, &rs_modify );
1460 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1461 mlnext = ml->sml_next;
1466 org_req_dn = op->o_req_dn;
1467 org_req_ndn = op->o_req_ndn;
1469 org_ndn = op->o_ndn;
1470 org_managedsait = get_manageDSAit( op );
1471 op->o_dn = op->o_bd->be_rootdn;
1472 op->o_ndn = op->o_bd->be_rootndn;
1473 op->o_managedsait = 1;
1475 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1476 op->o_delete_glue_parent ) {
1477 op->o_delete_glue_parent = 0;
1478 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1479 slap_callback cb = { NULL };
1480 cb.sc_response = slap_null_cb;
1481 dnParent( &op->o_req_ndn, &pdn );
1483 op->o_req_ndn = pdn;
1484 op->o_callback = &cb;
1485 /* give it a root privil ? */
1486 op->o_bd->be_delete( op, &rs_delete );
1492 op->o_managedsait = org_managedsait;
1494 op->o_ndn = org_ndn;
1495 op->o_req_dn = org_req_dn;
1496 op->o_req_ndn = org_req_ndn;
1497 op->o_delete_glue_parent = 0;
1499 ber_bvfree( np_prev->npe_name );
1500 ber_bvfree( np_prev->npe_nname );
1501 op->o_req_dn.bv_val = NULL;
1502 op->o_req_ndn.bv_val = NULL;
1517 Backend *be = op->o_bd;
1518 slap_callback cb = { NULL };
1523 struct berval dn = {0, NULL};
1524 struct berval ndn = {0, NULL};
1526 SlapReply rs_add = {REP_RESULT};
1529 op->o_tag = LDAP_REQ_ADD;
1530 op->o_callback = &cb;
1531 cb.sc_response = null_callback;
1532 cb.sc_private = NULL;
1537 /* count RDNs in suffix */
1538 if ( be->be_nsuffix[0].bv_len ) {
1539 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1549 /* Start with BE suffix */
1550 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1551 comma = strrchr(dn.bv_val, ',');
1552 if ( ptr ) *ptr = ',';
1553 if ( comma ) *comma = '\0';
1558 dn.bv_len -= ptr - dn.bv_val;
1561 /* the normalizedDNs are always the same length, no counting
1564 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1565 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1566 ndn.bv_len = be->be_nsuffix[0].bv_len;
1569 while ( ndn.bv_val > e->e_nname.bv_val ) {
1570 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1571 ber_dupbv( &glue->e_name, &dn );
1572 ber_dupbv( &glue->e_nname, &ndn );
1574 a = ch_calloc( 1, sizeof( Attribute ));
1575 a->a_desc = slap_schema.si_ad_objectClass;
1577 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1578 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1579 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1580 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1582 a->a_nvals = a->a_vals;
1584 a->a_next = glue->e_attrs;
1587 a = ch_calloc( 1, sizeof( Attribute ));
1588 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1590 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1591 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1592 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1594 a->a_nvals = a->a_vals;
1596 a->a_next = glue->e_attrs;
1599 op->o_req_dn = glue->e_name;
1600 op->o_req_ndn = glue->e_nname;
1602 rc = be->be_add ( op, &rs_add );
1603 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1604 be_entry_release_w( op, glue );
1606 /* incl. ALREADY EXIST */
1610 /* Move to next child */
1611 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1614 if ( ptr == e->e_name.bv_val ) break;
1616 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1617 for( ptr = ndn.bv_val-2;
1618 ptr > e->e_nname.bv_val && *ptr != ',';
1624 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1627 op->o_req_dn = e->e_name;
1628 op->o_req_ndn = e->e_nname;
1630 rc = be->be_add ( op, &rs_add );
1631 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1632 be_entry_release_w( op, e );
1640 static struct berval ocbva[] = {
1642 BER_BVC("subentry"),
1643 BER_BVC("syncConsumerSubentry"),
1647 static struct berval cnbva[] = {
1652 static struct berval ssbva[] = {
1657 static struct berval scbva[] = {
1663 syncrepl_updateCookie(
1667 struct sync_cookie *syncCookie
1670 Backend *be = op->o_bd;
1672 Modifications *mlnext;
1674 Modifications *modlist = NULL;
1675 Modifications **modtail = &modlist;
1678 char txtbuf[SLAP_TEXT_BUFLEN];
1679 size_t textlen = sizeof txtbuf;
1684 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1685 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1686 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1688 slap_callback cb = { NULL };
1689 SlapReply rs_add = {REP_RESULT};
1690 SlapReply rs_modify = {REP_RESULT};
1692 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1693 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1695 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1696 mod->sml_op = LDAP_MOD_REPLACE;
1697 mod->sml_desc = slap_schema.si_ad_objectClass;
1698 mod->sml_type = mod->sml_desc->ad_cname;
1699 mod->sml_values = ocbva;
1701 modtail = &mod->sml_next;
1703 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1704 assert( si->si_rid < 1000 );
1705 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1706 slap_syncrepl_bvc.bv_len + 1,
1707 "syncrepl%ld", si->si_rid );
1708 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1709 mod->sml_op = LDAP_MOD_REPLACE;
1710 mod->sml_desc = slap_schema.si_ad_cn;
1711 mod->sml_type = mod->sml_desc->ad_cname;
1712 mod->sml_values = cnbva;
1714 modtail = &mod->sml_next;
1716 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1717 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1718 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1719 mod->sml_op = LDAP_MOD_REPLACE;
1720 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1721 mod->sml_type = mod->sml_desc->ad_cname;
1722 mod->sml_values = scbva;
1724 modtail = &mod->sml_next;
1726 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1727 mod->sml_op = LDAP_MOD_REPLACE;
1728 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1729 mod->sml_type = mod->sml_desc->ad_cname;
1730 mod->sml_values = ssbva;
1732 modtail = &mod->sml_next;
1736 op->o_tag = LDAP_REQ_ADD;
1737 rc = slap_mods_opattrs( op, modlist, modtail,
1738 &text,txtbuf, textlen );
1740 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1741 ml->sml_op = LDAP_MOD_REPLACE;
1744 if( rc != LDAP_SUCCESS ) {
1746 LDAP_LOG( OPERATION, ERR,
1747 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1749 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1754 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1756 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1757 assert( si->si_rid < 1000 );
1758 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1759 slap_syncrepl_cn_bvc.bv_len + 1,
1760 "cn=syncrepl%ld", si->si_rid );
1762 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1764 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1765 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1767 if ( slap_syncrepl_dn_bv.bv_val ) {
1768 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1773 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1775 if( rc != LDAP_SUCCESS ) {
1777 LDAP_LOG( OPERATION, ERR,
1778 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1780 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1785 cb.sc_response = null_callback;
1788 op->o_callback = &cb;
1789 op->o_req_dn = e->e_name;
1790 op->o_req_ndn = e->e_nname;
1792 /* update persistent cookie */
1793 update_cookie_retry:
1794 op->o_tag = LDAP_REQ_MODIFY;
1795 op->orm_modlist = modlist;
1796 rc = be->be_modify( op, &rs_modify );
1798 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1799 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1800 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1801 op->o_tag = LDAP_REQ_ADD;
1803 rc = be->be_add( op, &rs_add );
1804 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1805 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1806 goto update_cookie_retry;
1807 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1808 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1810 LDAP_LOG( OPERATION, ERR,
1811 "cookie will be non-persistent\n",
1814 Debug( LDAP_DEBUG_ANY,
1815 "cookie will be non-persistent\n",
1820 LDAP_LOG( OPERATION, ERR,
1821 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1823 Debug( LDAP_DEBUG_ANY,
1824 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1828 be_entry_release_w( op, e );
1833 LDAP_LOG( OPERATION, ERR,
1834 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1836 Debug( LDAP_DEBUG_ANY,
1837 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1848 if ( cnbva[0].bv_val ) {
1849 ch_free( cnbva[0].bv_val );
1850 cnbva[0].bv_val = NULL;
1852 if ( scbva[0].bv_val ) {
1853 ch_free( scbva[0].bv_val );
1854 scbva[0].bv_val = NULL;
1857 if ( mlnext->sml_next ) {
1858 slap_mods_free( mlnext->sml_next );
1859 mlnext->sml_next = NULL;
1862 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1863 mlnext = ml->sml_next;
1871 syncrepl_isupdate( Operation *op )
1873 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1877 syncrepl_isupdate_dn(
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 ) ) ) {
1901 syncinfo_t *si = op->o_callback->sc_private;
1903 if ( rs->sr_type == REP_SEARCH ) {
1904 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1906 LDAP_LOG( OPERATION, ERR,
1907 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1909 Debug( LDAP_DEBUG_ANY,
1910 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1913 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1915 } else if ( rs->sr_type == REP_RESULT ) {
1916 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1918 LDAP_LOG( OPERATION, ERR,
1919 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1921 Debug( LDAP_DEBUG_ANY,
1922 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1927 return LDAP_SUCCESS;
1931 nonpresent_callback(
1936 syncinfo_t *si = op->o_callback->sc_private;
1939 struct berval* present_uuid = NULL;
1940 struct nonpresent_entry *np_entry;
1942 if ( rs->sr_type == REP_RESULT ) {
1943 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1944 si->si_presentlist = NULL;
1946 } else if ( rs->sr_type == REP_SEARCH ) {
1947 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1949 if ( a == NULL ) return 0;
1951 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1954 if ( present_uuid == NULL ) {
1955 np_entry = (struct nonpresent_entry *)
1956 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1957 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1958 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1959 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1962 avl_delete( &si->si_presentlist,
1963 &a->a_nvals[0], syncuuid_cmp );
1964 ch_free( present_uuid->bv_val );
1965 ch_free( present_uuid );
1968 return LDAP_SUCCESS;
1976 if ( rs->sr_err != LDAP_SUCCESS &&
1977 rs->sr_err != LDAP_REFERRAL &&
1978 rs->sr_err != LDAP_ALREADY_EXISTS &&
1979 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1980 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1983 LDAP_LOG( OPERATION, ERR,
1984 "null_callback : error code 0x%x\n",
1987 Debug( LDAP_DEBUG_ANY,
1988 "null_callback : error code 0x%x\n",
1992 return LDAP_SUCCESS;
1996 slap_create_syncrepl_entry(
1998 struct berval *context_csn,
2006 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2008 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2010 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2013 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2015 if ( context_csn ) {
2016 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2017 context_csn, NULL );
2021 bv.bv_len = sizeof("{}")-1;
2022 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2024 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2025 ber_dupbv( &e->e_nname, &e->e_name );
2031 slap_uuidstr_from_normalized(
2032 struct berval* uuidstr,
2033 struct berval* normalized,
2037 unsigned char nibble;
2040 if ( normalized == NULL )
2043 if ( normalized->bv_len != 16 ) {
2050 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
2055 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2057 sl_free( new, ctx );
2061 for ( i = 0; i < 16; i++ ) {
2062 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2063 new->bv_val[(i<<1)+d] = '-';
2067 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2068 if ( nibble < 10 ) {
2069 new->bv_val[(i<<1)+d] = nibble + '0';
2071 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2074 nibble = (normalized->bv_val[i]) & 0xF;
2075 if ( nibble < 10 ) {
2076 new->bv_val[(i<<1)+d+1] = nibble + '0';
2078 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2082 new->bv_val[new->bv_len] = '\0';
2088 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2090 const struct berval *uuid1 = v_uuid1;
2091 const struct berval *uuid2 = v_uuid2;
2092 int rc = uuid1->bv_len - uuid2->bv_len;
2093 if ( rc ) return rc;
2094 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2098 avl_ber_bvfree( void *bv )
2103 if ( ((struct berval *)bv)->bv_val != NULL ) {
2104 ch_free ( ((struct berval *)bv)->bv_val );
2106 ch_free ( (char *) bv );