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"
34 /* FIXME: for ldap_ld_free() */
36 #include "../../libraries/libldap/ldap-int.h"
38 struct nonpresent_entry {
39 struct berval *npe_name;
40 struct berval *npe_nname;
41 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
44 typedef struct syncinfo_s {
45 struct slap_backend_db *si_be;
48 struct berval si_provideruri;
49 slap_bindconf si_bindconf;
50 struct berval si_filterstr;
51 struct berval si_base;
55 AttributeName *si_anlist;
56 AttributeName *si_exanlist;
61 int si_schemachecking;
64 time_t *si_retryinterval;
65 int *si_retrynum_init;
67 struct sync_cookie si_syncCookie;
72 int si_refreshPresent;
73 Avlnode *si_presentlist;
75 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
76 ldap_pvt_thread_mutex_t si_mutex;
79 static int syncuuid_cmp( const void *, const void * );
80 static void avl_ber_bvfree( void * );
81 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
82 static int syncrepl_message_to_entry(
83 syncinfo_t *, Operation *, LDAPMessage *,
84 Modifications **, Entry **, int );
85 static int syncrepl_entry(
86 syncinfo_t *, Operation*, Entry*,
87 Modifications**,int, struct berval*,
90 static void syncrepl_updateCookie(
91 syncinfo_t *, Operation *, struct berval *,
92 struct sync_cookie * );
93 static struct berval * slap_uuidstr_from_normalized(
94 struct berval *, struct berval *, void * );
96 /* callback functions */
97 static int dn_callback( struct slap_op *, struct slap_rep * );
98 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
99 static int null_callback( struct slap_op *, struct slap_rep * );
101 static AttributeDescription *sync_descs[4];
104 init_syncrepl(syncinfo_t *si)
107 char **attrs, **exattrs;
109 if ( !sync_descs[0] ) {
110 sync_descs[0] = slap_schema.si_ad_objectClass;
111 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
112 sync_descs[2] = slap_schema.si_ad_entryCSN;
113 sync_descs[3] = NULL;
116 if ( si->si_allattrs && si->si_allopattrs )
119 attrs = anlist2attrs( si->si_anlist );
122 if ( si->si_allattrs ) {
125 if ( !is_at_operational( at_find( attrs[i] ))) {
126 for ( j = i; attrs[j] != NULL; j++ ) {
129 attrs[j] = attrs[j+1];
135 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
136 attrs[i] = ch_strdup("*");
139 } else if ( si->si_allopattrs ) {
142 if ( is_at_operational( at_find( attrs[i] ))) {
143 for ( j = i; attrs[j] != NULL; j++ ) {
146 attrs[j] = attrs[j+1];
152 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
153 attrs[i] = ch_strdup("+");
157 for ( i = 0; sync_descs[i] != NULL; i++ ) {
160 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
161 for ( k = j; attrs[k] != NULL; k++ ) {
164 attrs[k] = attrs[k+1];
172 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
174 if ( si->si_allopattrs ) {
175 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
177 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
180 if ( attrs == NULL ) {
181 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
185 if ( si->si_allopattrs ) {
186 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
188 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
189 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
197 if ( si->si_allattrs == si->si_allopattrs ) {
198 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
199 attrs[i++] = ch_strdup( "*" );
200 attrs[i++] = ch_strdup( "+" );
201 } else if ( si->si_allattrs && !si->si_allopattrs ) {
202 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
203 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
204 attrs[i++] = ch_strdup( "*" );
205 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
206 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
208 } else if ( !si->si_allattrs && si->si_allopattrs ) {
209 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
210 attrs[i++] = ch_strdup( "+" );
211 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
216 si->si_attrs = attrs;
218 exattrs = anlist2attrs( si->si_exanlist );
221 for ( n = 0; exattrs[n] != NULL; n++ ) ;
223 for ( i = 0; sync_descs[i] != NULL; i++ ) {
225 while ( exattrs[j] != NULL ) {
226 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
227 for ( k = j; exattrs[k] != NULL; k++ ) {
229 ch_free( exattrs[k] );
230 exattrs[k] = exattrs[k+1];
238 for ( i = 0; exattrs[i] != NULL; i++ ) {
239 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
241 if ( ( oc = si->si_anlist[j].an_oc ) ) {
243 while ( oc->soc_required[k] ) {
244 if ( !strcmp( exattrs[i],
245 oc->soc_required[k]->sat_cname.bv_val )) {
246 for ( l = i; exattrs[l]; l++ ) {
248 ch_free( exattrs[i] );
249 exattrs[l] = exattrs[l+1];
259 for ( i = 0; exattrs[i] != NULL; i++ ) ;
262 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
265 si->si_exattrs = exattrs;
273 BerElementBuffer berbuf;
274 BerElement *ber = (BerElement *)&berbuf;
275 LDAPControl c[2], *ctrls[3];
276 struct timeval timeout;
280 /* setup LDAP SYNC control */
281 ber_init2( ber, NULL, LBER_USE_DER );
282 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
284 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
286 ber_printf( ber, "{eO}",
288 &si->si_syncCookie.octet_str );
290 ber_printf( ber, "{e}",
294 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
299 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
300 c[0].ldctl_iscritical = si->si_type < 0;
303 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
304 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
305 c[1].ldctl_value = si->si_bindconf.sb_authzId;
306 c[1].ldctl_iscritical = 1;
313 timeout.tv_sec = si->si_tlimit;
316 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
317 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
318 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
319 si->si_slimit, &msgid );
330 int cmdline_cookie_found = 0;
332 struct sync_cookie *sc = NULL;
338 psub = &si->si_be->be_nsuffix[0];
340 /* Init connection to master */
341 rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
342 if ( rc != LDAP_SUCCESS ) {
343 Debug( LDAP_DEBUG_ANY,
344 "do_syncrep1: ldap_initialize failed (%s)\n",
345 si->si_provideruri.bv_val, 0, 0 );
349 op->o_protocol = LDAP_VERSION3;
350 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
354 if ( si->si_bindconf.sb_tls ) {
355 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
356 if( rc != LDAP_SUCCESS ) {
357 Debug( LDAP_DEBUG_ANY,
358 "%s: ldap_start_tls failed (%d)\n",
359 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
361 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
365 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
366 #ifdef HAVE_CYRUS_SASL
369 if ( si->si_bindconf.sb_secprops != NULL ) {
370 rc = ldap_set_option( si->si_ld,
371 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
373 if( rc != LDAP_OPT_SUCCESS ) {
374 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
375 "(%s,SECPROPS,\"%s\") failed!\n",
376 si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
381 defaults = lutil_sasl_defaults( si->si_ld,
382 si->si_bindconf.sb_saslmech.bv_val,
383 si->si_bindconf.sb_realm.bv_val,
384 si->si_bindconf.sb_authcId.bv_val,
385 si->si_bindconf.sb_cred.bv_val,
386 si->si_bindconf.sb_authzId.bv_val );
388 rc = ldap_sasl_interactive_bind_s( si->si_ld,
389 si->si_bindconf.sb_binddn.bv_val,
390 si->si_bindconf.sb_saslmech.bv_val,
396 lutil_sasl_freedefs( defaults );
398 /* FIXME: different error behaviors according to
400 * 2) on err policy : exit, retry, backoff ...
402 if ( rc != LDAP_SUCCESS ) {
403 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
405 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
406 "ldap_sasl_interactive_bind_s failed (%d)\n",
409 /* FIXME (see above comment) */
410 /* if Kerberos credentials cache is not active, retry */
411 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
412 rc == LDAP_LOCAL_ERROR )
414 rc = LDAP_SERVER_DOWN;
419 #else /* HAVE_CYRUS_SASL */
420 /* Should never get here, we trapped this at config time */
422 fprintf( stderr, "not compiled with SASL support\n" );
427 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
428 rc = ldap_sasl_bind_s( si->si_ld,
429 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
430 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
431 if ( rc != LDAP_SUCCESS ) {
432 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
433 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
438 /* Set SSF to strongest of TLS, SASL SSFs */
441 op->o_transport_ssf = 0;
443 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
444 == LDAP_SUCCESS && ssl != NULL )
446 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
448 #endif /* HAVE_TLS */
449 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
450 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
451 ? op->o_sasl_ssf : op->o_tls_ssf;
454 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
455 /* get contextCSN shadow replica from database */
456 BerVarray csn = NULL;
458 assert( si->si_rid < 1000 );
459 op->o_req_ndn = op->o_bd->be_nsuffix[0];
460 op->o_req_dn = op->o_req_ndn;
462 /* try to read stored contextCSN */
463 backend_attribute( op, NULL, &op->o_req_ndn,
464 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
466 ch_free( si->si_syncCookie.ctxcsn.bv_val );
467 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
468 ber_bvarray_free_x( csn, op->o_tmpmemctx );
471 si->si_syncCookie.rid = si->si_rid;
473 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
474 if ( si->si_rid == sc->rid ) {
475 cmdline_cookie_found = 1;
480 if ( cmdline_cookie_found ) {
481 /* cookie is supplied in the command line */
483 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
485 /* ctxcsn wasn't parsed yet, do it now */
486 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
487 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
488 /* if cmdline cookie does not have ctxcsn */
489 /* component, set it to an initial value */
490 slap_init_sync_cookie_ctxcsn( sc );
492 slap_sync_cookie_free( &si->si_syncCookie, 0 );
493 slap_dup_sync_cookie( &si->si_syncCookie, sc );
494 slap_sync_cookie_free( sc, 1 );
497 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
498 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
501 rc = ldap_sync_search( si, op->o_tmpmemctx );
503 if( rc != LDAP_SUCCESS ) {
504 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
505 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
511 ldap_unbind_ext( si->si_ld, NULL, NULL );
524 LDAPControl **rctrls = NULL;
527 BerElementBuffer berbuf;
528 BerElement *ber = (BerElement *)&berbuf;
530 LDAPMessage *res = NULL;
531 LDAPMessage *msg = NULL;
534 struct berval *retdata = NULL;
539 struct berval syncUUID = BER_BVNULL;
540 struct sync_cookie syncCookie = { BER_BVNULL };
541 struct sync_cookie syncCookie_req = { BER_BVNULL };
542 struct berval cookie = BER_BVNULL;
550 Modifications *modlist = NULL;
555 struct timeval *tout_p = NULL;
556 struct timeval tout = { 0, 0 };
558 int refreshDeletes = 0;
560 BerVarray syncUUIDs = NULL;
563 if ( slapd_shutdown ) {
568 ber_init2( ber, NULL, LBER_USE_DER );
569 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
571 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
573 psub = &si->si_be->be_nsuffix[0];
575 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
577 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
583 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
584 tout_p, &res )) > 0 )
586 if ( slapd_shutdown ) {
590 for( msg = ldap_first_message( si->si_ld, res );
592 msg = ldap_next_message( si->si_ld, msg ) )
594 if ( slapd_shutdown ) {
598 switch( ldap_msgtype( msg ) ) {
599 case LDAP_RES_SEARCH_ENTRY:
600 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
601 /* we can't work without the control */
603 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
604 "got search entry without "
605 "control\n", 0, 0, 0 );
610 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
611 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
612 /* FIXME: what if syncUUID is NULL or empty? */
613 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
614 ber_scanf( ber, /*"{"*/ "m}", &cookie );
615 if ( !BER_BVISNULL( &cookie ) ) {
616 ch_free( syncCookie.octet_str.bv_val );
617 ber_dupbv( &syncCookie.octet_str, &cookie );
619 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
621 slap_parse_sync_cookie( &syncCookie, NULL );
624 if ( syncrepl_message_to_entry( si, op, msg,
625 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
626 rc_efree = syncrepl_entry( si, op, entry, &modlist,
627 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
628 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
630 syncrepl_updateCookie( si, op, psub, &syncCookie );
633 ldap_controls_free( rctrls );
635 slap_mods_free( modlist, 1 );
637 if ( rc_efree && entry ) {
643 case LDAP_RES_SEARCH_REFERENCE:
644 Debug( LDAP_DEBUG_ANY,
645 "do_syncrep2: reference received error\n", 0, 0, 0 );
648 case LDAP_RES_SEARCH_RESULT:
649 Debug( LDAP_DEBUG_SYNC,
650 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
651 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
655 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
657 ber_scanf( ber, "{" /*"}"*/);
658 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
659 ber_scanf( ber, "m", &cookie );
660 if ( !BER_BVISNULL( &cookie ) ) {
661 ch_free( syncCookie.octet_str.bv_val );
662 ber_dupbv( &syncCookie.octet_str, &cookie);
664 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
666 slap_parse_sync_cookie( &syncCookie, NULL );
669 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
671 ber_scanf( ber, "b", &refreshDeletes );
673 ber_scanf( ber, /*"{"*/ "}" );
675 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
677 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
680 value_match( &match, slap_schema.si_ad_entryCSN,
681 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
682 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
683 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
686 if ( !BER_BVISNULL( &syncCookie.octet_str ) &&
687 match < 0 && err == LDAP_SUCCESS )
689 syncrepl_updateCookie( si, op, psub, &syncCookie );
692 ldap_controls_free( rctrls );
694 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
695 /* FIXME : different error behaviors according to
696 * 1) err code : LDAP_BUSY ...
697 * 2) on err policy : stop service, stop sync, retry
699 if ( refreshDeletes == 0 && match < 0 &&
700 err == LDAP_SUCCESS )
702 syncrepl_del_nonpresent( op, si, NULL );
704 avl_free( si->si_presentlist, avl_ber_bvfree );
705 si->si_presentlist = NULL;
712 case LDAP_RES_INTERMEDIATE:
713 rc = ldap_parse_intermediate( si->si_ld, msg,
714 &retoid, &retdata, NULL, 0 );
715 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
716 ber_init2( ber, retdata, LBER_USE_DER );
718 switch ( si_tag = ber_peek_tag( ber, &len )) {
720 case LDAP_TAG_SYNC_NEW_COOKIE:
721 Debug( LDAP_DEBUG_SYNC,
722 "do_syncrep2: %s - %s%s\n",
723 "LDAP_RES_INTERMEDIATE",
724 "NEW_COOKIE", "\n" );
725 ber_scanf( ber, "tm", &tag, &cookie );
727 case LDAP_TAG_SYNC_REFRESH_DELETE:
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 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
736 si->si_refreshDelete = 1;
738 si->si_refreshPresent = 1;
740 ber_scanf( ber, "t{" /*"}"*/, &tag );
741 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
743 ber_scanf( ber, "m", &cookie );
744 if ( !BER_BVISNULL( &cookie ) ) {
745 ch_free( syncCookie.octet_str.bv_val );
746 ber_dupbv( &syncCookie.octet_str, &cookie );
748 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
750 slap_parse_sync_cookie( &syncCookie, NULL );
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 ch_free( syncCookie.octet_str.bv_val );
773 ber_dupbv( &syncCookie.octet_str, &cookie );
775 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
777 slap_parse_sync_cookie( &syncCookie, NULL );
780 if ( ber_peek_tag( ber, &len ) ==
781 LDAP_TAG_REFRESHDELETES )
783 ber_scanf( ber, "b", &refreshDeletes );
785 ber_scanf( ber, "[W]", &syncUUIDs );
786 ber_scanf( ber, /*"{"*/ "}" );
787 if ( refreshDeletes ) {
788 syncrepl_del_nonpresent( op, si, syncUUIDs );
789 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
791 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
792 struct berval *syncuuid_bv;
793 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
794 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
795 avl_insert( &si->si_presentlist,
796 (caddr_t) syncuuid_bv,
797 syncuuid_cmp, avl_dup_error );
799 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
803 Debug( LDAP_DEBUG_ANY,
804 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
805 (long) si_tag, 0, 0 );
806 ldap_memfree( retoid );
807 ber_bvfree( retdata );
811 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
813 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
816 value_match( &match, slap_schema.si_ad_entryCSN,
817 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
818 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
819 &syncCookie_req.ctxcsn,
820 &syncCookie.ctxcsn, &text );
823 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
826 syncrepl_updateCookie( si, op, psub, &syncCookie);
829 if ( si->si_refreshPresent == 1 ) {
831 syncrepl_del_nonpresent( op, si, NULL );
835 ldap_memfree( retoid );
836 ber_bvfree( retdata );
840 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
841 "unknown intermediate response (%d)\n",
843 ldap_memfree( retoid );
844 ber_bvfree( retdata );
850 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
851 "unknown message\n", 0, 0, 0 );
855 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
856 slap_sync_cookie_free( &syncCookie_req, 0 );
857 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
858 slap_sync_cookie_free( &syncCookie, 0 );
868 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
869 errstr = ldap_err2string( rc );
871 Debug( LDAP_DEBUG_ANY,
872 "do_syncrep2 : %s\n", errstr, 0, 0 );
876 slap_sync_cookie_free( &syncCookie, 0 );
877 slap_sync_cookie_free( &syncCookie_req, 0 );
879 if ( res ) ldap_msgfree( res );
881 if ( rc && si->si_ld ) {
882 ldap_unbind_ext( si->si_ld, NULL, NULL );
894 struct re_s* rtask = arg;
895 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
896 Connection conn = {0};
897 char opbuf[OPERATION_BUFFER_SIZE];
899 int rc = LDAP_SUCCESS;
906 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
911 ldap_pvt_thread_mutex_lock( &si->si_mutex );
913 switch( abs( si->si_type )) {
914 case LDAP_SYNC_REFRESH_ONLY:
915 case LDAP_SYNC_REFRESH_AND_PERSIST:
918 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
922 if ( slapd_shutdown ) {
924 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
925 connection_client_stop( s );
926 ldap_unbind_ext( si->si_ld, NULL, NULL );
929 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
933 op = (Operation *)opbuf;
934 connection_fake_init( &conn, op, ctx );
936 /* use global malloc for now */
937 op->o_tmpmemctx = NULL;
938 op->o_tmpmfuncs = &ch_mfuncs;
940 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
941 op->o_bd = be = si->si_be;
942 op->o_dn = op->o_bd->be_rootdn;
943 op->o_ndn = op->o_bd->be_rootndn;
945 /* Establish session, do search */
948 si->si_refreshDelete = 0;
949 si->si_refreshPresent = 0;
950 rc = do_syncrep1( op, si );
953 /* Process results */
954 if ( rc == LDAP_SUCCESS ) {
955 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
957 rc = do_syncrep2( op, si );
959 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
960 /* If we succeeded, enable the connection for further listening.
961 * If we failed, tear down the connection and reschedule.
963 if ( rc == LDAP_SUCCESS ) {
965 rc = connection_client_setup( s, do_syncrepl, arg );
967 connection_client_enable( s );
969 } else if ( !first ) {
973 if ( rc == -2 ) rc = 0;
977 /* At this point, we have 4 cases:
978 * 1) for any hard failure, give up and remove this task
979 * 2) for ServerDown, reschedule this task to run
980 * 3) for Refresh and Success, reschedule to run
981 * 4) for Persist and Success, reschedule to defer
983 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
985 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
986 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
990 connection_client_stop( s );
993 if ( rc == LDAP_SUCCESS ) {
994 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
997 rtask->interval.tv_sec = si->si_interval;
998 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
999 if ( si->si_retrynum ) {
1000 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
1001 si->si_retrynum[i] = si->si_retrynum_init[i];
1003 si->si_retrynum[i] = -2;
1006 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1007 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
1011 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1012 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1013 } else if ( si->si_retrynum[i] >= -1 ) {
1014 if ( si->si_retrynum[i] > 0 )
1015 si->si_retrynum[i]--;
1016 rtask->interval.tv_sec = si->si_retryinterval[i];
1017 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1018 slap_wake_listener();
1022 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1023 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1029 syncrepl_message_to_entry(
1033 Modifications **modlist,
1039 BerElement *ber = NULL;
1042 Modifications **modtail = modlist;
1045 char txtbuf[SLAP_TEXT_BUFLEN];
1046 size_t textlen = sizeof txtbuf;
1048 struct berval bdn = {0, NULL}, dn, ndn;
1053 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1054 Debug( LDAP_DEBUG_ANY,
1055 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1059 op->o_tag = LDAP_REQ_ADD;
1061 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1063 if ( rc != LDAP_SUCCESS ) {
1064 Debug( LDAP_DEBUG_ANY,
1065 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1069 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1070 ber_dupbv( &op->o_req_dn, &dn );
1071 ber_dupbv( &op->o_req_ndn, &ndn );
1072 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1073 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1075 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1078 return LDAP_SUCCESS;
1081 if ( entry == NULL ) {
1085 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1087 e->e_name = op->o_req_dn;
1088 e->e_nname = op->o_req_ndn;
1090 while ( ber_remaining( ber ) ) {
1091 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1092 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1097 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1099 mod->sml_op = LDAP_MOD_REPLACE;
1101 mod->sml_next = NULL;
1102 mod->sml_desc = NULL;
1103 mod->sml_type = tmp.sml_type;
1104 mod->sml_values = tmp.sml_values;
1105 mod->sml_nvalues = NULL;
1108 modtail = &mod->sml_next;
1111 if ( *modlist == NULL ) {
1112 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1118 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1120 if ( rc != LDAP_SUCCESS ) {
1121 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1126 /* Strip out dynamically generated attrs */
1127 for ( modtail = modlist; *modtail ; ) {
1129 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1130 *modtail = mod->sml_next;
1131 slap_mod_free( &mod->sml_mod, 0 );
1134 modtail = &mod->sml_next;
1138 /* Strip out attrs in exattrs list */
1139 for ( modtail = modlist; *modtail ; ) {
1141 if ( ldap_charray_inlist( si->si_exattrs,
1142 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1143 *modtail = mod->sml_next;
1144 slap_mod_free( &mod->sml_mod, 0 );
1147 modtail = &mod->sml_next;
1151 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1152 if( rc != LDAP_SUCCESS ) {
1153 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1158 ber_free ( ber, 0 );
1159 if ( rc != LDAP_SUCCESS ) {
1169 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1171 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1172 * entry if a previous refresh was interrupted before sending us a new
1173 * context state. We try to compare the new entry to the existing entry
1174 * and ignore the new entry if they are the same.
1176 * Also, we may get an update where the entryDN has changed, due to
1177 * a ModDn on the provider. We detect this as well, so we can issue
1178 * the corresponding operation locally.
1180 * In the case of a modify, we get a list of all the attributes
1181 * in the original entry. Rather than deleting the entry and re-adding it,
1182 * we issue a Modify request that deletes all the attributes and adds all
1183 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1186 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1187 * a ModDN we will issue both operations on the local database.
1189 typedef struct dninfo {
1193 int renamed; /* Was an existing entry renamed? */
1194 int wasChanged; /* are the attributes changed? */
1195 int attrs; /* how many attribute types are in the ads list */
1196 AttributeDescription **ads;
1204 Modifications** modlist,
1206 struct berval* syncUUID,
1207 struct sync_cookie* syncCookie_req,
1208 struct berval* syncCSN )
1210 Backend *be = op->o_bd;
1211 slap_callback cb = { NULL, NULL, NULL, NULL };
1212 struct berval *syncuuid_bv = NULL;
1213 struct berval syncUUID_strrep = BER_BVNULL;
1214 struct berval uuid_bv = BER_BVNULL;
1216 SlapReply rs_search = {REP_RESULT};
1217 SlapReply rs_delete = {REP_RESULT};
1218 SlapReply rs_add = {REP_RESULT};
1219 SlapReply rs_modify = {REP_RESULT};
1221 #ifdef LDAP_COMP_MATCH
1222 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1224 AttributeAssertion ava = { NULL, BER_BVNULL };
1226 int rc = LDAP_SUCCESS;
1227 int ret = LDAP_SUCCESS;
1229 struct berval pdn = BER_BVNULL;
1233 switch( syncstate ) {
1234 case LDAP_SYNC_PRESENT:
1235 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1237 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1240 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1242 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1244 case LDAP_SYNC_DELETE:
1245 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1247 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1249 case LDAP_SYNC_MODIFY:
1250 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1252 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1255 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1257 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1260 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1261 if ( !si->si_refreshPresent ) {
1262 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1263 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1264 syncuuid_cmp, avl_dup_error );
1268 if ( syncstate == LDAP_SYNC_PRESENT ) {
1270 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1271 if ( entry == NULL ) {
1276 f.f_choice = LDAP_FILTER_EQUALITY;
1278 ava.aa_desc = slap_schema.si_ad_entryUUID;
1279 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1280 ava.aa_value = *syncUUID;
1281 op->ors_filter = &f;
1283 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1284 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1285 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1286 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1287 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1288 syncUUID->bv_val, syncUUID->bv_len );
1289 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1290 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1292 op->o_tag = LDAP_REQ_SEARCH;
1293 op->ors_scope = LDAP_SCOPE_SUBTREE;
1294 op->ors_deref = LDAP_DEREF_NEVER;
1296 /* get the entry for this UUID */
1297 op->o_req_dn = si->si_base;
1298 op->o_req_ndn = si->si_base;
1300 op->o_time = slap_get_time();
1301 op->ors_tlimit = SLAP_NO_LIMIT;
1304 op->ors_attrs = slap_anlist_all_attributes;
1305 op->ors_attrsonly = 0;
1307 /* set callback function */
1308 op->o_callback = &cb;
1309 cb.sc_response = dn_callback;
1310 cb.sc_private = &dni;
1311 dni.new_entry = entry;
1313 if ( limits_check( op, &rs_search ) == 0 ) {
1314 rc = be->be_search( op, &rs_search );
1315 Debug( LDAP_DEBUG_SYNC,
1316 "syncrepl_entry: %s (%d)\n",
1317 "be_search", rc, 0 );
1320 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1321 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1324 cb.sc_response = null_callback;
1327 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1328 Debug( LDAP_DEBUG_SYNC,
1329 "syncrepl_entry: %s\n",
1330 entry->e_name.bv_val, 0, 0 );
1332 Debug( LDAP_DEBUG_SYNC,
1333 "syncrepl_entry: %s\n",
1334 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1337 if ( syncstate != LDAP_SYNC_DELETE ) {
1338 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1341 /* add if missing */
1342 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1343 &syncUUID_strrep, syncUUID );
1345 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1346 /* replace only if necessary */
1347 if ( a->a_nvals != a->a_vals ) {
1348 ber_memfree( a->a_nvals[0].bv_val );
1349 ber_dupbv( &a->a_nvals[0], syncUUID );
1351 ber_memfree( a->a_vals[0].bv_val );
1352 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1356 switch ( syncstate ) {
1358 case LDAP_SYNC_MODIFY:
1360 if ( BER_BVISNULL( &dni.dn )) {
1362 op->o_req_dn = entry->e_name;
1363 op->o_req_ndn = entry->e_nname;
1364 op->o_tag = LDAP_REQ_ADD;
1367 rc = be->be_add( op, &rs_add );
1368 Debug( LDAP_DEBUG_SYNC,
1369 "syncrepl_entry: %s (%d)\n",
1371 switch ( rs_add.sr_err ) {
1373 be_entry_release_w( op, entry );
1378 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1379 * only if the suffix entry is not present */
1380 case LDAP_NO_SUCH_OBJECT:
1381 syncrepl_add_glue( op, entry );
1385 /* if an entry was added via syncrepl_add_glue(),
1386 * it likely has no entryUUID, so the previous
1387 * be_search() doesn't find it. In this case,
1388 * give syncrepl a chance to modify it. Also
1389 * allow for entries that were recreated with the
1390 * same DN but a different entryUUID.
1392 case LDAP_ALREADY_EXISTS:
1394 Operation op2 = *op;
1395 SlapReply rs2 = { 0 };
1396 slap_callback cb2 = { 0 };
1398 op2.o_tag = LDAP_REQ_SEARCH;
1399 op2.o_req_dn = entry->e_name;
1400 op2.o_req_ndn = entry->e_nname;
1401 op2.ors_scope = LDAP_SCOPE_BASE;
1402 op2.ors_deref = LDAP_DEREF_NEVER;
1403 op2.ors_attrs = slap_anlist_all_attributes;
1404 op2.ors_attrsonly = 0;
1405 op2.ors_limit = NULL;
1407 op2.ors_tlimit = SLAP_NO_LIMIT;
1409 f.f_choice = LDAP_FILTER_PRESENT;
1410 f.f_desc = slap_schema.si_ad_objectClass;
1411 op2.ors_filter = &f;
1412 op2.ors_filterstr = generic_filterstr;
1414 op2.o_callback = &cb2;
1415 cb2.sc_response = dn_callback;
1416 cb2.sc_private = &dni;
1418 be->be_search( &op2, &rs2 );
1426 Debug( LDAP_DEBUG_ANY,
1427 "syncrepl_entry : be_add failed (%d)\n",
1428 rs_add.sr_err, 0, 0 );
1435 op->o_req_dn = dni.dn;
1436 op->o_req_ndn = dni.ndn;
1437 if ( dni.renamed ) {
1438 struct berval noldp, newp, nnewp;
1440 op->o_tag = LDAP_REQ_MODRDN;
1441 dnRdn( &entry->e_name, &op->orr_newrdn );
1442 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1444 dnParent( &dni.ndn, &noldp );
1445 dnParent( &entry->e_nname, &nnewp );
1446 if ( !dn_match( &noldp, &newp )) {
1447 dnParent( &entry->e_name, &newp );
1448 op->orr_newSup = &newp;
1449 op->orr_nnewSup = &nnewp;
1451 op->orr_deleteoldrdn = 0;
1452 rc = be->be_modrdn( op, &rs_modify );
1453 Debug( LDAP_DEBUG_SYNC,
1454 "syncrepl_entry: %s (%d)\n",
1455 "be_modrdn", rc, 0 );
1456 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1457 op->o_req_dn = entry->e_name;
1458 op->o_req_ndn = entry->e_nname;
1464 if ( dni.wasChanged ) {
1465 Modifications *mod, *modhead = NULL;
1466 Modifications *modtail = NULL;
1469 op->o_tag = LDAP_REQ_MODIFY;
1471 assert( *modlist != NULL );
1473 /* Delete all the old attrs */
1474 for ( i = 0; i < dni.attrs; i++ ) {
1475 mod = ch_malloc( sizeof( Modifications ) );
1476 mod->sml_op = LDAP_MOD_DELETE;
1478 mod->sml_desc = dni.ads[i];
1479 mod->sml_type = mod->sml_desc->ad_cname;
1480 mod->sml_values = NULL;
1481 mod->sml_nvalues = NULL;
1482 if ( !modhead ) modhead = mod;
1484 modtail->sml_next = mod;
1489 /* Append passed in list to ours */
1491 modtail->sml_next = *modlist;
1497 /* Find end of this list */
1498 for ( ; mod != NULL; mod = mod->sml_next ) {
1502 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1503 mod->sml_op = LDAP_MOD_REPLACE;
1505 mod->sml_desc = slap_schema.si_ad_entryUUID;
1506 mod->sml_type = mod->sml_desc->ad_cname;
1507 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1508 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1509 ber_dupbv( &uuid_bv, syncUUID );
1510 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1511 modtail->sml_next = mod;
1513 op->o_tag = LDAP_REQ_MODIFY;
1514 op->orm_modlist = *modlist;
1516 rc = be->be_modify( op, &rs_modify );
1517 Debug( LDAP_DEBUG_SYNC,
1518 "syncrepl_entry: %s (%d)\n",
1519 "be_modify", rc, 0 );
1520 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1521 Debug( LDAP_DEBUG_ANY,
1522 "syncrepl_entry : be_modify failed (%d)\n",
1523 rs_modify.sr_err, 0, 0 );
1528 case LDAP_SYNC_DELETE :
1529 if ( !BER_BVISNULL( &dni.dn )) {
1530 op->o_req_dn = dni.dn;
1531 op->o_req_ndn = dni.ndn;
1532 op->o_tag = LDAP_REQ_DELETE;
1533 rc = be->be_delete( op, &rs_delete );
1534 Debug( LDAP_DEBUG_SYNC,
1535 "syncrepl_entry: %s (%d)\n",
1536 "be_delete", rc, 0 );
1538 while ( rs_delete.sr_err == LDAP_SUCCESS
1539 && op->o_delete_glue_parent ) {
1540 op->o_delete_glue_parent = 0;
1541 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1542 slap_callback cb = { NULL };
1543 cb.sc_response = slap_null_cb;
1544 dnParent( &op->o_req_ndn, &pdn );
1546 op->o_req_ndn = pdn;
1547 op->o_callback = &cb;
1548 op->o_bd->be_delete( op, &rs_delete );
1558 Debug( LDAP_DEBUG_ANY,
1559 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1565 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1566 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1567 BER_BVZERO( &syncUUID_strrep );
1570 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1572 if ( !BER_BVISNULL( &dni.ndn ) ) {
1573 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1575 if ( !BER_BVISNULL( &dni.dn ) ) {
1576 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1581 static struct berval gcbva[] = {
1587 #define NP_DELETE_ONE 2
1590 syncrepl_del_nonpresent(
1595 Backend* be = op->o_bd;
1596 slap_callback cb = { NULL };
1597 SlapReply rs_search = {REP_RESULT};
1598 SlapReply rs_delete = {REP_RESULT};
1599 SlapReply rs_modify = {REP_RESULT};
1600 struct nonpresent_entry *np_list, *np_prev;
1602 AttributeName an[2];
1604 struct berval pdn = BER_BVNULL;
1606 op->o_req_dn = si->si_base;
1607 op->o_req_ndn = si->si_base;
1609 cb.sc_response = nonpresent_callback;
1612 op->o_callback = &cb;
1613 op->o_tag = LDAP_REQ_SEARCH;
1614 op->ors_scope = si->si_scope;
1615 op->ors_deref = LDAP_DEREF_NEVER;
1616 op->o_time = slap_get_time();
1617 op->ors_tlimit = SLAP_NO_LIMIT;
1622 #ifdef LDAP_COMP_MATCH
1623 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1625 AttributeAssertion eq = { NULL, BER_BVNULL };
1629 op->ors_attrsonly = 1;
1630 op->ors_attrs = slap_anlist_no_attrs;
1631 op->ors_limit = NULL;
1632 op->ors_filter = &uf;
1635 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1637 uf.f_choice = LDAP_FILTER_EQUALITY;
1638 si->si_refreshDelete |= NP_DELETE_ONE;
1640 for (i=0; uuids[i].bv_val; i++) {
1642 uf.f_av_value = uuids[i];
1643 rc = be->be_search( op, &rs_search );
1645 si->si_refreshDelete ^= NP_DELETE_ONE;
1647 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1648 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1649 an[0].an_desc = slap_schema.si_ad_entryUUID;
1651 op->ors_slimit = SLAP_NO_LIMIT;
1652 op->ors_attrsonly = 0;
1653 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1654 op->ors_filterstr = si->si_filterstr;
1655 op->o_nocaching = 1;
1657 if ( limits_check( op, &rs_search ) == 0 ) {
1658 rc = be->be_search( op, &rs_search );
1660 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1663 op->o_nocaching = 0;
1665 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1667 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1669 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1670 while ( np_list != NULL ) {
1671 LDAP_LIST_REMOVE( np_list, npe_link );
1673 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1674 op->o_tag = LDAP_REQ_DELETE;
1675 op->o_callback = &cb;
1676 cb.sc_response = null_callback;
1678 op->o_req_dn = *np_prev->npe_name;
1679 op->o_req_ndn = *np_prev->npe_nname;
1680 rc = op->o_bd->be_delete( op, &rs_delete );
1682 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1683 Modifications mod1, mod2;
1684 mod1.sml_op = LDAP_MOD_REPLACE;
1686 mod1.sml_desc = slap_schema.si_ad_objectClass;
1687 mod1.sml_type = mod1.sml_desc->ad_cname;
1688 mod1.sml_values = &gcbva[0];
1689 mod1.sml_nvalues = NULL;
1690 mod1.sml_next = &mod2;
1692 mod2.sml_op = LDAP_MOD_REPLACE;
1694 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
1695 mod2.sml_type = mod2.sml_desc->ad_cname;
1696 mod2.sml_values = &gcbva[1];
1697 mod2.sml_nvalues = NULL;
1698 mod2.sml_next = NULL;
1700 op->o_tag = LDAP_REQ_MODIFY;
1701 op->orm_modlist = &mod1;
1703 rc = be->be_modify( op, &rs_modify );
1706 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1707 op->o_delete_glue_parent ) {
1708 op->o_delete_glue_parent = 0;
1709 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1710 slap_callback cb = { NULL };
1711 cb.sc_response = slap_null_cb;
1712 dnParent( &op->o_req_ndn, &pdn );
1714 op->o_req_ndn = pdn;
1715 op->o_callback = &cb;
1716 /* give it a root privil ? */
1717 op->o_bd->be_delete( op, &rs_delete );
1723 op->o_delete_glue_parent = 0;
1725 ber_bvfree( np_prev->npe_name );
1726 ber_bvfree( np_prev->npe_nname );
1730 slap_graduate_commit_csn( op );
1741 Backend *be = op->o_bd;
1742 slap_callback cb = { NULL };
1747 struct berval dn = {0, NULL};
1748 struct berval ndn = {0, NULL};
1750 SlapReply rs_add = {REP_RESULT};
1753 op->o_tag = LDAP_REQ_ADD;
1754 op->o_callback = &cb;
1755 cb.sc_response = null_callback;
1756 cb.sc_private = NULL;
1761 /* count RDNs in suffix */
1762 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1763 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1773 /* Start with BE suffix */
1774 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1775 comma = strrchr( dn.bv_val, ',' );
1776 if ( ptr ) *ptr = ',';
1777 if ( comma ) *comma = '\0';
1782 dn.bv_len -= ptr - dn.bv_val;
1785 /* the normalizedDNs are always the same length, no counting
1788 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1789 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1790 ndn.bv_len = be->be_nsuffix[0].bv_len;
1793 while ( ndn.bv_val > e->e_nname.bv_val ) {
1794 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1795 ber_dupbv( &glue->e_name, &dn );
1796 ber_dupbv( &glue->e_nname, &ndn );
1798 a = ch_calloc( 1, sizeof( Attribute ));
1799 a->a_desc = slap_schema.si_ad_objectClass;
1801 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1802 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1803 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1804 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1806 a->a_nvals = a->a_vals;
1808 a->a_next = glue->e_attrs;
1811 a = ch_calloc( 1, sizeof( Attribute ));
1812 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1814 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1815 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1816 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1818 a->a_nvals = a->a_vals;
1820 a->a_next = glue->e_attrs;
1823 op->o_req_dn = glue->e_name;
1824 op->o_req_ndn = glue->e_nname;
1826 rc = be->be_add ( op, &rs_add );
1827 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1828 be_entry_release_w( op, glue );
1830 /* incl. ALREADY EXIST */
1834 /* Move to next child */
1835 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1838 if ( ptr == e->e_name.bv_val ) break;
1840 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1841 for( ptr = ndn.bv_val-2;
1842 ptr > e->e_nname.bv_val && *ptr != ',';
1848 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1851 op->o_req_dn = e->e_name;
1852 op->o_req_ndn = e->e_nname;
1854 rc = be->be_add ( op, &rs_add );
1855 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1856 be_entry_release_w( op, e );
1865 syncrepl_updateCookie(
1869 struct sync_cookie *syncCookie )
1871 Backend *be = op->o_bd;
1872 Modifications mod = { { 0 } };
1873 struct berval vals[ 2 ];
1877 slap_callback cb = { NULL };
1878 SlapReply rs_modify = {REP_RESULT};
1880 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1881 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1883 mod.sml_op = LDAP_MOD_REPLACE;
1884 mod.sml_desc = slap_schema.si_ad_contextCSN;
1885 mod.sml_type = mod.sml_desc->ad_cname;
1886 mod.sml_values = vals;
1887 vals[0] = si->si_syncCookie.ctxcsn;
1888 vals[1].bv_val = NULL;
1891 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
1893 op->o_tag = LDAP_REQ_MODIFY;
1895 assert( si->si_rid < 1000 );
1897 cb.sc_response = null_callback;
1900 op->o_callback = &cb;
1901 op->o_req_dn = op->o_bd->be_suffix[0];
1902 op->o_req_ndn = op->o_bd->be_nsuffix[0];
1904 /* update contextCSN */
1905 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
1906 op->orm_modlist = &mod;
1907 rc = be->be_modify( op, &rs_modify );
1910 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1911 Debug( LDAP_DEBUG_ANY,
1912 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1915 slap_graduate_commit_csn( op );
1925 dninfo *dni = op->o_callback->sc_private;
1927 if ( rs->sr_type == REP_SEARCH ) {
1928 if ( !BER_BVISNULL( &dni->dn ) ) {
1929 Debug( LDAP_DEBUG_ANY,
1930 "dn_callback : consistency error - "
1931 "entryUUID is not unique\n", 0, 0, 0 );
1933 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
1934 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1935 /* If there is a new entry, see if it differs from the old.
1936 * We compare the non-normalized values so that cosmetic changes
1937 * in the provider are always propagated.
1939 if ( dni->new_entry ) {
1940 Attribute *old, *new;
1943 /* Did the DN change? Note that we don't explicitly try to
1944 * discover if the deleteOldRdn argument applies here. It
1945 * would save an unnecessary Modify if we detected it, but
1946 * that's a fair amount of trouble to compare the two attr
1949 if ( !dn_match( &rs->sr_entry->e_name,
1950 &dni->new_entry->e_name ) )
1955 for ( i = 0, old = rs->sr_entry->e_attrs;
1957 i++, old = old->a_next )
1962 /* We assume that attributes are saved in the same order
1963 * in the remote and local databases. So if we walk through
1964 * the attributeDescriptions one by one they should match in
1965 * lock step. If not, we signal a change. Otherwise we test
1968 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
1970 old = old->a_next, new = new->a_next )
1972 if ( old->a_desc != new->a_desc ) {
1973 dni->wasChanged = 1;
1976 for ( i = 0; ; i++ ) {
1978 nold = BER_BVISNULL( &old->a_vals[i] );
1979 nnew = BER_BVISNULL( &new->a_vals[i] );
1980 /* If both are empty, stop looking */
1981 if ( nold && nnew ) {
1984 /* If they are different, stop looking */
1985 if ( nold != nnew ) {
1986 dni->wasChanged = 1;
1989 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
1990 dni->wasChanged = 1;
1994 if ( dni->wasChanged ) break;
1996 if ( dni->wasChanged ) {
1997 dni->ads = op->o_tmpalloc( dni->attrs *
1998 sizeof(AttributeDescription *), op->o_tmpmemctx );
2000 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2001 dni->ads[i] = old->a_desc;
2007 } else if ( rs->sr_type == REP_RESULT ) {
2008 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2009 Debug( LDAP_DEBUG_ANY,
2010 "dn_callback : consistency error - "
2011 "entryUUID is not unique\n", 0, 0, 0 );
2015 return LDAP_SUCCESS;
2019 nonpresent_callback(
2023 syncinfo_t *si = op->o_callback->sc_private;
2026 struct berval* present_uuid = NULL;
2027 struct nonpresent_entry *np_entry;
2029 if ( rs->sr_type == REP_RESULT ) {
2030 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2031 si->si_presentlist = NULL;
2033 } else if ( rs->sr_type == REP_SEARCH ) {
2034 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2035 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2037 if ( a == NULL ) return 0;
2039 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2043 if ( present_uuid == NULL ) {
2044 np_entry = (struct nonpresent_entry *)
2045 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2046 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2047 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2048 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2051 avl_delete( &si->si_presentlist,
2052 &a->a_nvals[0], syncuuid_cmp );
2053 ch_free( present_uuid->bv_val );
2054 ch_free( present_uuid );
2057 return LDAP_SUCCESS;
2065 if ( rs->sr_err != LDAP_SUCCESS &&
2066 rs->sr_err != LDAP_REFERRAL &&
2067 rs->sr_err != LDAP_ALREADY_EXISTS &&
2068 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2069 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2071 Debug( LDAP_DEBUG_ANY,
2072 "null_callback : error code 0x%x\n",
2075 return LDAP_SUCCESS;
2078 static struct berval *
2079 slap_uuidstr_from_normalized(
2080 struct berval* uuidstr,
2081 struct berval* normalized,
2085 unsigned char nibble;
2088 if ( normalized == NULL ) return NULL;
2089 if ( normalized->bv_len != 16 ) return NULL;
2094 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2095 if ( new == NULL ) {
2102 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2103 if ( new != uuidstr ) {
2104 slap_sl_free( new, ctx );
2109 for ( i = 0; i < 16; i++ ) {
2110 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2111 new->bv_val[(i<<1)+d] = '-';
2115 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2116 if ( nibble < 10 ) {
2117 new->bv_val[(i<<1)+d] = nibble + '0';
2119 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2122 nibble = (normalized->bv_val[i]) & 0xF;
2123 if ( nibble < 10 ) {
2124 new->bv_val[(i<<1)+d+1] = nibble + '0';
2126 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2130 new->bv_val[new->bv_len] = '\0';
2135 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2137 const struct berval *uuid1 = v_uuid1;
2138 const struct berval *uuid2 = v_uuid2;
2139 int rc = uuid1->bv_len - uuid2->bv_len;
2140 if ( rc ) return rc;
2141 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2145 avl_ber_bvfree( void *v_bv )
2147 struct berval *bv = (struct berval *)v_bv;
2149 if( v_bv == NULL ) return;
2150 if ( !BER_BVISNULL( bv ) ) {
2151 ch_free( bv->bv_val );
2153 ch_free( (char *) bv );
2157 syncinfo_free( syncinfo_t *sie )
2159 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2160 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2161 ch_free( sie->si_provideruri.bv_val );
2164 bindconf_free( &sie->si_bindconf );
2166 if ( sie->si_filterstr.bv_val ) {
2167 ch_free( sie->si_filterstr.bv_val );
2169 if ( sie->si_base.bv_val ) {
2170 ch_free( sie->si_base.bv_val );
2172 if ( sie->si_attrs ) {
2174 while ( sie->si_attrs[i] != NULL ) {
2175 ch_free( sie->si_attrs[i] );
2178 ch_free( sie->si_attrs );
2180 if ( sie->si_exattrs ) {
2182 while ( sie->si_exattrs[i] != NULL ) {
2183 ch_free( sie->si_exattrs[i] );
2186 ch_free( sie->si_exattrs );
2188 if ( sie->si_anlist ) {
2190 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2191 ch_free( sie->si_anlist[i].an_name.bv_val );
2194 ch_free( sie->si_anlist );
2196 if ( sie->si_exanlist ) {
2198 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2199 ch_free( sie->si_exanlist[i].an_name.bv_val );
2202 ch_free( sie->si_exanlist );
2204 if ( sie->si_retryinterval ) {
2205 ch_free( sie->si_retryinterval );
2207 if ( sie->si_retrynum ) {
2208 ch_free( sie->si_retrynum );
2210 if ( sie->si_retrynum_init ) {
2211 ch_free( sie->si_retrynum_init );
2213 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2214 if ( sie->si_presentlist ) {
2215 avl_free( sie->si_presentlist, avl_ber_bvfree );
2218 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2220 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2221 struct nonpresent_entry* npe;
2222 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2223 LDAP_LIST_REMOVE( npe, npe_link );
2224 if ( npe->npe_name ) {
2225 if ( npe->npe_name->bv_val ) {
2226 ch_free( npe->npe_name->bv_val );
2228 ch_free( npe->npe_name );
2230 if ( npe->npe_nname ) {
2231 if ( npe->npe_nname->bv_val ) {
2232 ch_free( npe->npe_nname->bv_val );
2234 ch_free( npe->npe_nname );
2243 /* NOTE: used & documented in slapd.conf(5) */
2245 #define PROVIDERSTR "provider"
2246 #define SCHEMASTR "schemachecking"
2247 #define FILTERSTR "filter"
2248 #define SEARCHBASESTR "searchbase"
2249 #define SCOPESTR "scope"
2250 #define ATTRSONLYSTR "attrsonly"
2251 #define ATTRSSTR "attrs"
2252 #define TYPESTR "type"
2253 #define INTERVALSTR "interval"
2254 #define RETRYSTR "retry"
2255 #define SLIMITSTR "sizelimit"
2256 #define TLIMITSTR "timelimit"
2258 /* FIXME: undocumented */
2259 #define OLDAUTHCSTR "bindprincipal"
2260 #define EXATTRSSTR "exattrs"
2261 #define MANAGEDSAITSTR "manageDSAit"
2264 #define LASTMODSTR "lastmod"
2265 #define LMGENSTR "gen"
2266 #define LMNOSTR "no"
2267 #define LMREQSTR "req"
2268 #define SRVTABSTR "srvtab"
2269 #define SUFFIXSTR "suffix"
2272 #define GOT_ID 0x0001
2273 #define GOT_PROVIDER 0x0002
2276 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2282 { BER_BVC("base"), LDAP_SCOPE_BASE },
2283 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2284 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2285 #ifdef LDAP_SCOPE_SUBORDINATE
2286 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2287 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2289 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2290 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2295 parse_syncrepl_line(
2305 for ( i = 1; i < cargc; i++ ) {
2306 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2307 STRLENOF( IDSTR "=" ) ) )
2310 /* '\0' string terminator accounts for '=' */
2311 val = cargv[ i ] + STRLENOF( IDSTR "=" );
2313 if ( tmp >= 1000 || tmp < 0 ) {
2314 fprintf( stderr, "Error: parse_syncrepl_line: "
2315 "syncrepl id %d is out of range [0..999]\n", tmp );
2320 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2321 STRLENOF( PROVIDERSTR "=" ) ) )
2323 val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2324 ber_str2bv( val, 0, 1, &si->si_provideruri );
2325 gots |= GOT_PROVIDER;
2326 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2327 STRLENOF( SCHEMASTR "=" ) ) )
2329 val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2330 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2331 si->si_schemachecking = 1;
2332 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2333 si->si_schemachecking = 0;
2335 si->si_schemachecking = 1;
2337 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2338 STRLENOF( FILTERSTR "=" ) ) )
2340 val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2341 ber_str2bv( val, 0, 1, &si->si_filterstr );
2342 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2343 STRLENOF( SEARCHBASESTR "=" ) ) )
2348 val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2349 if ( si->si_base.bv_val ) {
2350 ch_free( si->si_base.bv_val );
2352 ber_str2bv( val, 0, 0, &bv );
2353 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2354 if ( rc != LDAP_SUCCESS ) {
2355 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2356 val, rc, ldap_err2string( rc ) );
2359 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2360 STRLENOF( SCOPESTR "=" ) ) )
2363 val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2364 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2365 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2366 si->si_scope = scopes[j].val;
2370 if ( BER_BVISNULL(&scopes[j].key) ) {
2371 fprintf( stderr, "Error: parse_syncrepl_line: "
2372 "unknown scope \"%s\"\n", val);
2375 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2376 STRLENOF( ATTRSONLYSTR "=" ) ) )
2378 si->si_attrsonly = 1;
2379 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2380 STRLENOF( ATTRSSTR "=" ) ) )
2382 val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2383 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2385 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2386 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2387 if ( si->si_anlist == NULL ) {
2388 ch_free( attr_fname );
2391 si->si_anfile = attr_fname;
2393 char *str, *s, *next;
2394 char delimstr[] = " ,\t";
2395 str = ch_strdup( val );
2396 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2398 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2400 if ( strlen(s) == 1 && *s == '*' ) {
2401 si->si_allattrs = 1;
2402 *(val + ( s - str )) = delimstr[0];
2404 if ( strlen(s) == 1 && *s == '+' ) {
2405 si->si_allopattrs = 1;
2406 *(val + ( s - str )) = delimstr[0];
2410 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2411 if ( si->si_anlist == NULL ) {
2415 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2416 STRLENOF( EXATTRSSTR "=" ) ) )
2418 val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2419 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2421 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2422 si->si_exanlist = file2anlist(
2423 si->si_exanlist, attr_fname, " ,\t" );
2424 if ( si->si_exanlist == NULL ) {
2425 ch_free( attr_fname );
2428 ch_free( attr_fname );
2430 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2431 if ( si->si_exanlist == NULL ) {
2435 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2436 STRLENOF( TYPESTR "=" ) ) )
2438 val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2439 if ( !strncasecmp( val, "refreshOnly",
2440 STRLENOF("refreshOnly") ))
2442 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2443 } else if ( !strncasecmp( val, "refreshAndPersist",
2444 STRLENOF("refreshAndPersist") ))
2446 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2447 si->si_interval = 60;
2449 fprintf( stderr, "Error: parse_syncrepl_line: "
2450 "unknown sync type \"%s\"\n", val);
2453 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2454 STRLENOF( INTERVALSTR "=" ) ) )
2456 val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2457 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2458 si->si_interval = 0;
2466 hstr = strchr( dstr, ':' );
2467 if ( hstr == NULL ) {
2468 fprintf( stderr, "Error: parse_syncrepl_line: "
2469 "invalid interval \"%s\"\n", val );
2473 mstr = strchr( hstr, ':' );
2474 if ( mstr == NULL ) {
2475 fprintf( stderr, "Error: parse_syncrepl_line: "
2476 "invalid interval \"%s\"\n", val );
2480 sstr = strchr( mstr, ':' );
2481 if ( sstr == NULL ) {
2482 fprintf( stderr, "Error: parse_syncrepl_line: "
2483 "invalid interval \"%s\"\n", val );
2492 if (( hh > 24 ) || ( hh < 0 ) ||
2493 ( mm > 60 ) || ( mm < 0 ) ||
2494 ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2495 fprintf( stderr, "Error: parse_syncrepl_line: "
2496 "invalid interval \"%s\"\n", val );
2499 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2501 if ( si->si_interval < 0 ) {
2502 fprintf( stderr, "Error: parse_syncrepl_line: "
2503 "invalid interval \"%ld\"\n",
2504 (long) si->si_interval);
2507 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2508 STRLENOF( RETRYSTR "=" ) ) )
2513 val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2514 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2515 retry_list[0] = NULL;
2517 slap_str2clist( &retry_list, val, " ,\t" );
2519 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2523 "Error: incomplete syncrepl retry list\n" );
2524 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2525 ch_free( retry_list[k] );
2527 ch_free( retry_list );
2528 exit( EXIT_FAILURE );
2530 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2531 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2532 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2533 for ( j = 0; j < n; j++ ) {
2534 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2535 if ( *retry_list[j*2+1] == '+' ) {
2536 si->si_retrynum_init[j] = -1;
2537 si->si_retrynum[j] = -1;
2541 si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2542 si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2545 si->si_retrynum_init[j] = -2;
2546 si->si_retrynum[j] = -2;
2547 si->si_retryinterval[j] = 0;
2549 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2550 ch_free( retry_list[k] );
2552 ch_free( retry_list );
2553 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2554 STRLENOF( MANAGEDSAITSTR "=" ) ) )
2556 val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2557 si->si_manageDSAit = atoi( val );
2558 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2559 STRLENOF( SLIMITSTR "=") ) )
2561 val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2562 si->si_slimit = atoi( val );
2563 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2564 STRLENOF( TLIMITSTR "=" ) ) )
2566 val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2567 si->si_tlimit = atoi( val );
2568 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2569 fprintf( stderr, "Error: parse_syncrepl_line: "
2570 "unknown keyword \"%s\"\n", cargv[ i ] );
2575 if ( gots != GOT_ALL ) {
2577 "Error: Malformed \"syncrepl\" line in slapd config file" );
2594 if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
2595 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
2596 "required for syncrepl\n", be->be_type, 0, 0 );
2599 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2602 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2606 si->si_bindconf.sb_tls = SB_TLS_OFF;
2607 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2608 si->si_schemachecking = 0;
2609 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2610 &si->si_filterstr );
2611 si->si_base.bv_val = NULL;
2612 si->si_scope = LDAP_SCOPE_SUBTREE;
2613 si->si_attrsonly = 0;
2614 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2615 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2616 si->si_attrs = NULL;
2617 si->si_allattrs = 0;
2618 si->si_allopattrs = 0;
2619 si->si_exattrs = NULL;
2620 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2621 si->si_interval = 86400;
2622 si->si_retryinterval = NULL;
2623 si->si_retrynum_init = NULL;
2624 si->si_retrynum = NULL;
2625 si->si_manageDSAit = 0;
2629 si->si_presentlist = NULL;
2630 LDAP_LIST_INIT( &si->si_nonpresentlist );
2631 ldap_pvt_thread_mutex_init( &si->si_mutex );
2633 rc = parse_syncrepl_line( cargv, cargc, si );
2637 init_syncrepl( si );
2638 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
2639 do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
2644 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2645 syncinfo_free( si );
2648 Debug( LDAP_DEBUG_CONFIG,
2649 "Config: ** successfully added syncrepl \"%s\"\n",
2650 BER_BVISNULL( &si->si_provideruri ) ?
2651 "(null)" : si->si_provideruri.bv_val, 0, 0 );
2652 if ( !si->si_schemachecking ) {
2653 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2655 be->be_syncinfo = si;
2661 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
2664 char buf[BUFSIZ*2], *ptr;
2667 bindconf_unparse( &si->si_bindconf, &bc );
2669 ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
2670 si->si_rid, si->si_provideruri.bv_val );
2671 if ( !BER_BVISNULL( &bc )) {
2672 ptr = lutil_strcopy( ptr, bc.bv_val );
2675 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
2676 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
2677 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
2680 if ( !BER_BVISNULL( &si->si_base )) {
2681 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
2682 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
2685 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
2686 if ( si->si_scope == scopes[i].val ) {
2687 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
2688 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
2692 if ( si->si_attrsonly ) {
2693 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
2695 if ( si->si_anfile ) {
2696 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
2697 ptr = lutil_strcopy( ptr, si->si_anfile );
2698 } else if ( si->si_allattrs || si->si_allopattrs ||
2699 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
2701 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
2703 ptr = anlist_unparse( si->si_anlist, ptr );
2704 if ( si->si_allattrs ) {
2705 if ( old != ptr ) *ptr++ = ',';
2708 if ( si->si_allopattrs ) {
2709 if ( old != ptr ) *ptr++ = ',';
2714 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
2715 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
2716 ptr = anlist_unparse( si->si_exanlist, ptr );
2718 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
2719 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
2721 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
2722 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
2723 "refreshAndPersist" : "refreshOnly" );
2725 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
2728 dd = si->si_interval;
2735 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
2736 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
2737 } else if ( si->si_retryinterval ) {
2739 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
2740 for (i=0; si->si_retryinterval[i]; i++) {
2741 if ( space ) *ptr++ = ' ';
2743 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
2744 if ( si->si_retrynum_init[i] == -1 )
2747 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
2752 if ( si->si_slimit ) {
2753 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
2754 ptr += sprintf( ptr, "%d", si->si_slimit );
2757 if ( si->si_tlimit ) {
2758 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
2759 ptr += sprintf( ptr, "%d", si->si_tlimit );
2761 bc.bv_len = ptr - buf;
2763 ber_dupbv( bv, &bc );
2767 syncrepl_config(ConfigArgs *c) {
2768 if (c->op == SLAP_CONFIG_EMIT) {
2769 if ( c->be->be_syncinfo ) {
2771 syncrepl_unparse( c->be->be_syncinfo, &bv );
2772 ber_bvarray_add( &c->rvalue_vals, &bv );
2776 } else if ( c->op == LDAP_MOD_DELETE ) {
2779 if ( c->be->be_syncinfo ) {
2780 re = c->be->be_syncinfo->si_re;
2782 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
2783 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
2784 ldap_pvt_runqueue_remove( &slapd_rq, re );
2786 syncinfo_free( c->be->be_syncinfo );
2787 c->be->be_syncinfo = NULL;
2789 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
2792 if(SLAP_SHADOW(c->be)) {
2793 Debug(LDAP_DEBUG_ANY, "%s: "
2794 "syncrepl: database already shadowed.\n",
2797 } else if(add_syncrepl(c->be, c->argv, c->argc)) {
2800 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);