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-2005 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 static int syncuuid_cmp( const void *, const void * );
37 static void avl_ber_bvfree( void * );
38 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
40 /* callback functions */
41 static int dn_callback( struct slap_op *, struct slap_rep * );
42 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
43 static int null_callback( struct slap_op *, struct slap_rep * );
45 static AttributeDescription *sync_descs[4];
48 init_syncrepl(syncinfo_t *si)
51 char **attrs, **exattrs;
53 if ( !sync_descs[0] ) {
54 sync_descs[0] = slap_schema.si_ad_objectClass;
55 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
56 sync_descs[2] = slap_schema.si_ad_entryCSN;
60 if ( si->si_allattrs && si->si_allopattrs )
63 attrs = anlist2attrs( si->si_anlist );
66 if ( si->si_allattrs ) {
69 if ( !is_at_operational( at_find( attrs[i] ))) {
70 for ( j = i; attrs[j] != NULL; j++ ) {
73 attrs[j] = attrs[j+1];
79 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
80 attrs[i] = ch_strdup("*");
83 } else if ( si->si_allopattrs ) {
86 if ( is_at_operational( at_find( attrs[i] ))) {
87 for ( j = i; attrs[j] != NULL; j++ ) {
90 attrs[j] = attrs[j+1];
96 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
97 attrs[i] = ch_strdup("+");
101 for ( i = 0; sync_descs[i] != NULL; i++ ) {
104 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
105 for ( k = j; attrs[k] != NULL; k++ ) {
108 attrs[k] = attrs[k+1];
116 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
118 if ( si->si_allopattrs ) {
119 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
121 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
124 if ( attrs == NULL ) {
125 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
129 if ( si->si_allopattrs ) {
130 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
132 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
133 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
141 if ( si->si_allattrs == si->si_allopattrs ) {
142 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
143 attrs[i++] = ch_strdup( "*" );
144 attrs[i++] = ch_strdup( "+" );
145 } else if ( si->si_allattrs && !si->si_allopattrs ) {
146 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
147 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
148 attrs[i++] = ch_strdup( "*" );
149 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
150 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
152 } else 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( sync_descs[0]->ad_cname.bv_val );
160 si->si_attrs = attrs;
162 exattrs = anlist2attrs( si->si_exanlist );
165 for ( n = 0; exattrs[n] != NULL; n++ ) ;
167 for ( i = 0; sync_descs[i] != NULL; i++ ) {
169 while ( exattrs[j] != NULL ) {
170 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
171 for ( k = j; exattrs[k] != NULL; k++ ) {
173 ch_free( exattrs[k] );
174 exattrs[k] = exattrs[k+1];
182 for ( i = 0; exattrs[i] != NULL; i++ ) {
183 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
185 if ( ( oc = si->si_anlist[j].an_oc ) ) {
187 while ( oc->soc_required[k] ) {
188 if ( !strcmp( exattrs[i],
189 oc->soc_required[k]->sat_cname.bv_val )) {
190 for ( l = i; exattrs[l]; l++ ) {
192 ch_free( exattrs[i] );
193 exattrs[l] = exattrs[l+1];
203 for ( i = 0; exattrs[i] != NULL; i++ ) ;
206 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
209 si->si_exattrs = exattrs;
217 BerElementBuffer berbuf;
218 BerElement *ber = (BerElement *)&berbuf;
219 LDAPControl c[2], *ctrls[3];
220 struct timeval timeout;
224 /* setup LDAP SYNC control */
225 ber_init2( ber, NULL, LBER_USE_DER );
226 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
228 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
230 ber_printf( ber, "{eO}",
232 &si->si_syncCookie.octet_str );
234 ber_printf( ber, "{e}",
238 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
243 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
244 c[0].ldctl_iscritical = si->si_type < 0;
247 if ( si->si_authzId ) {
248 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
249 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
250 c[1].ldctl_iscritical = 1;
257 timeout.tv_sec = si->si_tlimit;
260 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
261 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
262 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
263 si->si_slimit, &msgid );
274 int cmdline_cookie_found = 0;
276 struct sync_cookie *sc = NULL;
282 psub = &si->si_be->be_nsuffix[0];
284 /* Init connection to master */
285 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
286 if ( rc != LDAP_SUCCESS ) {
287 Debug( LDAP_DEBUG_ANY,
288 "do_syncrep1: ldap_initialize failed (%s)\n",
289 si->si_provideruri, 0, 0 );
293 op->o_protocol = LDAP_VERSION3;
294 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
299 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
300 if( rc != LDAP_SUCCESS ) {
301 Debug( LDAP_DEBUG_ANY,
302 "%s: ldap_start_tls failed (%d)\n",
303 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
305 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
309 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
310 #ifdef HAVE_CYRUS_SASL
313 if ( si->si_secprops != NULL ) {
314 rc = ldap_set_option( si->si_ld,
315 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
317 if( rc != LDAP_OPT_SUCCESS ) {
318 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
319 "(%s,SECPROPS,\"%s\") failed!\n",
320 si->si_provideruri, si->si_secprops, 0 );
325 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
326 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
328 rc = ldap_sasl_interactive_bind_s( si->si_ld,
336 lutil_sasl_freedefs( defaults );
338 /* FIXME: different error behaviors according to
340 * 2) on err policy : exit, retry, backoff ...
342 if ( rc != LDAP_SUCCESS ) {
343 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
344 "ldap_sasl_interactive_bind_s failed (%d)\n",
347 /* FIXME (see above comment) */
348 /* if Kerberos credentials cache is not active, retry */
349 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
350 rc == LDAP_LOCAL_ERROR )
352 rc = LDAP_SERVER_DOWN;
357 #else /* HAVE_CYRUS_SASL */
358 /* Should never get here, we trapped this at config time */
360 fprintf( stderr, "not compiled with SASL support\n" );
366 rc = ldap_bind_s( si->si_ld,
367 si->si_binddn, si->si_passwd, si->si_bindmethod );
368 if ( rc != LDAP_SUCCESS ) {
369 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
370 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
375 /* Set SSF to strongest of TLS, SASL SSFs */
378 op->o_transport_ssf = 0;
380 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
381 == LDAP_SUCCESS && ssl != NULL )
383 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
385 #endif /* HAVE_TLS */
386 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
387 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
388 ? op->o_sasl_ssf : op->o_tls_ssf;
391 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
392 /* get contextCSN shadow replica from database */
393 BerVarray csn = NULL;
394 struct berval newcookie;
396 assert( si->si_rid < 1000 );
397 op->o_req_ndn = op->o_bd->be_nsuffix[0];
398 op->o_req_dn = op->o_req_ndn;
400 /* try to read stored contextCSN */
401 backend_attribute( op, NULL, &op->o_req_ndn,
402 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
404 ch_free( si->si_syncCookie.ctxcsn.bv_val );
405 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
406 ber_bvarray_free_x( csn, op->o_tmpmemctx );
409 si->si_syncCookie.rid = si->si_rid;
411 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
412 if ( si->si_rid == sc->rid ) {
413 cmdline_cookie_found = 1;
418 if ( cmdline_cookie_found ) {
419 /* cookie is supplied in the command line */
421 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
423 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
424 /* if cmdline cookie does not have ctxcsn */
425 /* component, set it to an initial value */
426 slap_init_sync_cookie_ctxcsn( sc );
428 slap_sync_cookie_free( &si->si_syncCookie, 0 );
429 slap_dup_sync_cookie( &si->si_syncCookie, sc );
430 slap_sync_cookie_free( sc, 1 );
433 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
434 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
437 rc = ldap_sync_search( si, op->o_tmpmemctx );
439 if( rc != LDAP_SUCCESS ) {
440 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
441 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
447 ldap_unbind( si->si_ld );
460 LDAPControl **rctrls = NULL;
463 BerElementBuffer berbuf;
464 BerElement *ber = (BerElement *)&berbuf;
466 LDAPMessage *res = NULL;
467 LDAPMessage *msg = NULL;
470 struct berval *retdata = NULL;
475 struct berval syncUUID = BER_BVNULL;
476 struct sync_cookie syncCookie = { 0 };
477 struct sync_cookie syncCookie_req = { 0 };
478 struct berval cookie = BER_BVNULL;
486 Modifications *modlist = NULL;
491 struct timeval *tout_p = NULL;
492 struct timeval tout = { 0, 0 };
494 int refreshDeletes = 0;
496 BerVarray syncUUIDs = NULL;
499 if ( slapd_shutdown ) {
504 ber_init2( ber, NULL, LBER_USE_DER );
505 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
507 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
509 psub = &si->si_be->be_nsuffix[0];
511 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
513 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
519 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
520 tout_p, &res )) > 0 )
522 if ( slapd_shutdown ) {
526 for( msg = ldap_first_message( si->si_ld, res );
528 msg = ldap_next_message( si->si_ld, msg ) )
530 switch( ldap_msgtype( msg ) ) {
531 case LDAP_RES_SEARCH_ENTRY:
532 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
533 /* we can't work without the control */
535 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
536 "got search entry without "
537 "control\n", 0, 0, 0 );
542 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
543 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
544 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
545 ber_scanf( ber, /*"{"*/ "m}", &cookie );
546 if ( !BER_BVISNULL( &cookie ) ) {
547 ch_free( syncCookie.octet_str.bv_val );
548 ber_dupbv( &syncCookie.octet_str, &cookie );
550 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
552 slap_parse_sync_cookie( &syncCookie );
555 if ( syncrepl_message_to_entry( si, op, msg,
556 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
557 rc_efree = syncrepl_entry( si, op, entry, &modlist,
558 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
559 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
561 syncrepl_updateCookie( si, op, psub, &syncCookie );
564 ldap_controls_free( rctrls );
566 slap_mods_free( modlist );
568 if ( rc_efree && entry ) {
574 case LDAP_RES_SEARCH_REFERENCE:
575 Debug( LDAP_DEBUG_ANY,
576 "do_syncrep2: reference received error\n", 0, 0, 0 );
579 case LDAP_RES_SEARCH_RESULT:
580 Debug( LDAP_DEBUG_SYNC,
581 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
582 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
586 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
588 ber_scanf( ber, "{" /*"}"*/);
589 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
590 ber_scanf( ber, "m", &cookie );
591 if ( !BER_BVISNULL( &cookie ) ) {
592 ch_free( syncCookie.octet_str.bv_val );
593 ber_dupbv( &syncCookie.octet_str, &cookie);
595 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
597 slap_parse_sync_cookie( &syncCookie );
600 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
602 ber_scanf( ber, "b", &refreshDeletes );
604 ber_scanf( ber, /*"{"*/ "}" );
606 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
608 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
611 value_match( &match, slap_schema.si_ad_entryCSN,
612 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
613 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
614 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
617 if ( !BER_BVISNULL( &syncCookie.octet_str ) &&
618 match < 0 && err == LDAP_SUCCESS )
620 syncrepl_updateCookie( si, op, psub, &syncCookie );
623 ldap_controls_free( rctrls );
625 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
626 /* FIXME : different error behaviors according to
627 * 1) err code : LDAP_BUSY ...
628 * 2) on err policy : stop service, stop sync, retry
630 if ( refreshDeletes == 0 && match < 0 &&
631 err == LDAP_SUCCESS )
633 syncrepl_del_nonpresent( op, si, NULL );
635 avl_free( si->si_presentlist, avl_ber_bvfree );
636 si->si_presentlist = NULL;
643 case LDAP_RES_INTERMEDIATE:
644 rc = ldap_parse_intermediate( si->si_ld, msg,
645 &retoid, &retdata, NULL, 0 );
646 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
647 ber_init2( ber, retdata, LBER_USE_DER );
649 switch ( si_tag = ber_peek_tag( ber, &len )) {
651 case LDAP_TAG_SYNC_NEW_COOKIE:
652 Debug( LDAP_DEBUG_SYNC,
653 "do_syncrep2: %s - %s%s\n",
654 "LDAP_RES_INTERMEDIATE",
655 "NEW_COOKIE", "\n" );
656 ber_scanf( ber, "tm", &tag, &cookie );
658 case LDAP_TAG_SYNC_REFRESH_DELETE:
659 case LDAP_TAG_SYNC_REFRESH_PRESENT:
660 Debug( LDAP_DEBUG_SYNC,
661 "do_syncrep2: %s - %s%s\n",
662 "LDAP_RES_INTERMEDIATE",
663 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
664 "REFRESH_PRESENT" : "REFRESH_DELETE",
666 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
667 si->si_refreshDelete = 1;
669 si->si_refreshPresent = 1;
671 ber_scanf( ber, "t{" /*"}"*/, &tag );
672 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
674 ber_scanf( ber, "m", &cookie );
675 if ( !BER_BVISNULL( &cookie ) ) {
676 ch_free( syncCookie.octet_str.bv_val );
677 ber_dupbv( &syncCookie.octet_str, &cookie );
679 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
681 slap_parse_sync_cookie( &syncCookie );
684 if ( ber_peek_tag( ber, &len ) ==
685 LDAP_TAG_REFRESHDONE )
687 ber_scanf( ber, "b", &refreshDone );
689 ber_scanf( ber, /*"{"*/ "}" );
691 case LDAP_TAG_SYNC_ID_SET:
692 Debug( LDAP_DEBUG_SYNC,
693 "do_syncrep2: %s - %s%s\n",
694 "LDAP_RES_INTERMEDIATE",
697 ber_scanf( ber, "t{" /*"}"*/, &tag );
698 if ( ber_peek_tag( ber, &len ) ==
699 LDAP_TAG_SYNC_COOKIE )
701 ber_scanf( ber, "m", &cookie );
702 if ( !BER_BVISNULL( &cookie ) ) {
703 ch_free( syncCookie.octet_str.bv_val );
704 ber_dupbv( &syncCookie.octet_str, &cookie );
706 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
708 slap_parse_sync_cookie( &syncCookie );
711 if ( ber_peek_tag( ber, &len ) ==
712 LDAP_TAG_REFRESHDELETES )
714 ber_scanf( ber, "b", &refreshDeletes );
716 ber_scanf( ber, "[W]", &syncUUIDs );
717 ber_scanf( ber, /*"{"*/ "}" );
718 if ( refreshDeletes ) {
719 syncrepl_del_nonpresent( op, si, syncUUIDs );
720 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
722 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
723 struct berval *syncuuid_bv;
724 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
725 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
726 avl_insert( &si->si_presentlist,
727 (caddr_t) syncuuid_bv,
728 syncuuid_cmp, avl_dup_error );
730 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
734 Debug( LDAP_DEBUG_ANY,
735 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
736 (long) si_tag, 0, 0 );
737 ldap_memfree( retoid );
738 ber_bvfree( retdata );
742 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
744 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
747 value_match( &match, slap_schema.si_ad_entryCSN,
748 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
749 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
750 &syncCookie_req.ctxcsn,
751 &syncCookie.ctxcsn, &text );
754 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
757 syncrepl_updateCookie( si, op, psub, &syncCookie);
760 if ( si->si_refreshPresent == 1 ) {
762 syncrepl_del_nonpresent( op, si, NULL );
766 ldap_memfree( retoid );
767 ber_bvfree( retdata );
771 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
772 "unknown intermediate response (%d)\n",
774 ldap_memfree( retoid );
775 ber_bvfree( retdata );
781 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
782 "unknown message\n", 0, 0, 0 );
786 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
787 slap_sync_cookie_free( &syncCookie_req, 0 );
788 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
789 slap_sync_cookie_free( &syncCookie, 0 );
799 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
800 errstr = ldap_err2string( rc );
802 Debug( LDAP_DEBUG_ANY,
803 "do_syncrep2 : %s\n", errstr, 0, 0 );
807 slap_sync_cookie_free( &syncCookie, 0 );
808 slap_sync_cookie_free( &syncCookie_req, 0 );
810 if ( res ) ldap_msgfree( res );
812 if ( rc && si->si_ld ) {
813 ldap_unbind( si->si_ld );
825 struct re_s* rtask = arg;
826 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
827 Connection conn = {0};
828 char opbuf[OPERATION_BUFFER_SIZE];
830 int rc = LDAP_SUCCESS;
837 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
842 switch( abs( si->si_type )) {
843 case LDAP_SYNC_REFRESH_ONLY:
844 case LDAP_SYNC_REFRESH_AND_PERSIST:
850 if ( slapd_shutdown ) {
852 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
853 connection_client_stop( s );
854 ldap_unbind( si->si_ld );
860 op = (Operation *)opbuf;
861 connection_fake_init( &conn, op, ctx );
863 /* use global malloc for now */
864 op->o_tmpmemctx = NULL;
865 op->o_tmpmfuncs = &ch_mfuncs;
867 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
868 op->o_bd = be = si->si_be;
869 op->o_dn = op->o_bd->be_rootdn;
870 op->o_ndn = op->o_bd->be_rootndn;
872 /* Establish session, do search */
875 si->si_refreshDelete = 0;
876 si->si_refreshPresent = 0;
877 rc = do_syncrep1( op, si );
880 /* Process results */
881 if ( rc == LDAP_SUCCESS ) {
882 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
884 rc = do_syncrep2( op, si );
886 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
887 /* If we succeeded, enable the connection for further listening.
888 * If we failed, tear down the connection and reschedule.
890 if ( rc == LDAP_SUCCESS ) {
892 rc = connection_client_setup( s, do_syncrepl, arg );
894 connection_client_enable( s );
896 } else if ( !first ) {
900 if ( rc == -2 ) rc = 0;
904 /* At this point, we have 4 cases:
905 * 1) for any hard failure, give up and remove this task
906 * 2) for ServerDown, reschedule this task to run
907 * 3) for Refresh and Success, reschedule to run
908 * 4) for Persist and Success, reschedule to defer
910 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
912 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
913 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
917 connection_client_stop( s );
920 if ( rc == LDAP_SUCCESS ) {
921 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
924 rtask->interval.tv_sec = si->si_interval;
925 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
926 if ( si->si_retrynum ) {
927 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
928 si->si_retrynum[i] = si->si_retrynum_init[i];
930 si->si_retrynum[i] = -2;
933 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
934 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
938 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
939 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
940 } else if ( si->si_retrynum[i] >= -1 ) {
941 if ( si->si_retrynum[i] > 0 )
942 si->si_retrynum[i]--;
943 rtask->interval.tv_sec = si->si_retryinterval[i];
944 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
945 slap_wake_listener();
949 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
955 syncrepl_message_to_entry(
959 Modifications **modlist,
965 BerElement *ber = NULL;
968 Modifications **modtail = modlist;
971 char txtbuf[SLAP_TEXT_BUFLEN];
972 size_t textlen = sizeof txtbuf;
974 struct berval bdn = {0, NULL}, dn, ndn;
979 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
980 Debug( LDAP_DEBUG_ANY,
981 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
985 op->o_tag = LDAP_REQ_ADD;
987 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
989 if ( rc != LDAP_SUCCESS ) {
990 Debug( LDAP_DEBUG_ANY,
991 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
995 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
996 ber_dupbv( &op->o_req_dn, &dn );
997 ber_dupbv( &op->o_req_ndn, &ndn );
998 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
999 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1001 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1004 return LDAP_SUCCESS;
1007 if ( entry == NULL ) {
1011 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1013 e->e_name = op->o_req_dn;
1014 e->e_nname = op->o_req_ndn;
1016 while ( ber_remaining( ber ) ) {
1017 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1018 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1023 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1025 mod->sml_op = LDAP_MOD_REPLACE;
1026 mod->sml_next = NULL;
1027 mod->sml_desc = NULL;
1028 mod->sml_type = tmp.sml_type;
1029 mod->sml_values = tmp.sml_values;
1030 mod->sml_nvalues = NULL;
1033 modtail = &mod->sml_next;
1036 if ( *modlist == NULL ) {
1037 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1043 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1045 if ( rc != LDAP_SUCCESS ) {
1046 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1051 /* Strip out dynamically generated attrs */
1052 for ( modtail = modlist; *modtail ; ) {
1054 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1055 *modtail = mod->sml_next;
1056 slap_mod_free( &mod->sml_mod, 0 );
1059 modtail = &mod->sml_next;
1063 /* Strip out attrs in exattrs list */
1064 for ( modtail = modlist; *modtail ; ) {
1066 if ( ldap_charray_inlist( si->si_exattrs,
1067 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1068 *modtail = mod->sml_next;
1069 slap_mod_free( &mod->sml_mod, 0 );
1072 modtail = &mod->sml_next;
1076 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1077 if( rc != LDAP_SUCCESS ) {
1078 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1083 ber_free ( ber, 0 );
1084 if ( rc != LDAP_SUCCESS ) {
1094 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1096 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1097 * entry if a previous refresh was interrupted before sending us a new
1098 * context state. We try to compare the new entry to the existing entry
1099 * and ignore the new entry if they are the same.
1101 * Also, we may get an update where the entryDN has changed, due to
1102 * a ModDn on the provider. We detect this as well, so we can issue
1103 * the corresponding operation locally.
1105 * In the case of a modify, we get a list of all the attributes
1106 * in the original entry. Rather than deleting the entry and re-adding it,
1107 * we issue a Modify request that deletes all the attributes and adds all
1108 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1111 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1112 * a ModDN we will issue both operations on the local database.
1114 typedef struct dninfo {
1118 int renamed; /* Was an existing entry renamed? */
1119 int wasChanged; /* are the attributes changed? */
1120 int attrs; /* how many attribute types are in the ads list */
1121 AttributeDescription **ads;
1129 Modifications** modlist,
1131 struct berval* syncUUID,
1132 struct sync_cookie* syncCookie_req,
1133 struct berval* syncCSN )
1135 Backend *be = op->o_bd;
1136 slap_callback cb = { NULL };
1137 struct berval *syncuuid_bv = NULL;
1138 struct berval syncUUID_strrep = BER_BVNULL;
1139 struct berval uuid_bv = BER_BVNULL;
1141 SlapReply rs_search = {REP_RESULT};
1142 SlapReply rs_delete = {REP_RESULT};
1143 SlapReply rs_add = {REP_RESULT};
1144 SlapReply rs_modify = {REP_RESULT};
1146 AttributeAssertion ava = {0};
1147 int rc = LDAP_SUCCESS;
1148 int ret = LDAP_SUCCESS;
1150 struct berval pdn = BER_BVNULL;
1154 switch( syncstate ) {
1155 case LDAP_SYNC_PRESENT:
1156 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1158 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1161 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1163 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1165 case LDAP_SYNC_DELETE:
1166 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1168 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1170 case LDAP_SYNC_MODIFY:
1171 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1173 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1176 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1178 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1181 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1182 if ( !si->si_refreshPresent ) {
1183 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1184 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1185 syncuuid_cmp, avl_dup_error );
1189 if ( syncstate == LDAP_SYNC_PRESENT ) {
1191 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1192 if ( entry == NULL ) {
1197 f.f_choice = LDAP_FILTER_EQUALITY;
1199 ava.aa_desc = slap_schema.si_ad_entryUUID;
1200 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1201 ava.aa_value = *syncUUID;
1202 op->ors_filter = &f;
1204 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1205 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1206 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1207 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1208 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1209 syncUUID->bv_val, syncUUID->bv_len );
1210 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1211 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1213 op->o_tag = LDAP_REQ_SEARCH;
1214 op->ors_scope = LDAP_SCOPE_SUBTREE;
1216 /* get the entry for this UUID */
1217 op->o_req_dn = si->si_base;
1218 op->o_req_ndn = si->si_base;
1220 op->o_time = slap_get_time();
1221 op->ors_tlimit = SLAP_NO_LIMIT;
1224 op->ors_attrs = slap_anlist_all_attributes;
1225 op->ors_attrsonly = 0;
1227 /* set callback function */
1228 op->o_callback = &cb;
1229 cb.sc_response = dn_callback;
1230 cb.sc_private = &dni;
1231 dni.new_entry = entry;
1233 if ( limits_check( op, &rs_search ) == 0 ) {
1234 rc = be->be_search( op, &rs_search );
1235 Debug( LDAP_DEBUG_SYNC,
1236 "syncrepl_entry: %s (%d)\n",
1237 "be_search", rc, 0 );
1240 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1241 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1244 cb.sc_response = null_callback;
1247 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1248 Debug( LDAP_DEBUG_SYNC,
1249 "syncrepl_entry: %s\n",
1250 entry->e_name.bv_val, 0, 0 );
1252 Debug( LDAP_DEBUG_SYNC,
1253 "syncrepl_entry: %s\n",
1254 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1257 if ( syncstate != LDAP_SYNC_DELETE ) {
1258 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1261 /* add if missing */
1262 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1263 &syncUUID_strrep, syncUUID );
1265 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1266 /* replace only if necessary */
1267 if ( a->a_nvals != a->a_vals ) {
1268 ber_memfree( a->a_nvals[0].bv_val );
1269 ber_dupbv( &a->a_nvals[0], syncUUID );
1271 ber_memfree( a->a_vals[0].bv_val );
1272 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1276 switch ( syncstate ) {
1278 case LDAP_SYNC_MODIFY:
1280 if ( BER_BVISNULL( &dni.dn )) {
1282 op->o_req_dn = entry->e_name;
1283 op->o_req_ndn = entry->e_nname;
1284 op->o_tag = LDAP_REQ_ADD;
1287 rc = be->be_add( op, &rs_add );
1288 Debug( LDAP_DEBUG_SYNC,
1289 "syncrepl_entry: %s (%d)\n",
1291 switch ( rs_add.sr_err ) {
1293 be_entry_release_w( op, entry );
1298 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1299 * only if the suffix entry is not present */
1300 case LDAP_NO_SUCH_OBJECT:
1301 syncrepl_add_glue( op, entry );
1305 /* if an entry was added via syncrepl_add_glue(),
1306 * it likely has no entryUUID, so the previous
1307 * be_search() doesn't find it. In this case,
1308 * give syncrepl a chance to modify it. Also
1309 * allow for entries that were recreated with the
1310 * same DN but a different entryUUID.
1312 case LDAP_ALREADY_EXISTS:
1314 Operation op2 = *op;
1315 SlapReply rs2 = { 0 };
1316 slap_callback cb2 = { 0 };
1318 op2.o_tag = LDAP_REQ_SEARCH;
1319 op2.o_req_dn = entry->e_name;
1320 op2.o_req_ndn = entry->e_nname;
1321 op2.ors_scope = LDAP_SCOPE_BASE;
1322 op2.ors_attrs = slap_anlist_all_attributes;
1323 op2.ors_attrsonly = 0;
1324 op2.ors_limit = NULL;
1326 op2.ors_tlimit = SLAP_NO_LIMIT;
1328 f.f_choice = LDAP_FILTER_PRESENT;
1329 f.f_desc = slap_schema.si_ad_objectClass;
1330 op2.ors_filter = &f;
1331 op2.ors_filterstr = generic_filterstr;
1333 op2.o_callback = &cb2;
1334 cb2.sc_response = dn_callback;
1335 cb2.sc_private = &dni;
1337 be->be_search( &op2, &rs2 );
1345 Debug( LDAP_DEBUG_ANY,
1346 "syncrepl_entry : be_add failed (%d)\n",
1347 rs_add.sr_err, 0, 0 );
1354 op->o_req_dn = dni.dn;
1355 op->o_req_ndn = dni.ndn;
1356 if ( dni.renamed ) {
1357 struct berval noldp, newp, nnewp;
1359 op->o_tag = LDAP_REQ_MODRDN;
1360 dnRdn( &entry->e_name, &op->orr_newrdn );
1361 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1363 dnParent( &dni.ndn, &noldp );
1364 dnParent( &entry->e_nname, &nnewp );
1365 if ( !dn_match( &noldp, &newp )) {
1366 dnParent( &entry->e_name, &newp );
1367 op->orr_newSup = &newp;
1368 op->orr_nnewSup = &nnewp;
1370 op->orr_deleteoldrdn = 0;
1371 rc = be->be_modrdn( op, &rs_modify );
1372 Debug( LDAP_DEBUG_SYNC,
1373 "syncrepl_entry: %s (%d)\n",
1374 "be_modrdn", rc, 0 );
1375 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1376 op->o_req_dn = entry->e_name;
1377 op->o_req_ndn = entry->e_nname;
1383 if ( dni.wasChanged ) {
1384 Modifications *mod, *modhead = NULL;
1385 Modifications *modtail = NULL;
1388 op->o_tag = LDAP_REQ_MODIFY;
1392 /* Delete all the old attrs */
1393 for ( i = 0; i < dni.attrs; i++ ) {
1394 mod = ch_malloc( sizeof( Modifications ) );
1395 mod->sml_op = LDAP_MOD_DELETE;
1396 mod->sml_desc = dni.ads[i];
1397 mod->sml_type = mod->sml_desc->ad_cname;
1398 mod->sml_values = NULL;
1399 mod->sml_nvalues = NULL;
1400 if ( !modhead ) modhead = mod;
1402 modtail->sml_next = mod;
1407 /* Append passed in list to ours */
1409 modtail->sml_next = *modlist;
1415 /* Find end of this list */
1416 for ( ; mod != NULL; mod = mod->sml_next ) {
1420 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1421 mod->sml_op = LDAP_MOD_REPLACE;
1422 mod->sml_desc = slap_schema.si_ad_entryUUID;
1423 mod->sml_type = mod->sml_desc->ad_cname;
1424 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1425 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1426 ber_dupbv( &uuid_bv, syncUUID );
1427 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1428 modtail->sml_next = mod;
1430 op->o_tag = LDAP_REQ_MODIFY;
1431 op->orm_modlist = *modlist;
1433 rc = be->be_modify( op, &rs_modify );
1434 Debug( LDAP_DEBUG_SYNC,
1435 "syncrepl_entry: %s (%d)\n",
1436 "be_modify", rc, 0 );
1437 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1438 Debug( LDAP_DEBUG_ANY,
1439 "syncrepl_entry : be_modify failed (%d)\n",
1440 rs_modify.sr_err, 0, 0 );
1445 case LDAP_SYNC_DELETE :
1446 if ( !BER_BVISNULL( &dni.dn )) {
1447 op->o_req_dn = dni.dn;
1448 op->o_req_ndn = dni.ndn;
1449 op->o_tag = LDAP_REQ_DELETE;
1450 rc = be->be_delete( op, &rs_delete );
1451 Debug( LDAP_DEBUG_SYNC,
1452 "syncrepl_entry: %s (%d)\n",
1453 "be_delete", rc, 0 );
1455 while ( rs_delete.sr_err == LDAP_SUCCESS
1456 && op->o_delete_glue_parent ) {
1457 op->o_delete_glue_parent = 0;
1458 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1459 slap_callback cb = { NULL };
1460 cb.sc_response = slap_null_cb;
1461 dnParent( &op->o_req_ndn, &pdn );
1463 op->o_req_ndn = pdn;
1464 op->o_callback = &cb;
1465 op->o_bd->be_delete( op, &rs_delete );
1475 Debug( LDAP_DEBUG_ANY,
1476 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1482 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1483 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1484 BER_BVZERO( &syncUUID_strrep );
1487 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1489 if ( !BER_BVISNULL( &dni.ndn ) ) {
1490 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1492 if ( !BER_BVISNULL( &dni.dn ) ) {
1493 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1498 static struct berval gcbva[] = {
1504 #define NP_DELETE_ONE 2
1507 syncrepl_del_nonpresent(
1512 Backend* be = op->o_bd;
1513 slap_callback cb = { NULL };
1514 SlapReply rs_search = {REP_RESULT};
1515 SlapReply rs_delete = {REP_RESULT};
1516 SlapReply rs_modify = {REP_RESULT};
1517 struct nonpresent_entry *np_list, *np_prev;
1519 AttributeName an[2];
1521 struct berval pdn = BER_BVNULL;
1522 struct berval org_req_dn = BER_BVNULL;
1523 struct berval org_req_ndn = BER_BVNULL;
1525 op->o_req_dn = si->si_base;
1526 op->o_req_ndn = si->si_base;
1528 cb.sc_response = nonpresent_callback;
1531 op->o_callback = &cb;
1532 op->o_tag = LDAP_REQ_SEARCH;
1533 op->ors_scope = si->si_scope;
1534 op->ors_deref = LDAP_DEREF_NEVER;
1535 op->o_time = slap_get_time();
1536 op->ors_tlimit = SLAP_NO_LIMIT;
1541 AttributeAssertion eq;
1544 op->ors_attrsonly = 1;
1545 op->ors_attrs = slap_anlist_no_attrs;
1546 op->ors_limit = NULL;
1547 op->ors_filter = &uf;
1550 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1552 uf.f_choice = LDAP_FILTER_EQUALITY;
1553 si->si_refreshDelete |= NP_DELETE_ONE;
1555 for (i=0; uuids[i].bv_val; i++) {
1557 uf.f_av_value = uuids[i];
1558 rc = be->be_search( op, &rs_search );
1560 si->si_refreshDelete ^= NP_DELETE_ONE;
1562 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1563 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1564 an[0].an_desc = slap_schema.si_ad_entryUUID;
1566 op->ors_slimit = SLAP_NO_LIMIT;
1567 op->ors_attrsonly = 0;
1568 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1569 op->ors_filterstr = si->si_filterstr;
1570 op->o_nocaching = 1;
1572 if ( limits_check( op, &rs_search ) == 0 ) {
1573 rc = be->be_search( op, &rs_search );
1575 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1578 op->o_nocaching = 0;
1580 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1582 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1584 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1585 while ( np_list != NULL ) {
1586 LDAP_LIST_REMOVE( np_list, npe_link );
1588 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1589 op->o_tag = LDAP_REQ_DELETE;
1590 op->o_callback = &cb;
1591 cb.sc_response = null_callback;
1593 op->o_req_dn = *np_prev->npe_name;
1594 op->o_req_ndn = *np_prev->npe_nname;
1595 rc = op->o_bd->be_delete( op, &rs_delete );
1597 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1598 Modifications mod1, mod2;
1599 mod1.sml_op = LDAP_MOD_REPLACE;
1600 mod1.sml_desc = slap_schema.si_ad_objectClass;
1601 mod1.sml_type = mod1.sml_desc->ad_cname;
1602 mod1.sml_values = &gcbva[0];
1603 mod1.sml_nvalues = NULL;
1604 mod1.sml_next = &mod2;
1606 mod2.sml_op = LDAP_MOD_REPLACE;
1607 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
1608 mod2.sml_type = mod2.sml_desc->ad_cname;
1609 mod2.sml_values = &gcbva[1];
1610 mod2.sml_nvalues = NULL;
1611 mod2.sml_next = NULL;
1613 op->o_tag = LDAP_REQ_MODIFY;
1614 op->orm_modlist = &mod1;
1616 rc = be->be_modify( op, &rs_modify );
1619 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1620 op->o_delete_glue_parent ) {
1621 op->o_delete_glue_parent = 0;
1622 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1623 slap_callback cb = { NULL };
1624 cb.sc_response = slap_null_cb;
1625 dnParent( &op->o_req_ndn, &pdn );
1627 op->o_req_ndn = pdn;
1628 op->o_callback = &cb;
1629 /* give it a root privil ? */
1630 op->o_bd->be_delete( op, &rs_delete );
1636 op->o_delete_glue_parent = 0;
1638 ber_bvfree( np_prev->npe_name );
1639 ber_bvfree( np_prev->npe_nname );
1643 slap_graduate_commit_csn( op );
1654 Backend *be = op->o_bd;
1655 slap_callback cb = { NULL };
1660 struct berval dn = {0, NULL};
1661 struct berval ndn = {0, NULL};
1663 SlapReply rs_add = {REP_RESULT};
1666 op->o_tag = LDAP_REQ_ADD;
1667 op->o_callback = &cb;
1668 cb.sc_response = null_callback;
1669 cb.sc_private = NULL;
1674 /* count RDNs in suffix */
1675 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1676 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1686 /* Start with BE suffix */
1687 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1688 comma = strrchr( dn.bv_val, ',' );
1689 if ( ptr ) *ptr = ',';
1690 if ( comma ) *comma = '\0';
1695 dn.bv_len -= ptr - dn.bv_val;
1698 /* the normalizedDNs are always the same length, no counting
1701 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1702 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1703 ndn.bv_len = be->be_nsuffix[0].bv_len;
1706 while ( ndn.bv_val > e->e_nname.bv_val ) {
1707 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1708 ber_dupbv( &glue->e_name, &dn );
1709 ber_dupbv( &glue->e_nname, &ndn );
1711 a = ch_calloc( 1, sizeof( Attribute ));
1712 a->a_desc = slap_schema.si_ad_objectClass;
1714 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1715 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1716 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1717 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1719 a->a_nvals = a->a_vals;
1721 a->a_next = glue->e_attrs;
1724 a = ch_calloc( 1, sizeof( Attribute ));
1725 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1727 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1728 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1729 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1731 a->a_nvals = a->a_vals;
1733 a->a_next = glue->e_attrs;
1736 op->o_req_dn = glue->e_name;
1737 op->o_req_ndn = glue->e_nname;
1739 rc = be->be_add ( op, &rs_add );
1740 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1741 be_entry_release_w( op, glue );
1743 /* incl. ALREADY EXIST */
1747 /* Move to next child */
1748 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1751 if ( ptr == e->e_name.bv_val ) break;
1753 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1754 for( ptr = ndn.bv_val-2;
1755 ptr > e->e_nname.bv_val && *ptr != ',';
1761 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1764 op->o_req_dn = e->e_name;
1765 op->o_req_ndn = e->e_nname;
1767 rc = be->be_add ( op, &rs_add );
1768 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1769 be_entry_release_w( op, e );
1778 syncrepl_updateCookie(
1782 struct sync_cookie *syncCookie )
1784 Backend *be = op->o_bd;
1785 Modifications mod = {0};
1786 struct berval vals[2];
1789 char txtbuf[SLAP_TEXT_BUFLEN];
1790 size_t textlen = sizeof txtbuf;
1794 slap_callback cb = { NULL };
1795 SlapReply rs_modify = {REP_RESULT};
1797 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1798 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1800 mod.sml_op = LDAP_MOD_REPLACE;
1801 mod.sml_desc = slap_schema.si_ad_contextCSN;
1802 mod.sml_type = mod.sml_desc->ad_cname;
1803 mod.sml_values = vals;
1804 vals[0] = si->si_syncCookie.ctxcsn;
1805 vals[1].bv_val = NULL;
1808 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1810 op->o_tag = LDAP_REQ_MODIFY;
1812 assert( si->si_rid < 1000 );
1814 cb.sc_response = null_callback;
1817 op->o_callback = &cb;
1818 op->o_req_dn = op->o_bd->be_suffix[0];
1819 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1821 /* update contextCSN */
1822 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
1823 op->orm_modlist = &mod;
1824 rc = be->be_modify( op, &rs_modify );
1827 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1828 Debug( LDAP_DEBUG_ANY,
1829 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1833 slap_graduate_commit_csn( op );
1843 dninfo *dni = op->o_callback->sc_private;
1845 if ( rs->sr_type == REP_SEARCH ) {
1846 if ( !BER_BVISNULL( &dni->dn ) ) {
1847 Debug( LDAP_DEBUG_ANY,
1848 "dn_callback : consistency error - "
1849 "entryUUID is not unique\n", 0, 0, 0 );
1851 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
1852 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1853 /* If there is a new entry, see if it differs from the old.
1854 * We compare the non-normalized values so that cosmetic changes
1855 * in the provider are always propagated.
1857 if ( dni->new_entry ) {
1858 Attribute *old, *new;
1861 /* Did the DN change? Note that we don't explicitly try to
1862 * discover if the deleteOldRdn argument applies here. It
1863 * would save an unnecessary Modify if we detected it, but
1864 * that's a fair amount of trouble to compare the two attr
1867 if ( !dn_match( &rs->sr_entry->e_name,
1868 &dni->new_entry->e_name ) )
1873 for ( i = 0, old = rs->sr_entry->e_attrs;
1875 i++, old = old->a_next )
1880 /* We assume that attributes are saved in the same order
1881 * in the remote and local databases. So if we walk through
1882 * the attributeDescriptions one by one they should match in
1883 * lock step. If not, we signal a change. Otherwise we test
1886 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
1888 old = old->a_next, new = new->a_next )
1890 if ( old->a_desc != new->a_desc ) {
1891 dni->wasChanged = 1;
1894 for ( i = 0; ; i++ ) {
1896 nold = BER_BVISNULL( &old->a_vals[i] );
1897 nnew = BER_BVISNULL( &new->a_vals[i] );
1898 /* If both are empty, stop looking */
1899 if ( nold && nnew ) {
1902 /* If they are different, stop looking */
1903 if ( nold != nnew ) {
1904 dni->wasChanged = 1;
1907 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
1908 dni->wasChanged = 1;
1912 if ( dni->wasChanged ) break;
1914 if ( dni->wasChanged ) {
1915 dni->ads = op->o_tmpalloc( dni->attrs *
1916 sizeof(AttributeDescription *), op->o_tmpmemctx );
1918 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
1919 dni->ads[i] = old->a_desc;
1925 } else if ( rs->sr_type == REP_RESULT ) {
1926 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1927 Debug( LDAP_DEBUG_ANY,
1928 "dn_callback : consistency error - "
1929 "entryUUID is not unique\n", 0, 0, 0 );
1933 return LDAP_SUCCESS;
1937 nonpresent_callback(
1941 syncinfo_t *si = op->o_callback->sc_private;
1944 struct berval* present_uuid = NULL;
1945 struct nonpresent_entry *np_entry;
1947 if ( rs->sr_type == REP_RESULT ) {
1948 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1949 si->si_presentlist = NULL;
1951 } else if ( rs->sr_type == REP_SEARCH ) {
1952 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
1953 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1955 if ( a == NULL ) return 0;
1957 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1961 if ( present_uuid == NULL ) {
1962 np_entry = (struct nonpresent_entry *)
1963 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1964 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1965 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1966 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1969 avl_delete( &si->si_presentlist,
1970 &a->a_nvals[0], syncuuid_cmp );
1971 ch_free( present_uuid->bv_val );
1972 ch_free( present_uuid );
1975 return LDAP_SUCCESS;
1983 if ( rs->sr_err != LDAP_SUCCESS &&
1984 rs->sr_err != LDAP_REFERRAL &&
1985 rs->sr_err != LDAP_ALREADY_EXISTS &&
1986 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1987 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1989 Debug( LDAP_DEBUG_ANY,
1990 "null_callback : error code 0x%x\n",
1993 return LDAP_SUCCESS;
1997 slap_uuidstr_from_normalized(
1998 struct berval* uuidstr,
1999 struct berval* normalized,
2003 unsigned char nibble;
2006 if ( normalized == NULL ) return NULL;
2007 if ( normalized->bv_len != 16 ) return NULL;
2012 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2013 if ( new == NULL ) {
2020 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2021 if ( new != uuidstr ) {
2022 slap_sl_free( new, ctx );
2027 for ( i = 0; i < 16; i++ ) {
2028 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2029 new->bv_val[(i<<1)+d] = '-';
2033 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2034 if ( nibble < 10 ) {
2035 new->bv_val[(i<<1)+d] = nibble + '0';
2037 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2040 nibble = (normalized->bv_val[i]) & 0xF;
2041 if ( nibble < 10 ) {
2042 new->bv_val[(i<<1)+d+1] = nibble + '0';
2044 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2048 new->bv_val[new->bv_len] = '\0';
2053 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2055 const struct berval *uuid1 = v_uuid1;
2056 const struct berval *uuid2 = v_uuid2;
2057 int rc = uuid1->bv_len - uuid2->bv_len;
2058 if ( rc ) return rc;
2059 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2063 avl_ber_bvfree( void *v_bv )
2065 struct berval *bv = (struct berval *)v_bv;
2067 if( v_bv == NULL ) return;
2068 if ( !BER_BVISNULL( bv ) ) {
2069 ch_free( bv->bv_val );
2071 ch_free( (char *) bv );
2075 syncinfo_free( syncinfo_t *sie )
2077 if ( sie->si_provideruri ) {
2078 ch_free( sie->si_provideruri );
2080 if ( sie->si_provideruri_bv ) {
2081 ber_bvarray_free( sie->si_provideruri_bv );
2083 if ( sie->si_binddn ) {
2084 ch_free( sie->si_binddn );
2086 if ( sie->si_passwd ) {
2087 ch_free( sie->si_passwd );
2089 if ( sie->si_saslmech ) {
2090 ch_free( sie->si_saslmech );
2092 if ( sie->si_secprops ) {
2093 ch_free( sie->si_secprops );
2095 if ( sie->si_realm ) {
2096 ch_free( sie->si_realm );
2098 if ( sie->si_authcId ) {
2099 ch_free( sie->si_authcId );
2101 if ( sie->si_authzId ) {
2102 ch_free( sie->si_authzId );
2104 if ( sie->si_filterstr.bv_val ) {
2105 ch_free( sie->si_filterstr.bv_val );
2107 if ( sie->si_base.bv_val ) {
2108 ch_free( sie->si_base.bv_val );
2110 if ( sie->si_attrs ) {
2112 while ( sie->si_attrs[i] != NULL ) {
2113 ch_free( sie->si_attrs[i] );
2116 ch_free( sie->si_attrs );
2118 if ( sie->si_exattrs ) {
2120 while ( sie->si_exattrs[i] != NULL ) {
2121 ch_free( sie->si_exattrs[i] );
2124 ch_free( sie->si_exattrs );
2126 if ( sie->si_anlist ) {
2128 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2129 ch_free( sie->si_anlist[i].an_name.bv_val );
2132 ch_free( sie->si_anlist );
2134 if ( sie->si_exanlist ) {
2136 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2137 ch_free( sie->si_exanlist[i].an_name.bv_val );
2140 ch_free( sie->si_exanlist );
2142 if ( sie->si_retryinterval ) {
2143 ch_free( sie->si_retryinterval );
2145 if ( sie->si_retrynum ) {
2146 ch_free( sie->si_retrynum );
2148 if ( sie->si_retrynum_init ) {
2149 ch_free( sie->si_retrynum_init );
2151 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2152 if ( sie->si_presentlist ) {
2153 avl_free( sie->si_presentlist, avl_ber_bvfree );
2156 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2158 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2159 struct nonpresent_entry* npe;
2160 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2161 LDAP_LIST_REMOVE( npe, npe_link );
2162 if ( npe->npe_name ) {
2163 if ( npe->npe_name->bv_val ) {
2164 ch_free( npe->npe_name->bv_val );
2166 ch_free( npe->npe_name );
2168 if ( npe->npe_nname ) {
2169 if ( npe->npe_nname->bv_val ) {
2170 ch_free( npe->npe_nname->bv_val );
2172 ch_free( npe->npe_nname );