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 );
459 LDAPControl **rctrls = NULL;
462 BerElementBuffer berbuf;
463 BerElement *ber = (BerElement *)&berbuf;
465 LDAPMessage *res = NULL;
466 LDAPMessage *msg = NULL;
469 struct berval *retdata = NULL;
474 struct berval syncUUID = BER_BVNULL;
475 struct sync_cookie syncCookie = { NULL, -1, NULL };
476 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
477 struct berval cookie = BER_BVNULL;
485 Modifications *modlist = NULL;
490 struct timeval *tout_p = NULL;
491 struct timeval tout = { 0, 0 };
493 int refreshDeletes = 0;
495 BerVarray syncUUIDs = NULL;
498 if ( slapd_shutdown ) {
503 ber_init2( ber, NULL, LBER_USE_DER );
504 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
507 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
509 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
512 psub = &si->si_be->be_nsuffix[0];
514 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
516 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
522 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
525 if ( slapd_shutdown ) {
529 for( msg = ldap_first_message( si->si_ld, res );
531 msg = ldap_next_message( si->si_ld, msg ) )
533 switch( ldap_msgtype( msg ) ) {
534 case LDAP_RES_SEARCH_ENTRY:
535 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
536 /* we can't work without the control */
542 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
543 ber_scanf( ber, "{em", &syncstate, &syncUUID );
544 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
545 ber_scanf( ber, "m}", &cookie );
546 if ( cookie.bv_val ) {
547 struct berval tmp_bv;
548 ber_dupbv( &tmp_bv, &cookie );
549 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
551 if ( syncCookie.octet_str &&
552 syncCookie.octet_str[0].bv_val )
553 slap_parse_sync_cookie( &syncCookie );
555 if ( syncrepl_message_to_entry( si, op, msg,
556 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
557 rc_efree = syncrepl_entry( si, op, entry, modlist,
558 syncstate, &syncUUID, &syncCookie_req );
559 if ( syncCookie.octet_str &&
560 syncCookie.octet_str[0].bv_val ) {
561 syncrepl_updateCookie( si, op, psub, &syncCookie );
564 ldap_controls_free( rctrls );
566 slap_mods_free( modlist );
568 if ( rc_efree && entry ) {
574 case LDAP_RES_SEARCH_REFERENCE:
576 LDAP_LOG( OPERATION, ERR,
577 "do_syncrep2 : reference received\n", 0, 0, 0 );
579 Debug( LDAP_DEBUG_ANY,
580 "do_syncrep2 : reference received\n", 0, 0, 0 );
584 case LDAP_RES_SEARCH_RESULT:
585 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
589 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
591 ber_scanf( ber, "{" /*"}"*/);
592 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
594 ber_scanf( ber, "m", &cookie );
595 if ( cookie.bv_val ) {
596 struct berval tmp_bv;
597 ber_dupbv( &tmp_bv, &cookie );
598 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
600 if ( syncCookie.octet_str &&
601 syncCookie.octet_str[0].bv_val )
602 slap_parse_sync_cookie( &syncCookie );
604 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
606 ber_scanf( ber, "b", &refreshDeletes );
608 ber_scanf( ber, "}" );
610 if ( syncCookie_req.ctxcsn == NULL ) {
612 } else if ( syncCookie.ctxcsn == NULL ) {
615 value_match( &match, slap_schema.si_ad_entryCSN,
616 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
617 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
618 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
620 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
621 && match < 0 && err == LDAP_SUCCESS ) {
622 syncrepl_updateCookie( si, op, psub, &syncCookie );
625 ldap_controls_free( rctrls );
627 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
628 /* FIXME : different error behaviors according to
629 * 1) err code : LDAP_BUSY ...
630 * 2) on err policy : stop service, stop sync, retry
632 if ( refreshDeletes == 0 && match < 0 &&
633 err == LDAP_SUCCESS ) {
634 syncrepl_del_nonpresent( op, si );
636 avl_free( si->si_presentlist, avl_ber_bvfree );
637 si->si_presentlist = NULL;
644 case LDAP_RES_INTERMEDIATE:
645 rc = ldap_parse_intermediate( si->si_ld, msg,
646 &retoid, &retdata, NULL, 0 );
647 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
648 int si_refreshDelete = 0;
649 int si_refreshPresent = 0;
650 ber_init2( ber, retdata, LBER_USE_DER );
652 switch ( si_tag = ber_peek_tag( ber, &len )) {
654 case LDAP_TAG_SYNC_NEW_COOKIE:
655 ber_scanf( ber, "tm", &tag, &cookie );
657 case LDAP_TAG_SYNC_REFRESH_DELETE:
658 si_refreshDelete = 1;
659 case LDAP_TAG_SYNC_REFRESH_PRESENT:
660 si_refreshPresent = 1;
661 ber_scanf( ber, "t{", &tag );
662 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
664 ber_scanf( ber, "m", &cookie );
665 if ( cookie.bv_val ) {
666 struct berval tmp_bv;
667 ber_dupbv( &tmp_bv, &cookie );
668 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
670 if ( syncCookie.octet_str &&
671 syncCookie.octet_str[0].bv_val )
672 slap_parse_sync_cookie( &syncCookie );
674 if ( ber_peek_tag( ber, &len ) ==
675 LDAP_TAG_REFRESHDONE )
677 ber_scanf( ber, "b", &refreshDone );
679 ber_scanf( ber, "}" );
681 case LDAP_TAG_SYNC_ID_SET:
682 ber_scanf( ber, "t{", &tag );
683 if ( ber_peek_tag( ber, &len ) ==
684 LDAP_TAG_SYNC_COOKIE ) {
685 ber_scanf( ber, "m", &cookie );
686 if ( cookie.bv_val ) {
687 struct berval tmp_bv;
688 ber_dupbv( &tmp_bv, &cookie );
689 ber_bvarray_add( &syncCookie.octet_str,
692 if ( syncCookie.octet_str &&
693 syncCookie.octet_str[0].bv_val )
694 slap_parse_sync_cookie( &syncCookie );
696 if ( ber_peek_tag( ber, &len ) ==
697 LDAP_TAG_REFRESHDELETES )
699 ber_scanf( ber, "b", &refreshDeletes );
701 ber_scanf( ber, "[W]", &syncUUIDs );
702 ber_scanf( ber, "}" );
703 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
704 struct berval *syncuuid_bv;
705 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
706 avl_insert( &si->si_presentlist,
707 (caddr_t) syncuuid_bv,
708 syncuuid_cmp, avl_dup_error );
710 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
714 LDAP_LOG( OPERATION, ERR,
715 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
716 (long) si_tag, 0, 0 );
718 Debug( LDAP_DEBUG_ANY,
719 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
720 (long) si_tag, 0, 0 );
722 ldap_memfree( retoid );
723 ber_bvfree( retdata );
727 if ( syncCookie_req.ctxcsn == NULL ) {
729 } else if ( syncCookie.ctxcsn == NULL ) {
732 value_match( &match, slap_schema.si_ad_entryCSN,
733 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
734 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
735 &syncCookie_req.ctxcsn[0],
736 &syncCookie.ctxcsn[0], &text );
739 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
741 syncrepl_updateCookie( si, op, psub, &syncCookie);
744 if ( si_refreshPresent == 1 ) {
746 syncrepl_del_nonpresent( op, si );
750 ldap_memfree( retoid );
751 ber_bvfree( retdata );
755 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
756 " unknown intermediate "
757 "response\n", 0, 0, 0 );
759 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
760 "unknown intermediate response (%d)\n",
763 ldap_memfree( retoid );
764 ber_bvfree( retdata );
770 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
771 "unknown message\n", 0, 0, 0 );
773 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
774 "unknown message\n", 0, 0, 0 );
779 if ( syncCookie.octet_str ) {
780 slap_sync_cookie_free( &syncCookie_req, 0 );
781 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
782 slap_sync_cookie_free( &syncCookie, 0 );
792 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
793 errstr = ldap_err2string( rc );
796 LDAP_LOG( OPERATION, ERR,
797 "do_syncrep2 : %s\n", errstr, 0, 0 );
799 Debug( LDAP_DEBUG_ANY,
800 "do_syncrep2 : %s\n", errstr, 0, 0 );
805 slap_sync_cookie_free( &syncCookie, 0 );
806 slap_sync_cookie_free( &syncCookie_req, 0 );
808 avl_free( si->si_presentlist, avl_ber_bvfree );
809 si->si_presentlist = NULL;
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;
837 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
839 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
845 switch( abs( si->si_type )) {
846 case LDAP_SYNC_REFRESH_ONLY:
847 case LDAP_SYNC_REFRESH_AND_PERSIST:
853 if ( slapd_shutdown && si->si_ld ) {
854 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
855 connection_client_stop( s );
856 ldap_unbind( si->si_ld );
861 connection_fake_init( &conn, &op, ctx );
863 /* use global malloc for now */
864 op.o_tmpmemctx = NULL;
865 op.o_tmpmfuncs = &ch_mfuncs;
867 op.o_dn = si->si_updatedn;
868 op.o_ndn = si->si_updatedn;
869 op.o_managedsait = 1;
872 op.o_sync_state.ctxcsn = NULL;
873 op.o_sync_state.sid = -1;
874 op.o_sync_state.octet_str = NULL;
875 op.o_sync_slog_size = -1;
876 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
877 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
879 /* Establish session, do search */
882 rc = do_syncrep1( &op, si );
885 /* Process results */
886 if ( rc == LDAP_SUCCESS ) {
887 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
889 rc = do_syncrep2( &op, si );
891 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
892 /* If we succeeded, enable the connection for further listening.
893 * If we failed, tear down the connection and reschedule.
895 if ( rc == LDAP_SUCCESS ) {
897 rc = connection_client_setup( s, do_syncrepl,
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 } else if ( si->si_retrynum[i] >= -1 ) {
947 if ( si->si_retrynum[i] > 0 )
948 si->si_retrynum[i]--;
949 rtask->interval.tv_sec = si->si_retryinterval[i];
950 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
951 slap_wake_listener();
955 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
961 syncrepl_message_to_entry(
965 Modifications **modlist,
971 BerElement *ber = NULL;
974 Modifications **modtail = modlist;
977 char txtbuf[SLAP_TEXT_BUFLEN];
978 size_t textlen = sizeof txtbuf;
980 struct berval bdn = {0, NULL}, dn, ndn;
985 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
987 LDAP_LOG( OPERATION, ERR,
988 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
990 Debug( LDAP_DEBUG_ANY,
991 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
996 op->o_tag = LDAP_REQ_ADD;
998 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1000 if ( rc != LDAP_SUCCESS ) {
1002 LDAP_LOG( OPERATION, ERR,
1003 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1005 Debug( LDAP_DEBUG_ANY,
1006 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1011 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1012 ber_dupbv( &op->o_req_dn, &dn );
1013 ber_dupbv( &op->o_req_ndn, &ndn );
1014 sl_free( ndn.bv_val, op->o_tmpmemctx );
1015 sl_free( dn.bv_val, op->o_tmpmemctx );
1017 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1020 return LDAP_SUCCESS;
1023 if ( entry == NULL ) {
1027 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1029 e->e_name = op->o_req_dn;
1030 e->e_nname = op->o_req_ndn;
1032 while ( ber_remaining( ber ) ) {
1033 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1034 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1039 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1041 mod->sml_op = LDAP_MOD_REPLACE;
1042 mod->sml_next = NULL;
1043 mod->sml_desc = NULL;
1044 mod->sml_type = tmp.sml_type;
1045 mod->sml_values = tmp.sml_values;
1046 mod->sml_nvalues = NULL;
1049 modtail = &mod->sml_next;
1052 if ( *modlist == NULL ) {
1054 LDAP_LOG( OPERATION, ERR,
1055 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1057 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1064 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1066 if ( rc != LDAP_SUCCESS ) {
1068 LDAP_LOG( OPERATION, ERR,
1069 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1071 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1077 /* Strip out dynamically generated attrs */
1078 for ( modtail = modlist; *modtail ; ) {
1080 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1081 *modtail = mod->sml_next;
1082 slap_mod_free( &mod->sml_mod, 0 );
1085 modtail = &mod->sml_next;
1089 /* Strip out attrs in exattrs list */
1090 for ( modtail = modlist; *modtail ; ) {
1092 if ( ldap_charray_inlist( si->si_exattrs,
1093 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1094 *modtail = mod->sml_next;
1095 slap_mod_free( &mod->sml_mod, 0 );
1098 modtail = &mod->sml_next;
1102 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1103 if( rc != LDAP_SUCCESS ) {
1105 LDAP_LOG( OPERATION, ERR,
1106 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1108 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1114 ber_free ( ber, 0 );
1115 if ( rc != LDAP_SUCCESS ) {
1130 Modifications* modlist,
1132 struct berval* syncUUID,
1133 struct sync_cookie* syncCookie_req
1136 Backend *be = op->o_bd;
1137 slap_callback cb = { NULL };
1138 struct berval *syncuuid_bv = NULL;
1139 struct berval syncUUID_strrep = BER_BVNULL;
1140 struct berval uuid_bv = BER_BVNULL;
1142 SlapReply rs_search = {REP_RESULT};
1143 SlapReply rs_delete = {REP_RESULT};
1144 SlapReply rs_add = {REP_RESULT};
1145 SlapReply rs_modify = {REP_RESULT};
1147 AttributeAssertion ava = {0};
1148 int rc = LDAP_SUCCESS;
1149 int ret = LDAP_SUCCESS;
1152 struct berval pdn = BER_BVNULL;
1153 struct berval org_req_dn = BER_BVNULL;
1154 struct berval org_req_ndn = BER_BVNULL;
1155 struct berval org_dn = BER_BVNULL;
1156 struct berval org_ndn = BER_BVNULL;
1157 int org_managedsait;
1159 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1161 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1162 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1163 syncuuid_cmp, avl_dup_error );
1166 if ( syncstate == LDAP_SYNC_PRESENT ) {
1168 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1169 if ( entry == NULL ) {
1174 f.f_choice = LDAP_FILTER_EQUALITY;
1176 ava.aa_desc = slap_schema.si_ad_entryUUID;
1177 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1178 ava.aa_value = *syncUUID;
1179 op->ors_filter = &f;
1181 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1182 op->ors_filterstr.bv_val = (char *) sl_malloc(
1183 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1184 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1185 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1186 syncUUID->bv_val, syncUUID->bv_len );
1187 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1189 op->o_tag = LDAP_REQ_SEARCH;
1190 op->ors_scope = LDAP_SCOPE_SUBTREE;
1192 /* get syncrepl cookie of shadow replica from subentry */
1193 op->o_req_dn = si->si_base;
1194 op->o_req_ndn = si->si_base;
1196 op->o_time = slap_get_time();
1197 op->ors_tlimit = SLAP_NO_LIMIT;
1200 /* set callback function */
1201 op->o_callback = &cb;
1202 cb.sc_response = dn_callback;
1205 si->si_syncUUID_ndn.bv_val = NULL;
1207 if ( limits_check( op, &rs_search ) == 0 ) {
1208 rc = be->be_search( op, &rs_search );
1211 if ( op->ors_filterstr.bv_val ) {
1212 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1215 cb.sc_response = null_callback;
1218 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1221 if ( syncstate != LDAP_SYNC_DELETE ) {
1222 op->o_no_psearch = 1;
1225 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1226 if ( op->o_sync_csn.bv_val ) {
1227 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1232 op->o_req_dn = si->si_syncUUID_ndn;
1233 op->o_req_ndn = si->si_syncUUID_ndn;
1234 op->o_tag = LDAP_REQ_DELETE;
1235 rc = be->be_delete( op, &rs_delete );
1237 org_req_dn = op->o_req_dn;
1238 org_req_ndn = op->o_req_ndn;
1240 org_ndn = op->o_ndn;
1241 org_managedsait = get_manageDSAit( op );
1242 op->o_dn = op->o_bd->be_rootdn;
1243 op->o_ndn = op->o_bd->be_rootndn;
1244 op->o_managedsait = 1;
1246 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1247 op->o_delete_glue_parent = 0;
1248 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1249 slap_callback cb = { NULL };
1250 cb.sc_response = slap_null_cb;
1251 dnParent( &op->o_req_ndn, &pdn );
1253 op->o_req_ndn = pdn;
1254 op->o_callback = &cb;
1255 op->o_bd->be_delete( op, &rs_delete );
1261 op->o_managedsait = org_managedsait;
1263 op->o_ndn = org_ndn;
1264 op->o_req_dn = org_req_dn;
1265 op->o_req_ndn = org_req_ndn;
1266 op->o_delete_glue_parent = 0;
1268 op->o_no_psearch = 0;
1271 switch ( syncstate ) {
1273 case LDAP_SYNC_MODIFY:
1274 if ( rs_search.sr_err == LDAP_SUCCESS ||
1275 rs_search.sr_err == LDAP_REFERRAL ||
1276 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1277 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1279 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1280 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1281 &syncUUID_strrep, syncUUID );
1283 op->o_tag = LDAP_REQ_ADD;
1285 op->o_req_dn = entry->e_name;
1286 op->o_req_ndn = entry->e_nname;
1288 rc = be->be_add( op, &rs_add );
1290 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1291 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1292 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1294 Modifications *modtail = modlist;
1298 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1302 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1303 ber_dupbv( &uuid_bv, syncUUID );
1304 mod->sml_op = LDAP_MOD_REPLACE;
1305 mod->sml_desc = slap_schema.si_ad_entryUUID;
1306 mod->sml_type = mod->sml_desc->ad_cname;
1307 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1308 modtail->sml_next = mod;
1310 op->o_tag = LDAP_REQ_MODIFY;
1311 op->orm_modlist = modlist;
1312 op->o_req_dn = entry->e_name;
1313 op->o_req_ndn = entry->e_nname;
1315 rc = be->be_modify( op, &rs_modify );
1316 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1318 LDAP_LOG( OPERATION, ERR,
1319 "syncrepl_entry : be_modify failed (%d)\n",
1320 rs_modify.sr_err, 0, 0 );
1322 Debug( LDAP_DEBUG_ANY,
1323 "syncrepl_entry : be_modify failed (%d)\n",
1324 rs_modify.sr_err, 0, 0 );
1329 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1330 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1331 syncrepl_add_glue( op, entry );
1336 LDAP_LOG( OPERATION, ERR,
1337 "syncrepl_entry : be_add failed (%d)\n",
1338 rs_add.sr_err, 0, 0 );
1340 Debug( LDAP_DEBUG_ANY,
1341 "syncrepl_entry : be_add failed (%d)\n",
1342 rs_add.sr_err, 0, 0 );
1348 be_entry_release_w( op, entry );
1354 LDAP_LOG( OPERATION, ERR,
1355 "syncrepl_entry : be_search failed (%d)\n",
1356 rs_search.sr_err, 0, 0 );
1358 Debug( LDAP_DEBUG_ANY,
1359 "syncrepl_entry : be_search failed (%d)\n",
1360 rs_search.sr_err, 0, 0 );
1366 case LDAP_SYNC_DELETE :
1367 /* Already deleted */
1373 LDAP_LOG( OPERATION, ERR,
1374 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1376 Debug( LDAP_DEBUG_ANY,
1377 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1385 if ( syncUUID_strrep.bv_val ) {
1386 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1388 if ( si->si_syncUUID_ndn.bv_val ) {
1389 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1394 static struct berval gcbva[] = {
1401 syncrepl_del_nonpresent(
1406 Backend* be = op->o_bd;
1407 slap_callback cb = { NULL };
1408 SlapReply rs_search = {REP_RESULT};
1409 SlapReply rs_delete = {REP_RESULT};
1410 SlapReply rs_modify = {REP_RESULT};
1411 struct nonpresent_entry *np_list, *np_prev;
1414 Modifications *mlnext;
1416 Modifications *modlist = NULL;
1417 Modifications **modtail = &modlist;
1420 struct berval pdn = BER_BVNULL;
1421 struct berval org_req_dn = BER_BVNULL;
1422 struct berval org_req_ndn = BER_BVNULL;
1423 struct berval org_dn = BER_BVNULL;
1424 struct berval org_ndn = BER_BVNULL;
1425 int org_managedsait;
1427 op->o_req_dn = si->si_base;
1428 op->o_req_ndn = si->si_base;
1430 cb.sc_response = nonpresent_callback;
1433 op->o_callback = &cb;
1434 op->o_tag = LDAP_REQ_SEARCH;
1435 op->ors_scope = si->si_scope;
1436 op->ors_deref = LDAP_DEREF_NEVER;
1437 op->o_time = slap_get_time();
1438 op->ors_tlimit = SLAP_NO_LIMIT;
1439 op->ors_slimit = SLAP_NO_LIMIT;
1440 op->ors_attrsonly = 0;
1441 op->ors_attrs = NULL;
1442 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1443 op->ors_filterstr = si->si_filterstr;
1445 op->o_nocaching = 1;
1446 op->o_managedsait = 0;
1448 if ( limits_check( op, &rs_search ) == 0 ) {
1449 rc = be->be_search( op, &rs_search );
1452 op->o_managedsait = 1;
1453 op->o_nocaching = 0;
1455 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1457 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1458 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1459 while ( np_list != NULL ) {
1460 LDAP_LIST_REMOVE( np_list, npe_link );
1462 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1463 op->o_tag = LDAP_REQ_DELETE;
1464 op->o_callback = &cb;
1465 cb.sc_response = null_callback;
1467 op->o_req_dn = *np_prev->npe_name;
1468 op->o_req_ndn = *np_prev->npe_nname;
1469 rc = op->o_bd->be_delete( op, &rs_delete );
1471 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1472 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1473 mod->sml_op = LDAP_MOD_REPLACE;
1474 mod->sml_desc = slap_schema.si_ad_objectClass;
1475 mod->sml_type = mod->sml_desc->ad_cname;
1476 mod->sml_values = &gcbva[0];
1478 modtail = &mod->sml_next;
1480 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1481 mod->sml_op = LDAP_MOD_REPLACE;
1482 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1483 mod->sml_type = mod->sml_desc->ad_cname;
1484 mod->sml_values = &gcbva[1];
1486 modtail = &mod->sml_next;
1488 op->o_tag = LDAP_REQ_MODIFY;
1489 op->orm_modlist = modlist;
1491 rc = be->be_modify( op, &rs_modify );
1493 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1494 mlnext = ml->sml_next;
1499 org_req_dn = op->o_req_dn;
1500 org_req_ndn = op->o_req_ndn;
1502 org_ndn = op->o_ndn;
1503 org_managedsait = get_manageDSAit( op );
1504 op->o_dn = op->o_bd->be_rootdn;
1505 op->o_ndn = op->o_bd->be_rootndn;
1506 op->o_managedsait = 1;
1508 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1509 op->o_delete_glue_parent ) {
1510 op->o_delete_glue_parent = 0;
1511 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1512 slap_callback cb = { NULL };
1513 cb.sc_response = slap_null_cb;
1514 dnParent( &op->o_req_ndn, &pdn );
1516 op->o_req_ndn = pdn;
1517 op->o_callback = &cb;
1518 /* give it a root privil ? */
1519 op->o_bd->be_delete( op, &rs_delete );
1525 op->o_managedsait = org_managedsait;
1527 op->o_ndn = org_ndn;
1528 op->o_req_dn = org_req_dn;
1529 op->o_req_ndn = org_req_ndn;
1530 op->o_delete_glue_parent = 0;
1532 ber_bvfree( np_prev->npe_name );
1533 ber_bvfree( np_prev->npe_nname );
1534 op->o_req_dn.bv_val = NULL;
1535 op->o_req_ndn.bv_val = NULL;
1550 Backend *be = op->o_bd;
1551 slap_callback cb = { NULL };
1556 struct berval dn = {0, NULL};
1557 struct berval ndn = {0, NULL};
1559 SlapReply rs_add = {REP_RESULT};
1562 op->o_tag = LDAP_REQ_ADD;
1563 op->o_callback = &cb;
1564 cb.sc_response = null_callback;
1565 cb.sc_private = NULL;
1570 /* count RDNs in suffix */
1571 if ( be->be_nsuffix[0].bv_len ) {
1572 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1582 /* Start with BE suffix */
1583 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1584 comma = strrchr(dn.bv_val, ',');
1585 if ( ptr ) *ptr = ',';
1586 if ( comma ) *comma = '\0';
1591 dn.bv_len -= ptr - dn.bv_val;
1594 /* the normalizedDNs are always the same length, no counting
1597 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1598 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1599 ndn.bv_len = be->be_nsuffix[0].bv_len;
1602 while ( ndn.bv_val > e->e_nname.bv_val ) {
1603 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1604 ber_dupbv( &glue->e_name, &dn );
1605 ber_dupbv( &glue->e_nname, &ndn );
1607 a = ch_calloc( 1, sizeof( Attribute ));
1608 a->a_desc = slap_schema.si_ad_objectClass;
1610 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1611 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1612 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1613 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1615 a->a_nvals = a->a_vals;
1617 a->a_next = glue->e_attrs;
1620 a = ch_calloc( 1, sizeof( Attribute ));
1621 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1623 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1624 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1625 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1627 a->a_nvals = a->a_vals;
1629 a->a_next = glue->e_attrs;
1632 op->o_req_dn = glue->e_name;
1633 op->o_req_ndn = glue->e_nname;
1635 rc = be->be_add ( op, &rs_add );
1636 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1637 be_entry_release_w( op, glue );
1639 /* incl. ALREADY EXIST */
1643 /* Move to next child */
1644 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1647 if ( ptr == e->e_name.bv_val ) break;
1649 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1650 for( ptr = ndn.bv_val-2;
1651 ptr > e->e_nname.bv_val && *ptr != ',';
1657 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1660 op->o_req_dn = e->e_name;
1661 op->o_req_ndn = e->e_nname;
1663 rc = be->be_add ( op, &rs_add );
1664 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1665 be_entry_release_w( op, e );
1673 static struct berval ocbva[] = {
1675 BER_BVC("subentry"),
1676 BER_BVC("syncConsumerSubentry"),
1680 static struct berval cnbva[] = {
1685 static struct berval ssbva[] = {
1690 static struct berval scbva[] = {
1696 syncrepl_updateCookie(
1700 struct sync_cookie *syncCookie
1703 Backend *be = op->o_bd;
1705 Modifications *mlnext;
1707 Modifications *modlist = NULL;
1708 Modifications **modtail = &modlist;
1711 char txtbuf[SLAP_TEXT_BUFLEN];
1712 size_t textlen = sizeof txtbuf;
1717 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1718 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1719 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1721 slap_callback cb = { NULL };
1722 SlapReply rs_add = {REP_RESULT};
1723 SlapReply rs_modify = {REP_RESULT};
1725 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1726 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1728 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1729 mod->sml_op = LDAP_MOD_REPLACE;
1730 mod->sml_desc = slap_schema.si_ad_objectClass;
1731 mod->sml_type = mod->sml_desc->ad_cname;
1732 mod->sml_values = ocbva;
1734 modtail = &mod->sml_next;
1736 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1737 assert( si->si_rid < 1000 );
1738 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1739 slap_syncrepl_bvc.bv_len + 1,
1740 "syncrepl%ld", si->si_rid );
1741 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1742 mod->sml_op = LDAP_MOD_REPLACE;
1743 mod->sml_desc = slap_schema.si_ad_cn;
1744 mod->sml_type = mod->sml_desc->ad_cname;
1745 mod->sml_values = cnbva;
1747 modtail = &mod->sml_next;
1749 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1750 mod->sml_op = LDAP_MOD_REPLACE;
1751 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1752 mod->sml_type = mod->sml_desc->ad_cname;
1753 mod->sml_values = ssbva;
1755 modtail = &mod->sml_next;
1757 /* Keep this last, so we can avoid touching the previous
1758 * attributes unnecessarily.
1760 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1761 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1762 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1763 mod->sml_op = LDAP_MOD_REPLACE;
1764 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1765 mod->sml_type = mod->sml_desc->ad_cname;
1766 mod->sml_values = scbva;
1768 modtail = &mod->sml_next;
1772 op->o_tag = LDAP_REQ_ADD;
1773 rc = slap_mods_opattrs( op, modlist, modtail,
1774 &text,txtbuf, textlen );
1776 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1777 ml->sml_op = LDAP_MOD_REPLACE;
1780 if( rc != LDAP_SUCCESS ) {
1782 LDAP_LOG( OPERATION, ERR,
1783 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1785 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1790 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1792 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1793 assert( si->si_rid < 1000 );
1794 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1795 slap_syncrepl_cn_bvc.bv_len + 1,
1796 "cn=syncrepl%ld", si->si_rid );
1798 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1800 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1801 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1803 if ( slap_syncrepl_dn_bv.bv_val ) {
1804 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1809 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1811 if( rc != LDAP_SUCCESS ) {
1813 LDAP_LOG( OPERATION, ERR,
1814 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1816 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1821 cb.sc_response = null_callback;
1824 op->o_callback = &cb;
1825 op->o_req_dn = e->e_name;
1826 op->o_req_ndn = e->e_nname;
1828 /* update persistent cookie */
1829 update_cookie_retry:
1830 op->o_tag = LDAP_REQ_MODIFY;
1831 /* Just modify the cookie value, not the entire entry */
1832 op->orm_modlist = mod;
1833 rc = be->be_modify( op, &rs_modify );
1835 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1836 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1837 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1838 op->o_tag = LDAP_REQ_ADD;
1840 rc = be->be_add( op, &rs_add );
1841 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1842 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1843 goto update_cookie_retry;
1844 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1845 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1847 LDAP_LOG( OPERATION, ERR,
1848 "cookie will be non-persistent\n",
1851 Debug( LDAP_DEBUG_ANY,
1852 "cookie will be non-persistent\n",
1857 LDAP_LOG( OPERATION, ERR,
1858 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1860 Debug( LDAP_DEBUG_ANY,
1861 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1865 be_entry_release_w( op, e );
1870 LDAP_LOG( OPERATION, ERR,
1871 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1873 Debug( LDAP_DEBUG_ANY,
1874 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1885 if ( cnbva[0].bv_val ) {
1886 ch_free( cnbva[0].bv_val );
1887 cnbva[0].bv_val = NULL;
1889 if ( scbva[0].bv_val ) {
1890 ch_free( scbva[0].bv_val );
1891 scbva[0].bv_val = NULL;
1894 if ( mlnext->sml_next ) {
1895 slap_mods_free( mlnext->sml_next );
1896 mlnext->sml_next = NULL;
1899 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1900 mlnext = ml->sml_next;
1908 syncrepl_isupdate( Operation *op )
1910 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1914 syncrepl_isupdate_dn(
1922 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1923 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1924 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1938 syncinfo_t *si = op->o_callback->sc_private;
1940 if ( rs->sr_type == REP_SEARCH ) {
1941 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1943 LDAP_LOG( OPERATION, ERR,
1944 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1946 Debug( LDAP_DEBUG_ANY,
1947 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1950 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1952 } else if ( rs->sr_type == REP_RESULT ) {
1953 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1955 LDAP_LOG( OPERATION, ERR,
1956 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1958 Debug( LDAP_DEBUG_ANY,
1959 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1964 return LDAP_SUCCESS;
1968 nonpresent_callback(
1973 syncinfo_t *si = op->o_callback->sc_private;
1976 struct berval* present_uuid = NULL;
1977 struct nonpresent_entry *np_entry;
1979 if ( rs->sr_type == REP_RESULT ) {
1980 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1981 si->si_presentlist = NULL;
1983 } else if ( rs->sr_type == REP_SEARCH ) {
1984 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1986 if ( a == NULL ) return 0;
1988 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1991 if ( present_uuid == NULL ) {
1992 np_entry = (struct nonpresent_entry *)
1993 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1994 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1995 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1996 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1999 avl_delete( &si->si_presentlist,
2000 &a->a_nvals[0], syncuuid_cmp );
2001 ch_free( present_uuid->bv_val );
2002 ch_free( present_uuid );
2005 return LDAP_SUCCESS;
2013 if ( rs->sr_err != LDAP_SUCCESS &&
2014 rs->sr_err != LDAP_REFERRAL &&
2015 rs->sr_err != LDAP_ALREADY_EXISTS &&
2016 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2017 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2020 LDAP_LOG( OPERATION, ERR,
2021 "null_callback : error code 0x%x\n",
2024 Debug( LDAP_DEBUG_ANY,
2025 "null_callback : error code 0x%x\n",
2029 return LDAP_SUCCESS;
2033 slap_create_syncrepl_entry(
2035 struct berval *context_csn,
2043 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2045 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2047 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2050 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2052 if ( context_csn ) {
2053 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2054 context_csn, NULL );
2058 bv.bv_len = sizeof("{}")-1;
2059 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2061 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2062 ber_dupbv( &e->e_nname, &e->e_name );
2068 slap_uuidstr_from_normalized(
2069 struct berval* uuidstr,
2070 struct berval* normalized,
2074 unsigned char nibble;
2077 if ( normalized == NULL )
2080 if ( normalized->bv_len != 16 ) {
2087 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
2092 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2094 sl_free( new, ctx );
2098 for ( i = 0; i < 16; i++ ) {
2099 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2100 new->bv_val[(i<<1)+d] = '-';
2104 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2105 if ( nibble < 10 ) {
2106 new->bv_val[(i<<1)+d] = nibble + '0';
2108 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2111 nibble = (normalized->bv_val[i]) & 0xF;
2112 if ( nibble < 10 ) {
2113 new->bv_val[(i<<1)+d+1] = nibble + '0';
2115 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2119 new->bv_val[new->bv_len] = '\0';
2125 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2127 const struct berval *uuid1 = v_uuid1;
2128 const struct berval *uuid2 = v_uuid2;
2129 int rc = uuid1->bv_len - uuid2->bv_len;
2130 if ( rc ) return rc;
2131 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2135 avl_ber_bvfree( void *bv )
2140 if ( ((struct berval *)bv)->bv_val != NULL ) {
2141 ch_free ( ((struct berval *)bv)->bv_val );
2143 ch_free ( (char *) bv );