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 switch( syncstate ) {
1199 case LDAP_SYNC_PRESENT:
1200 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1202 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1205 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1207 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1209 case LDAP_SYNC_DELETE:
1210 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1212 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1214 case LDAP_SYNC_MODIFY:
1215 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1217 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1220 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1222 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1225 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1226 if ( !si_refreshPresent ) {
1227 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1228 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1229 syncuuid_cmp, avl_dup_error );
1233 if ( syncstate == LDAP_SYNC_PRESENT ) {
1235 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1236 if ( entry == NULL ) {
1241 f.f_choice = LDAP_FILTER_EQUALITY;
1243 ava.aa_desc = slap_schema.si_ad_entryUUID;
1244 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1245 ava.aa_value = *syncUUID;
1246 op->ors_filter = &f;
1248 op->ors_filterstr.bv_len = STRLENOF( "entryUUID=" ) + syncUUID->bv_len;
1249 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1250 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1251 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", STRLENOF( "entryUUID=" ) );
1252 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "entryUUID=" )],
1253 syncUUID->bv_val, syncUUID->bv_len );
1254 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1256 op->o_tag = LDAP_REQ_SEARCH;
1257 op->ors_scope = LDAP_SCOPE_SUBTREE;
1259 /* get syncrepl cookie of shadow replica from subentry */
1260 op->o_req_dn = si->si_base;
1261 op->o_req_ndn = si->si_base;
1263 op->o_time = slap_get_time();
1264 op->ors_tlimit = SLAP_NO_LIMIT;
1267 op->ors_attrs = slap_anlist_no_attrs;
1268 op->ors_attrsonly = 1;
1270 /* set callback function */
1271 op->o_callback = &cb;
1272 cb.sc_response = dn_callback;
1275 BER_BVZERO( &si->si_syncUUID_ndn );
1277 if ( limits_check( op, &rs_search ) == 0 ) {
1278 rc = be->be_search( op, &rs_search );
1279 Debug( LDAP_DEBUG_SYNC,
1280 "syncrepl_entry: %s (%d)\n",
1281 "be_search", rc, 0 );
1284 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1285 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1288 cb.sc_response = null_callback;
1291 if ( entry && entry->e_name.bv_val ) {
1292 Debug( LDAP_DEBUG_SYNC,
1293 "syncrepl_entry: %s\n",
1294 entry->e_name.bv_val, 0, 0 );
1296 Debug( LDAP_DEBUG_SYNC,
1297 "syncrepl_entry: %s\n",
1298 si->si_syncUUID_ndn.bv_val, 0, 0 );
1301 if ( rs_search.sr_err == LDAP_SUCCESS &&
1302 !BER_BVISNULL( &si->si_syncUUID_ndn ))
1306 if ( syncstate != LDAP_SYNC_DELETE ) {
1307 op->o_no_psearch = 1;
1310 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1311 if ( !BER_BVISNULL( &op->o_sync_csn ) ) {
1312 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1317 op->o_req_dn = si->si_syncUUID_ndn;
1318 op->o_req_ndn = si->si_syncUUID_ndn;
1319 op->o_tag = LDAP_REQ_DELETE;
1320 rc = be->be_delete( op, &rs_delete );
1321 Debug( LDAP_DEBUG_SYNC,
1322 "syncrepl_entry: %s (%d)\n",
1323 "be_delete", rc, 0 );
1325 org_req_dn = op->o_req_dn;
1326 org_req_ndn = op->o_req_ndn;
1328 org_ndn = op->o_ndn;
1329 org_managedsait = get_manageDSAit( op );
1330 op->o_dn = op->o_bd->be_rootdn;
1331 op->o_ndn = op->o_bd->be_rootndn;
1332 op->o_managedsait = 1;
1334 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1335 op->o_delete_glue_parent = 0;
1336 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1337 slap_callback cb = { NULL };
1338 cb.sc_response = slap_null_cb;
1339 dnParent( &op->o_req_ndn, &pdn );
1341 op->o_req_ndn = pdn;
1342 op->o_callback = &cb;
1343 op->o_bd->be_delete( op, &rs_delete );
1349 op->o_managedsait = org_managedsait;
1351 op->o_ndn = org_ndn;
1352 op->o_req_dn = org_req_dn;
1353 op->o_req_ndn = org_req_ndn;
1354 op->o_delete_glue_parent = 0;
1356 op->o_no_psearch = 0;
1359 switch ( syncstate ) {
1361 case LDAP_SYNC_MODIFY:
1362 if ( rs_search.sr_err == LDAP_SUCCESS ||
1363 rs_search.sr_err == LDAP_REFERRAL ||
1364 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1365 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1367 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1368 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1369 &syncUUID_strrep, syncUUID );
1371 op->o_tag = LDAP_REQ_ADD;
1373 op->o_req_dn = entry->e_name;
1374 op->o_req_ndn = entry->e_nname;
1376 rc = be->be_add( op, &rs_add );
1377 Debug( LDAP_DEBUG_SYNC,
1378 "syncrepl_entry: %s (%d)\n",
1381 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1382 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1383 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1385 Modifications *modtail = modlist;
1389 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1393 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1394 ber_dupbv( &uuid_bv, syncUUID );
1395 mod->sml_op = LDAP_MOD_REPLACE;
1396 mod->sml_desc = slap_schema.si_ad_entryUUID;
1397 mod->sml_type = mod->sml_desc->ad_cname;
1398 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1399 modtail->sml_next = mod;
1401 op->o_tag = LDAP_REQ_MODIFY;
1402 op->orm_modlist = modlist;
1403 op->o_req_dn = entry->e_name;
1404 op->o_req_ndn = entry->e_nname;
1406 rc = be->be_modify( op, &rs_modify );
1407 Debug( LDAP_DEBUG_SYNC,
1408 "syncrepl_entry: %s (%d)\n",
1409 "be_modify", rc, 0 );
1410 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1411 Debug( LDAP_DEBUG_ANY,
1412 "syncrepl_entry : be_modify failed (%d)\n",
1413 rs_modify.sr_err, 0, 0 );
1417 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1418 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1419 syncrepl_add_glue( op, entry );
1423 Debug( LDAP_DEBUG_ANY,
1424 "syncrepl_entry : be_add failed (%d)\n",
1425 rs_add.sr_err, 0, 0 );
1430 be_entry_release_w( op, entry );
1435 Debug( LDAP_DEBUG_ANY,
1436 "syncrepl_entry : be_search failed (%d)\n",
1437 rs_search.sr_err, 0, 0 );
1442 case LDAP_SYNC_DELETE :
1443 /* Already deleted */
1448 Debug( LDAP_DEBUG_ANY,
1449 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1456 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1457 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1458 BER_BVZERO( &syncUUID_strrep );
1460 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1461 ch_free( si->si_syncUUID_ndn.bv_val );
1462 BER_BVZERO( &si->si_syncUUID_ndn );
1467 static struct berval gcbva[] = {
1474 syncrepl_del_nonpresent(
1478 Backend* be = op->o_bd;
1479 slap_callback cb = { NULL };
1480 SlapReply rs_search = {REP_RESULT};
1481 SlapReply rs_delete = {REP_RESULT};
1482 SlapReply rs_modify = {REP_RESULT};
1483 struct nonpresent_entry *np_list, *np_prev;
1486 Modifications *mlnext;
1488 Modifications *modlist = NULL;
1489 Modifications **modtail = &modlist;
1490 AttributeName an[2];
1492 struct berval pdn = BER_BVNULL;
1493 struct berval org_req_dn = BER_BVNULL;
1494 struct berval org_req_ndn = BER_BVNULL;
1495 struct berval org_dn = BER_BVNULL;
1496 struct berval org_ndn = BER_BVNULL;
1497 int org_managedsait;
1499 op->o_req_dn = si->si_base;
1500 op->o_req_ndn = si->si_base;
1502 cb.sc_response = nonpresent_callback;
1505 op->o_callback = &cb;
1506 op->o_tag = LDAP_REQ_SEARCH;
1507 op->ors_scope = si->si_scope;
1508 op->ors_deref = LDAP_DEREF_NEVER;
1509 op->o_time = slap_get_time();
1510 op->ors_tlimit = SLAP_NO_LIMIT;
1511 op->ors_slimit = SLAP_NO_LIMIT;
1513 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1514 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1515 an[0].an_desc = slap_schema.si_ad_entryUUID;
1518 op->ors_attrsonly = 0;
1519 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1520 op->ors_filterstr = si->si_filterstr;
1522 op->o_nocaching = 1;
1523 op->o_managedsait = 0;
1525 if ( limits_check( op, &rs_search ) == 0 ) {
1526 rc = be->be_search( op, &rs_search );
1529 op->o_managedsait = 1;
1530 op->o_nocaching = 0;
1532 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1534 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1535 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1536 while ( np_list != NULL ) {
1537 LDAP_LIST_REMOVE( np_list, npe_link );
1539 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1540 op->o_tag = LDAP_REQ_DELETE;
1541 op->o_callback = &cb;
1542 cb.sc_response = null_callback;
1544 op->o_req_dn = *np_prev->npe_name;
1545 op->o_req_ndn = *np_prev->npe_nname;
1546 rc = op->o_bd->be_delete( op, &rs_delete );
1548 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1549 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1550 mod->sml_op = LDAP_MOD_REPLACE;
1551 mod->sml_desc = slap_schema.si_ad_objectClass;
1552 mod->sml_type = mod->sml_desc->ad_cname;
1553 mod->sml_values = &gcbva[0];
1555 modtail = &mod->sml_next;
1557 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1558 mod->sml_op = LDAP_MOD_REPLACE;
1559 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1560 mod->sml_type = mod->sml_desc->ad_cname;
1561 mod->sml_values = &gcbva[1];
1563 modtail = &mod->sml_next;
1565 op->o_tag = LDAP_REQ_MODIFY;
1566 op->orm_modlist = modlist;
1568 rc = be->be_modify( op, &rs_modify );
1570 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1571 mlnext = ml->sml_next;
1576 org_req_dn = op->o_req_dn;
1577 org_req_ndn = op->o_req_ndn;
1579 org_ndn = op->o_ndn;
1580 org_managedsait = get_manageDSAit( op );
1581 op->o_dn = op->o_bd->be_rootdn;
1582 op->o_ndn = op->o_bd->be_rootndn;
1583 op->o_managedsait = 1;
1585 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1586 op->o_delete_glue_parent ) {
1587 op->o_delete_glue_parent = 0;
1588 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1589 slap_callback cb = { NULL };
1590 cb.sc_response = slap_null_cb;
1591 dnParent( &op->o_req_ndn, &pdn );
1593 op->o_req_ndn = pdn;
1594 op->o_callback = &cb;
1595 /* give it a root privil ? */
1596 op->o_bd->be_delete( op, &rs_delete );
1602 op->o_managedsait = org_managedsait;
1604 op->o_ndn = org_ndn;
1605 op->o_req_dn = org_req_dn;
1606 op->o_req_ndn = org_req_ndn;
1607 op->o_delete_glue_parent = 0;
1609 ber_bvfree( np_prev->npe_name );
1610 ber_bvfree( np_prev->npe_nname );
1611 BER_BVZERO( &op->o_req_dn );
1612 BER_BVZERO( &op->o_req_ndn );
1625 Backend *be = op->o_bd;
1626 slap_callback cb = { NULL };
1631 struct berval dn = {0, NULL};
1632 struct berval ndn = {0, NULL};
1634 SlapReply rs_add = {REP_RESULT};
1637 op->o_tag = LDAP_REQ_ADD;
1638 op->o_callback = &cb;
1639 cb.sc_response = null_callback;
1640 cb.sc_private = NULL;
1645 /* count RDNs in suffix */
1646 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1647 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1657 /* Start with BE suffix */
1658 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1659 comma = strrchr( dn.bv_val, ',' );
1660 if ( ptr ) *ptr = ',';
1661 if ( comma ) *comma = '\0';
1666 dn.bv_len -= ptr - dn.bv_val;
1669 /* the normalizedDNs are always the same length, no counting
1672 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1673 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1674 ndn.bv_len = be->be_nsuffix[0].bv_len;
1677 while ( ndn.bv_val > e->e_nname.bv_val ) {
1678 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1679 ber_dupbv( &glue->e_name, &dn );
1680 ber_dupbv( &glue->e_nname, &ndn );
1682 a = ch_calloc( 1, sizeof( Attribute ));
1683 a->a_desc = slap_schema.si_ad_objectClass;
1685 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1686 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1687 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1688 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1690 a->a_nvals = a->a_vals;
1692 a->a_next = glue->e_attrs;
1695 a = ch_calloc( 1, sizeof( Attribute ));
1696 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1698 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1699 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1700 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1702 a->a_nvals = a->a_vals;
1704 a->a_next = glue->e_attrs;
1707 op->o_req_dn = glue->e_name;
1708 op->o_req_ndn = glue->e_nname;
1710 rc = be->be_add ( op, &rs_add );
1711 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1712 be_entry_release_w( op, glue );
1714 /* incl. ALREADY EXIST */
1718 /* Move to next child */
1719 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1722 if ( ptr == e->e_name.bv_val ) break;
1724 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1725 for( ptr = ndn.bv_val-2;
1726 ptr > e->e_nname.bv_val && *ptr != ',';
1732 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1735 op->o_req_dn = e->e_name;
1736 op->o_req_ndn = e->e_nname;
1738 rc = be->be_add ( op, &rs_add );
1739 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1740 be_entry_release_w( op, e );
1748 static struct berval ocbva[] = {
1750 BER_BVC("subentry"),
1751 BER_BVC("syncConsumerSubentry"),
1755 static struct berval cnbva[] = {
1760 static struct berval ssbva[] = {
1765 static struct berval scbva[] = {
1771 syncrepl_updateCookie(
1775 struct sync_cookie *syncCookie )
1777 Backend *be = op->o_bd;
1779 Modifications *mlnext;
1781 Modifications *modlist = NULL;
1782 Modifications **modtail = &modlist;
1785 char txtbuf[SLAP_TEXT_BUFLEN];
1786 size_t textlen = sizeof txtbuf;
1791 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1792 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1793 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1795 slap_callback cb = { NULL };
1796 SlapReply rs_add = {REP_RESULT};
1797 SlapReply rs_modify = {REP_RESULT};
1799 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1800 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1802 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1803 mod->sml_op = LDAP_MOD_REPLACE;
1804 mod->sml_desc = slap_schema.si_ad_objectClass;
1805 mod->sml_type = mod->sml_desc->ad_cname;
1806 mod->sml_values = ocbva;
1808 modtail = &mod->sml_next;
1810 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1811 assert( si->si_rid < 1000 );
1812 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1813 slap_syncrepl_bvc.bv_len + 1,
1814 "syncrepl%ld", si->si_rid );
1815 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1816 mod->sml_op = LDAP_MOD_REPLACE;
1817 mod->sml_desc = slap_schema.si_ad_cn;
1818 mod->sml_type = mod->sml_desc->ad_cname;
1819 mod->sml_values = cnbva;
1821 modtail = &mod->sml_next;
1823 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1824 mod->sml_op = LDAP_MOD_REPLACE;
1825 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1826 mod->sml_type = mod->sml_desc->ad_cname;
1827 mod->sml_values = ssbva;
1829 modtail = &mod->sml_next;
1831 /* Keep this last, so we can avoid touching the previous
1832 * attributes unnecessarily.
1834 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1835 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1836 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1837 mod->sml_op = LDAP_MOD_REPLACE;
1838 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1839 mod->sml_type = mod->sml_desc->ad_cname;
1840 mod->sml_values = scbva;
1842 modtail = &mod->sml_next;
1846 op->o_tag = LDAP_REQ_ADD;
1847 rc = slap_mods_opattrs( op, modlist, modtail,
1848 &text, txtbuf, textlen, 0 );
1850 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1851 ml->sml_op = LDAP_MOD_REPLACE;
1854 if( rc != LDAP_SUCCESS ) {
1855 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1859 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1861 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1862 assert( si->si_rid < 1000 );
1863 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1864 slap_syncrepl_cn_bvc.bv_len + 1,
1865 "cn=syncrepl%ld", si->si_rid );
1867 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1869 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1870 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1872 if ( !BER_BVISNULL( &slap_syncrepl_dn_bv ) ) {
1873 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1878 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1880 if( rc != LDAP_SUCCESS ) {
1881 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1885 cb.sc_response = null_callback;
1888 op->o_callback = &cb;
1889 op->o_req_dn = e->e_name;
1890 op->o_req_ndn = e->e_nname;
1892 /* update persistent cookie */
1893 update_cookie_retry:
1894 op->o_tag = LDAP_REQ_MODIFY;
1895 /* Just modify the cookie value, not the entire entry */
1896 op->orm_modlist = mod;
1897 rc = be->be_modify( op, &rs_modify );
1899 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1900 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1901 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1902 op->o_tag = LDAP_REQ_ADD;
1904 rc = be->be_add( op, &rs_add );
1905 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1906 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1907 goto update_cookie_retry;
1908 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1909 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1910 Debug( LDAP_DEBUG_ANY,
1911 "cookie will be non-persistent\n",
1914 Debug( LDAP_DEBUG_ANY,
1915 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1918 be_entry_release_w( op, e );
1922 Debug( LDAP_DEBUG_ANY,
1923 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1933 if ( !BER_BVISNULL( &cnbva[0] ) ) {
1934 ch_free( cnbva[0].bv_val );
1935 BER_BVZERO( &cnbva[0] );
1937 if ( !BER_BVISNULL( &scbva[0] ) ) {
1938 ch_free( scbva[0].bv_val );
1939 BER_BVZERO( &scbva[0] );
1942 if ( mlnext->sml_next ) {
1943 slap_mods_free( mlnext->sml_next );
1944 mlnext->sml_next = NULL;
1947 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1948 mlnext = ml->sml_next;
1956 syncrepl_isupdate( Operation *op )
1958 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1962 syncrepl_isupdate_dn(
1964 struct berval* ndn )
1969 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1970 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1971 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1984 syncinfo_t *si = op->o_callback->sc_private;
1986 if ( rs->sr_type == REP_SEARCH ) {
1987 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1988 Debug( LDAP_DEBUG_ANY,
1989 "dn_callback : consistency error - "
1990 "entryUUID is not unique\n", 0, 0, 0 );
1992 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1994 } else if ( rs->sr_type == REP_RESULT ) {
1995 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1996 Debug( LDAP_DEBUG_ANY,
1997 "dn_callback : consistency error - "
1998 "entryUUID is not unique\n", 0, 0, 0 );
2002 return LDAP_SUCCESS;
2006 nonpresent_callback(
2010 syncinfo_t *si = op->o_callback->sc_private;
2013 struct berval* present_uuid = NULL;
2014 struct nonpresent_entry *np_entry;
2016 if ( rs->sr_type == REP_RESULT ) {
2017 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2018 si->si_presentlist = NULL;
2020 } else if ( rs->sr_type == REP_SEARCH ) {
2021 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2023 if ( a == NULL ) return 0;
2025 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2028 if ( present_uuid == NULL ) {
2029 np_entry = (struct nonpresent_entry *)
2030 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2031 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2032 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2033 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2036 avl_delete( &si->si_presentlist,
2037 &a->a_nvals[0], syncuuid_cmp );
2038 ch_free( present_uuid->bv_val );
2039 ch_free( present_uuid );
2042 return LDAP_SUCCESS;
2050 if ( rs->sr_err != LDAP_SUCCESS &&
2051 rs->sr_err != LDAP_REFERRAL &&
2052 rs->sr_err != LDAP_ALREADY_EXISTS &&
2053 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2054 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2056 Debug( LDAP_DEBUG_ANY,
2057 "null_callback : error code 0x%x\n",
2060 return LDAP_SUCCESS;
2064 slap_create_syncrepl_entry(
2066 struct berval *context_csn,
2074 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2076 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2078 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2081 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2083 if ( context_csn ) {
2084 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2085 context_csn, NULL );
2088 BER_BVSTR( &bv, "{}" );
2089 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2091 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2092 ber_dupbv( &e->e_nname, &e->e_name );
2098 slap_uuidstr_from_normalized(
2099 struct berval* uuidstr,
2100 struct berval* normalized,
2104 unsigned char nibble;
2107 if ( normalized == NULL ) return NULL;
2108 if ( normalized->bv_len != 16 ) return NULL;
2113 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2114 if ( new == NULL ) {
2121 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2122 if ( new != uuidstr ) {
2123 slap_sl_free( new, ctx );
2128 for ( i = 0; i < 16; i++ ) {
2129 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2130 new->bv_val[(i<<1)+d] = '-';
2134 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2135 if ( nibble < 10 ) {
2136 new->bv_val[(i<<1)+d] = nibble + '0';
2138 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2141 nibble = (normalized->bv_val[i]) & 0xF;
2142 if ( nibble < 10 ) {
2143 new->bv_val[(i<<1)+d+1] = nibble + '0';
2145 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2149 new->bv_val[new->bv_len] = '\0';
2154 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2156 const struct berval *uuid1 = v_uuid1;
2157 const struct berval *uuid2 = v_uuid2;
2158 int rc = uuid1->bv_len - uuid2->bv_len;
2159 if ( rc ) return rc;
2160 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2164 avl_ber_bvfree( void *v_bv )
2166 struct berval *bv = (struct berval *)v_bv;
2168 if( v_bv == NULL ) return;
2169 if ( !BER_BVISNULL( bv ) ) {
2170 ch_free( bv->bv_val );
2172 ch_free( (char *) bv );
2176 syncinfo_free( syncinfo_t *sie )
2178 if ( sie->si_provideruri ) {
2179 ch_free( sie->si_provideruri );
2181 if ( sie->si_provideruri_bv ) {
2182 ber_bvarray_free( sie->si_provideruri_bv );
2184 if ( sie->si_updatedn.bv_val ) {
2185 ch_free( sie->si_updatedn.bv_val );
2187 if ( sie->si_binddn ) {
2188 ch_free( sie->si_binddn );
2190 if ( sie->si_passwd ) {
2191 ch_free( sie->si_passwd );
2193 if ( sie->si_saslmech ) {
2194 ch_free( sie->si_saslmech );
2196 if ( sie->si_secprops ) {
2197 ch_free( sie->si_secprops );
2199 if ( sie->si_realm ) {
2200 ch_free( sie->si_realm );
2202 if ( sie->si_authcId ) {
2203 ch_free( sie->si_authcId );
2205 if ( sie->si_authzId ) {
2206 ch_free( sie->si_authzId );
2208 if ( sie->si_filterstr.bv_val ) {
2209 ch_free( sie->si_filterstr.bv_val );
2211 if ( sie->si_base.bv_val ) {
2212 ch_free( sie->si_base.bv_val );
2214 if ( sie->si_attrs ) {
2216 while ( sie->si_attrs[i] != NULL ) {
2217 ch_free( sie->si_attrs[i] );
2220 ch_free( sie->si_attrs );
2222 if ( sie->si_exattrs ) {
2224 while ( sie->si_exattrs[i] != NULL ) {
2225 ch_free( sie->si_exattrs[i] );
2228 ch_free( sie->si_exattrs );
2230 if ( sie->si_anlist ) {
2232 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2233 ch_free( sie->si_anlist[i].an_name.bv_val );
2236 ch_free( sie->si_anlist );
2238 if ( sie->si_exanlist ) {
2240 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2241 ch_free( sie->si_exanlist[i].an_name.bv_val );
2244 ch_free( sie->si_exanlist );
2246 if ( sie->si_retryinterval ) {
2247 ch_free( sie->si_retryinterval );
2249 if ( sie->si_retrynum ) {
2250 ch_free( sie->si_retrynum );
2252 if ( sie->si_retrynum_init ) {
2253 ch_free( sie->si_retrynum_init );
2255 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2256 if ( sie->si_syncUUID_ndn.bv_val ) {
2257 ch_free( sie->si_syncUUID_ndn.bv_val );
2259 if ( sie->si_presentlist ) {
2260 avl_free( sie->si_presentlist, avl_ber_bvfree );
2263 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2265 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2266 struct nonpresent_entry* npe;
2267 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2268 LDAP_LIST_REMOVE( npe, npe_link );
2269 if ( npe->npe_name ) {
2270 if ( npe->npe_name->bv_val ) {
2271 ch_free( npe->npe_name->bv_val );
2273 ch_free( npe->npe_name );
2275 if ( npe->npe_nname ) {
2276 if ( npe->npe_nname->bv_val ) {
2277 ch_free( npe->npe_nname->bv_val );
2279 ch_free( npe->npe_nname );