1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
29 #include "lutil_ldap.h"
33 #define SYNCREPL_STR "syncreplxxx"
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
48 static AttributeDescription *sync_descs[4];
50 struct runqueue_s syncrepl_rq;
53 init_syncrepl(syncinfo_t *si)
58 if ( !sync_descs[0] ) {
59 sync_descs[0] = slap_schema.si_ad_objectClass;
60 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61 sync_descs[2] = slap_schema.si_ad_entryCSN;
65 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
68 /* Delete Attributes */
69 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
74 ch_free( si->si_attrs[j] );
75 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 si->si_attrs[k] = si->si_attrs[k+1];
81 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
85 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
87 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
91 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
94 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
96 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
99 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
118 BerElementBuffer berbuf;
119 BerElement *ber = (BerElement *)&berbuf;
120 LDAPControl c[2], *ctrls[3];
121 struct timeval timeout;
125 /* setup LDAP SYNC control */
126 ber_init2( ber, NULL, LBER_USE_DER );
127 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
129 if ( si->si_syncCookie.octet_str &&
130 si->si_syncCookie.octet_str[0].bv_val ) {
131 ber_printf( ber, "{eO}", abs(si->si_type),
132 &si->si_syncCookie.octet_str[0] );
134 ber_printf( ber, "{e}", abs(si->si_type) );
137 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
142 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143 c[0].ldctl_iscritical = si->si_type < 0;
146 if ( si->si_authzId ) {
147 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149 c[1].ldctl_iscritical = 1;
156 timeout.tv_sec = si->si_tlimit;
159 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
162 si->si_slimit, &msgid );
174 int cmdline_cookie_found = 0;
176 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
177 struct berval syncrepl_cn_bv;
178 struct sync_cookie *sc = NULL;
179 struct sync_cookie syncCookie = { NULL, -1, NULL };
185 psub = &si->si_be->be_nsuffix[0];
187 /* Init connection to master */
189 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
190 if ( rc != LDAP_SUCCESS ) {
192 LDAP_LOG( OPERATION, ERR,
193 "do_syncrep1: ldap_initialize failed (%s)\n",
194 si->si_provideruri, 0, 0 );
196 Debug( LDAP_DEBUG_ANY,
197 "do_syncrep1: ldap_initialize failed (%s)\n",
198 si->si_provideruri, 0, 0 );
203 op->o_protocol = LDAP_VERSION3;
204 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
209 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
210 if( rc != LDAP_SUCCESS ) {
212 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
213 "%s: ldap_start_tls failed (%d)\n",
214 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
217 Debug( LDAP_DEBUG_ANY,
218 "%s: ldap_start_tls failed (%d)\n",
219 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
222 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
226 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
227 #ifdef HAVE_CYRUS_SASL
230 if ( si->si_secprops != NULL ) {
231 rc = ldap_set_option( si->si_ld,
232 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
234 if( rc != LDAP_OPT_SUCCESS ) {
236 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
237 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
238 si->si_provideruri, si->si_secprops, 0 );
240 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
241 "(%s,SECPROPS,\"%s\") failed!\n",
242 si->si_provideruri, si->si_secprops, 0 );
248 defaults = lutil_sasl_defaults( si->si_ld,
249 si->si_saslmech, si->si_realm,
250 si->si_authcId, si->si_passwd, si->si_authzId );
252 rc = ldap_sasl_interactive_bind_s( si->si_ld,
260 lutil_sasl_freedefs( defaults );
262 /* FIXME: different error behaviors according to
264 * 2) on err policy : exit, retry, backoff ...
266 if ( rc != LDAP_SUCCESS ) {
268 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
269 "ldap_sasl_interactive_bind_s failed (%d)\n",
272 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
273 "ldap_sasl_interactive_bind_s failed (%d)\n",
277 /* FIXME (see above comment) */
278 /* if Kerberos credentials cache is not active, retry */
279 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
280 rc == LDAP_LOCAL_ERROR )
282 rc = LDAP_SERVER_DOWN;
287 #else /* HAVE_CYRUS_SASL */
288 /* Should never get here, we trapped this at config time */
289 fprintf( stderr, "not compiled with SASL support\n" );
294 rc = ldap_bind_s( si->si_ld,
295 si->si_binddn, si->si_passwd, si->si_bindmethod );
296 if ( rc != LDAP_SUCCESS ) {
298 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
299 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
301 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
302 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
308 /* Set SSF to strongest of TLS, SASL SSFs */
312 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl ) == LDAP_SUCCESS &&
314 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
316 #endif /* HAVE_TLS */
317 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
318 op->o_transport_ssf = op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf ) ?
319 op->o_sasl_ssf : op->o_tls_ssf;
321 /* get syncrepl cookie of shadow replica from subentry */
323 assert( si->si_rid < 1000 );
324 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
325 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
326 CN_STR "syncrepl%ld", si->si_rid );
327 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
328 op->o_req_dn = op->o_req_ndn;
330 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
331 if ( si->si_rid == sc->rid ) {
332 cmdline_cookie_found = 1;
337 if ( cmdline_cookie_found ) {
338 /* cookie is supplied in the command line */
339 BerVarray cookie = NULL;
340 struct berval cookie_bv;
342 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
343 slap_sync_cookie_free( &si->si_syncCookie, 0 );
345 /* read stored cookie if it exists */
346 backend_attribute( op, NULL, &op->o_req_ndn,
347 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
350 /* no stored cookie */
351 if ( sc->ctxcsn == NULL ||
352 sc->ctxcsn->bv_val == NULL ) {
353 /* if cmdline cookie does not have ctxcsn */
354 /* component, set it to an initial value */
355 slap_init_sync_cookie_ctxcsn( sc );
357 slap_dup_sync_cookie( &si->si_syncCookie, sc );
358 slap_sync_cookie_free( sc, 1 );
362 struct berval newcookie = BER_BVNULL;
363 ber_dupbv( &cookie_bv, &cookie[0] );
364 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
365 slap_parse_sync_cookie( &si->si_syncCookie );
366 ber_bvarray_free( si->si_syncCookie.octet_str );
367 si->si_syncCookie.octet_str = NULL;
368 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
369 if ( sc->sid != -1 ) {
370 /* command line cookie wins */
371 si->si_syncCookie.sid = sc->sid;
373 if ( sc->ctxcsn != NULL ) {
374 /* command line cookie wins */
375 if ( si->si_syncCookie.ctxcsn ) {
376 ber_bvarray_free( si->si_syncCookie.ctxcsn );
377 si->si_syncCookie.ctxcsn = NULL;
379 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
380 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
382 if ( sc->rid != -1 ) {
383 /* command line cookie wins */
384 si->si_syncCookie.rid = sc->rid;
386 slap_sync_cookie_free( sc, 1 );
388 slap_compose_sync_cookie( NULL, &newcookie,
389 &si->si_syncCookie.ctxcsn[0],
390 si->si_syncCookie.sid, si->si_syncCookie.rid );
391 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
394 /* no command line cookie is specified */
395 if ( si->si_syncCookie.octet_str == NULL ) {
396 BerVarray cookie = NULL;
397 struct berval cookie_bv;
398 /* try to read stored cookie */
399 backend_attribute( op, NULL, &op->o_req_ndn,
400 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
402 ber_dupbv( &cookie_bv, &cookie[0] );
403 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
404 slap_parse_sync_cookie( &si->si_syncCookie );
405 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
410 rc = ldap_sync_search( si, op->o_tmpmemctx );
412 if( rc != LDAP_SUCCESS ) {
414 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
415 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
417 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
418 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
425 ldap_unbind( si->si_ld );
438 LDAPControl **rctrls = NULL;
441 BerElementBuffer berbuf;
442 BerElement *ber = (BerElement *)&berbuf;
444 LDAPMessage *res = NULL;
445 LDAPMessage *msg = NULL;
448 struct berval *retdata = NULL;
453 struct berval syncUUID = BER_BVNULL;
454 struct sync_cookie syncCookie = { NULL, -1, NULL };
455 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
456 struct berval cookie = BER_BVNULL;
464 Modifications *modlist = NULL;
469 struct timeval *tout_p = NULL;
470 struct timeval tout = { 0, 0 };
472 int refreshDeletes = 0;
474 BerVarray syncUUIDs = NULL;
477 if ( slapd_shutdown ) {
482 ber_init2( ber, NULL, LBER_USE_DER );
483 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
486 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
488 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
491 psub = &si->si_be->be_nsuffix[0];
493 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
495 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
501 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
504 if ( slapd_shutdown ) {
508 for( msg = ldap_first_message( si->si_ld, res );
510 msg = ldap_next_message( si->si_ld, msg ) )
512 switch( ldap_msgtype( msg ) ) {
513 case LDAP_RES_SEARCH_ENTRY:
514 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
515 /* we can't work without the control */
521 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
522 ber_scanf( ber, "{em", &syncstate, &syncUUID );
523 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
524 ber_scanf( ber, "m}", &cookie );
525 if ( cookie.bv_val ) {
526 struct berval tmp_bv;
527 ber_dupbv( &tmp_bv, &cookie );
528 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
530 if ( syncCookie.octet_str &&
531 syncCookie.octet_str[0].bv_val )
532 slap_parse_sync_cookie( &syncCookie );
534 if ( syncrepl_message_to_entry( si, op, msg,
535 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
536 rc_efree = syncrepl_entry( si, op, entry, modlist,
537 syncstate, &syncUUID, &syncCookie_req );
538 if ( syncCookie.octet_str &&
539 syncCookie.octet_str[0].bv_val ) {
540 syncrepl_updateCookie( si, op, psub, &syncCookie );
543 ldap_controls_free( rctrls );
545 slap_mods_free( modlist );
547 if ( rc_efree && entry ) {
553 case LDAP_RES_SEARCH_REFERENCE:
555 LDAP_LOG( OPERATION, ERR,
556 "do_syncrep2 : reference received\n", 0, 0, 0 );
558 Debug( LDAP_DEBUG_ANY,
559 "do_syncrep2 : reference received\n", 0, 0, 0 );
563 case LDAP_RES_SEARCH_RESULT:
564 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
568 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
570 ber_scanf( ber, "{" /*"}"*/);
571 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
573 ber_scanf( ber, "m", &cookie );
574 if ( cookie.bv_val ) {
575 struct berval tmp_bv;
576 ber_dupbv( &tmp_bv, &cookie );
577 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
579 if ( syncCookie.octet_str &&
580 syncCookie.octet_str[0].bv_val )
581 slap_parse_sync_cookie( &syncCookie );
583 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
585 ber_scanf( ber, "b", &refreshDeletes );
587 ber_scanf( ber, "}" );
589 if ( syncCookie_req.ctxcsn == NULL ) {
591 } else if ( syncCookie.ctxcsn == NULL ) {
594 value_match( &match, slap_schema.si_ad_entryCSN,
595 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
596 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
597 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
599 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
600 && match < 0 && err == LDAP_SUCCESS ) {
601 syncrepl_updateCookie( si, op, psub, &syncCookie );
604 ldap_controls_free( rctrls );
606 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
607 /* FIXME : different error behaviors according to
608 * 1) err code : LDAP_BUSY ...
609 * 2) on err policy : stop service, stop sync, retry
611 if ( refreshDeletes == 0 && match < 0 &&
612 err == LDAP_SUCCESS ) {
613 syncrepl_del_nonpresent( op, si );
615 avl_free( si->si_presentlist, avl_ber_bvfree );
616 si->si_presentlist = NULL;
623 case LDAP_RES_INTERMEDIATE:
624 rc = ldap_parse_intermediate( si->si_ld, msg,
625 &retoid, &retdata, NULL, 0 );
626 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
627 int si_refreshDelete = 0;
628 int si_refreshPresent = 0;
629 ber_init2( ber, retdata, LBER_USE_DER );
631 switch ( si_tag = ber_peek_tag( ber, &len )) {
633 case LDAP_TAG_SYNC_NEW_COOKIE:
634 ber_scanf( ber, "tm", &tag, &cookie );
636 case LDAP_TAG_SYNC_REFRESH_DELETE:
637 si_refreshDelete = 1;
638 case LDAP_TAG_SYNC_REFRESH_PRESENT:
639 si_refreshPresent = 1;
640 ber_scanf( ber, "t{", &tag );
641 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
643 ber_scanf( ber, "m", &cookie );
644 if ( cookie.bv_val ) {
645 struct berval tmp_bv;
646 ber_dupbv( &tmp_bv, &cookie );
647 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
649 if ( syncCookie.octet_str &&
650 syncCookie.octet_str[0].bv_val )
651 slap_parse_sync_cookie( &syncCookie );
653 if ( ber_peek_tag( ber, &len ) ==
654 LDAP_TAG_REFRESHDONE )
656 ber_scanf( ber, "b", &refreshDone );
658 ber_scanf( ber, "}" );
660 case LDAP_TAG_SYNC_ID_SET:
661 ber_scanf( ber, "t{", &tag );
662 if ( ber_peek_tag( ber, &len ) ==
663 LDAP_TAG_SYNC_COOKIE ) {
664 ber_scanf( ber, "m", &cookie );
665 if ( cookie.bv_val ) {
666 struct berval tmp_bv;
667 ber_dupbv( &tmp_bv, &cookie );
668 ber_bvarray_add( &syncCookie.octet_str,
671 if ( syncCookie.octet_str &&
672 syncCookie.octet_str[0].bv_val )
673 slap_parse_sync_cookie( &syncCookie );
675 if ( ber_peek_tag( ber, &len ) ==
676 LDAP_TAG_REFRESHDELETES )
678 ber_scanf( ber, "b", &refreshDeletes );
680 ber_scanf( ber, "[W]", &syncUUIDs );
681 ber_scanf( ber, "}" );
682 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
683 struct berval *syncuuid_bv;
684 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
685 avl_insert( &si->si_presentlist,
686 (caddr_t) syncuuid_bv,
687 syncuuid_cmp, avl_dup_error );
689 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
693 LDAP_LOG( OPERATION, ERR,
694 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
695 (long) si_tag, 0, 0 );
697 Debug( LDAP_DEBUG_ANY,
698 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
699 (long) si_tag, 0, 0 );
701 ldap_memfree( retoid );
702 ber_bvfree( retdata );
706 if ( syncCookie_req.ctxcsn == NULL ) {
708 } else if ( syncCookie.ctxcsn == NULL ) {
711 value_match( &match, slap_schema.si_ad_entryCSN,
712 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
713 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
714 &syncCookie_req.ctxcsn[0],
715 &syncCookie.ctxcsn[0], &text );
718 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
720 syncrepl_updateCookie( si, op, psub, &syncCookie);
723 if ( si_refreshPresent == 1 ) {
725 syncrepl_del_nonpresent( op, si );
729 ldap_memfree( retoid );
730 ber_bvfree( retdata );
734 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
735 " unknown intermediate "
736 "response\n", 0, 0, 0 );
738 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
739 "unknown intermediate response (%d)\n",
742 ldap_memfree( retoid );
743 ber_bvfree( retdata );
749 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
750 "unknown message\n", 0, 0, 0 );
752 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
753 "unknown message\n", 0, 0, 0 );
758 if ( syncCookie.octet_str ) {
759 slap_sync_cookie_free( &syncCookie_req, 0 );
760 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
761 slap_sync_cookie_free( &syncCookie, 0 );
771 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
772 errstr = ldap_err2string( rc );
775 LDAP_LOG( OPERATION, ERR,
776 "do_syncrep2 : %s\n", errstr, 0, 0 );
778 Debug( LDAP_DEBUG_ANY,
779 "do_syncrep2 : %s\n", errstr, 0, 0 );
784 slap_sync_cookie_free( &syncCookie, 0 );
785 slap_sync_cookie_free( &syncCookie_req, 0 );
787 if ( res ) ldap_msgfree( res );
789 if ( rc && si->si_ld ) {
790 ldap_unbind( si->si_ld );
802 struct re_s* rtask = arg;
803 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
804 Connection conn = {0};
806 int rc = LDAP_SUCCESS;
813 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
815 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
821 switch( abs( si->si_type )) {
822 case LDAP_SYNC_REFRESH_ONLY:
823 case LDAP_SYNC_REFRESH_AND_PERSIST:
829 if ( slapd_shutdown && si->si_ld ) {
830 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
831 connection_client_stop( s );
832 ldap_unbind( si->si_ld );
837 connection_fake_init( &conn, &op, ctx );
839 op.o_dn = si->si_updatedn;
840 op.o_ndn = si->si_updatedn;
841 op.o_managedsait = 1;
844 op.o_sync_state.ctxcsn = NULL;
845 op.o_sync_state.sid = -1;
846 op.o_sync_state.octet_str = NULL;
847 op.o_sync_slog_size = -1;
848 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
849 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
851 /* Establish session, do search */
854 rc = do_syncrep1( &op, si );
857 /* Process results */
858 if ( rc == LDAP_SUCCESS ) {
859 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
861 rc = do_syncrep2( &op, si );
863 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
864 /* If we succeeded, enable the connection for further listening.
865 * If we failed, tear down the connection and reschedule.
867 if ( rc == LDAP_SUCCESS ) {
869 rc = connection_client_setup( s, do_syncrepl,
872 connection_client_enable( s );
874 } else if ( !first ) {
878 if ( rc == -2 ) rc = 0;
882 /* At this point, we have 4 cases:
883 * 1) for any hard failure, give up and remove this task
884 * 2) for ServerDown, reschedule this task to run
885 * 3) for Refresh and Success, reschedule to run
886 * 4) for Persist and Success, reschedule to defer
888 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
890 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
891 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
895 connection_client_stop( s );
898 if ( rc == LDAP_SUCCESS ) {
899 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
902 rtask->interval.tv_sec = si->si_interval;
903 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
904 if ( si->si_retrynum ) {
905 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
906 si->si_retrynum[i] = si->si_retrynum_init[i];
908 si->si_retrynum[i] = -2;
911 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
912 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
916 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
917 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
918 } else if ( si->si_retrynum[i] >= -1 ) {
919 if ( si->si_retrynum[i] > 0 )
920 si->si_retrynum[i]--;
921 rtask->interval.tv_sec = si->si_retryinterval[i];
922 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
923 slap_wake_listener();
927 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
933 syncrepl_message_to_entry(
937 Modifications **modlist,
943 BerElement *ber = NULL;
946 Modifications **modtail = modlist;
949 char txtbuf[SLAP_TEXT_BUFLEN];
950 size_t textlen = sizeof txtbuf;
952 struct berval bdn = {0, NULL}, dn, ndn;
957 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
959 LDAP_LOG( OPERATION, ERR,
960 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
962 Debug( LDAP_DEBUG_ANY,
963 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
968 op->o_tag = LDAP_REQ_ADD;
970 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
972 if ( rc != LDAP_SUCCESS ) {
974 LDAP_LOG( OPERATION, ERR,
975 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
977 Debug( LDAP_DEBUG_ANY,
978 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
983 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
984 ber_dupbv( &op->o_req_dn, &dn );
985 ber_dupbv( &op->o_req_ndn, &ndn );
986 sl_free( ndn.bv_val, op->o_tmpmemctx );
987 sl_free( dn.bv_val, op->o_tmpmemctx );
989 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
994 if ( entry == NULL ) {
998 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1000 e->e_name = op->o_req_dn;
1001 e->e_nname = op->o_req_ndn;
1003 while ( ber_remaining( ber ) ) {
1004 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1005 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1010 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1012 mod->sml_op = LDAP_MOD_REPLACE;
1013 mod->sml_next = NULL;
1014 mod->sml_desc = NULL;
1015 mod->sml_type = tmp.sml_type;
1016 mod->sml_values = tmp.sml_values;
1017 mod->sml_nvalues = NULL;
1020 modtail = &mod->sml_next;
1023 if ( *modlist == NULL ) {
1025 LDAP_LOG( OPERATION, ERR,
1026 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1028 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1033 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1035 if ( rc != LDAP_SUCCESS ) {
1037 LDAP_LOG( OPERATION, ERR,
1038 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1040 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1046 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1047 if( rc != LDAP_SUCCESS ) {
1049 LDAP_LOG( OPERATION, ERR,
1050 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1052 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1058 ber_free ( ber, 0 );
1059 if ( rc != LDAP_SUCCESS ) {
1072 Modifications* modlist,
1074 struct berval* syncUUID,
1075 struct sync_cookie* syncCookie_req
1078 Backend *be = op->o_bd;
1079 slap_callback cb = { NULL };
1080 struct berval *syncuuid_bv = NULL;
1081 struct berval syncUUID_strrep = BER_BVNULL;
1082 struct berval uuid_bv = BER_BVNULL;
1084 SlapReply rs_search = {REP_RESULT};
1085 SlapReply rs_delete = {REP_RESULT};
1086 SlapReply rs_add = {REP_RESULT};
1087 SlapReply rs_modify = {REP_RESULT};
1089 AttributeAssertion ava = {0};
1090 int rc = LDAP_SUCCESS;
1091 int ret = LDAP_SUCCESS;
1094 struct berval pdn = BER_BVNULL;
1095 struct berval org_req_dn = BER_BVNULL;
1096 struct berval org_req_ndn = BER_BVNULL;
1097 struct berval org_dn = BER_BVNULL;
1098 struct berval org_ndn = BER_BVNULL;
1099 int org_managedsait;
1101 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1103 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1104 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1105 syncuuid_cmp, avl_dup_error );
1108 if ( syncstate == LDAP_SYNC_PRESENT ) {
1112 f.f_choice = LDAP_FILTER_EQUALITY;
1114 ava.aa_desc = slap_schema.si_ad_entryUUID;
1115 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1116 ava.aa_value = *syncUUID;
1117 op->ors_filter = &f;
1119 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1120 op->ors_filterstr.bv_val = (char *) sl_malloc(
1121 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1122 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1123 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1124 syncUUID->bv_val, syncUUID->bv_len );
1125 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1127 op->o_tag = LDAP_REQ_SEARCH;
1128 op->ors_scope = LDAP_SCOPE_SUBTREE;
1130 /* get syncrepl cookie of shadow replica from subentry */
1131 op->o_req_dn = si->si_base;
1132 op->o_req_ndn = si->si_base;
1134 op->o_time = slap_get_time();
1135 op->ors_tlimit = SLAP_NO_LIMIT;
1138 /* set callback function */
1139 op->o_callback = &cb;
1140 cb.sc_response = dn_callback;
1143 si->si_syncUUID_ndn.bv_val = NULL;
1145 if ( limits_check( op, &rs_search ) == 0 ) {
1146 rc = be->be_search( op, &rs_search );
1149 if ( op->ors_filterstr.bv_val ) {
1150 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1153 cb.sc_response = null_callback;
1156 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1159 if ( syncstate != LDAP_SYNC_DELETE ) {
1160 op->o_no_psearch = 1;
1163 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1164 if ( op->o_sync_csn.bv_val ) {
1165 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1170 op->o_req_dn = si->si_syncUUID_ndn;
1171 op->o_req_ndn = si->si_syncUUID_ndn;
1172 op->o_tag = LDAP_REQ_DELETE;
1173 rc = be->be_delete( op, &rs_delete );
1175 org_req_dn = op->o_req_dn;
1176 org_req_ndn = op->o_req_ndn;
1178 org_ndn = op->o_ndn;
1179 org_managedsait = get_manageDSAit( op );
1180 op->o_dn = op->o_bd->be_rootdn;
1181 op->o_ndn = op->o_bd->be_rootndn;
1182 op->o_managedsait = 1;
1184 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1185 op->o_delete_glue_parent = 0;
1186 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1187 slap_callback cb = { NULL };
1188 cb.sc_response = slap_null_cb;
1189 dnParent( &op->o_req_ndn, &pdn );
1191 op->o_req_ndn = pdn;
1192 op->o_callback = &cb;
1193 op->o_bd->be_delete( op, &rs_delete );
1199 op->o_managedsait = org_managedsait;
1201 op->o_ndn = org_ndn;
1202 op->o_req_dn = org_req_dn;
1203 op->o_req_ndn = org_req_ndn;
1204 op->o_delete_glue_parent = 0;
1206 op->o_no_psearch = 0;
1209 switch ( syncstate ) {
1211 case LDAP_SYNC_MODIFY:
1212 if ( rs_search.sr_err == LDAP_SUCCESS ||
1213 rs_search.sr_err == LDAP_REFERRAL ||
1214 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1215 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1217 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1218 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1219 &syncUUID_strrep, syncUUID );
1221 op->o_tag = LDAP_REQ_ADD;
1223 op->o_req_dn = e->e_name;
1224 op->o_req_ndn = e->e_nname;
1226 rc = be->be_add( op, &rs_add );
1228 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1229 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1230 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1232 Modifications *modtail = modlist;
1236 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1240 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1241 ber_dupbv( &uuid_bv, syncUUID );
1242 mod->sml_op = LDAP_MOD_REPLACE;
1243 mod->sml_desc = slap_schema.si_ad_entryUUID;
1244 mod->sml_type = mod->sml_desc->ad_cname;
1245 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1246 modtail->sml_next = mod;
1248 op->o_tag = LDAP_REQ_MODIFY;
1249 op->orm_modlist = modlist;
1250 op->o_req_dn = e->e_name;
1251 op->o_req_ndn = e->e_nname;
1253 rc = be->be_modify( op, &rs_modify );
1254 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1256 LDAP_LOG( OPERATION, ERR,
1257 "syncrepl_entry : be_modify failed (%d)\n",
1258 rs_modify.sr_err, 0, 0 );
1260 Debug( LDAP_DEBUG_ANY,
1261 "syncrepl_entry : be_modify failed (%d)\n",
1262 rs_modify.sr_err, 0, 0 );
1267 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1268 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1269 syncrepl_add_glue( op, e );
1274 LDAP_LOG( OPERATION, ERR,
1275 "syncrepl_entry : be_add failed (%d)\n",
1276 rs_add.sr_err, 0, 0 );
1278 Debug( LDAP_DEBUG_ANY,
1279 "syncrepl_entry : be_add failed (%d)\n",
1280 rs_add.sr_err, 0, 0 );
1286 be_entry_release_w( op, e );
1292 LDAP_LOG( OPERATION, ERR,
1293 "syncrepl_entry : be_search failed (%d)\n",
1294 rs_search.sr_err, 0, 0 );
1296 Debug( LDAP_DEBUG_ANY,
1297 "syncrepl_entry : be_search failed (%d)\n",
1298 rs_search.sr_err, 0, 0 );
1304 case LDAP_SYNC_DELETE :
1305 /* Already deleted */
1311 LDAP_LOG( OPERATION, ERR,
1312 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1314 Debug( LDAP_DEBUG_ANY,
1315 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1323 if ( syncUUID_strrep.bv_val ) {
1324 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1326 if ( si->si_syncUUID_ndn.bv_val ) {
1327 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1332 static struct berval gcbva[] = {
1339 syncrepl_del_nonpresent(
1344 Backend* be = op->o_bd;
1345 slap_callback cb = { NULL };
1346 SlapReply rs_search = {REP_RESULT};
1347 SlapReply rs_delete = {REP_RESULT};
1348 SlapReply rs_modify = {REP_RESULT};
1349 struct nonpresent_entry *np_list, *np_prev;
1352 Modifications *mlnext;
1354 Modifications *modlist = NULL;
1355 Modifications **modtail = &modlist;
1358 struct berval pdn = BER_BVNULL;
1359 struct berval org_req_dn = BER_BVNULL;
1360 struct berval org_req_ndn = BER_BVNULL;
1361 struct berval org_dn = BER_BVNULL;
1362 struct berval org_ndn = BER_BVNULL;
1363 int org_managedsait;
1365 op->o_req_dn = si->si_base;
1366 op->o_req_ndn = si->si_base;
1368 cb.sc_response = nonpresent_callback;
1371 op->o_callback = &cb;
1372 op->o_tag = LDAP_REQ_SEARCH;
1373 op->ors_scope = si->si_scope;
1374 op->ors_deref = LDAP_DEREF_NEVER;
1375 op->o_time = slap_get_time();
1376 op->ors_tlimit = SLAP_NO_LIMIT;
1377 op->ors_slimit = SLAP_NO_LIMIT;
1378 op->ors_attrsonly = 0;
1379 op->ors_attrs = NULL;
1380 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1381 op->ors_filterstr = si->si_filterstr;
1383 op->o_nocaching = 1;
1384 op->o_managedsait = 0;
1386 if ( limits_check( op, &rs_search ) == 0 ) {
1387 rc = be->be_search( op, &rs_search );
1390 op->o_managedsait = 1;
1391 op->o_nocaching = 0;
1393 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1395 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1396 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1397 while ( np_list != NULL ) {
1398 LDAP_LIST_REMOVE( np_list, npe_link );
1400 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1401 op->o_tag = LDAP_REQ_DELETE;
1402 op->o_callback = &cb;
1403 cb.sc_response = null_callback;
1405 op->o_req_dn = *np_prev->npe_name;
1406 op->o_req_ndn = *np_prev->npe_nname;
1407 rc = op->o_bd->be_delete( op, &rs_delete );
1409 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1410 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1411 mod->sml_op = LDAP_MOD_REPLACE;
1412 mod->sml_desc = slap_schema.si_ad_objectClass;
1413 mod->sml_type = mod->sml_desc->ad_cname;
1414 mod->sml_values = &gcbva[0];
1416 modtail = &mod->sml_next;
1418 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1419 mod->sml_op = LDAP_MOD_REPLACE;
1420 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1421 mod->sml_type = mod->sml_desc->ad_cname;
1422 mod->sml_values = &gcbva[1];
1424 modtail = &mod->sml_next;
1426 op->o_tag = LDAP_REQ_MODIFY;
1427 op->orm_modlist = modlist;
1429 rc = be->be_modify( op, &rs_modify );
1431 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1432 mlnext = ml->sml_next;
1437 org_req_dn = op->o_req_dn;
1438 org_req_ndn = op->o_req_ndn;
1440 org_ndn = op->o_ndn;
1441 org_managedsait = get_manageDSAit( op );
1442 op->o_dn = op->o_bd->be_rootdn;
1443 op->o_ndn = op->o_bd->be_rootndn;
1444 op->o_managedsait = 1;
1446 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1447 op->o_delete_glue_parent ) {
1448 op->o_delete_glue_parent = 0;
1449 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1450 slap_callback cb = { NULL };
1451 cb.sc_response = slap_null_cb;
1452 dnParent( &op->o_req_ndn, &pdn );
1454 op->o_req_ndn = pdn;
1455 op->o_callback = &cb;
1456 /* give it a root privil ? */
1457 op->o_bd->be_delete( op, &rs_delete );
1463 op->o_managedsait = org_managedsait;
1465 op->o_ndn = org_ndn;
1466 op->o_req_dn = org_req_dn;
1467 op->o_req_ndn = org_req_ndn;
1468 op->o_delete_glue_parent = 0;
1470 ber_bvfree( np_prev->npe_name );
1471 ber_bvfree( np_prev->npe_nname );
1472 op->o_req_dn.bv_val = NULL;
1473 op->o_req_ndn.bv_val = NULL;
1488 Backend *be = op->o_bd;
1489 slap_callback cb = { NULL };
1494 struct berval dn = {0, NULL};
1495 struct berval ndn = {0, NULL};
1497 SlapReply rs_add = {REP_RESULT};
1500 op->o_tag = LDAP_REQ_ADD;
1501 op->o_callback = &cb;
1502 cb.sc_response = null_callback;
1503 cb.sc_private = NULL;
1508 /* count RDNs in suffix */
1509 if ( be->be_nsuffix[0].bv_len ) {
1510 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1520 /* Start with BE suffix */
1521 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1522 comma = strrchr(dn.bv_val, ',');
1523 if ( ptr ) *ptr = ',';
1524 if ( comma ) *comma = '\0';
1529 dn.bv_len -= ptr - dn.bv_val;
1532 /* the normalizedDNs are always the same length, no counting
1535 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1536 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1537 ndn.bv_len = be->be_nsuffix[0].bv_len;
1540 while ( ndn.bv_val > e->e_nname.bv_val ) {
1541 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1542 ber_dupbv( &glue->e_name, &dn );
1543 ber_dupbv( &glue->e_nname, &ndn );
1545 a = ch_calloc( 1, sizeof( Attribute ));
1546 a->a_desc = slap_schema.si_ad_objectClass;
1548 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1549 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1550 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1551 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1553 a->a_nvals = a->a_vals;
1555 a->a_next = glue->e_attrs;
1558 a = ch_calloc( 1, sizeof( Attribute ));
1559 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1561 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1562 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1563 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1565 a->a_nvals = a->a_vals;
1567 a->a_next = glue->e_attrs;
1570 op->o_req_dn = glue->e_name;
1571 op->o_req_ndn = glue->e_nname;
1573 rc = be->be_add ( op, &rs_add );
1574 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1575 be_entry_release_w( op, glue );
1577 /* incl. ALREADY EXIST */
1581 /* Move to next child */
1582 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1585 if ( ptr == e->e_name.bv_val ) break;
1587 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1588 for( ptr = ndn.bv_val-2;
1589 ptr > e->e_nname.bv_val && *ptr != ',';
1595 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1598 op->o_req_dn = e->e_name;
1599 op->o_req_ndn = e->e_nname;
1601 rc = be->be_add ( op, &rs_add );
1602 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1603 be_entry_release_w( op, e );
1611 static struct berval ocbva[] = {
1613 BER_BVC("subentry"),
1614 BER_BVC("syncConsumerSubentry"),
1618 static struct berval cnbva[] = {
1623 static struct berval ssbva[] = {
1628 static struct berval scbva[] = {
1634 syncrepl_updateCookie(
1638 struct sync_cookie *syncCookie
1641 Backend *be = op->o_bd;
1643 Modifications *mlnext;
1645 Modifications *modlist = NULL;
1646 Modifications **modtail = &modlist;
1649 char txtbuf[SLAP_TEXT_BUFLEN];
1650 size_t textlen = sizeof txtbuf;
1655 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1656 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1657 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1659 slap_callback cb = { NULL };
1660 SlapReply rs_add = {REP_RESULT};
1661 SlapReply rs_modify = {REP_RESULT};
1663 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1664 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1666 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1667 mod->sml_op = LDAP_MOD_REPLACE;
1668 mod->sml_desc = slap_schema.si_ad_objectClass;
1669 mod->sml_type = mod->sml_desc->ad_cname;
1670 mod->sml_values = ocbva;
1672 modtail = &mod->sml_next;
1674 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1675 assert( si->si_rid < 1000 );
1676 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1677 slap_syncrepl_bvc.bv_len + 1,
1678 "syncrepl%ld", si->si_rid );
1679 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1680 mod->sml_op = LDAP_MOD_REPLACE;
1681 mod->sml_desc = slap_schema.si_ad_cn;
1682 mod->sml_type = mod->sml_desc->ad_cname;
1683 mod->sml_values = cnbva;
1685 modtail = &mod->sml_next;
1687 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1688 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1689 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1690 mod->sml_op = LDAP_MOD_REPLACE;
1691 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1692 mod->sml_type = mod->sml_desc->ad_cname;
1693 mod->sml_values = scbva;
1695 modtail = &mod->sml_next;
1697 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1698 mod->sml_op = LDAP_MOD_REPLACE;
1699 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1700 mod->sml_type = mod->sml_desc->ad_cname;
1701 mod->sml_values = ssbva;
1703 modtail = &mod->sml_next;
1707 op->o_tag = LDAP_REQ_ADD;
1708 rc = slap_mods_opattrs( op, modlist, modtail,
1709 &text,txtbuf, textlen );
1711 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1712 ml->sml_op = LDAP_MOD_REPLACE;
1715 if( rc != LDAP_SUCCESS ) {
1717 LDAP_LOG( OPERATION, ERR,
1718 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1720 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1725 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1727 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1728 assert( si->si_rid < 1000 );
1729 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1730 slap_syncrepl_cn_bvc.bv_len + 1,
1731 "cn=syncrepl%ld", si->si_rid );
1733 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1735 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1736 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1738 if ( slap_syncrepl_dn_bv.bv_val ) {
1739 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1744 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1746 if( rc != LDAP_SUCCESS ) {
1748 LDAP_LOG( OPERATION, ERR,
1749 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1751 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1756 cb.sc_response = null_callback;
1759 op->o_callback = &cb;
1760 op->o_req_dn = e->e_name;
1761 op->o_req_ndn = e->e_nname;
1763 /* update persistent cookie */
1764 update_cookie_retry:
1765 op->o_tag = LDAP_REQ_MODIFY;
1766 op->orm_modlist = modlist;
1767 rc = be->be_modify( op, &rs_modify );
1769 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1770 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1771 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1772 op->o_tag = LDAP_REQ_ADD;
1774 rc = be->be_add( op, &rs_add );
1775 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1776 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1777 goto update_cookie_retry;
1778 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1779 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1781 LDAP_LOG( OPERATION, ERR,
1782 "cookie will be non-persistent\n",
1785 Debug( LDAP_DEBUG_ANY,
1786 "cookie will be non-persistent\n",
1791 LDAP_LOG( OPERATION, ERR,
1792 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1794 Debug( LDAP_DEBUG_ANY,
1795 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1799 be_entry_release_w( op, e );
1804 LDAP_LOG( OPERATION, ERR,
1805 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1807 Debug( LDAP_DEBUG_ANY,
1808 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1819 if ( cnbva[0].bv_val ) {
1820 ch_free( cnbva[0].bv_val );
1821 cnbva[0].bv_val = NULL;
1823 if ( scbva[0].bv_val ) {
1824 ch_free( scbva[0].bv_val );
1825 scbva[0].bv_val = NULL;
1828 if ( mlnext->sml_next ) {
1829 slap_mods_free( mlnext->sml_next );
1830 mlnext->sml_next = NULL;
1833 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1834 mlnext = ml->sml_next;
1842 syncrepl_isupdate( Operation *op )
1844 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1848 syncrepl_isupdate_dn(
1856 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1857 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1858 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1872 syncinfo_t *si = op->o_callback->sc_private;
1874 if ( rs->sr_type == REP_SEARCH ) {
1875 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1877 LDAP_LOG( OPERATION, ERR,
1878 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1880 Debug( LDAP_DEBUG_ANY,
1881 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1884 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1886 } else if ( rs->sr_type == REP_RESULT ) {
1887 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1889 LDAP_LOG( OPERATION, ERR,
1890 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1892 Debug( LDAP_DEBUG_ANY,
1893 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1898 return LDAP_SUCCESS;
1902 nonpresent_callback(
1907 syncinfo_t *si = op->o_callback->sc_private;
1910 struct berval* present_uuid = NULL;
1911 struct nonpresent_entry *np_entry;
1913 if ( rs->sr_type == REP_RESULT ) {
1914 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1915 si->si_presentlist = NULL;
1917 } else if ( rs->sr_type == REP_SEARCH ) {
1918 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1920 if ( a == NULL ) return 0;
1922 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1925 if ( present_uuid == NULL ) {
1926 np_entry = (struct nonpresent_entry *)
1927 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1928 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1929 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1930 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1933 avl_delete( &si->si_presentlist,
1934 &a->a_nvals[0], syncuuid_cmp );
1935 ch_free( present_uuid->bv_val );
1936 ch_free( present_uuid );
1939 return LDAP_SUCCESS;
1947 if ( rs->sr_err != LDAP_SUCCESS &&
1948 rs->sr_err != LDAP_REFERRAL &&
1949 rs->sr_err != LDAP_ALREADY_EXISTS &&
1950 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1951 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1954 LDAP_LOG( OPERATION, ERR,
1955 "null_callback : error code 0x%x\n",
1958 Debug( LDAP_DEBUG_ANY,
1959 "null_callback : error code 0x%x\n",
1963 return LDAP_SUCCESS;
1967 slap_create_syncrepl_entry(
1969 struct berval *context_csn,
1977 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1979 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1981 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1984 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1986 if ( context_csn ) {
1987 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1988 context_csn, NULL );
1992 bv.bv_len = sizeof("{}")-1;
1993 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1995 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1996 ber_dupbv( &e->e_nname, &e->e_name );
2002 slap_uuidstr_from_normalized(
2003 struct berval* uuidstr,
2004 struct berval* normalized,
2008 unsigned char nibble;
2011 if ( normalized == NULL )
2014 if ( normalized->bv_len != 16 ) {
2021 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
2026 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2028 sl_free( new, ctx );
2032 for ( i = 0; i < 16; i++ ) {
2033 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2034 new->bv_val[(i<<1)+d] = '-';
2038 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2039 if ( nibble < 10 ) {
2040 new->bv_val[(i<<1)+d] = nibble + '0';
2042 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2045 nibble = (normalized->bv_val[i]) & 0xF;
2046 if ( nibble < 10 ) {
2047 new->bv_val[(i<<1)+d+1] = nibble + '0';
2049 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2053 new->bv_val[new->bv_len] = '\0';
2059 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2061 const struct berval *uuid1 = v_uuid1;
2062 const struct berval *uuid2 = v_uuid2;
2063 int rc = uuid1->bv_len - uuid2->bv_len;
2064 if ( rc ) return rc;
2065 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2069 avl_ber_bvfree( void *bv )
2074 if ( ((struct berval *)bv)->bv_val != NULL ) {
2075 ch_free ( ((struct berval *)bv)->bv_val );
2077 ch_free ( (char *) bv );