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>
28 #include "lutil_ldap.h"
32 /* FIXME: for ldap_ld_free() */
34 #include "../../libraries/libldap/ldap-int.h"
36 #define SYNCREPL_STR "syncreplxxx"
39 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
40 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
42 static int syncuuid_cmp( const void *, const void * );
43 static void avl_ber_bvfree( void * );
44 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
46 /* callback functions */
47 static int dn_callback( struct slap_op *, struct slap_rep * );
48 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
49 static int null_callback( struct slap_op *, struct slap_rep * );
51 static int si_refreshDelete = 0;
52 static int si_refreshPresent = 0;
54 static AttributeDescription *sync_descs[4];
56 struct runqueue_s syncrepl_rq;
59 init_syncrepl(syncinfo_t *si)
62 char **attrs, **exattrs;
64 if ( !sync_descs[0] ) {
65 sync_descs[0] = slap_schema.si_ad_objectClass;
66 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
67 sync_descs[2] = slap_schema.si_ad_entryCSN;
71 if ( si->si_allattrs && si->si_allopattrs )
74 attrs = anlist2attrs( si->si_anlist );
77 if ( si->si_allattrs ) {
80 if ( !is_at_operational( at_find( attrs[i] ))) {
81 for ( j = i; attrs[j] != NULL; j++ ) {
84 attrs[j] = attrs[j+1];
90 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
91 attrs[i] = ch_strdup("*");
94 } else if ( si->si_allopattrs ) {
97 if ( is_at_operational( at_find( attrs[i] ))) {
98 for ( j = i; attrs[j] != NULL; j++ ) {
101 attrs[j] = attrs[j+1];
107 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
108 attrs[i] = ch_strdup("+");
112 for ( i = 0; sync_descs[i] != NULL; i++ ) {
115 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
116 for ( k = j; attrs[k] != NULL; k++ ) {
119 attrs[k] = attrs[k+1];
127 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
129 if ( si->si_allopattrs ) {
130 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
132 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
135 if ( attrs == NULL ) {
136 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
140 if ( si->si_allopattrs ) {
141 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
143 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
144 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
152 if ( si->si_allattrs == si->si_allopattrs ) {
153 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
154 attrs[i++] = ch_strdup( "*" );
155 attrs[i++] = ch_strdup( "+" );
156 } else if ( si->si_allattrs && !si->si_allopattrs ) {
157 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
158 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
159 attrs[i++] = ch_strdup( "*" );
160 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
161 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
163 } else if ( !si->si_allattrs && si->si_allopattrs ) {
164 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
165 attrs[i++] = ch_strdup( "+" );
166 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
171 si->si_attrs = attrs;
173 exattrs = anlist2attrs( si->si_exanlist );
176 for ( n = 0; exattrs[n] != NULL; n++ ) ;
178 for ( i = 0; sync_descs[i] != NULL; i++ ) {
180 while ( exattrs[j] != NULL ) {
181 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
182 for ( k = j; exattrs[k] != NULL; k++ ) {
184 ch_free( exattrs[k] );
185 exattrs[k] = exattrs[k+1];
193 for ( i = 0; exattrs[i] != NULL; i++ ) {
194 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
196 if ( ( oc = si->si_anlist[j].an_oc ) ) {
198 while ( oc->soc_required[k] ) {
199 if ( !strcmp( exattrs[i],
200 oc->soc_required[k]->sat_cname.bv_val )) {
201 for ( l = i; exattrs[l]; l++ ) {
203 ch_free( exattrs[i] );
204 exattrs[l] = exattrs[l+1];
214 for ( i = 0; exattrs[i] != NULL; i++ ) ;
217 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
220 si->si_exattrs = exattrs;
228 BerElementBuffer berbuf;
229 BerElement *ber = (BerElement *)&berbuf;
230 LDAPControl c[2], *ctrls[3];
231 struct timeval timeout;
235 /* setup LDAP SYNC control */
236 ber_init2( ber, NULL, LBER_USE_DER );
237 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
239 if ( si->si_syncCookie.octet_str &&
240 !BER_BVISNULL( &si->si_syncCookie.octet_str[0] ) )
242 ber_printf( ber, "{eO}",
244 &si->si_syncCookie.octet_str[0] );
246 ber_printf( ber, "{e}",
250 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
255 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
256 c[0].ldctl_iscritical = si->si_type < 0;
259 if ( si->si_authzId ) {
260 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
261 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
262 c[1].ldctl_iscritical = 1;
269 timeout.tv_sec = si->si_tlimit;
272 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
273 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
274 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
275 si->si_slimit, &msgid );
286 int cmdline_cookie_found = 0;
288 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
289 struct berval syncrepl_cn_bv;
290 struct sync_cookie *sc = NULL;
296 psub = &si->si_be->be_nsuffix[0];
298 /* Init connection to master */
299 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
300 if ( rc != LDAP_SUCCESS ) {
301 Debug( LDAP_DEBUG_ANY,
302 "do_syncrep1: ldap_initialize failed (%s)\n",
303 si->si_provideruri, 0, 0 );
307 op->o_protocol = LDAP_VERSION3;
308 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
313 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
314 if( rc != LDAP_SUCCESS ) {
315 Debug( LDAP_DEBUG_ANY,
316 "%s: ldap_start_tls failed (%d)\n",
317 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
319 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
323 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
324 #ifdef HAVE_CYRUS_SASL
327 if ( si->si_secprops != NULL ) {
328 rc = ldap_set_option( si->si_ld,
329 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
331 if( rc != LDAP_OPT_SUCCESS ) {
332 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
333 "(%s,SECPROPS,\"%s\") failed!\n",
334 si->si_provideruri, si->si_secprops, 0 );
339 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
340 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
342 rc = ldap_sasl_interactive_bind_s( si->si_ld,
350 lutil_sasl_freedefs( defaults );
352 /* FIXME: different error behaviors according to
354 * 2) on err policy : exit, retry, backoff ...
356 if ( rc != LDAP_SUCCESS ) {
357 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
358 "ldap_sasl_interactive_bind_s failed (%d)\n",
361 /* FIXME (see above comment) */
362 /* if Kerberos credentials cache is not active, retry */
363 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
364 rc == LDAP_LOCAL_ERROR )
366 rc = LDAP_SERVER_DOWN;
371 #else /* HAVE_CYRUS_SASL */
372 /* Should never get here, we trapped this at config time */
374 fprintf( stderr, "not compiled with SASL support\n" );
380 rc = ldap_bind_s( si->si_ld,
381 si->si_binddn, si->si_passwd, si->si_bindmethod );
382 if ( rc != LDAP_SUCCESS ) {
383 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
384 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
389 /* Set SSF to strongest of TLS, SASL SSFs */
392 op->o_transport_ssf = 0;
394 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
395 == LDAP_SUCCESS && ssl != NULL )
397 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
399 #endif /* HAVE_TLS */
400 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
401 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
402 ? op->o_sasl_ssf : op->o_tls_ssf;
404 /* get syncrepl cookie of shadow replica from subentry */
405 assert( si->si_rid < 1000 );
406 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
407 syncrepl_cn_bv.bv_len = snprintf( syncrepl_cbuf, sizeof(syncrepl_cbuf),
408 CN_STR "syncrepl%ld", si->si_rid );
409 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
410 op->o_req_dn = op->o_req_ndn;
412 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
413 if ( si->si_rid == sc->rid ) {
414 cmdline_cookie_found = 1;
419 if ( cmdline_cookie_found ) {
420 /* cookie is supplied in the command line */
421 BerVarray cookie = NULL;
422 struct berval cookie_bv;
424 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
425 slap_sync_cookie_free( &si->si_syncCookie, 0 );
427 /* read stored cookie if it exists */
428 backend_attribute( op, NULL, &op->o_req_ndn,
429 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
432 /* no stored cookie */
433 if ( sc->ctxcsn == NULL ||
434 BER_BVISNULL( sc->ctxcsn ) ) {
435 /* if cmdline cookie does not have ctxcsn */
436 /* component, set it to an initial value */
437 slap_init_sync_cookie_ctxcsn( sc );
439 slap_dup_sync_cookie( &si->si_syncCookie, sc );
440 slap_sync_cookie_free( sc, 1 );
445 struct berval newcookie = BER_BVNULL;
446 ber_dupbv( &cookie_bv, &cookie[0] );
447 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
448 slap_parse_sync_cookie( &si->si_syncCookie );
449 ber_bvarray_free( si->si_syncCookie.octet_str );
450 si->si_syncCookie.octet_str = NULL;
451 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
452 if ( sc->sid != -1 ) {
453 /* command line cookie wins */
454 si->si_syncCookie.sid = sc->sid;
456 if ( sc->ctxcsn != NULL ) {
457 /* command line cookie wins */
458 if ( si->si_syncCookie.ctxcsn ) {
459 ber_bvarray_free( si->si_syncCookie.ctxcsn );
460 si->si_syncCookie.ctxcsn = NULL;
462 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
463 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
465 if ( sc->rid != -1 ) {
466 /* command line cookie wins */
467 si->si_syncCookie.rid = sc->rid;
469 slap_sync_cookie_free( sc, 1 );
471 slap_compose_sync_cookie( NULL, &newcookie,
472 &si->si_syncCookie.ctxcsn[0],
473 si->si_syncCookie.sid, si->si_syncCookie.rid );
474 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
478 /* no command line cookie is specified */
479 if ( si->si_syncCookie.octet_str == NULL ) {
480 BerVarray cookie = NULL;
481 struct berval cookie_bv;
482 /* try to read stored cookie */
483 backend_attribute( op, NULL, &op->o_req_ndn,
484 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
486 ber_dupbv( &cookie_bv, &cookie[0] );
487 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
488 slap_parse_sync_cookie( &si->si_syncCookie );
489 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
494 rc = ldap_sync_search( si, op->o_tmpmemctx );
496 if( rc != LDAP_SUCCESS ) {
497 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
498 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
504 ldap_unbind( si->si_ld );
509 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
519 LDAPControl **rctrls = NULL;
522 BerElementBuffer berbuf;
523 BerElement *ber = (BerElement *)&berbuf;
525 LDAPMessage *res = NULL;
526 LDAPMessage *msg = NULL;
529 struct berval *retdata = NULL;
534 struct berval syncUUID = BER_BVNULL;
535 struct sync_cookie syncCookie = { NULL, -1, NULL };
536 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
537 struct berval cookie = BER_BVNULL;
545 Modifications *modlist = NULL;
550 struct timeval *tout_p = NULL;
551 struct timeval tout = { 0, 0 };
553 int refreshDeletes = 0;
555 BerVarray syncUUIDs = NULL;
558 if ( slapd_shutdown ) {
563 ber_init2( ber, NULL, LBER_USE_DER );
564 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
566 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
568 psub = &si->si_be->be_nsuffix[0];
570 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
572 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
578 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
579 tout_p, &res )) > 0 )
581 if ( slapd_shutdown ) {
585 for( msg = ldap_first_message( si->si_ld, res );
587 msg = ldap_next_message( si->si_ld, msg ) )
589 switch( ldap_msgtype( msg ) ) {
590 case LDAP_RES_SEARCH_ENTRY:
591 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
592 /* we can't work without the control */
594 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
595 "got search entry without "
596 "control\n", 0, 0, 0 );
601 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
602 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
603 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
604 ber_scanf( ber, /*"{"*/ "m}", &cookie );
605 if ( !BER_BVISNULL( &cookie ) ) {
606 struct berval tmp_bv;
607 ber_dupbv( &tmp_bv, &cookie );
608 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
610 if ( syncCookie.octet_str &&
611 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
613 slap_parse_sync_cookie( &syncCookie );
616 if ( syncrepl_message_to_entry( si, op, msg,
617 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
618 rc_efree = syncrepl_entry( si, op, entry, modlist,
619 syncstate, &syncUUID, &syncCookie_req );
620 if ( syncCookie.octet_str &&
621 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
623 syncrepl_updateCookie( si, op, psub, &syncCookie );
626 ldap_controls_free( rctrls );
628 slap_mods_free( modlist );
630 if ( rc_efree && entry ) {
636 case LDAP_RES_SEARCH_REFERENCE:
637 Debug( LDAP_DEBUG_ANY,
638 "do_syncrep2: reference received error\n", 0, 0, 0 );
641 case LDAP_RES_SEARCH_RESULT:
642 Debug( LDAP_DEBUG_SYNC,
643 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
644 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
648 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
650 ber_scanf( ber, "{" /*"}"*/);
651 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
652 ber_scanf( ber, "m", &cookie );
653 if ( !BER_BVISNULL( &cookie ) ) {
654 struct berval tmp_bv;
655 ber_dupbv( &tmp_bv, &cookie );
656 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
658 if ( syncCookie.octet_str &&
659 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
661 slap_parse_sync_cookie( &syncCookie );
664 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
666 ber_scanf( ber, "b", &refreshDeletes );
668 ber_scanf( ber, /*"{"*/ "}" );
670 if ( syncCookie_req.ctxcsn == NULL ) {
672 } else if ( syncCookie.ctxcsn == NULL ) {
675 value_match( &match, slap_schema.si_ad_entryCSN,
676 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
677 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
678 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
681 if ( syncCookie.octet_str && !BER_BVISNULL( syncCookie.octet_str ) &&
682 match < 0 && err == LDAP_SUCCESS )
684 syncrepl_updateCookie( si, op, psub, &syncCookie );
687 ldap_controls_free( rctrls );
689 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
690 /* FIXME : different error behaviors according to
691 * 1) err code : LDAP_BUSY ...
692 * 2) on err policy : stop service, stop sync, retry
694 if ( refreshDeletes == 0 && match < 0 &&
695 err == LDAP_SUCCESS )
697 syncrepl_del_nonpresent( op, si );
699 avl_free( si->si_presentlist, avl_ber_bvfree );
700 si->si_presentlist = NULL;
707 case LDAP_RES_INTERMEDIATE:
708 rc = ldap_parse_intermediate( si->si_ld, msg,
709 &retoid, &retdata, NULL, 0 );
710 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
711 ber_init2( ber, retdata, LBER_USE_DER );
713 switch ( si_tag = ber_peek_tag( ber, &len )) {
715 case LDAP_TAG_SYNC_NEW_COOKIE:
716 Debug( LDAP_DEBUG_SYNC,
717 "do_syncrep2: %s - %s%s\n",
718 "LDAP_RES_INTERMEDIATE",
719 "NEW_COOKIE", "\n" );
720 ber_scanf( ber, "tm", &tag, &cookie );
722 case LDAP_TAG_SYNC_REFRESH_DELETE:
723 Debug( LDAP_DEBUG_SYNC,
724 "do_syncrep2: %s - %s%s\n",
725 "LDAP_RES_INTERMEDIATE",
726 "REFRESH_DELETE\n", "\n" );
727 si_refreshDelete = 1;
728 case LDAP_TAG_SYNC_REFRESH_PRESENT:
729 Debug( LDAP_DEBUG_SYNC,
730 "do_syncrep2: %s - %s%s\n",
731 "LDAP_RES_INTERMEDIATE",
732 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
733 "REFRESH_PRESENT" : "REFRESH_DELETE",
735 si_refreshDelete = 1;
736 si_refreshPresent = 1;
737 ber_scanf( ber, "t{" /*"}"*/, &tag );
738 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
740 ber_scanf( ber, "m", &cookie );
741 if ( !BER_BVISNULL( &cookie ) ) {
742 struct berval tmp_bv;
743 ber_dupbv( &tmp_bv, &cookie );
744 ber_bvarray_add( &syncCookie.octet_str,
747 if ( syncCookie.octet_str &&
748 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
750 slap_parse_sync_cookie( &syncCookie );
753 if ( ber_peek_tag( ber, &len ) ==
754 LDAP_TAG_REFRESHDONE )
756 ber_scanf( ber, "b", &refreshDone );
758 ber_scanf( ber, /*"{"*/ "}" );
760 case LDAP_TAG_SYNC_ID_SET:
761 Debug( LDAP_DEBUG_SYNC,
762 "do_syncrep2: %s - %s%s\n",
763 "LDAP_RES_INTERMEDIATE",
766 ber_scanf( ber, "t{" /*"}"*/, &tag );
767 if ( ber_peek_tag( ber, &len ) ==
768 LDAP_TAG_SYNC_COOKIE )
770 ber_scanf( ber, "m", &cookie );
771 if ( !BER_BVISNULL( &cookie ) ) {
772 struct berval tmp_bv;
773 ber_dupbv( &tmp_bv, &cookie );
774 ber_bvarray_add( &syncCookie.octet_str,
777 if ( syncCookie.octet_str &&
778 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
780 slap_parse_sync_cookie( &syncCookie );
783 if ( ber_peek_tag( ber, &len ) ==
784 LDAP_TAG_REFRESHDELETES )
786 ber_scanf( ber, "b", &refreshDeletes );
788 ber_scanf( ber, "[W]", &syncUUIDs );
789 ber_scanf( ber, /*"{"*/ "}" );
790 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
791 struct berval *syncuuid_bv;
792 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
793 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
794 avl_insert( &si->si_presentlist,
795 (caddr_t) syncuuid_bv,
796 syncuuid_cmp, avl_dup_error );
798 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
801 Debug( LDAP_DEBUG_ANY,
802 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
803 (long) si_tag, 0, 0 );
804 ldap_memfree( retoid );
805 ber_bvfree( retdata );
809 if ( syncCookie_req.ctxcsn == NULL ) {
811 } else if ( syncCookie.ctxcsn == NULL ) {
814 value_match( &match, slap_schema.si_ad_entryCSN,
815 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
816 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
817 &syncCookie_req.ctxcsn[0],
818 &syncCookie.ctxcsn[0], &text );
821 if ( syncCookie.ctxcsn && !BER_BVISNULL( &syncCookie.ctxcsn[0] ) &&
824 syncrepl_updateCookie( si, op, psub, &syncCookie);
827 if ( si_refreshPresent == 1 ) {
829 syncrepl_del_nonpresent( op, si );
833 ldap_memfree( retoid );
834 ber_bvfree( retdata );
838 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
839 "unknown intermediate response (%d)\n",
841 ldap_memfree( retoid );
842 ber_bvfree( retdata );
848 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
849 "unknown message\n", 0, 0, 0 );
853 if ( syncCookie.octet_str ) {
854 slap_sync_cookie_free( &syncCookie_req, 0 );
855 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
856 slap_sync_cookie_free( &syncCookie, 0 );
866 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
867 errstr = ldap_err2string( rc );
869 Debug( LDAP_DEBUG_ANY,
870 "do_syncrep2 : %s\n", errstr, 0, 0 );
874 slap_sync_cookie_free( &syncCookie, 0 );
875 slap_sync_cookie_free( &syncCookie_req, 0 );
877 if ( res ) ldap_msgfree( res );
879 if ( rc && si->si_ld ) {
880 ldap_unbind( si->si_ld );
892 struct re_s* rtask = arg;
893 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
894 Connection conn = {0};
896 int rc = LDAP_SUCCESS;
903 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
908 switch( abs( si->si_type )) {
909 case LDAP_SYNC_REFRESH_ONLY:
910 case LDAP_SYNC_REFRESH_AND_PERSIST:
916 if ( slapd_shutdown && si->si_ld ) {
917 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
918 connection_client_stop( s );
919 ldap_unbind( si->si_ld );
924 connection_fake_init( &conn, &op, ctx );
926 /* use global malloc for now */
927 op.o_tmpmemctx = NULL;
928 op.o_tmpmfuncs = &ch_mfuncs;
930 op.o_dn = si->si_updatedn;
931 op.o_ndn = si->si_updatedn;
932 op.o_managedsait = 1;
933 op.o_bd = be = si->si_be;
935 op.o_sync_state.ctxcsn = NULL;
936 op.o_sync_state.sid = -1;
937 op.o_sync_state.octet_str = NULL;
938 op.o_sync_slog_size = -1;
939 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
940 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
942 /* Establish session, do search */
945 si_refreshDelete = 0;
946 si_refreshPresent = 0;
947 rc = do_syncrep1( &op, si );
950 /* Process results */
951 if ( rc == LDAP_SUCCESS ) {
952 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
954 rc = do_syncrep2( &op, si );
956 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
957 /* If we succeeded, enable the connection for further listening.
958 * If we failed, tear down the connection and reschedule.
960 if ( rc == LDAP_SUCCESS ) {
962 rc = connection_client_setup( s, do_syncrepl, arg );
964 connection_client_enable( s );
966 } else if ( !first ) {
970 if ( rc == -2 ) rc = 0;
974 /* At this point, we have 4 cases:
975 * 1) for any hard failure, give up and remove this task
976 * 2) for ServerDown, reschedule this task to run
977 * 3) for Refresh and Success, reschedule to run
978 * 4) for Persist and Success, reschedule to defer
980 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
982 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
983 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
987 connection_client_stop( s );
990 if ( rc == LDAP_SUCCESS ) {
991 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
994 rtask->interval.tv_sec = si->si_interval;
995 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
996 if ( si->si_retrynum ) {
997 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
998 si->si_retrynum[i] = si->si_retrynum_init[i];
1000 si->si_retrynum[i] = -2;
1003 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1004 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
1008 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1009 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
1010 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
1011 syncinfo_free( si );
1012 } else if ( si->si_retrynum[i] >= -1 ) {
1013 if ( si->si_retrynum[i] > 0 )
1014 si->si_retrynum[i]--;
1015 rtask->interval.tv_sec = si->si_retryinterval[i];
1016 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
1017 slap_wake_listener();
1021 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
1027 syncrepl_message_to_entry(
1031 Modifications **modlist,
1037 BerElement *ber = NULL;
1040 Modifications **modtail = modlist;
1043 char txtbuf[SLAP_TEXT_BUFLEN];
1044 size_t textlen = sizeof txtbuf;
1046 struct berval bdn = {0, NULL}, dn, ndn;
1051 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1052 Debug( LDAP_DEBUG_ANY,
1053 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1057 op->o_tag = LDAP_REQ_ADD;
1059 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1061 if ( rc != LDAP_SUCCESS ) {
1062 Debug( LDAP_DEBUG_ANY,
1063 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1067 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1068 ber_dupbv( &op->o_req_dn, &dn );
1069 ber_dupbv( &op->o_req_ndn, &ndn );
1070 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1071 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1073 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1076 return LDAP_SUCCESS;
1079 if ( entry == NULL ) {
1083 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1085 e->e_name = op->o_req_dn;
1086 e->e_nname = op->o_req_ndn;
1088 while ( ber_remaining( ber ) ) {
1089 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1090 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1095 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1097 mod->sml_op = LDAP_MOD_REPLACE;
1098 mod->sml_next = NULL;
1099 mod->sml_desc = NULL;
1100 mod->sml_type = tmp.sml_type;
1101 mod->sml_values = tmp.sml_values;
1102 mod->sml_nvalues = NULL;
1105 modtail = &mod->sml_next;
1108 if ( *modlist == NULL ) {
1109 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1115 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1117 if ( rc != LDAP_SUCCESS ) {
1118 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1123 /* Strip out dynamically generated attrs */
1124 for ( modtail = modlist; *modtail ; ) {
1126 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1127 *modtail = mod->sml_next;
1128 slap_mod_free( &mod->sml_mod, 0 );
1131 modtail = &mod->sml_next;
1135 /* Strip out attrs in exattrs list */
1136 for ( modtail = modlist; *modtail ; ) {
1138 if ( ldap_charray_inlist( si->si_exattrs,
1139 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1140 *modtail = mod->sml_next;
1141 slap_mod_free( &mod->sml_mod, 0 );
1144 modtail = &mod->sml_next;
1148 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1149 if( rc != LDAP_SUCCESS ) {
1150 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1155 ber_free ( ber, 0 );
1156 if ( rc != LDAP_SUCCESS ) {
1171 Modifications* modlist,
1173 struct berval* syncUUID,
1174 struct sync_cookie* syncCookie_req )
1176 Backend *be = op->o_bd;
1177 slap_callback cb = { NULL };
1178 struct berval *syncuuid_bv = NULL;
1179 struct berval syncUUID_strrep = BER_BVNULL;
1180 struct berval uuid_bv = BER_BVNULL;
1182 SlapReply rs_search = {REP_RESULT};
1183 SlapReply rs_delete = {REP_RESULT};
1184 SlapReply rs_add = {REP_RESULT};
1185 SlapReply rs_modify = {REP_RESULT};
1187 AttributeAssertion ava = {0};
1188 int rc = LDAP_SUCCESS;
1189 int ret = LDAP_SUCCESS;
1191 struct berval pdn = BER_BVNULL;
1192 struct berval org_req_dn = BER_BVNULL;
1193 struct berval org_req_ndn = BER_BVNULL;
1194 struct berval org_dn = BER_BVNULL;
1195 struct berval org_ndn = BER_BVNULL;
1196 int org_managedsait;
1198 Debug( LDAP_DEBUG_SYNC, "%s: %s",
1200 "LDAP_RES_SEARCH_ENTRY", 0 );
1201 switch( syncstate ) {
1202 case LDAP_SYNC_PRESENT:
1203 Debug( LDAP_DEBUG_SYNC, "%s: %s",
1205 "LDAP_SYNC_PRESENT", "\n" );
1208 Debug( LDAP_DEBUG_SYNC, "%s: %s",
1210 "LDAP_SYNC_ADD", "\n" );
1212 case LDAP_SYNC_DELETE:
1213 Debug( LDAP_DEBUG_SYNC, "%s: %s",
1215 "LDAP_SYNC_DELETE", "\n" );
1217 case LDAP_SYNC_MODIFY:
1218 Debug( LDAP_DEBUG_SYNC, "%s: %s",
1220 "LDAP_SYNC_MODIFY", "\n" );
1223 Debug( LDAP_DEBUG_ANY, "%s: %s",
1225 "UNKNONW syncstate", "\n" );
1228 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1229 if ( !si_refreshPresent ) {
1230 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1231 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1232 syncuuid_cmp, avl_dup_error );
1236 if ( syncstate == LDAP_SYNC_PRESENT ) {
1238 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1239 if ( entry == NULL ) {
1244 f.f_choice = LDAP_FILTER_EQUALITY;
1246 ava.aa_desc = slap_schema.si_ad_entryUUID;
1247 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1248 ava.aa_value = *syncUUID;
1249 op->ors_filter = &f;
1251 op->ors_filterstr.bv_len = STRLENOF( "entryUUID=" ) + syncUUID->bv_len;
1252 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1253 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1254 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", STRLENOF( "entryUUID=" ) );
1255 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "entryUUID=" )],
1256 syncUUID->bv_val, syncUUID->bv_len );
1257 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1259 op->o_tag = LDAP_REQ_SEARCH;
1260 op->ors_scope = LDAP_SCOPE_SUBTREE;
1262 /* get syncrepl cookie of shadow replica from subentry */
1263 op->o_req_dn = si->si_base;
1264 op->o_req_ndn = si->si_base;
1266 op->o_time = slap_get_time();
1267 op->ors_tlimit = SLAP_NO_LIMIT;
1270 op->ors_attrs = slap_anlist_no_attrs;
1271 op->ors_attrsonly = 1;
1273 /* set callback function */
1274 op->o_callback = &cb;
1275 cb.sc_response = dn_callback;
1278 BER_BVZERO( &si->si_syncUUID_ndn );
1280 if ( limits_check( op, &rs_search ) == 0 ) {
1281 rc = be->be_search( op, &rs_search );
1282 Debug( LDAP_DEBUG_SYNC,
1283 "syncrepl_entry: %s (%d)\n",
1284 "be_search", rc, 0 );
1287 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1288 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1291 cb.sc_response = null_callback;
1294 if ( entry && entry->e_name.bv_val ) {
1295 Debug( LDAP_DEBUG_SYNC,
1296 "syncrepl_entry: %s\n",
1297 entry->e_name.bv_val, 0, 0 );
1299 Debug( LDAP_DEBUG_SYNC,
1300 "syncrepl_entry: %s\n",
1301 si->si_syncUUID_ndn.bv_val, 0, 0 );
1304 if ( rs_search.sr_err == LDAP_SUCCESS &&
1305 !BER_BVISNULL( &si->si_syncUUID_ndn ))
1309 if ( syncstate != LDAP_SYNC_DELETE ) {
1310 op->o_no_psearch = 1;
1313 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1314 if ( !BER_BVISNULL( &op->o_sync_csn ) ) {
1315 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1320 op->o_req_dn = si->si_syncUUID_ndn;
1321 op->o_req_ndn = si->si_syncUUID_ndn;
1322 op->o_tag = LDAP_REQ_DELETE;
1323 rc = be->be_delete( op, &rs_delete );
1324 Debug( LDAP_DEBUG_SYNC,
1325 "syncrepl_entry: %s (%d)\n",
1326 "be_delete", rc, 0 );
1328 org_req_dn = op->o_req_dn;
1329 org_req_ndn = op->o_req_ndn;
1331 org_ndn = op->o_ndn;
1332 org_managedsait = get_manageDSAit( op );
1333 op->o_dn = op->o_bd->be_rootdn;
1334 op->o_ndn = op->o_bd->be_rootndn;
1335 op->o_managedsait = 1;
1337 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1338 op->o_delete_glue_parent = 0;
1339 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1340 slap_callback cb = { NULL };
1341 cb.sc_response = slap_null_cb;
1342 dnParent( &op->o_req_ndn, &pdn );
1344 op->o_req_ndn = pdn;
1345 op->o_callback = &cb;
1346 op->o_bd->be_delete( op, &rs_delete );
1352 op->o_managedsait = org_managedsait;
1354 op->o_ndn = org_ndn;
1355 op->o_req_dn = org_req_dn;
1356 op->o_req_ndn = org_req_ndn;
1357 op->o_delete_glue_parent = 0;
1359 op->o_no_psearch = 0;
1362 switch ( syncstate ) {
1364 case LDAP_SYNC_MODIFY:
1365 if ( rs_search.sr_err == LDAP_SUCCESS ||
1366 rs_search.sr_err == LDAP_REFERRAL ||
1367 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1368 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1370 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1371 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1372 &syncUUID_strrep, syncUUID );
1374 op->o_tag = LDAP_REQ_ADD;
1376 op->o_req_dn = entry->e_name;
1377 op->o_req_ndn = entry->e_nname;
1379 rc = be->be_add( op, &rs_add );
1380 Debug( LDAP_DEBUG_SYNC,
1381 "syncrepl_entry: %s (%d)\n",
1384 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1385 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1386 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1388 Modifications *modtail = modlist;
1392 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1396 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1397 ber_dupbv( &uuid_bv, syncUUID );
1398 mod->sml_op = LDAP_MOD_REPLACE;
1399 mod->sml_desc = slap_schema.si_ad_entryUUID;
1400 mod->sml_type = mod->sml_desc->ad_cname;
1401 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1402 modtail->sml_next = mod;
1404 op->o_tag = LDAP_REQ_MODIFY;
1405 op->orm_modlist = modlist;
1406 op->o_req_dn = entry->e_name;
1407 op->o_req_ndn = entry->e_nname;
1409 rc = be->be_modify( op, &rs_modify );
1410 Debug( LDAP_DEBUG_SYNC,
1411 "syncrepl_entry: %s (%d)\n",
1412 "be_modify", rc, 0 );
1413 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1414 Debug( LDAP_DEBUG_ANY,
1415 "syncrepl_entry : be_modify failed (%d)\n",
1416 rs_modify.sr_err, 0, 0 );
1420 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1421 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1422 syncrepl_add_glue( op, entry );
1426 Debug( LDAP_DEBUG_ANY,
1427 "syncrepl_entry : be_add failed (%d)\n",
1428 rs_add.sr_err, 0, 0 );
1433 be_entry_release_w( op, entry );
1438 Debug( LDAP_DEBUG_ANY,
1439 "syncrepl_entry : be_search failed (%d)\n",
1440 rs_search.sr_err, 0, 0 );
1445 case LDAP_SYNC_DELETE :
1446 /* Already deleted */
1451 Debug( LDAP_DEBUG_ANY,
1452 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1459 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1460 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1461 BER_BVZERO( &syncUUID_strrep );
1463 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1464 ch_free( si->si_syncUUID_ndn.bv_val );
1465 BER_BVZERO( &si->si_syncUUID_ndn );
1470 static struct berval gcbva[] = {
1477 syncrepl_del_nonpresent(
1481 Backend* be = op->o_bd;
1482 slap_callback cb = { NULL };
1483 SlapReply rs_search = {REP_RESULT};
1484 SlapReply rs_delete = {REP_RESULT};
1485 SlapReply rs_modify = {REP_RESULT};
1486 struct nonpresent_entry *np_list, *np_prev;
1489 Modifications *mlnext;
1491 Modifications *modlist = NULL;
1492 Modifications **modtail = &modlist;
1493 AttributeName an[2];
1495 struct berval pdn = BER_BVNULL;
1496 struct berval org_req_dn = BER_BVNULL;
1497 struct berval org_req_ndn = BER_BVNULL;
1498 struct berval org_dn = BER_BVNULL;
1499 struct berval org_ndn = BER_BVNULL;
1500 int org_managedsait;
1502 op->o_req_dn = si->si_base;
1503 op->o_req_ndn = si->si_base;
1505 cb.sc_response = nonpresent_callback;
1508 op->o_callback = &cb;
1509 op->o_tag = LDAP_REQ_SEARCH;
1510 op->ors_scope = si->si_scope;
1511 op->ors_deref = LDAP_DEREF_NEVER;
1512 op->o_time = slap_get_time();
1513 op->ors_tlimit = SLAP_NO_LIMIT;
1514 op->ors_slimit = SLAP_NO_LIMIT;
1516 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1517 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1518 an[0].an_desc = slap_schema.si_ad_entryUUID;
1521 op->ors_attrsonly = 0;
1522 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1523 op->ors_filterstr = si->si_filterstr;
1525 op->o_nocaching = 1;
1526 op->o_managedsait = 0;
1528 if ( limits_check( op, &rs_search ) == 0 ) {
1529 rc = be->be_search( op, &rs_search );
1532 op->o_managedsait = 1;
1533 op->o_nocaching = 0;
1535 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1537 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1538 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1539 while ( np_list != NULL ) {
1540 LDAP_LIST_REMOVE( np_list, npe_link );
1542 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1543 op->o_tag = LDAP_REQ_DELETE;
1544 op->o_callback = &cb;
1545 cb.sc_response = null_callback;
1547 op->o_req_dn = *np_prev->npe_name;
1548 op->o_req_ndn = *np_prev->npe_nname;
1549 rc = op->o_bd->be_delete( op, &rs_delete );
1551 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1552 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1553 mod->sml_op = LDAP_MOD_REPLACE;
1554 mod->sml_desc = slap_schema.si_ad_objectClass;
1555 mod->sml_type = mod->sml_desc->ad_cname;
1556 mod->sml_values = &gcbva[0];
1558 modtail = &mod->sml_next;
1560 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1561 mod->sml_op = LDAP_MOD_REPLACE;
1562 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1563 mod->sml_type = mod->sml_desc->ad_cname;
1564 mod->sml_values = &gcbva[1];
1566 modtail = &mod->sml_next;
1568 op->o_tag = LDAP_REQ_MODIFY;
1569 op->orm_modlist = modlist;
1571 rc = be->be_modify( op, &rs_modify );
1573 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1574 mlnext = ml->sml_next;
1579 org_req_dn = op->o_req_dn;
1580 org_req_ndn = op->o_req_ndn;
1582 org_ndn = op->o_ndn;
1583 org_managedsait = get_manageDSAit( op );
1584 op->o_dn = op->o_bd->be_rootdn;
1585 op->o_ndn = op->o_bd->be_rootndn;
1586 op->o_managedsait = 1;
1588 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1589 op->o_delete_glue_parent ) {
1590 op->o_delete_glue_parent = 0;
1591 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1592 slap_callback cb = { NULL };
1593 cb.sc_response = slap_null_cb;
1594 dnParent( &op->o_req_ndn, &pdn );
1596 op->o_req_ndn = pdn;
1597 op->o_callback = &cb;
1598 /* give it a root privil ? */
1599 op->o_bd->be_delete( op, &rs_delete );
1605 op->o_managedsait = org_managedsait;
1607 op->o_ndn = org_ndn;
1608 op->o_req_dn = org_req_dn;
1609 op->o_req_ndn = org_req_ndn;
1610 op->o_delete_glue_parent = 0;
1612 ber_bvfree( np_prev->npe_name );
1613 ber_bvfree( np_prev->npe_nname );
1614 BER_BVZERO( &op->o_req_dn );
1615 BER_BVZERO( &op->o_req_ndn );
1628 Backend *be = op->o_bd;
1629 slap_callback cb = { NULL };
1634 struct berval dn = {0, NULL};
1635 struct berval ndn = {0, NULL};
1637 SlapReply rs_add = {REP_RESULT};
1640 op->o_tag = LDAP_REQ_ADD;
1641 op->o_callback = &cb;
1642 cb.sc_response = null_callback;
1643 cb.sc_private = NULL;
1648 /* count RDNs in suffix */
1649 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1650 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1660 /* Start with BE suffix */
1661 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1662 comma = strrchr( dn.bv_val, ',' );
1663 if ( ptr ) *ptr = ',';
1664 if ( comma ) *comma = '\0';
1669 dn.bv_len -= ptr - dn.bv_val;
1672 /* the normalizedDNs are always the same length, no counting
1675 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1676 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1677 ndn.bv_len = be->be_nsuffix[0].bv_len;
1680 while ( ndn.bv_val > e->e_nname.bv_val ) {
1681 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1682 ber_dupbv( &glue->e_name, &dn );
1683 ber_dupbv( &glue->e_nname, &ndn );
1685 a = ch_calloc( 1, sizeof( Attribute ));
1686 a->a_desc = slap_schema.si_ad_objectClass;
1688 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1689 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1690 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1691 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1693 a->a_nvals = a->a_vals;
1695 a->a_next = glue->e_attrs;
1698 a = ch_calloc( 1, sizeof( Attribute ));
1699 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1701 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1702 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1703 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1705 a->a_nvals = a->a_vals;
1707 a->a_next = glue->e_attrs;
1710 op->o_req_dn = glue->e_name;
1711 op->o_req_ndn = glue->e_nname;
1713 rc = be->be_add ( op, &rs_add );
1714 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1715 be_entry_release_w( op, glue );
1717 /* incl. ALREADY EXIST */
1721 /* Move to next child */
1722 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1725 if ( ptr == e->e_name.bv_val ) break;
1727 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1728 for( ptr = ndn.bv_val-2;
1729 ptr > e->e_nname.bv_val && *ptr != ',';
1735 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1738 op->o_req_dn = e->e_name;
1739 op->o_req_ndn = e->e_nname;
1741 rc = be->be_add ( op, &rs_add );
1742 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1743 be_entry_release_w( op, e );
1751 static struct berval ocbva[] = {
1753 BER_BVC("subentry"),
1754 BER_BVC("syncConsumerSubentry"),
1758 static struct berval cnbva[] = {
1763 static struct berval ssbva[] = {
1768 static struct berval scbva[] = {
1774 syncrepl_updateCookie(
1778 struct sync_cookie *syncCookie )
1780 Backend *be = op->o_bd;
1782 Modifications *mlnext;
1784 Modifications *modlist = NULL;
1785 Modifications **modtail = &modlist;
1788 char txtbuf[SLAP_TEXT_BUFLEN];
1789 size_t textlen = sizeof txtbuf;
1794 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1795 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1796 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1798 slap_callback cb = { NULL };
1799 SlapReply rs_add = {REP_RESULT};
1800 SlapReply rs_modify = {REP_RESULT};
1802 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1803 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1805 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1806 mod->sml_op = LDAP_MOD_REPLACE;
1807 mod->sml_desc = slap_schema.si_ad_objectClass;
1808 mod->sml_type = mod->sml_desc->ad_cname;
1809 mod->sml_values = ocbva;
1811 modtail = &mod->sml_next;
1813 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1814 assert( si->si_rid < 1000 );
1815 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1816 slap_syncrepl_bvc.bv_len + 1,
1817 "syncrepl%ld", si->si_rid );
1818 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1819 mod->sml_op = LDAP_MOD_REPLACE;
1820 mod->sml_desc = slap_schema.si_ad_cn;
1821 mod->sml_type = mod->sml_desc->ad_cname;
1822 mod->sml_values = cnbva;
1824 modtail = &mod->sml_next;
1826 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1827 mod->sml_op = LDAP_MOD_REPLACE;
1828 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1829 mod->sml_type = mod->sml_desc->ad_cname;
1830 mod->sml_values = ssbva;
1832 modtail = &mod->sml_next;
1834 /* Keep this last, so we can avoid touching the previous
1835 * attributes unnecessarily.
1837 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1838 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1839 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1840 mod->sml_op = LDAP_MOD_REPLACE;
1841 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1842 mod->sml_type = mod->sml_desc->ad_cname;
1843 mod->sml_values = scbva;
1845 modtail = &mod->sml_next;
1849 op->o_tag = LDAP_REQ_ADD;
1850 rc = slap_mods_opattrs( op, modlist, modtail,
1851 &text, txtbuf, textlen, 0 );
1853 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1854 ml->sml_op = LDAP_MOD_REPLACE;
1857 if( rc != LDAP_SUCCESS ) {
1858 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1862 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1864 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1865 assert( si->si_rid < 1000 );
1866 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1867 slap_syncrepl_cn_bvc.bv_len + 1,
1868 "cn=syncrepl%ld", si->si_rid );
1870 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1872 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1873 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1875 if ( !BER_BVISNULL( &slap_syncrepl_dn_bv ) ) {
1876 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1881 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1883 if( rc != LDAP_SUCCESS ) {
1884 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1888 cb.sc_response = null_callback;
1891 op->o_callback = &cb;
1892 op->o_req_dn = e->e_name;
1893 op->o_req_ndn = e->e_nname;
1895 /* update persistent cookie */
1896 update_cookie_retry:
1897 op->o_tag = LDAP_REQ_MODIFY;
1898 /* Just modify the cookie value, not the entire entry */
1899 op->orm_modlist = mod;
1900 rc = be->be_modify( op, &rs_modify );
1902 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1903 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1904 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1905 op->o_tag = LDAP_REQ_ADD;
1907 rc = be->be_add( op, &rs_add );
1908 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1909 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1910 goto update_cookie_retry;
1911 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1912 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1913 Debug( LDAP_DEBUG_ANY,
1914 "cookie will be non-persistent\n",
1917 Debug( LDAP_DEBUG_ANY,
1918 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1921 be_entry_release_w( op, e );
1925 Debug( LDAP_DEBUG_ANY,
1926 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1936 if ( !BER_BVISNULL( &cnbva[0] ) ) {
1937 ch_free( cnbva[0].bv_val );
1938 BER_BVZERO( &cnbva[0] );
1940 if ( !BER_BVISNULL( &scbva[0] ) ) {
1941 ch_free( scbva[0].bv_val );
1942 BER_BVZERO( &scbva[0] );
1945 if ( mlnext->sml_next ) {
1946 slap_mods_free( mlnext->sml_next );
1947 mlnext->sml_next = NULL;
1950 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1951 mlnext = ml->sml_next;
1959 syncrepl_isupdate( Operation *op )
1961 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1965 syncrepl_isupdate_dn(
1967 struct berval* ndn )
1972 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1973 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1974 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1987 syncinfo_t *si = op->o_callback->sc_private;
1989 if ( rs->sr_type == REP_SEARCH ) {
1990 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1991 Debug( LDAP_DEBUG_ANY,
1992 "dn_callback : consistency error - "
1993 "entryUUID is not unique\n", 0, 0, 0 );
1995 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1997 } else if ( rs->sr_type == REP_RESULT ) {
1998 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1999 Debug( LDAP_DEBUG_ANY,
2000 "dn_callback : consistency error - "
2001 "entryUUID is not unique\n", 0, 0, 0 );
2005 return LDAP_SUCCESS;
2009 nonpresent_callback(
2013 syncinfo_t *si = op->o_callback->sc_private;
2016 struct berval* present_uuid = NULL;
2017 struct nonpresent_entry *np_entry;
2019 if ( rs->sr_type == REP_RESULT ) {
2020 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2021 si->si_presentlist = NULL;
2023 } else if ( rs->sr_type == REP_SEARCH ) {
2024 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2026 if ( a == NULL ) return 0;
2028 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2031 if ( present_uuid == NULL ) {
2032 np_entry = (struct nonpresent_entry *)
2033 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2034 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2035 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2036 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2039 avl_delete( &si->si_presentlist,
2040 &a->a_nvals[0], syncuuid_cmp );
2041 ch_free( present_uuid->bv_val );
2042 ch_free( present_uuid );
2045 return LDAP_SUCCESS;
2053 if ( rs->sr_err != LDAP_SUCCESS &&
2054 rs->sr_err != LDAP_REFERRAL &&
2055 rs->sr_err != LDAP_ALREADY_EXISTS &&
2056 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2057 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2059 Debug( LDAP_DEBUG_ANY,
2060 "null_callback : error code 0x%x\n",
2063 return LDAP_SUCCESS;
2067 slap_create_syncrepl_entry(
2069 struct berval *context_csn,
2077 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2079 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2081 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2084 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2086 if ( context_csn ) {
2087 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2088 context_csn, NULL );
2091 BER_BVSTR( &bv, "{}" );
2092 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2094 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2095 ber_dupbv( &e->e_nname, &e->e_name );
2101 slap_uuidstr_from_normalized(
2102 struct berval* uuidstr,
2103 struct berval* normalized,
2107 unsigned char nibble;
2110 if ( normalized == NULL ) return NULL;
2111 if ( normalized->bv_len != 16 ) return NULL;
2116 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2117 if ( new == NULL ) {
2124 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2125 if ( new != uuidstr ) {
2126 slap_sl_free( new, ctx );
2131 for ( i = 0; i < 16; i++ ) {
2132 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2133 new->bv_val[(i<<1)+d] = '-';
2137 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2138 if ( nibble < 10 ) {
2139 new->bv_val[(i<<1)+d] = nibble + '0';
2141 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2144 nibble = (normalized->bv_val[i]) & 0xF;
2145 if ( nibble < 10 ) {
2146 new->bv_val[(i<<1)+d+1] = nibble + '0';
2148 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2152 new->bv_val[new->bv_len] = '\0';
2157 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2159 const struct berval *uuid1 = v_uuid1;
2160 const struct berval *uuid2 = v_uuid2;
2161 int rc = uuid1->bv_len - uuid2->bv_len;
2162 if ( rc ) return rc;
2163 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2167 avl_ber_bvfree( void *v_bv )
2169 struct berval *bv = (struct berval *)v_bv;
2171 if( v_bv == NULL ) return;
2172 if ( !BER_BVISNULL( bv ) ) {
2173 ch_free( bv->bv_val );
2175 ch_free( (char *) bv );
2179 syncinfo_free( syncinfo_t *sie )
2181 if ( sie->si_provideruri ) {
2182 ch_free( sie->si_provideruri );
2184 if ( sie->si_provideruri_bv ) {
2185 ber_bvarray_free( sie->si_provideruri_bv );
2187 if ( sie->si_updatedn.bv_val ) {
2188 ch_free( sie->si_updatedn.bv_val );
2190 if ( sie->si_binddn ) {
2191 ch_free( sie->si_binddn );
2193 if ( sie->si_passwd ) {
2194 ch_free( sie->si_passwd );
2196 if ( sie->si_saslmech ) {
2197 ch_free( sie->si_saslmech );
2199 if ( sie->si_secprops ) {
2200 ch_free( sie->si_secprops );
2202 if ( sie->si_realm ) {
2203 ch_free( sie->si_realm );
2205 if ( sie->si_authcId ) {
2206 ch_free( sie->si_authcId );
2208 if ( sie->si_authzId ) {
2209 ch_free( sie->si_authzId );
2211 if ( sie->si_filterstr.bv_val ) {
2212 ch_free( sie->si_filterstr.bv_val );
2214 if ( sie->si_base.bv_val ) {
2215 ch_free( sie->si_base.bv_val );
2217 if ( sie->si_attrs ) {
2219 while ( sie->si_attrs[i] != NULL ) {
2220 ch_free( sie->si_attrs[i] );
2223 ch_free( sie->si_attrs );
2225 if ( sie->si_exattrs ) {
2227 while ( sie->si_exattrs[i] != NULL ) {
2228 ch_free( sie->si_exattrs[i] );
2231 ch_free( sie->si_exattrs );
2233 if ( sie->si_anlist ) {
2235 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2236 ch_free( sie->si_anlist[i].an_name.bv_val );
2239 ch_free( sie->si_anlist );
2241 if ( sie->si_exanlist ) {
2243 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2244 ch_free( sie->si_exanlist[i].an_name.bv_val );
2247 ch_free( sie->si_exanlist );
2249 if ( sie->si_retryinterval ) {
2250 ch_free( sie->si_retryinterval );
2252 if ( sie->si_retrynum ) {
2253 ch_free( sie->si_retrynum );
2255 if ( sie->si_retrynum_init ) {
2256 ch_free( sie->si_retrynum_init );
2258 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2259 if ( sie->si_syncUUID_ndn.bv_val ) {
2260 ch_free( sie->si_syncUUID_ndn.bv_val );
2262 if ( sie->si_presentlist ) {
2263 avl_free( sie->si_presentlist, avl_ber_bvfree );
2266 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2268 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2269 struct nonpresent_entry* npe;
2270 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2271 LDAP_LIST_REMOVE( npe, npe_link );
2272 if ( npe->npe_name ) {
2273 if ( npe->npe_name->bv_val ) {
2274 ch_free( npe->npe_name->bv_val );
2276 ch_free( npe->npe_name );
2278 if ( npe->npe_nname ) {
2279 if ( npe->npe_nname->bv_val ) {
2280 ch_free( npe->npe_nname->bv_val );
2282 ch_free( npe->npe_nname );