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( "+" );
113 for ( n = 0; si->si_exattrs[ n ] != NULL; n++ ) /* empty */;
115 /* Delete Attributes from exattrs list */
116 for ( i = 0; sync_descs[i] != NULL; i++ ) {
117 for ( j = 0; si->si_exattrs[j] != NULL; j++ ) {
118 if ( strcmp( si->si_exattrs[j], sync_descs[i]->ad_cname.bv_val )
121 ch_free( si->si_exattrs[j] );
122 for ( k = j; si->si_exattrs[k] != NULL; k++ ) {
123 si->si_exattrs[k] = si->si_exattrs[k+1];
137 BerElementBuffer berbuf;
138 BerElement *ber = (BerElement *)&berbuf;
139 LDAPControl c[2], *ctrls[3];
140 struct timeval timeout;
144 /* setup LDAP SYNC control */
145 ber_init2( ber, NULL, LBER_USE_DER );
146 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
148 if ( si->si_syncCookie.octet_str &&
149 si->si_syncCookie.octet_str[0].bv_val ) {
150 ber_printf( ber, "{eO}", abs(si->si_type),
151 &si->si_syncCookie.octet_str[0] );
153 ber_printf( ber, "{e}", abs(si->si_type) );
156 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
161 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
162 c[0].ldctl_iscritical = si->si_type < 0;
165 if ( si->si_authzId ) {
166 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
167 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
168 c[1].ldctl_iscritical = 1;
175 timeout.tv_sec = si->si_tlimit;
178 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
179 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
180 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
181 si->si_slimit, &msgid );
193 int cmdline_cookie_found = 0;
195 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
196 struct berval syncrepl_cn_bv;
197 struct sync_cookie *sc = NULL;
198 struct sync_cookie syncCookie = { NULL, -1, NULL };
204 psub = &si->si_be->be_nsuffix[0];
206 /* Init connection to master */
208 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
209 if ( rc != LDAP_SUCCESS ) {
211 LDAP_LOG( OPERATION, ERR,
212 "do_syncrep1: ldap_initialize failed (%s)\n",
213 si->si_provideruri, 0, 0 );
215 Debug( LDAP_DEBUG_ANY,
216 "do_syncrep1: ldap_initialize failed (%s)\n",
217 si->si_provideruri, 0, 0 );
222 op->o_protocol = LDAP_VERSION3;
223 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
228 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
229 if( rc != LDAP_SUCCESS ) {
231 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
232 "%s: ldap_start_tls failed (%d)\n",
233 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
236 Debug( LDAP_DEBUG_ANY,
237 "%s: ldap_start_tls failed (%d)\n",
238 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
241 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
245 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
246 #ifdef HAVE_CYRUS_SASL
249 if ( si->si_secprops != NULL ) {
250 rc = ldap_set_option( si->si_ld,
251 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
253 if( rc != LDAP_OPT_SUCCESS ) {
255 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
256 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
257 si->si_provideruri, si->si_secprops, 0 );
259 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
260 "(%s,SECPROPS,\"%s\") failed!\n",
261 si->si_provideruri, si->si_secprops, 0 );
267 defaults = lutil_sasl_defaults( si->si_ld,
268 si->si_saslmech, si->si_realm,
269 si->si_authcId, si->si_passwd, si->si_authzId );
271 rc = ldap_sasl_interactive_bind_s( si->si_ld,
279 lutil_sasl_freedefs( defaults );
281 /* FIXME: different error behaviors according to
283 * 2) on err policy : exit, retry, backoff ...
285 if ( rc != LDAP_SUCCESS ) {
287 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
288 "ldap_sasl_interactive_bind_s failed (%d)\n",
291 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
292 "ldap_sasl_interactive_bind_s failed (%d)\n",
296 /* FIXME (see above comment) */
297 /* if Kerberos credentials cache is not active, retry */
298 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
299 rc == LDAP_LOCAL_ERROR )
301 rc = LDAP_SERVER_DOWN;
306 #else /* HAVE_CYRUS_SASL */
307 /* Should never get here, we trapped this at config time */
308 fprintf( stderr, "not compiled with SASL support\n" );
313 rc = ldap_bind_s( si->si_ld,
314 si->si_binddn, si->si_passwd, si->si_bindmethod );
315 if ( rc != LDAP_SUCCESS ) {
317 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
318 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
320 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
321 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
327 /* Set SSF to strongest of TLS, SASL SSFs */
330 op->o_transport_ssf = 0;
332 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
333 == LDAP_SUCCESS && ssl != NULL )
335 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
337 #endif /* HAVE_TLS */
338 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
339 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
340 ? op->o_sasl_ssf : op->o_tls_ssf;
342 /* get syncrepl cookie of shadow replica from subentry */
344 assert( si->si_rid < 1000 );
345 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
346 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
347 CN_STR "syncrepl%ld", si->si_rid );
348 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
349 op->o_req_dn = op->o_req_ndn;
351 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
352 if ( si->si_rid == sc->rid ) {
353 cmdline_cookie_found = 1;
358 if ( cmdline_cookie_found ) {
359 /* cookie is supplied in the command line */
360 BerVarray cookie = NULL;
361 struct berval cookie_bv;
363 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
364 slap_sync_cookie_free( &si->si_syncCookie, 0 );
366 /* read stored cookie if it exists */
367 backend_attribute( op, NULL, &op->o_req_ndn,
368 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
371 /* no stored cookie */
372 if ( sc->ctxcsn == NULL ||
373 sc->ctxcsn->bv_val == NULL ) {
374 /* if cmdline cookie does not have ctxcsn */
375 /* component, set it to an initial value */
376 slap_init_sync_cookie_ctxcsn( sc );
378 slap_dup_sync_cookie( &si->si_syncCookie, sc );
379 slap_sync_cookie_free( sc, 1 );
383 struct berval newcookie = BER_BVNULL;
384 ber_dupbv( &cookie_bv, &cookie[0] );
385 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
386 slap_parse_sync_cookie( &si->si_syncCookie );
387 ber_bvarray_free( si->si_syncCookie.octet_str );
388 si->si_syncCookie.octet_str = NULL;
389 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
390 if ( sc->sid != -1 ) {
391 /* command line cookie wins */
392 si->si_syncCookie.sid = sc->sid;
394 if ( sc->ctxcsn != NULL ) {
395 /* command line cookie wins */
396 if ( si->si_syncCookie.ctxcsn ) {
397 ber_bvarray_free( si->si_syncCookie.ctxcsn );
398 si->si_syncCookie.ctxcsn = NULL;
400 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
401 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
403 if ( sc->rid != -1 ) {
404 /* command line cookie wins */
405 si->si_syncCookie.rid = sc->rid;
407 slap_sync_cookie_free( sc, 1 );
409 slap_compose_sync_cookie( NULL, &newcookie,
410 &si->si_syncCookie.ctxcsn[0],
411 si->si_syncCookie.sid, si->si_syncCookie.rid );
412 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
415 /* no command line cookie is specified */
416 if ( si->si_syncCookie.octet_str == NULL ) {
417 BerVarray cookie = NULL;
418 struct berval cookie_bv;
419 /* try to read stored cookie */
420 backend_attribute( op, NULL, &op->o_req_ndn,
421 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
423 ber_dupbv( &cookie_bv, &cookie[0] );
424 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
425 slap_parse_sync_cookie( &si->si_syncCookie );
426 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
431 rc = ldap_sync_search( si, op->o_tmpmemctx );
433 if( rc != LDAP_SUCCESS ) {
435 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
436 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
438 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
439 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
446 ldap_unbind( si->si_ld );
451 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
461 LDAPControl **rctrls = NULL;
464 BerElementBuffer berbuf;
465 BerElement *ber = (BerElement *)&berbuf;
467 LDAPMessage *res = NULL;
468 LDAPMessage *msg = NULL;
471 struct berval *retdata = NULL;
476 struct berval syncUUID = BER_BVNULL;
477 struct sync_cookie syncCookie = { NULL, -1, NULL };
478 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
479 struct berval cookie = BER_BVNULL;
487 Modifications *modlist = NULL;
492 struct timeval *tout_p = NULL;
493 struct timeval tout = { 0, 0 };
495 int refreshDeletes = 0;
497 BerVarray syncUUIDs = NULL;
500 if ( slapd_shutdown ) {
505 ber_init2( ber, NULL, LBER_USE_DER );
506 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
509 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
514 psub = &si->si_be->be_nsuffix[0];
516 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
518 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
524 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
527 if ( slapd_shutdown ) {
531 for( msg = ldap_first_message( si->si_ld, res );
533 msg = ldap_next_message( si->si_ld, msg ) )
535 switch( ldap_msgtype( msg ) ) {
536 case LDAP_RES_SEARCH_ENTRY:
537 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
538 /* we can't work without the control */
544 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
545 ber_scanf( ber, "{em", &syncstate, &syncUUID );
546 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
547 ber_scanf( ber, "m}", &cookie );
548 if ( cookie.bv_val ) {
549 struct berval tmp_bv;
550 ber_dupbv( &tmp_bv, &cookie );
551 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
553 if ( syncCookie.octet_str &&
554 syncCookie.octet_str[0].bv_val )
555 slap_parse_sync_cookie( &syncCookie );
557 if ( syncrepl_message_to_entry( si, op, msg,
558 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
559 rc_efree = syncrepl_entry( si, op, entry, modlist,
560 syncstate, &syncUUID, &syncCookie_req );
561 if ( syncCookie.octet_str &&
562 syncCookie.octet_str[0].bv_val ) {
563 syncrepl_updateCookie( si, op, psub, &syncCookie );
566 ldap_controls_free( rctrls );
568 slap_mods_free( modlist );
570 if ( rc_efree && entry ) {
576 case LDAP_RES_SEARCH_REFERENCE:
578 LDAP_LOG( OPERATION, ERR,
579 "do_syncrep2 : reference received\n", 0, 0, 0 );
581 Debug( LDAP_DEBUG_ANY,
582 "do_syncrep2 : reference received\n", 0, 0, 0 );
586 case LDAP_RES_SEARCH_RESULT:
587 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
591 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
593 ber_scanf( ber, "{" /*"}"*/);
594 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
596 ber_scanf( ber, "m", &cookie );
597 if ( cookie.bv_val ) {
598 struct berval tmp_bv;
599 ber_dupbv( &tmp_bv, &cookie );
600 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
602 if ( syncCookie.octet_str &&
603 syncCookie.octet_str[0].bv_val )
604 slap_parse_sync_cookie( &syncCookie );
606 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
608 ber_scanf( ber, "b", &refreshDeletes );
610 ber_scanf( ber, "}" );
612 if ( syncCookie_req.ctxcsn == NULL ) {
614 } else if ( syncCookie.ctxcsn == NULL ) {
617 value_match( &match, slap_schema.si_ad_entryCSN,
618 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
619 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
620 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
622 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
623 && match < 0 && err == LDAP_SUCCESS ) {
624 syncrepl_updateCookie( si, op, psub, &syncCookie );
627 ldap_controls_free( rctrls );
629 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
630 /* FIXME : different error behaviors according to
631 * 1) err code : LDAP_BUSY ...
632 * 2) on err policy : stop service, stop sync, retry
634 if ( refreshDeletes == 0 && match < 0 &&
635 err == LDAP_SUCCESS ) {
636 syncrepl_del_nonpresent( op, si );
638 avl_free( si->si_presentlist, avl_ber_bvfree );
639 si->si_presentlist = NULL;
646 case LDAP_RES_INTERMEDIATE:
647 rc = ldap_parse_intermediate( si->si_ld, msg,
648 &retoid, &retdata, NULL, 0 );
649 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
650 int si_refreshDelete = 0;
651 int si_refreshPresent = 0;
652 ber_init2( ber, retdata, LBER_USE_DER );
654 switch ( si_tag = ber_peek_tag( ber, &len )) {
656 case LDAP_TAG_SYNC_NEW_COOKIE:
657 ber_scanf( ber, "tm", &tag, &cookie );
659 case LDAP_TAG_SYNC_REFRESH_DELETE:
660 si_refreshDelete = 1;
661 case LDAP_TAG_SYNC_REFRESH_PRESENT:
662 si_refreshPresent = 1;
663 ber_scanf( ber, "t{", &tag );
664 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
666 ber_scanf( ber, "m", &cookie );
667 if ( cookie.bv_val ) {
668 struct berval tmp_bv;
669 ber_dupbv( &tmp_bv, &cookie );
670 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
672 if ( syncCookie.octet_str &&
673 syncCookie.octet_str[0].bv_val )
674 slap_parse_sync_cookie( &syncCookie );
676 if ( ber_peek_tag( ber, &len ) ==
677 LDAP_TAG_REFRESHDONE )
679 ber_scanf( ber, "b", &refreshDone );
681 ber_scanf( ber, "}" );
683 case LDAP_TAG_SYNC_ID_SET:
684 ber_scanf( ber, "t{", &tag );
685 if ( ber_peek_tag( ber, &len ) ==
686 LDAP_TAG_SYNC_COOKIE ) {
687 ber_scanf( ber, "m", &cookie );
688 if ( cookie.bv_val ) {
689 struct berval tmp_bv;
690 ber_dupbv( &tmp_bv, &cookie );
691 ber_bvarray_add( &syncCookie.octet_str,
694 if ( syncCookie.octet_str &&
695 syncCookie.octet_str[0].bv_val )
696 slap_parse_sync_cookie( &syncCookie );
698 if ( ber_peek_tag( ber, &len ) ==
699 LDAP_TAG_REFRESHDELETES )
701 ber_scanf( ber, "b", &refreshDeletes );
703 ber_scanf( ber, "[W]", &syncUUIDs );
704 ber_scanf( ber, "}" );
705 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
706 struct berval *syncuuid_bv;
707 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
708 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
709 avl_insert( &si->si_presentlist,
710 (caddr_t) syncuuid_bv,
711 syncuuid_cmp, avl_dup_error );
713 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
717 LDAP_LOG( OPERATION, ERR,
718 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
719 (long) si_tag, 0, 0 );
721 Debug( LDAP_DEBUG_ANY,
722 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
723 (long) si_tag, 0, 0 );
725 ldap_memfree( retoid );
726 ber_bvfree( retdata );
730 if ( syncCookie_req.ctxcsn == NULL ) {
732 } else if ( syncCookie.ctxcsn == NULL ) {
735 value_match( &match, slap_schema.si_ad_entryCSN,
736 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
737 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
738 &syncCookie_req.ctxcsn[0],
739 &syncCookie.ctxcsn[0], &text );
742 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
744 syncrepl_updateCookie( si, op, psub, &syncCookie);
747 if ( si_refreshPresent == 1 ) {
749 syncrepl_del_nonpresent( op, si );
753 ldap_memfree( retoid );
754 ber_bvfree( retdata );
758 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
759 " unknown intermediate "
760 "response\n", 0, 0, 0 );
762 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
763 "unknown intermediate response (%d)\n",
766 ldap_memfree( retoid );
767 ber_bvfree( retdata );
773 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
774 "unknown message\n", 0, 0, 0 );
776 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
777 "unknown message\n", 0, 0, 0 );
782 if ( syncCookie.octet_str ) {
783 slap_sync_cookie_free( &syncCookie_req, 0 );
784 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
785 slap_sync_cookie_free( &syncCookie, 0 );
795 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
796 errstr = ldap_err2string( rc );
799 LDAP_LOG( OPERATION, ERR,
800 "do_syncrep2 : %s\n", errstr, 0, 0 );
802 Debug( LDAP_DEBUG_ANY,
803 "do_syncrep2 : %s\n", errstr, 0, 0 );
808 slap_sync_cookie_free( &syncCookie, 0 );
809 slap_sync_cookie_free( &syncCookie_req, 0 );
811 if ( res ) ldap_msgfree( res );
813 if ( rc && si->si_ld ) {
814 ldap_unbind( si->si_ld );
826 struct re_s* rtask = arg;
827 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
828 Connection conn = {0};
830 int rc = LDAP_SUCCESS;
838 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
840 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
846 switch( abs( si->si_type )) {
847 case LDAP_SYNC_REFRESH_ONLY:
848 case LDAP_SYNC_REFRESH_AND_PERSIST:
854 if ( slapd_shutdown && si->si_ld ) {
855 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
856 connection_client_stop( s );
857 ldap_unbind( si->si_ld );
862 connection_fake_init( &conn, &op, ctx );
864 /* use global malloc for now */
865 op.o_tmpmemctx = NULL;
866 op.o_tmpmfuncs = &ch_mfuncs;
868 op.o_dn = si->si_updatedn;
869 op.o_ndn = si->si_updatedn;
870 op.o_managedsait = 1;
871 op.o_bd = be = si->si_be;
873 op.o_sync_state.ctxcsn = NULL;
874 op.o_sync_state.sid = -1;
875 op.o_sync_state.octet_str = NULL;
876 op.o_sync_slog_size = -1;
877 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
878 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
880 /* Establish session, do search */
883 rc = do_syncrep1( &op, si );
886 /* Process results */
887 if ( rc == LDAP_SUCCESS ) {
888 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
890 rc = do_syncrep2( &op, si );
892 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
893 /* If we succeeded, enable the connection for further listening.
894 * If we failed, tear down the connection and reschedule.
896 if ( rc == LDAP_SUCCESS ) {
898 rc = connection_client_setup( s, do_syncrepl, arg );
900 connection_client_enable( s );
902 } else if ( !first ) {
906 if ( rc == -2 ) rc = 0;
910 /* At this point, we have 4 cases:
911 * 1) for any hard failure, give up and remove this task
912 * 2) for ServerDown, reschedule this task to run
913 * 3) for Refresh and Success, reschedule to run
914 * 4) for Persist and Success, reschedule to defer
916 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
918 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
919 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
923 connection_client_stop( s );
926 if ( rc == LDAP_SUCCESS ) {
927 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
930 rtask->interval.tv_sec = si->si_interval;
931 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
932 if ( si->si_retrynum ) {
933 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
934 si->si_retrynum[i] = si->si_retrynum_init[i];
936 si->si_retrynum[i] = -2;
939 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
940 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
944 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
945 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
946 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
948 } else if ( si->si_retrynum[i] >= -1 ) {
949 if ( si->si_retrynum[i] > 0 )
950 si->si_retrynum[i]--;
951 rtask->interval.tv_sec = si->si_retryinterval[i];
952 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
953 slap_wake_listener();
957 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
963 syncrepl_message_to_entry(
967 Modifications **modlist,
973 BerElement *ber = NULL;
976 Modifications **modtail = modlist;
979 char txtbuf[SLAP_TEXT_BUFLEN];
980 size_t textlen = sizeof txtbuf;
982 struct berval bdn = {0, NULL}, dn, ndn;
987 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
989 LDAP_LOG( OPERATION, ERR,
990 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
992 Debug( LDAP_DEBUG_ANY,
993 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
998 op->o_tag = LDAP_REQ_ADD;
1000 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1002 if ( rc != LDAP_SUCCESS ) {
1004 LDAP_LOG( OPERATION, ERR,
1005 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1007 Debug( LDAP_DEBUG_ANY,
1008 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1013 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1014 ber_dupbv( &op->o_req_dn, &dn );
1015 ber_dupbv( &op->o_req_ndn, &ndn );
1016 sl_free( ndn.bv_val, op->o_tmpmemctx );
1017 sl_free( dn.bv_val, op->o_tmpmemctx );
1019 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1022 return LDAP_SUCCESS;
1025 if ( entry == NULL ) {
1029 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1031 e->e_name = op->o_req_dn;
1032 e->e_nname = op->o_req_ndn;
1034 while ( ber_remaining( ber ) ) {
1035 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1036 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1041 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1043 mod->sml_op = LDAP_MOD_REPLACE;
1044 mod->sml_next = NULL;
1045 mod->sml_desc = NULL;
1046 mod->sml_type = tmp.sml_type;
1047 mod->sml_values = tmp.sml_values;
1048 mod->sml_nvalues = NULL;
1051 modtail = &mod->sml_next;
1054 if ( *modlist == NULL ) {
1056 LDAP_LOG( OPERATION, ERR,
1057 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1059 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1066 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1068 if ( rc != LDAP_SUCCESS ) {
1070 LDAP_LOG( OPERATION, ERR,
1071 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1073 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1079 /* Strip out dynamically generated attrs */
1080 for ( modtail = modlist; *modtail ; ) {
1082 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1083 *modtail = mod->sml_next;
1084 slap_mod_free( &mod->sml_mod, 0 );
1087 modtail = &mod->sml_next;
1091 /* Strip out attrs in exattrs list */
1092 for ( modtail = modlist; *modtail ; ) {
1094 if ( ldap_charray_inlist( si->si_exattrs,
1095 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1096 *modtail = mod->sml_next;
1097 slap_mod_free( &mod->sml_mod, 0 );
1100 modtail = &mod->sml_next;
1104 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1105 if( rc != LDAP_SUCCESS ) {
1107 LDAP_LOG( OPERATION, ERR,
1108 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1110 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1116 ber_free ( ber, 0 );
1117 if ( rc != LDAP_SUCCESS ) {
1132 Modifications* modlist,
1134 struct berval* syncUUID,
1135 struct sync_cookie* syncCookie_req
1138 Backend *be = op->o_bd;
1139 slap_callback cb = { NULL };
1140 struct berval *syncuuid_bv = NULL;
1141 struct berval syncUUID_strrep = BER_BVNULL;
1142 struct berval uuid_bv = BER_BVNULL;
1144 SlapReply rs_search = {REP_RESULT};
1145 SlapReply rs_delete = {REP_RESULT};
1146 SlapReply rs_add = {REP_RESULT};
1147 SlapReply rs_modify = {REP_RESULT};
1149 AttributeAssertion ava = {0};
1150 int rc = LDAP_SUCCESS;
1151 int ret = LDAP_SUCCESS;
1154 struct berval pdn = BER_BVNULL;
1155 struct berval org_req_dn = BER_BVNULL;
1156 struct berval org_req_ndn = BER_BVNULL;
1157 struct berval org_dn = BER_BVNULL;
1158 struct berval org_ndn = BER_BVNULL;
1159 int org_managedsait;
1161 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1163 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1164 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1165 syncuuid_cmp, avl_dup_error );
1168 if ( syncstate == LDAP_SYNC_PRESENT ) {
1170 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1171 if ( entry == NULL ) {
1176 f.f_choice = LDAP_FILTER_EQUALITY;
1178 ava.aa_desc = slap_schema.si_ad_entryUUID;
1179 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1180 ava.aa_value = *syncUUID;
1181 op->ors_filter = &f;
1183 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1184 op->ors_filterstr.bv_val = (char *) sl_malloc(
1185 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1186 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1187 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1188 syncUUID->bv_val, syncUUID->bv_len );
1189 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1191 op->o_tag = LDAP_REQ_SEARCH;
1192 op->ors_scope = LDAP_SCOPE_SUBTREE;
1194 /* get syncrepl cookie of shadow replica from subentry */
1195 op->o_req_dn = si->si_base;
1196 op->o_req_ndn = si->si_base;
1198 op->o_time = slap_get_time();
1199 op->ors_tlimit = SLAP_NO_LIMIT;
1202 /* set callback function */
1203 op->o_callback = &cb;
1204 cb.sc_response = dn_callback;
1207 si->si_syncUUID_ndn.bv_val = NULL;
1209 if ( limits_check( op, &rs_search ) == 0 ) {
1210 rc = be->be_search( op, &rs_search );
1213 if ( op->ors_filterstr.bv_val ) {
1214 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1217 cb.sc_response = null_callback;
1220 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1223 if ( syncstate != LDAP_SYNC_DELETE ) {
1224 op->o_no_psearch = 1;
1227 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1228 if ( op->o_sync_csn.bv_val ) {
1229 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1234 op->o_req_dn = si->si_syncUUID_ndn;
1235 op->o_req_ndn = si->si_syncUUID_ndn;
1236 op->o_tag = LDAP_REQ_DELETE;
1237 rc = be->be_delete( op, &rs_delete );
1239 org_req_dn = op->o_req_dn;
1240 org_req_ndn = op->o_req_ndn;
1242 org_ndn = op->o_ndn;
1243 org_managedsait = get_manageDSAit( op );
1244 op->o_dn = op->o_bd->be_rootdn;
1245 op->o_ndn = op->o_bd->be_rootndn;
1246 op->o_managedsait = 1;
1248 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1249 op->o_delete_glue_parent = 0;
1250 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1251 slap_callback cb = { NULL };
1252 cb.sc_response = slap_null_cb;
1253 dnParent( &op->o_req_ndn, &pdn );
1255 op->o_req_ndn = pdn;
1256 op->o_callback = &cb;
1257 op->o_bd->be_delete( op, &rs_delete );
1263 op->o_managedsait = org_managedsait;
1265 op->o_ndn = org_ndn;
1266 op->o_req_dn = org_req_dn;
1267 op->o_req_ndn = org_req_ndn;
1268 op->o_delete_glue_parent = 0;
1270 op->o_no_psearch = 0;
1273 switch ( syncstate ) {
1275 case LDAP_SYNC_MODIFY:
1276 if ( rs_search.sr_err == LDAP_SUCCESS ||
1277 rs_search.sr_err == LDAP_REFERRAL ||
1278 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1279 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1281 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1282 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1283 &syncUUID_strrep, syncUUID );
1285 op->o_tag = LDAP_REQ_ADD;
1287 op->o_req_dn = entry->e_name;
1288 op->o_req_ndn = entry->e_nname;
1290 rc = be->be_add( op, &rs_add );
1292 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1293 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1294 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1296 Modifications *modtail = modlist;
1300 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1304 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1305 ber_dupbv( &uuid_bv, syncUUID );
1306 mod->sml_op = LDAP_MOD_REPLACE;
1307 mod->sml_desc = slap_schema.si_ad_entryUUID;
1308 mod->sml_type = mod->sml_desc->ad_cname;
1309 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1310 modtail->sml_next = mod;
1312 op->o_tag = LDAP_REQ_MODIFY;
1313 op->orm_modlist = modlist;
1314 op->o_req_dn = entry->e_name;
1315 op->o_req_ndn = entry->e_nname;
1317 rc = be->be_modify( op, &rs_modify );
1318 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1320 LDAP_LOG( OPERATION, ERR,
1321 "syncrepl_entry : be_modify failed (%d)\n",
1322 rs_modify.sr_err, 0, 0 );
1324 Debug( LDAP_DEBUG_ANY,
1325 "syncrepl_entry : be_modify failed (%d)\n",
1326 rs_modify.sr_err, 0, 0 );
1331 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1332 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1333 syncrepl_add_glue( op, entry );
1338 LDAP_LOG( OPERATION, ERR,
1339 "syncrepl_entry : be_add failed (%d)\n",
1340 rs_add.sr_err, 0, 0 );
1342 Debug( LDAP_DEBUG_ANY,
1343 "syncrepl_entry : be_add failed (%d)\n",
1344 rs_add.sr_err, 0, 0 );
1350 be_entry_release_w( op, entry );
1356 LDAP_LOG( OPERATION, ERR,
1357 "syncrepl_entry : be_search failed (%d)\n",
1358 rs_search.sr_err, 0, 0 );
1360 Debug( LDAP_DEBUG_ANY,
1361 "syncrepl_entry : be_search failed (%d)\n",
1362 rs_search.sr_err, 0, 0 );
1368 case LDAP_SYNC_DELETE :
1369 /* Already deleted */
1375 LDAP_LOG( OPERATION, ERR,
1376 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1378 Debug( LDAP_DEBUG_ANY,
1379 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1387 if ( syncUUID_strrep.bv_val ) {
1388 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1390 if ( si->si_syncUUID_ndn.bv_val ) {
1391 ch_free( si->si_syncUUID_ndn.bv_val );
1392 si->si_syncUUID_ndn.bv_val = NULL;
1397 static struct berval gcbva[] = {
1404 syncrepl_del_nonpresent(
1409 Backend* be = op->o_bd;
1410 slap_callback cb = { NULL };
1411 SlapReply rs_search = {REP_RESULT};
1412 SlapReply rs_delete = {REP_RESULT};
1413 SlapReply rs_modify = {REP_RESULT};
1414 struct nonpresent_entry *np_list, *np_prev;
1417 Modifications *mlnext;
1419 Modifications *modlist = NULL;
1420 Modifications **modtail = &modlist;
1423 struct berval pdn = BER_BVNULL;
1424 struct berval org_req_dn = BER_BVNULL;
1425 struct berval org_req_ndn = BER_BVNULL;
1426 struct berval org_dn = BER_BVNULL;
1427 struct berval org_ndn = BER_BVNULL;
1428 int org_managedsait;
1430 op->o_req_dn = si->si_base;
1431 op->o_req_ndn = si->si_base;
1433 cb.sc_response = nonpresent_callback;
1436 op->o_callback = &cb;
1437 op->o_tag = LDAP_REQ_SEARCH;
1438 op->ors_scope = si->si_scope;
1439 op->ors_deref = LDAP_DEREF_NEVER;
1440 op->o_time = slap_get_time();
1441 op->ors_tlimit = SLAP_NO_LIMIT;
1442 op->ors_slimit = SLAP_NO_LIMIT;
1443 op->ors_attrsonly = 0;
1444 op->ors_attrs = NULL;
1445 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1446 op->ors_filterstr = si->si_filterstr;
1448 op->o_nocaching = 1;
1449 op->o_managedsait = 0;
1451 if ( limits_check( op, &rs_search ) == 0 ) {
1452 rc = be->be_search( op, &rs_search );
1455 op->o_managedsait = 1;
1456 op->o_nocaching = 0;
1458 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1460 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1461 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1462 while ( np_list != NULL ) {
1463 LDAP_LIST_REMOVE( np_list, npe_link );
1465 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1466 op->o_tag = LDAP_REQ_DELETE;
1467 op->o_callback = &cb;
1468 cb.sc_response = null_callback;
1470 op->o_req_dn = *np_prev->npe_name;
1471 op->o_req_ndn = *np_prev->npe_nname;
1472 rc = op->o_bd->be_delete( op, &rs_delete );
1474 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1475 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1476 mod->sml_op = LDAP_MOD_REPLACE;
1477 mod->sml_desc = slap_schema.si_ad_objectClass;
1478 mod->sml_type = mod->sml_desc->ad_cname;
1479 mod->sml_values = &gcbva[0];
1481 modtail = &mod->sml_next;
1483 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1484 mod->sml_op = LDAP_MOD_REPLACE;
1485 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1486 mod->sml_type = mod->sml_desc->ad_cname;
1487 mod->sml_values = &gcbva[1];
1489 modtail = &mod->sml_next;
1491 op->o_tag = LDAP_REQ_MODIFY;
1492 op->orm_modlist = modlist;
1494 rc = be->be_modify( op, &rs_modify );
1496 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1497 mlnext = ml->sml_next;
1502 org_req_dn = op->o_req_dn;
1503 org_req_ndn = op->o_req_ndn;
1505 org_ndn = op->o_ndn;
1506 org_managedsait = get_manageDSAit( op );
1507 op->o_dn = op->o_bd->be_rootdn;
1508 op->o_ndn = op->o_bd->be_rootndn;
1509 op->o_managedsait = 1;
1511 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1512 op->o_delete_glue_parent ) {
1513 op->o_delete_glue_parent = 0;
1514 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1515 slap_callback cb = { NULL };
1516 cb.sc_response = slap_null_cb;
1517 dnParent( &op->o_req_ndn, &pdn );
1519 op->o_req_ndn = pdn;
1520 op->o_callback = &cb;
1521 /* give it a root privil ? */
1522 op->o_bd->be_delete( op, &rs_delete );
1528 op->o_managedsait = org_managedsait;
1530 op->o_ndn = org_ndn;
1531 op->o_req_dn = org_req_dn;
1532 op->o_req_ndn = org_req_ndn;
1533 op->o_delete_glue_parent = 0;
1535 ber_bvfree( np_prev->npe_name );
1536 ber_bvfree( np_prev->npe_nname );
1537 op->o_req_dn.bv_val = NULL;
1538 op->o_req_ndn.bv_val = NULL;
1553 Backend *be = op->o_bd;
1554 slap_callback cb = { NULL };
1559 struct berval dn = {0, NULL};
1560 struct berval ndn = {0, NULL};
1562 SlapReply rs_add = {REP_RESULT};
1565 op->o_tag = LDAP_REQ_ADD;
1566 op->o_callback = &cb;
1567 cb.sc_response = null_callback;
1568 cb.sc_private = NULL;
1573 /* count RDNs in suffix */
1574 if ( be->be_nsuffix[0].bv_len ) {
1575 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1585 /* Start with BE suffix */
1586 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1587 comma = strrchr(dn.bv_val, ',');
1588 if ( ptr ) *ptr = ',';
1589 if ( comma ) *comma = '\0';
1594 dn.bv_len -= ptr - dn.bv_val;
1597 /* the normalizedDNs are always the same length, no counting
1600 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1601 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1602 ndn.bv_len = be->be_nsuffix[0].bv_len;
1605 while ( ndn.bv_val > e->e_nname.bv_val ) {
1606 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1607 ber_dupbv( &glue->e_name, &dn );
1608 ber_dupbv( &glue->e_nname, &ndn );
1610 a = ch_calloc( 1, sizeof( Attribute ));
1611 a->a_desc = slap_schema.si_ad_objectClass;
1613 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1614 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1615 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1616 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1618 a->a_nvals = a->a_vals;
1620 a->a_next = glue->e_attrs;
1623 a = ch_calloc( 1, sizeof( Attribute ));
1624 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1626 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1627 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1628 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1630 a->a_nvals = a->a_vals;
1632 a->a_next = glue->e_attrs;
1635 op->o_req_dn = glue->e_name;
1636 op->o_req_ndn = glue->e_nname;
1638 rc = be->be_add ( op, &rs_add );
1639 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1640 be_entry_release_w( op, glue );
1642 /* incl. ALREADY EXIST */
1646 /* Move to next child */
1647 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1650 if ( ptr == e->e_name.bv_val ) break;
1652 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1653 for( ptr = ndn.bv_val-2;
1654 ptr > e->e_nname.bv_val && *ptr != ',';
1660 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1663 op->o_req_dn = e->e_name;
1664 op->o_req_ndn = e->e_nname;
1666 rc = be->be_add ( op, &rs_add );
1667 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1668 be_entry_release_w( op, e );
1676 static struct berval ocbva[] = {
1678 BER_BVC("subentry"),
1679 BER_BVC("syncConsumerSubentry"),
1683 static struct berval cnbva[] = {
1688 static struct berval ssbva[] = {
1693 static struct berval scbva[] = {
1699 syncrepl_updateCookie(
1703 struct sync_cookie *syncCookie
1706 Backend *be = op->o_bd;
1708 Modifications *mlnext;
1710 Modifications *modlist = NULL;
1711 Modifications **modtail = &modlist;
1714 char txtbuf[SLAP_TEXT_BUFLEN];
1715 size_t textlen = sizeof txtbuf;
1720 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1721 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1722 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1724 slap_callback cb = { NULL };
1725 SlapReply rs_add = {REP_RESULT};
1726 SlapReply rs_modify = {REP_RESULT};
1728 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1729 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1731 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1732 mod->sml_op = LDAP_MOD_REPLACE;
1733 mod->sml_desc = slap_schema.si_ad_objectClass;
1734 mod->sml_type = mod->sml_desc->ad_cname;
1735 mod->sml_values = ocbva;
1737 modtail = &mod->sml_next;
1739 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1740 assert( si->si_rid < 1000 );
1741 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1742 slap_syncrepl_bvc.bv_len + 1,
1743 "syncrepl%ld", si->si_rid );
1744 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1745 mod->sml_op = LDAP_MOD_REPLACE;
1746 mod->sml_desc = slap_schema.si_ad_cn;
1747 mod->sml_type = mod->sml_desc->ad_cname;
1748 mod->sml_values = cnbva;
1750 modtail = &mod->sml_next;
1752 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1753 mod->sml_op = LDAP_MOD_REPLACE;
1754 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1755 mod->sml_type = mod->sml_desc->ad_cname;
1756 mod->sml_values = ssbva;
1758 modtail = &mod->sml_next;
1760 /* Keep this last, so we can avoid touching the previous
1761 * attributes unnecessarily.
1763 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1764 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1765 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1766 mod->sml_op = LDAP_MOD_REPLACE;
1767 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1768 mod->sml_type = mod->sml_desc->ad_cname;
1769 mod->sml_values = scbva;
1771 modtail = &mod->sml_next;
1775 op->o_tag = LDAP_REQ_ADD;
1776 rc = slap_mods_opattrs( op, modlist, modtail,
1777 &text, txtbuf, textlen, 0 );
1779 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1780 ml->sml_op = LDAP_MOD_REPLACE;
1783 if( rc != LDAP_SUCCESS ) {
1785 LDAP_LOG( OPERATION, ERR,
1786 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1788 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1793 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1795 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1796 assert( si->si_rid < 1000 );
1797 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1798 slap_syncrepl_cn_bvc.bv_len + 1,
1799 "cn=syncrepl%ld", si->si_rid );
1801 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1803 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1804 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1806 if ( slap_syncrepl_dn_bv.bv_val ) {
1807 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1812 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1814 if( rc != LDAP_SUCCESS ) {
1816 LDAP_LOG( OPERATION, ERR,
1817 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1819 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1824 cb.sc_response = null_callback;
1827 op->o_callback = &cb;
1828 op->o_req_dn = e->e_name;
1829 op->o_req_ndn = e->e_nname;
1831 /* update persistent cookie */
1832 update_cookie_retry:
1833 op->o_tag = LDAP_REQ_MODIFY;
1834 /* Just modify the cookie value, not the entire entry */
1835 op->orm_modlist = mod;
1836 rc = be->be_modify( op, &rs_modify );
1838 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1839 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1840 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1841 op->o_tag = LDAP_REQ_ADD;
1843 rc = be->be_add( op, &rs_add );
1844 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1845 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1846 goto update_cookie_retry;
1847 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1848 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1850 LDAP_LOG( OPERATION, ERR,
1851 "cookie will be non-persistent\n",
1854 Debug( LDAP_DEBUG_ANY,
1855 "cookie will be non-persistent\n",
1860 LDAP_LOG( OPERATION, ERR,
1861 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1863 Debug( LDAP_DEBUG_ANY,
1864 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1868 be_entry_release_w( op, e );
1873 LDAP_LOG( OPERATION, ERR,
1874 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1876 Debug( LDAP_DEBUG_ANY,
1877 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1888 if ( cnbva[0].bv_val ) {
1889 ch_free( cnbva[0].bv_val );
1890 cnbva[0].bv_val = NULL;
1892 if ( scbva[0].bv_val ) {
1893 ch_free( scbva[0].bv_val );
1894 scbva[0].bv_val = NULL;
1897 if ( mlnext->sml_next ) {
1898 slap_mods_free( mlnext->sml_next );
1899 mlnext->sml_next = NULL;
1902 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1903 mlnext = ml->sml_next;
1911 syncrepl_isupdate( Operation *op )
1913 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1917 syncrepl_isupdate_dn(
1925 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1926 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1927 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1941 syncinfo_t *si = op->o_callback->sc_private;
1943 if ( rs->sr_type == REP_SEARCH ) {
1944 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1946 LDAP_LOG( OPERATION, ERR,
1947 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1949 Debug( LDAP_DEBUG_ANY,
1950 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1953 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1955 } else if ( rs->sr_type == REP_RESULT ) {
1956 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1958 LDAP_LOG( OPERATION, ERR,
1959 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1961 Debug( LDAP_DEBUG_ANY,
1962 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1967 return LDAP_SUCCESS;
1971 nonpresent_callback(
1976 syncinfo_t *si = op->o_callback->sc_private;
1979 struct berval* present_uuid = NULL;
1980 struct nonpresent_entry *np_entry;
1982 if ( rs->sr_type == REP_RESULT ) {
1983 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1984 si->si_presentlist = NULL;
1986 } else if ( rs->sr_type == REP_SEARCH ) {
1987 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1989 if ( a == NULL ) return 0;
1991 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1994 if ( present_uuid == NULL ) {
1995 np_entry = (struct nonpresent_entry *)
1996 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1997 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1998 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1999 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2002 avl_delete( &si->si_presentlist,
2003 &a->a_nvals[0], syncuuid_cmp );
2004 ch_free( present_uuid->bv_val );
2005 ch_free( present_uuid );
2008 return LDAP_SUCCESS;
2016 if ( rs->sr_err != LDAP_SUCCESS &&
2017 rs->sr_err != LDAP_REFERRAL &&
2018 rs->sr_err != LDAP_ALREADY_EXISTS &&
2019 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2020 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2023 LDAP_LOG( OPERATION, ERR,
2024 "null_callback : error code 0x%x\n",
2027 Debug( LDAP_DEBUG_ANY,
2028 "null_callback : error code 0x%x\n",
2032 return LDAP_SUCCESS;
2036 slap_create_syncrepl_entry(
2038 struct berval *context_csn,
2046 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2048 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2050 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2053 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2055 if ( context_csn ) {
2056 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2057 context_csn, NULL );
2061 bv.bv_len = sizeof("{}")-1;
2062 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2064 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2065 ber_dupbv( &e->e_nname, &e->e_name );
2071 slap_uuidstr_from_normalized(
2072 struct berval* uuidstr,
2073 struct berval* normalized,
2077 unsigned char nibble;
2080 if ( normalized == NULL )
2083 if ( normalized->bv_len != 16 ) {
2090 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
2095 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2097 sl_free( new, ctx );
2101 for ( i = 0; i < 16; i++ ) {
2102 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2103 new->bv_val[(i<<1)+d] = '-';
2107 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2108 if ( nibble < 10 ) {
2109 new->bv_val[(i<<1)+d] = nibble + '0';
2111 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2114 nibble = (normalized->bv_val[i]) & 0xF;
2115 if ( nibble < 10 ) {
2116 new->bv_val[(i<<1)+d+1] = nibble + '0';
2118 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2122 new->bv_val[new->bv_len] = '\0';
2128 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2130 const struct berval *uuid1 = v_uuid1;
2131 const struct berval *uuid2 = v_uuid2;
2132 int rc = uuid1->bv_len - uuid2->bv_len;
2133 if ( rc ) return rc;
2134 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2138 avl_ber_bvfree( void *bv )
2143 if ( ((struct berval *)bv)->bv_val != NULL ) {
2144 ch_free ( ((struct berval *)bv)->bv_val );
2146 ch_free ( (char *) bv );
2150 syncinfo_free( syncinfo_t *sie )
2152 if ( sie->si_provideruri ) {
2153 ch_free( sie->si_provideruri );
2155 if ( sie->si_provideruri_bv ) {
2156 ber_bvarray_free( sie->si_provideruri_bv );
2158 if ( sie->si_updatedn.bv_val ) {
2159 ch_free( sie->si_updatedn.bv_val );
2161 if ( sie->si_binddn ) {
2162 ch_free( sie->si_binddn );
2164 if ( sie->si_passwd ) {
2165 ch_free( sie->si_passwd );
2167 if ( sie->si_saslmech ) {
2168 ch_free( sie->si_saslmech );
2170 if ( sie->si_secprops ) {
2171 ch_free( sie->si_secprops );
2173 if ( sie->si_realm ) {
2174 ch_free( sie->si_realm );
2176 if ( sie->si_authcId ) {
2177 ch_free( sie->si_authcId );
2179 if ( sie->si_authzId ) {
2180 ch_free( sie->si_authzId );
2182 if ( sie->si_filterstr.bv_val ) {
2183 ch_free( sie->si_filterstr.bv_val );
2185 if ( sie->si_base.bv_val ) {
2186 ch_free( sie->si_base.bv_val );
2188 if ( sie->si_attrs ) {
2190 while ( sie->si_attrs[i] != NULL ) {
2191 ch_free( sie->si_attrs[i] );
2194 ch_free( sie->si_attrs );
2196 if ( sie->si_exattrs ) {
2198 while ( sie->si_exattrs[i] != NULL ) {
2199 ch_free( sie->si_exattrs[i] );
2202 ch_free( sie->si_exattrs );
2204 if ( sie->si_retryinterval ) {
2205 ch_free( sie->si_retryinterval );
2207 if ( sie->si_retrynum ) {
2208 ch_free( sie->si_retrynum );
2210 if ( sie->si_retrynum_init ) {
2211 ch_free( sie->si_retrynum_init );
2213 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2214 if ( sie->si_syncUUID_ndn.bv_val ) {
2215 ch_free( sie->si_syncUUID_ndn.bv_val );
2217 if ( sie->si_presentlist ) {
2218 avl_free( sie->si_presentlist, avl_ber_bvfree );
2221 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2223 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2224 struct nonpresent_entry* npe;
2225 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2226 LDAP_LIST_REMOVE( npe, npe_link );
2227 if ( npe->npe_name ) {
2228 if ( npe->npe_name->bv_val ) {
2229 ch_free( npe->npe_name->bv_val );
2231 ch_free( npe->npe_name );
2233 if ( npe->npe_nname ) {
2234 if ( npe->npe_nname->bv_val ) {
2235 ch_free( npe->npe_nname->bv_val );
2237 ch_free( npe->npe_nname );