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 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 typedef struct syncinfo_s {
48 struct slap_backend_db *si_be;
51 struct berval si_provideruri;
52 slap_bindconf si_bindconf;
53 struct berval si_base;
54 struct berval si_logbase;
55 struct berval si_filterstr;
56 struct berval si_logfilterstr;
60 AttributeName *si_anlist;
61 AttributeName *si_exanlist;
66 int si_schemachecking;
67 int si_type; /* the active type */
68 int si_ctype; /* the configured type */
70 time_t *si_retryinterval;
71 int *si_retrynum_init;
73 struct sync_cookie si_syncCookie;
78 int si_refreshPresent;
81 Avlnode *si_presentlist;
83 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
84 ldap_pvt_thread_mutex_t si_mutex;
87 static int syncuuid_cmp( const void *, const void * );
88 static void avl_ber_bvfree( void * );
89 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
90 static int syncrepl_message_to_op(
91 syncinfo_t *, Operation *, LDAPMessage * );
92 static int syncrepl_message_to_entry(
93 syncinfo_t *, Operation *, LDAPMessage *,
94 Modifications **, Entry **, int );
95 static int syncrepl_entry(
96 syncinfo_t *, Operation*, Entry*,
97 Modifications**,int, struct berval*,
100 static void syncrepl_updateCookie(
101 syncinfo_t *, Operation *, struct berval *,
102 struct sync_cookie * );
103 static struct berval * slap_uuidstr_from_normalized(
104 struct berval *, struct berval *, void * );
106 /* callback functions */
107 static int dn_callback( struct slap_op *, struct slap_rep * );
108 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
109 static int null_callback( struct slap_op *, struct slap_rep * );
111 static AttributeDescription *sync_descs[4];
114 init_syncrepl(syncinfo_t *si)
117 char **attrs, **exattrs;
119 if ( !sync_descs[0] ) {
120 sync_descs[0] = slap_schema.si_ad_objectClass;
121 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
122 sync_descs[2] = slap_schema.si_ad_entryCSN;
123 sync_descs[3] = NULL;
126 if ( si->si_allattrs && si->si_allopattrs )
129 attrs = anlist2attrs( si->si_anlist );
132 if ( si->si_allattrs ) {
135 if ( !is_at_operational( at_find( attrs[i] ))) {
136 for ( j = i; attrs[j] != NULL; j++ ) {
139 attrs[j] = attrs[j+1];
145 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
146 attrs[i] = ch_strdup("*");
149 } else if ( si->si_allopattrs ) {
152 if ( is_at_operational( at_find( attrs[i] ))) {
153 for ( j = i; attrs[j] != NULL; j++ ) {
156 attrs[j] = attrs[j+1];
162 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
163 attrs[i] = ch_strdup("+");
167 for ( i = 0; sync_descs[i] != NULL; i++ ) {
170 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
171 for ( k = j; attrs[k] != NULL; k++ ) {
174 attrs[k] = attrs[k+1];
182 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
184 if ( si->si_allopattrs ) {
185 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
187 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
190 if ( attrs == NULL ) {
191 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
195 if ( si->si_allopattrs ) {
196 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
198 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
199 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
207 if ( si->si_allattrs == si->si_allopattrs ) {
208 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
209 attrs[i++] = ch_strdup( "*" );
210 attrs[i++] = ch_strdup( "+" );
211 } else if ( si->si_allattrs && !si->si_allopattrs ) {
212 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
213 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
214 attrs[i++] = ch_strdup( "*" );
215 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
216 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
218 } else if ( !si->si_allattrs && si->si_allopattrs ) {
219 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
220 attrs[i++] = ch_strdup( "+" );
221 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
226 si->si_attrs = attrs;
228 exattrs = anlist2attrs( si->si_exanlist );
231 for ( n = 0; exattrs[n] != NULL; n++ ) ;
233 for ( i = 0; sync_descs[i] != NULL; i++ ) {
235 while ( exattrs[j] != NULL ) {
236 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
237 ch_free( exattrs[j] );
238 for ( k = j; exattrs[k] != NULL; k++ ) {
239 exattrs[k] = exattrs[k+1];
247 for ( i = 0; exattrs[i] != NULL; i++ ) {
248 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
250 if ( ( oc = si->si_anlist[j].an_oc ) ) {
252 while ( oc->soc_required[k] ) {
253 if ( !strcmp( exattrs[i],
254 oc->soc_required[k]->sat_cname.bv_val )) {
255 ch_free( exattrs[i] );
256 for ( l = i; exattrs[l]; l++ ) {
257 exattrs[l] = exattrs[l+1];
267 for ( i = 0; exattrs[i] != NULL; i++ ) ;
270 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
273 si->si_exattrs = exattrs;
276 typedef struct logschema {
278 struct berval ls_req;
279 struct berval ls_mod;
280 struct berval ls_newRdn;
281 struct berval ls_delRdn;
282 struct berval ls_newSup;
285 static logschema changelog_sc = {
287 BER_BVC("changeType"),
290 BER_BVC("deleteOldRDN"),
291 BER_BVC("newSuperior")
294 static logschema accesslog_sc = {
298 BER_BVC("reqNewRDN"),
299 BER_BVC("reqDeleteOldRDN"),
300 BER_BVC("reqNewSuperior")
308 BerElementBuffer berbuf;
309 BerElement *ber = (BerElement *)&berbuf;
310 LDAPControl c[2], *ctrls[3];
311 struct timeval timeout;
316 char **attrs, *lattrs[8];
321 /* setup LDAP SYNC control */
322 ber_init2( ber, NULL, LBER_USE_DER );
323 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
325 /* If we're using a log but we have no state, then fallback to
326 * normal mode for a full refresh.
328 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
329 si->si_logstate = SYNCLOG_FALLBACK;
331 /* Use the log parameters if we're in log mode */
332 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
334 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
338 lattrs[0] = ls->ls_dn.bv_val;
339 lattrs[1] = ls->ls_req.bv_val;
340 lattrs[2] = ls->ls_mod.bv_val;
341 lattrs[3] = ls->ls_newRdn.bv_val;
342 lattrs[4] = ls->ls_delRdn.bv_val;
343 lattrs[5] = ls->ls_newSup.bv_val;
344 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
348 base = si->si_logbase.bv_val;
349 filter = si->si_logfilterstr.bv_val;
352 scope = LDAP_SCOPE_ONELEVEL;
355 base = si->si_base.bv_val;
356 filter = si->si_filterstr.bv_val;
357 attrs = si->si_attrs;
358 attrsonly = si->si_attrsonly;
359 scope = si->si_scope;
361 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
362 si->si_type = LDAP_SYNC_REFRESH_ONLY;
364 si->si_type = si->si_ctype;
367 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
369 ber_printf( ber, "{eOb}",
370 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
372 ber_printf( ber, "{eb}",
373 abs(si->si_type), rhint );
376 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
381 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
382 c[0].ldctl_iscritical = si->si_type < 0;
385 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
386 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
387 c[1].ldctl_value = si->si_bindconf.sb_authzId;
388 c[1].ldctl_iscritical = 1;
395 timeout.tv_sec = si->si_tlimit;
398 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
399 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
400 si->si_slimit, &msgid );
411 int cmdline_cookie_found = 0;
413 struct sync_cookie *sc = NULL;
419 psub = &si->si_be->be_nsuffix[0];
421 /* Init connection to master */
422 rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
423 if ( rc != LDAP_SUCCESS ) {
424 Debug( LDAP_DEBUG_ANY,
425 "do_syncrep1: ldap_initialize failed (%s)\n",
426 si->si_provideruri.bv_val, 0, 0 );
430 op->o_protocol = LDAP_VERSION3;
431 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
435 if ( si->si_bindconf.sb_tls ) {
436 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
437 if( rc != LDAP_SUCCESS ) {
438 Debug( LDAP_DEBUG_ANY,
439 "%s: ldap_start_tls failed (%d)\n",
440 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
442 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
446 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
447 #ifdef HAVE_CYRUS_SASL
450 if ( si->si_bindconf.sb_secprops != NULL ) {
451 rc = ldap_set_option( si->si_ld,
452 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
454 if( rc != LDAP_OPT_SUCCESS ) {
455 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
456 "(%s,SECPROPS,\"%s\") failed!\n",
457 si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
462 defaults = lutil_sasl_defaults( si->si_ld,
463 si->si_bindconf.sb_saslmech.bv_val,
464 si->si_bindconf.sb_realm.bv_val,
465 si->si_bindconf.sb_authcId.bv_val,
466 si->si_bindconf.sb_cred.bv_val,
467 si->si_bindconf.sb_authzId.bv_val );
469 rc = ldap_sasl_interactive_bind_s( si->si_ld,
470 si->si_bindconf.sb_binddn.bv_val,
471 si->si_bindconf.sb_saslmech.bv_val,
477 lutil_sasl_freedefs( defaults );
479 /* FIXME: different error behaviors according to
481 * 2) on err policy : exit, retry, backoff ...
483 if ( rc != LDAP_SUCCESS ) {
484 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
486 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
487 "ldap_sasl_interactive_bind_s failed (%d)\n",
490 /* FIXME (see above comment) */
491 /* if Kerberos credentials cache is not active, retry */
492 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
493 rc == LDAP_LOCAL_ERROR )
495 rc = LDAP_SERVER_DOWN;
500 #else /* HAVE_CYRUS_SASL */
501 /* Should never get here, we trapped this at config time */
503 fprintf( stderr, "not compiled with SASL support\n" );
508 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
509 rc = ldap_sasl_bind_s( si->si_ld,
510 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
511 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
512 if ( rc != LDAP_SUCCESS ) {
513 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
514 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
519 /* Set SSF to strongest of TLS, SASL SSFs */
522 op->o_transport_ssf = 0;
524 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
525 == LDAP_SUCCESS && ssl != NULL )
527 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
529 #endif /* HAVE_TLS */
530 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
531 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
532 ? op->o_sasl_ssf : op->o_tls_ssf;
535 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
536 /* get contextCSN shadow replica from database */
537 BerVarray csn = NULL;
539 assert( si->si_rid < 1000 );
540 op->o_req_ndn = op->o_bd->be_nsuffix[0];
541 op->o_req_dn = op->o_req_ndn;
543 /* try to read stored contextCSN */
544 backend_attribute( op, NULL, &op->o_req_ndn,
545 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
547 ch_free( si->si_syncCookie.ctxcsn.bv_val );
548 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
549 ber_bvarray_free_x( csn, op->o_tmpmemctx );
552 si->si_syncCookie.rid = si->si_rid;
554 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
555 if ( si->si_rid == sc->rid ) {
556 cmdline_cookie_found = 1;
561 if ( cmdline_cookie_found ) {
562 /* cookie is supplied in the command line */
564 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
566 /* ctxcsn wasn't parsed yet, do it now */
567 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
568 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
569 /* if cmdline cookie does not have ctxcsn */
570 /* component, set it to an initial value */
571 slap_init_sync_cookie_ctxcsn( sc );
573 slap_sync_cookie_free( &si->si_syncCookie, 0 );
574 slap_dup_sync_cookie( &si->si_syncCookie, sc );
575 slap_sync_cookie_free( sc, 1 );
578 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
579 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
582 rc = ldap_sync_search( si, op->o_tmpmemctx );
584 if( rc != LDAP_SUCCESS ) {
585 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
586 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
592 ldap_unbind_ext( si->si_ld, NULL, NULL );
605 LDAPControl **rctrls = NULL;
608 BerElementBuffer berbuf;
609 BerElement *ber = (BerElement *)&berbuf;
611 LDAPMessage *res = NULL;
612 LDAPMessage *msg = NULL;
615 struct berval *retdata = NULL;
620 struct berval syncUUID = BER_BVNULL;
621 struct sync_cookie syncCookie = { BER_BVNULL };
622 struct sync_cookie syncCookie_req = { BER_BVNULL };
623 struct berval cookie = BER_BVNULL;
631 Modifications *modlist = NULL;
636 struct timeval *tout_p = NULL;
637 struct timeval tout = { 0, 0 };
639 int refreshDeletes = 0;
641 BerVarray syncUUIDs = NULL;
644 if ( slapd_shutdown ) {
649 ber_init2( ber, NULL, LBER_USE_DER );
650 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
652 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
654 psub = &si->si_be->be_nsuffix[0];
656 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
658 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
664 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
665 tout_p, &res )) > 0 )
667 if ( slapd_shutdown ) {
671 for( msg = ldap_first_message( si->si_ld, res );
673 msg = ldap_next_message( si->si_ld, msg ) )
675 if ( slapd_shutdown ) {
679 switch( ldap_msgtype( msg ) ) {
680 case LDAP_RES_SEARCH_ENTRY:
681 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
682 /* we can't work without the control */
684 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
685 "got search entry without "
686 "control\n", 0, 0, 0 );
691 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
692 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
693 /* FIXME: what if syncUUID is NULL or empty? */
694 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
695 ber_scanf( ber, /*"{"*/ "m}", &cookie );
696 if ( !BER_BVISNULL( &cookie ) ) {
697 ch_free( syncCookie.octet_str.bv_val );
698 ber_dupbv( &syncCookie.octet_str, &cookie );
700 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
702 slap_parse_sync_cookie( &syncCookie, NULL );
705 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
708 if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
709 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
710 syncrepl_updateCookie( si, op, psub, &syncCookie );
712 } else if ( syncrepl_message_to_entry( si, op, msg,
713 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
714 rc_efree = syncrepl_entry( si, op, entry, &modlist,
715 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
716 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
718 syncrepl_updateCookie( si, op, psub, &syncCookie );
721 ldap_controls_free( rctrls );
723 slap_mods_free( modlist, 1 );
725 if ( rc_efree && entry ) {
731 case LDAP_RES_SEARCH_REFERENCE:
732 Debug( LDAP_DEBUG_ANY,
733 "do_syncrep2: reference received error\n", 0, 0, 0 );
736 case LDAP_RES_SEARCH_RESULT:
737 Debug( LDAP_DEBUG_SYNC,
738 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
739 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
741 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
742 if ( si->si_logstate == SYNCLOG_LOGGING ) {
743 si->si_logstate = SYNCLOG_FALLBACK;
750 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
752 ber_scanf( ber, "{" /*"}"*/);
753 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
754 ber_scanf( ber, "m", &cookie );
755 if ( !BER_BVISNULL( &cookie ) ) {
756 ch_free( syncCookie.octet_str.bv_val );
757 ber_dupbv( &syncCookie.octet_str, &cookie);
759 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
761 slap_parse_sync_cookie( &syncCookie, NULL );
764 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
766 ber_scanf( ber, "b", &refreshDeletes );
768 ber_scanf( ber, /*"{"*/ "}" );
770 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
772 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
775 value_match( &match, slap_schema.si_ad_entryCSN,
776 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
777 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
778 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
781 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
782 match < 0 && err == LDAP_SUCCESS )
784 syncrepl_updateCookie( si, op, psub, &syncCookie );
787 ldap_controls_free( rctrls );
789 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
790 /* FIXME : different error behaviors according to
791 * 1) err code : LDAP_BUSY ...
792 * 2) on err policy : stop service, stop sync, retry
794 if ( refreshDeletes == 0 && match < 0 &&
795 err == LDAP_SUCCESS )
797 syncrepl_del_nonpresent( op, si, NULL );
799 avl_free( si->si_presentlist, avl_ber_bvfree );
800 si->si_presentlist = NULL;
803 if ( err == LDAP_SUCCESS
804 && si->si_logstate == SYNCLOG_FALLBACK ) {
805 si->si_logstate = SYNCLOG_LOGGING;
806 rc = LDAP_SYNC_REFRESH_REQUIRED;
813 case LDAP_RES_INTERMEDIATE:
814 rc = ldap_parse_intermediate( si->si_ld, msg,
815 &retoid, &retdata, NULL, 0 );
816 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
817 ber_init2( ber, retdata, LBER_USE_DER );
819 switch ( si_tag = ber_peek_tag( ber, &len )) {
821 case LDAP_TAG_SYNC_NEW_COOKIE:
822 Debug( LDAP_DEBUG_SYNC,
823 "do_syncrep2: %s - %s%s\n",
824 "LDAP_RES_INTERMEDIATE",
825 "NEW_COOKIE", "\n" );
826 ber_scanf( ber, "tm", &tag, &cookie );
828 case LDAP_TAG_SYNC_REFRESH_DELETE:
829 case LDAP_TAG_SYNC_REFRESH_PRESENT:
830 Debug( LDAP_DEBUG_SYNC,
831 "do_syncrep2: %s - %s%s\n",
832 "LDAP_RES_INTERMEDIATE",
833 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
834 "REFRESH_PRESENT" : "REFRESH_DELETE",
836 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
837 si->si_refreshDelete = 1;
839 si->si_refreshPresent = 1;
841 ber_scanf( ber, "t{" /*"}"*/, &tag );
842 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
844 ber_scanf( ber, "m", &cookie );
845 if ( !BER_BVISNULL( &cookie ) ) {
846 ch_free( syncCookie.octet_str.bv_val );
847 ber_dupbv( &syncCookie.octet_str, &cookie );
849 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
851 slap_parse_sync_cookie( &syncCookie, NULL );
854 if ( ber_peek_tag( ber, &len ) ==
855 LDAP_TAG_REFRESHDONE )
857 ber_scanf( ber, "b", &refreshDone );
859 ber_scanf( ber, /*"{"*/ "}" );
861 case LDAP_TAG_SYNC_ID_SET:
862 Debug( LDAP_DEBUG_SYNC,
863 "do_syncrep2: %s - %s%s\n",
864 "LDAP_RES_INTERMEDIATE",
867 ber_scanf( ber, "t{" /*"}"*/, &tag );
868 if ( ber_peek_tag( ber, &len ) ==
869 LDAP_TAG_SYNC_COOKIE )
871 ber_scanf( ber, "m", &cookie );
872 if ( !BER_BVISNULL( &cookie ) ) {
873 ch_free( syncCookie.octet_str.bv_val );
874 ber_dupbv( &syncCookie.octet_str, &cookie );
876 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
878 slap_parse_sync_cookie( &syncCookie, NULL );
881 if ( ber_peek_tag( ber, &len ) ==
882 LDAP_TAG_REFRESHDELETES )
884 ber_scanf( ber, "b", &refreshDeletes );
886 ber_scanf( ber, "[W]", &syncUUIDs );
887 ber_scanf( ber, /*"{"*/ "}" );
888 if ( refreshDeletes ) {
889 syncrepl_del_nonpresent( op, si, syncUUIDs );
890 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
892 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
893 struct berval *syncuuid_bv;
894 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
895 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
896 avl_insert( &si->si_presentlist,
897 (caddr_t) syncuuid_bv,
898 syncuuid_cmp, avl_dup_error );
900 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
904 Debug( LDAP_DEBUG_ANY,
905 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
906 (long) si_tag, 0, 0 );
907 ldap_memfree( retoid );
908 ber_bvfree( retdata );
912 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
914 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
917 value_match( &match, slap_schema.si_ad_entryCSN,
918 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
919 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
920 &syncCookie_req.ctxcsn,
921 &syncCookie.ctxcsn, &text );
924 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
927 syncrepl_updateCookie( si, op, psub, &syncCookie);
930 if ( si->si_refreshPresent == 1 ) {
932 syncrepl_del_nonpresent( op, si, NULL );
936 ldap_memfree( retoid );
937 ber_bvfree( retdata );
941 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
942 "unknown intermediate response (%d)\n",
944 ldap_memfree( retoid );
945 ber_bvfree( retdata );
951 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
952 "unknown message\n", 0, 0, 0 );
956 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
957 slap_sync_cookie_free( &syncCookie_req, 0 );
958 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
959 slap_sync_cookie_free( &syncCookie, 0 );
969 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
970 errstr = ldap_err2string( rc );
972 Debug( LDAP_DEBUG_ANY,
973 "do_syncrep2 : %s\n", errstr, 0, 0 );
977 slap_sync_cookie_free( &syncCookie, 0 );
978 slap_sync_cookie_free( &syncCookie_req, 0 );
980 if ( res ) ldap_msgfree( res );
982 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
984 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
985 connection_client_stop( s );
986 ldap_unbind_ext( si->si_ld, NULL, NULL );
998 struct re_s* rtask = arg;
999 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1000 Connection conn = {0};
1001 OperationBuffer opbuf;
1003 int rc = LDAP_SUCCESS;
1010 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1015 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1017 switch( abs( si->si_type )) {
1018 case LDAP_SYNC_REFRESH_ONLY:
1019 case LDAP_SYNC_REFRESH_AND_PERSIST:
1022 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1026 if ( slapd_shutdown ) {
1028 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1029 connection_client_stop( s );
1030 ldap_unbind_ext( si->si_ld, NULL, NULL );
1033 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1037 op = (Operation *) &opbuf;
1038 connection_fake_init( &conn, op, ctx );
1040 /* use global malloc for now */
1041 op->o_tmpmemctx = NULL;
1042 op->o_tmpmfuncs = &ch_mfuncs;
1044 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1045 op->o_bd = be = si->si_be;
1046 op->o_dn = op->o_bd->be_rootdn;
1047 op->o_ndn = op->o_bd->be_rootndn;
1049 /* Establish session, do search */
1052 si->si_refreshDelete = 0;
1053 si->si_refreshPresent = 0;
1054 rc = do_syncrep1( op, si );
1058 /* Process results */
1059 if ( rc == LDAP_SUCCESS ) {
1060 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1062 rc = do_syncrep2( op, si );
1063 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1064 rc = ldap_sync_search( si, op->o_tmpmemctx );
1068 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1069 /* If we succeeded, enable the connection for further listening.
1070 * If we failed, tear down the connection and reschedule.
1072 if ( rc == LDAP_SUCCESS ) {
1074 rc = connection_client_setup( s, do_syncrepl, arg );
1076 connection_client_enable( s );
1078 } else if ( !first ) {
1082 if ( rc == -2 ) rc = 0;
1086 /* At this point, we have 4 cases:
1087 * 1) for any hard failure, give up and remove this task
1088 * 2) for ServerDown, reschedule this task to run
1089 * 3) for Refresh and Success, reschedule to run
1090 * 4) for Persist and Success, reschedule to defer
1092 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1094 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1095 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1099 connection_client_stop( s );
1102 if ( rc == LDAP_SUCCESS ) {
1103 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1106 rtask->interval.tv_sec = si->si_interval;
1107 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1108 if ( si->si_retrynum ) {
1109 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
1110 si->si_retrynum[i] = si->si_retrynum_init[i];
1112 si->si_retrynum[i] = -2;
1115 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1116 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
1120 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1121 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1122 } else if ( si->si_retrynum[i] >= -1 ) {
1123 if ( si->si_retrynum[i] > 0 )
1124 si->si_retrynum[i]--;
1125 rtask->interval.tv_sec = si->si_retryinterval[i];
1126 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1127 slap_wake_listener();
1131 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1132 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1137 static slap_verbmasks modops[] = {
1138 { BER_BVC("add"), LDAP_REQ_ADD },
1139 { BER_BVC("delete"), LDAP_REQ_DELETE },
1140 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1141 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1145 static Modifications *
1146 syncrepl_accesslog_mods(
1153 AttributeDescription *ad;
1154 struct berval bv, bv2;
1156 Modifications *mod = NULL, *modlist = NULL, **modtail;
1161 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1165 colon = strchr( bv.bv_val, ':' );
1167 continue; /* invalid */
1168 bv.bv_len = colon - bv.bv_val;
1169 if ( slap_bv2ad( &bv, &ad, &text )) {
1173 /* Ignore dynamically generated attrs */
1174 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1176 /* Ignore excluded attrs */
1177 if ( ldap_charray_inlist( si->si_exattrs,
1178 ad->ad_type->sat_cname.bv_val ))
1182 case '+': op = LDAP_MOD_ADD; break;
1183 case '-': op = LDAP_MOD_DELETE; break;
1184 case '=': op = LDAP_MOD_REPLACE; break;
1185 case '#': op = LDAP_MOD_INCREMENT; break;
1189 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1190 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1193 mod->sml_next = NULL;
1195 mod->sml_type = ad->ad_cname;
1196 mod->sml_values = NULL;
1197 mod->sml_nvalues = NULL;
1200 modtail = &mod->sml_next;
1202 if ( colon[2] == ' ' ) {
1203 bv.bv_val = colon + 3;
1204 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1205 ber_dupbv( &bv2, &bv );
1206 ber_bvarray_add( &mod->sml_values, &bv2 );
1212 static Modifications *
1213 syncrepl_changelog_mods(
1218 return NULL; /* FIXME */
1222 syncrepl_message_to_op(
1228 BerElement *ber = NULL;
1229 Modifications *modlist = NULL;
1231 SlapReply rs = { REP_RESULT };
1232 slap_callback cb = { NULL, null_callback, NULL, NULL };
1235 char txtbuf[SLAP_TEXT_BUFLEN];
1236 size_t textlen = sizeof txtbuf;
1238 struct berval bdn, dn = BER_BVNULL, ndn;
1239 struct berval bv, *bvals = NULL;
1240 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1241 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1242 psup = BER_BVNULL, nsup = BER_BVNULL;
1243 int rc, deleteOldRdn = 0;
1245 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1246 Debug( LDAP_DEBUG_ANY,
1247 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1251 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1256 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1258 if ( rc != LDAP_SUCCESS ) {
1259 Debug( LDAP_DEBUG_ANY,
1260 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1264 op->o_tag = LBER_DEFAULT;
1266 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1268 if ( bv.bv_val == NULL )
1271 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1273 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1274 ber_dupbv( &op->o_req_dn, &dn );
1275 ber_dupbv( &op->o_req_ndn, &ndn );
1276 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1277 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1278 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1279 int i = verb_to_mask( bvals[0].bv_val, modops );
1281 Debug( LDAP_DEBUG_ANY,
1282 "syncrepl_message_to_op : unknown op %s",
1283 bvals[0].bv_val, 0, 0 );
1288 op->o_tag = modops[i].mask;
1289 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1290 /* Parse attribute into modlist */
1291 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1292 modlist = syncrepl_accesslog_mods( si, bvals );
1294 modlist = syncrepl_changelog_mods( si, bvals );
1295 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1297 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1298 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1300 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1302 } else if ( !ber_bvstrcasecmp( &bv,
1303 &slap_schema.si_ad_entryCSN->ad_cname )) {
1304 slap_queue_csn( op, bvals );
1309 /* If we didn't get a mod type or a target DN, bail out */
1310 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1315 op->o_callback = &cb;
1317 switch( op->o_tag ) {
1319 case LDAP_REQ_MODIFY:
1320 /* If we didn't get required data, bail */
1321 if ( !modlist ) goto done;
1323 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1325 if ( rc != LDAP_SUCCESS ) {
1326 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1331 if ( op->o_tag == LDAP_REQ_ADD ) {
1332 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1333 op->ora_e->e_name = op->o_req_dn;
1334 op->ora_e->e_nname = op->o_req_ndn;
1335 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1336 if( rc != LDAP_SUCCESS ) {
1337 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1340 rc = op->o_bd->be_add( op, &rs );
1342 be_entry_release_w( op, op->ora_e );
1344 op->orm_modlist = modlist;
1345 rc = op->o_bd->be_modify( op, &rs );
1348 case LDAP_REQ_MODRDN:
1349 if ( BER_BVISNULL( &rdn )) goto done;
1351 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1353 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1355 if ( !BER_BVISNULL( &sup )) {
1356 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1358 op->orr_newSup = &psup;
1359 op->orr_nnewSup = ⊅
1361 op->orr_newrdn = prdn;
1362 op->orr_nnewrdn = nrdn;
1363 op->orr_deleteoldrdn = deleteOldRdn;
1364 rc = op->o_bd->be_modrdn( op, &rs );
1366 case LDAP_REQ_DELETE:
1367 rc = op->o_bd->be_delete( op, &rs );
1371 slap_graduate_commit_csn( op );
1373 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1374 if ( !BER_BVISNULL( &rdn )) {
1375 if ( !BER_BVISNULL( &nsup ))
1376 ch_free( nsup.bv_val );
1377 if ( !BER_BVISNULL( &psup ))
1378 ch_free( psup.bv_val );
1379 if ( !BER_BVISNULL( &nrdn ))
1380 ch_free( nrdn.bv_val );
1381 if ( !BER_BVISNULL( &prdn ))
1382 ch_free( prdn.bv_val );
1384 ber_free ( ber, 0 );
1389 syncrepl_message_to_entry(
1393 Modifications **modlist,
1399 BerElement *ber = NULL;
1402 Modifications **modtail = modlist;
1405 char txtbuf[SLAP_TEXT_BUFLEN];
1406 size_t textlen = sizeof txtbuf;
1408 struct berval bdn = {0, NULL}, dn, ndn;
1413 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1414 Debug( LDAP_DEBUG_ANY,
1415 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1419 op->o_tag = LDAP_REQ_ADD;
1421 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1423 if ( rc != LDAP_SUCCESS ) {
1424 Debug( LDAP_DEBUG_ANY,
1425 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1429 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1430 ber_dupbv( &op->o_req_dn, &dn );
1431 ber_dupbv( &op->o_req_ndn, &ndn );
1432 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1433 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1435 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1438 return LDAP_SUCCESS;
1441 if ( entry == NULL ) {
1445 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1447 e->e_name = op->o_req_dn;
1448 e->e_nname = op->o_req_ndn;
1450 while ( ber_remaining( ber ) ) {
1451 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1452 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1457 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1459 mod->sml_op = LDAP_MOD_REPLACE;
1461 mod->sml_next = NULL;
1462 mod->sml_desc = NULL;
1463 mod->sml_type = tmp.sml_type;
1464 mod->sml_values = tmp.sml_values;
1465 mod->sml_nvalues = NULL;
1468 modtail = &mod->sml_next;
1471 if ( *modlist == NULL ) {
1472 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1478 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1480 if ( rc != LDAP_SUCCESS ) {
1481 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1486 /* Strip out dynamically generated attrs */
1487 for ( modtail = modlist; *modtail ; ) {
1489 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1490 *modtail = mod->sml_next;
1491 slap_mod_free( &mod->sml_mod, 0 );
1494 modtail = &mod->sml_next;
1498 /* Strip out attrs in exattrs list */
1499 for ( modtail = modlist; *modtail ; ) {
1501 if ( ldap_charray_inlist( si->si_exattrs,
1502 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1503 *modtail = mod->sml_next;
1504 slap_mod_free( &mod->sml_mod, 0 );
1507 modtail = &mod->sml_next;
1511 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1512 if( rc != LDAP_SUCCESS ) {
1513 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1518 ber_free ( ber, 0 );
1519 if ( rc != LDAP_SUCCESS ) {
1529 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1531 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1532 * entry if a previous refresh was interrupted before sending us a new
1533 * context state. We try to compare the new entry to the existing entry
1534 * and ignore the new entry if they are the same.
1536 * Also, we may get an update where the entryDN has changed, due to
1537 * a ModDn on the provider. We detect this as well, so we can issue
1538 * the corresponding operation locally.
1540 * In the case of a modify, we get a list of all the attributes
1541 * in the original entry. Rather than deleting the entry and re-adding it,
1542 * we issue a Modify request that deletes all the attributes and adds all
1543 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1546 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1547 * a ModDN we will issue both operations on the local database.
1549 typedef struct dninfo {
1553 int renamed; /* Was an existing entry renamed? */
1554 int wasChanged; /* are the attributes changed? */
1555 int attrs; /* how many attribute types are in the ads list */
1556 AttributeDescription **ads;
1564 Modifications** modlist,
1566 struct berval* syncUUID,
1567 struct sync_cookie* syncCookie_req,
1568 struct berval* syncCSN )
1570 Backend *be = op->o_bd;
1571 slap_callback cb = { NULL, NULL, NULL, NULL };
1572 struct berval *syncuuid_bv = NULL;
1573 struct berval syncUUID_strrep = BER_BVNULL;
1574 struct berval uuid_bv = BER_BVNULL;
1576 SlapReply rs_search = {REP_RESULT};
1577 SlapReply rs_delete = {REP_RESULT};
1578 SlapReply rs_add = {REP_RESULT};
1579 SlapReply rs_modify = {REP_RESULT};
1581 #ifdef LDAP_COMP_MATCH
1582 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1584 AttributeAssertion ava = { NULL, BER_BVNULL };
1586 int rc = LDAP_SUCCESS;
1587 int ret = LDAP_SUCCESS;
1589 struct berval pdn = BER_BVNULL;
1593 switch( syncstate ) {
1594 case LDAP_SYNC_PRESENT:
1595 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1597 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1600 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1602 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1604 case LDAP_SYNC_DELETE:
1605 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1607 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1609 case LDAP_SYNC_MODIFY:
1610 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1612 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1615 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1617 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1620 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1621 if ( !si->si_refreshPresent ) {
1622 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1623 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1624 syncuuid_cmp, avl_dup_error );
1628 if ( syncstate == LDAP_SYNC_PRESENT ) {
1630 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1631 if ( entry == NULL ) {
1636 f.f_choice = LDAP_FILTER_EQUALITY;
1638 ava.aa_desc = slap_schema.si_ad_entryUUID;
1639 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1640 ava.aa_value = *syncUUID;
1641 op->ors_filter = &f;
1643 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1644 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1645 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1646 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1647 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1648 syncUUID->bv_val, syncUUID->bv_len );
1649 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1650 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1652 op->o_tag = LDAP_REQ_SEARCH;
1653 op->ors_scope = LDAP_SCOPE_SUBTREE;
1654 op->ors_deref = LDAP_DEREF_NEVER;
1656 /* get the entry for this UUID */
1657 op->o_req_dn = si->si_base;
1658 op->o_req_ndn = si->si_base;
1660 op->o_time = slap_get_time();
1661 op->ors_tlimit = SLAP_NO_LIMIT;
1664 op->ors_attrs = slap_anlist_all_attributes;
1665 op->ors_attrsonly = 0;
1667 /* set callback function */
1668 op->o_callback = &cb;
1669 cb.sc_response = dn_callback;
1670 cb.sc_private = &dni;
1671 dni.new_entry = entry;
1673 if ( limits_check( op, &rs_search ) == 0 ) {
1674 rc = be->be_search( op, &rs_search );
1675 Debug( LDAP_DEBUG_SYNC,
1676 "syncrepl_entry: %s (%d)\n",
1677 "be_search", rc, 0 );
1680 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1681 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1684 cb.sc_response = null_callback;
1687 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1688 Debug( LDAP_DEBUG_SYNC,
1689 "syncrepl_entry: %s\n",
1690 entry->e_name.bv_val, 0, 0 );
1692 Debug( LDAP_DEBUG_SYNC,
1693 "syncrepl_entry: %s\n",
1694 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1697 if ( syncstate != LDAP_SYNC_DELETE ) {
1698 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1701 /* add if missing */
1702 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1703 &syncUUID_strrep, syncUUID );
1705 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1706 /* replace only if necessary */
1707 if ( a->a_nvals != a->a_vals ) {
1708 ber_memfree( a->a_nvals[0].bv_val );
1709 ber_dupbv( &a->a_nvals[0], syncUUID );
1711 ber_memfree( a->a_vals[0].bv_val );
1712 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1716 switch ( syncstate ) {
1718 case LDAP_SYNC_MODIFY:
1720 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1722 op->o_csn = a->a_vals[0];
1725 if ( BER_BVISNULL( &dni.dn )) {
1727 op->o_req_dn = entry->e_name;
1728 op->o_req_ndn = entry->e_nname;
1729 op->o_tag = LDAP_REQ_ADD;
1732 rc = be->be_add( op, &rs_add );
1733 Debug( LDAP_DEBUG_SYNC,
1734 "syncrepl_entry: %s (%d)\n",
1736 switch ( rs_add.sr_err ) {
1738 be_entry_release_w( op, entry );
1743 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1744 * only if the suffix entry is not present */
1745 case LDAP_NO_SUCH_OBJECT:
1746 syncrepl_add_glue( op, entry );
1750 /* if an entry was added via syncrepl_add_glue(),
1751 * it likely has no entryUUID, so the previous
1752 * be_search() doesn't find it. In this case,
1753 * give syncrepl a chance to modify it. Also
1754 * allow for entries that were recreated with the
1755 * same DN but a different entryUUID.
1757 case LDAP_ALREADY_EXISTS:
1759 Operation op2 = *op;
1760 SlapReply rs2 = { 0 };
1761 slap_callback cb2 = { 0 };
1763 op2.o_tag = LDAP_REQ_SEARCH;
1764 op2.o_req_dn = entry->e_name;
1765 op2.o_req_ndn = entry->e_nname;
1766 op2.ors_scope = LDAP_SCOPE_BASE;
1767 op2.ors_deref = LDAP_DEREF_NEVER;
1768 op2.ors_attrs = slap_anlist_all_attributes;
1769 op2.ors_attrsonly = 0;
1770 op2.ors_limit = NULL;
1772 op2.ors_tlimit = SLAP_NO_LIMIT;
1774 f.f_choice = LDAP_FILTER_PRESENT;
1775 f.f_desc = slap_schema.si_ad_objectClass;
1776 op2.ors_filter = &f;
1777 op2.ors_filterstr = generic_filterstr;
1779 op2.o_callback = &cb2;
1780 cb2.sc_response = dn_callback;
1781 cb2.sc_private = &dni;
1783 be->be_search( &op2, &rs2 );
1791 Debug( LDAP_DEBUG_ANY,
1792 "syncrepl_entry : be_add failed (%d)\n",
1793 rs_add.sr_err, 0, 0 );
1800 op->o_req_dn = dni.dn;
1801 op->o_req_ndn = dni.ndn;
1802 if ( dni.renamed ) {
1803 struct berval noldp, newp, nnewp;
1805 op->o_tag = LDAP_REQ_MODRDN;
1806 dnRdn( &entry->e_name, &op->orr_newrdn );
1807 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1809 dnParent( &dni.ndn, &noldp );
1810 dnParent( &entry->e_nname, &nnewp );
1811 if ( !dn_match( &noldp, &newp )) {
1812 dnParent( &entry->e_name, &newp );
1813 op->orr_newSup = &newp;
1814 op->orr_nnewSup = &nnewp;
1816 op->orr_deleteoldrdn = 0;
1817 rc = be->be_modrdn( op, &rs_modify );
1818 Debug( LDAP_DEBUG_SYNC,
1819 "syncrepl_entry: %s (%d)\n",
1820 "be_modrdn", rc, 0 );
1821 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1822 op->o_req_dn = entry->e_name;
1823 op->o_req_ndn = entry->e_nname;
1829 if ( dni.wasChanged ) {
1830 Modifications *mod, *modhead = NULL;
1831 Modifications *modtail = NULL;
1834 op->o_tag = LDAP_REQ_MODIFY;
1836 assert( *modlist != NULL );
1838 /* Delete all the old attrs */
1839 for ( i = 0; i < dni.attrs; i++ ) {
1840 mod = ch_malloc( sizeof( Modifications ) );
1841 mod->sml_op = LDAP_MOD_DELETE;
1843 mod->sml_desc = dni.ads[i];
1844 mod->sml_type = mod->sml_desc->ad_cname;
1845 mod->sml_values = NULL;
1846 mod->sml_nvalues = NULL;
1847 if ( !modhead ) modhead = mod;
1849 modtail->sml_next = mod;
1854 /* Append passed in list to ours */
1856 modtail->sml_next = *modlist;
1862 /* Find end of this list */
1863 for ( ; mod != NULL; mod = mod->sml_next ) {
1867 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1868 mod->sml_op = LDAP_MOD_REPLACE;
1870 mod->sml_desc = slap_schema.si_ad_entryUUID;
1871 mod->sml_type = mod->sml_desc->ad_cname;
1872 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1873 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1874 ber_dupbv( &uuid_bv, syncUUID );
1875 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1876 modtail->sml_next = mod;
1878 op->o_tag = LDAP_REQ_MODIFY;
1879 op->orm_modlist = *modlist;
1881 rc = be->be_modify( op, &rs_modify );
1882 Debug( LDAP_DEBUG_SYNC,
1883 "syncrepl_entry: %s (%d)\n",
1884 "be_modify", rc, 0 );
1885 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1886 Debug( LDAP_DEBUG_ANY,
1887 "syncrepl_entry : be_modify failed (%d)\n",
1888 rs_modify.sr_err, 0, 0 );
1893 case LDAP_SYNC_DELETE :
1894 if ( !BER_BVISNULL( &dni.dn )) {
1895 op->o_req_dn = dni.dn;
1896 op->o_req_ndn = dni.ndn;
1897 op->o_tag = LDAP_REQ_DELETE;
1898 rc = be->be_delete( op, &rs_delete );
1899 Debug( LDAP_DEBUG_SYNC,
1900 "syncrepl_entry: %s (%d)\n",
1901 "be_delete", rc, 0 );
1903 while ( rs_delete.sr_err == LDAP_SUCCESS
1904 && op->o_delete_glue_parent ) {
1905 op->o_delete_glue_parent = 0;
1906 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1907 slap_callback cb = { NULL };
1908 cb.sc_response = slap_null_cb;
1909 dnParent( &op->o_req_ndn, &pdn );
1911 op->o_req_ndn = pdn;
1912 op->o_callback = &cb;
1913 op->o_bd->be_delete( op, &rs_delete );
1923 Debug( LDAP_DEBUG_ANY,
1924 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1930 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1931 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1932 BER_BVZERO( &syncUUID_strrep );
1935 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1937 if ( !BER_BVISNULL( &dni.ndn ) ) {
1938 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1940 if ( !BER_BVISNULL( &dni.dn ) ) {
1941 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1943 BER_BVZERO( &op->o_csn );
1947 static struct berval gcbva[] = {
1953 #define NP_DELETE_ONE 2
1956 syncrepl_del_nonpresent(
1961 Backend* be = op->o_bd;
1962 slap_callback cb = { NULL };
1963 SlapReply rs_search = {REP_RESULT};
1964 SlapReply rs_delete = {REP_RESULT};
1965 SlapReply rs_modify = {REP_RESULT};
1966 struct nonpresent_entry *np_list, *np_prev;
1968 AttributeName an[2];
1970 struct berval pdn = BER_BVNULL;
1972 op->o_req_dn = si->si_base;
1973 op->o_req_ndn = si->si_base;
1975 cb.sc_response = nonpresent_callback;
1978 op->o_callback = &cb;
1979 op->o_tag = LDAP_REQ_SEARCH;
1980 op->ors_scope = si->si_scope;
1981 op->ors_deref = LDAP_DEREF_NEVER;
1982 op->o_time = slap_get_time();
1983 op->ors_tlimit = SLAP_NO_LIMIT;
1988 #ifdef LDAP_COMP_MATCH
1989 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1991 AttributeAssertion eq = { NULL, BER_BVNULL };
1995 op->ors_attrsonly = 1;
1996 op->ors_attrs = slap_anlist_no_attrs;
1997 op->ors_limit = NULL;
1998 op->ors_filter = &uf;
2001 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2003 uf.f_choice = LDAP_FILTER_EQUALITY;
2004 si->si_refreshDelete |= NP_DELETE_ONE;
2006 for (i=0; uuids[i].bv_val; i++) {
2008 uf.f_av_value = uuids[i];
2009 rc = be->be_search( op, &rs_search );
2011 si->si_refreshDelete ^= NP_DELETE_ONE;
2013 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2014 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2015 an[0].an_desc = slap_schema.si_ad_entryUUID;
2017 op->ors_slimit = SLAP_NO_LIMIT;
2018 op->ors_attrsonly = 0;
2019 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2020 op->ors_filterstr = si->si_filterstr;
2021 op->o_nocaching = 1;
2023 if ( limits_check( op, &rs_search ) == 0 ) {
2024 rc = be->be_search( op, &rs_search );
2026 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2029 op->o_nocaching = 0;
2031 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2033 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2035 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2036 while ( np_list != NULL ) {
2037 LDAP_LIST_REMOVE( np_list, npe_link );
2039 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2040 op->o_tag = LDAP_REQ_DELETE;
2041 op->o_callback = &cb;
2042 cb.sc_response = null_callback;
2044 op->o_req_dn = *np_prev->npe_name;
2045 op->o_req_ndn = *np_prev->npe_nname;
2046 rc = op->o_bd->be_delete( op, &rs_delete );
2048 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2049 Modifications mod1, mod2;
2050 mod1.sml_op = LDAP_MOD_REPLACE;
2052 mod1.sml_desc = slap_schema.si_ad_objectClass;
2053 mod1.sml_type = mod1.sml_desc->ad_cname;
2054 mod1.sml_values = &gcbva[0];
2055 mod1.sml_nvalues = NULL;
2056 mod1.sml_next = &mod2;
2058 mod2.sml_op = LDAP_MOD_REPLACE;
2060 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2061 mod2.sml_type = mod2.sml_desc->ad_cname;
2062 mod2.sml_values = &gcbva[1];
2063 mod2.sml_nvalues = NULL;
2064 mod2.sml_next = NULL;
2066 op->o_tag = LDAP_REQ_MODIFY;
2067 op->orm_modlist = &mod1;
2069 rc = be->be_modify( op, &rs_modify );
2072 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2073 op->o_delete_glue_parent ) {
2074 op->o_delete_glue_parent = 0;
2075 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2076 slap_callback cb = { NULL };
2077 cb.sc_response = slap_null_cb;
2078 dnParent( &op->o_req_ndn, &pdn );
2080 op->o_req_ndn = pdn;
2081 op->o_callback = &cb;
2082 /* give it a root privil ? */
2083 op->o_bd->be_delete( op, &rs_delete );
2089 op->o_delete_glue_parent = 0;
2091 ber_bvfree( np_prev->npe_name );
2092 ber_bvfree( np_prev->npe_nname );
2096 slap_graduate_commit_csn( op );
2107 Backend *be = op->o_bd;
2108 slap_callback cb = { NULL };
2113 struct berval dn = {0, NULL};
2114 struct berval ndn = {0, NULL};
2116 SlapReply rs_add = {REP_RESULT};
2119 op->o_tag = LDAP_REQ_ADD;
2120 op->o_callback = &cb;
2121 cb.sc_response = null_callback;
2122 cb.sc_private = NULL;
2127 /* count RDNs in suffix */
2128 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2129 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2139 /* Start with BE suffix */
2140 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2141 comma = strrchr( dn.bv_val, ',' );
2142 if ( ptr ) *ptr = ',';
2143 if ( comma ) *comma = '\0';
2148 dn.bv_len -= ptr - dn.bv_val;
2151 /* the normalizedDNs are always the same length, no counting
2154 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2155 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2156 ndn.bv_len = be->be_nsuffix[0].bv_len;
2159 while ( ndn.bv_val > e->e_nname.bv_val ) {
2160 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2161 ber_dupbv( &glue->e_name, &dn );
2162 ber_dupbv( &glue->e_nname, &ndn );
2164 a = ch_calloc( 1, sizeof( Attribute ));
2165 a->a_desc = slap_schema.si_ad_objectClass;
2167 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2168 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2169 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2170 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2172 a->a_nvals = a->a_vals;
2174 a->a_next = glue->e_attrs;
2177 a = ch_calloc( 1, sizeof( Attribute ));
2178 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2180 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2181 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2182 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2184 a->a_nvals = a->a_vals;
2186 a->a_next = glue->e_attrs;
2189 op->o_req_dn = glue->e_name;
2190 op->o_req_ndn = glue->e_nname;
2192 rc = be->be_add ( op, &rs_add );
2193 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2194 be_entry_release_w( op, glue );
2196 /* incl. ALREADY EXIST */
2200 /* Move to next child */
2201 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2204 if ( ptr == e->e_name.bv_val ) break;
2206 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2207 for( ptr = ndn.bv_val-2;
2208 ptr > e->e_nname.bv_val && *ptr != ',';
2214 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2217 op->o_req_dn = e->e_name;
2218 op->o_req_ndn = e->e_nname;
2220 rc = be->be_add ( op, &rs_add );
2221 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2222 be_entry_release_w( op, e );
2231 syncrepl_updateCookie(
2235 struct sync_cookie *syncCookie )
2237 Backend *be = op->o_bd;
2238 Modifications mod = { { 0 } };
2239 struct berval vals[ 2 ];
2243 slap_callback cb = { NULL };
2244 SlapReply rs_modify = {REP_RESULT};
2246 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2247 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2249 mod.sml_op = LDAP_MOD_REPLACE;
2250 mod.sml_desc = slap_schema.si_ad_contextCSN;
2251 mod.sml_type = mod.sml_desc->ad_cname;
2252 mod.sml_values = vals;
2253 vals[0] = si->si_syncCookie.ctxcsn;
2254 vals[1].bv_val = NULL;
2257 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2259 op->o_tag = LDAP_REQ_MODIFY;
2261 assert( si->si_rid < 1000 );
2263 cb.sc_response = null_callback;
2266 op->o_callback = &cb;
2267 op->o_req_dn = op->o_bd->be_suffix[0];
2268 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2270 /* update contextCSN */
2271 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2272 op->orm_modlist = &mod;
2273 rc = be->be_modify( op, &rs_modify );
2276 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2277 Debug( LDAP_DEBUG_ANY,
2278 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2281 slap_graduate_commit_csn( op );
2291 dninfo *dni = op->o_callback->sc_private;
2293 if ( rs->sr_type == REP_SEARCH ) {
2294 if ( !BER_BVISNULL( &dni->dn ) ) {
2295 Debug( LDAP_DEBUG_ANY,
2296 "dn_callback : consistency error - "
2297 "entryUUID is not unique\n", 0, 0, 0 );
2299 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2300 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2301 /* If there is a new entry, see if it differs from the old.
2302 * We compare the non-normalized values so that cosmetic changes
2303 * in the provider are always propagated.
2305 if ( dni->new_entry ) {
2306 Attribute *old, *new;
2309 /* Did the DN change? Note that we don't explicitly try to
2310 * discover if the deleteOldRdn argument applies here. It
2311 * would save an unnecessary Modify if we detected it, but
2312 * that's a fair amount of trouble to compare the two attr
2313 * lists in detail. (Just test normalized DN; we ignore
2314 * insignificant changes here.)
2316 if ( !dn_match( &rs->sr_entry->e_nname,
2317 &dni->new_entry->e_nname ) )
2322 for ( i = 0, old = rs->sr_entry->e_attrs;
2324 i++, old = old->a_next )
2329 /* We assume that attributes are saved in the same order
2330 * in the remote and local databases. So if we walk through
2331 * the attributeDescriptions one by one they should match in
2332 * lock step. If not, we signal a change. Otherwise we test
2335 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2337 old = old->a_next, new = new->a_next )
2339 if ( old->a_desc != new->a_desc ) {
2340 dni->wasChanged = 1;
2343 for ( i = 0; ; i++ ) {
2345 nold = BER_BVISNULL( &old->a_vals[i] );
2346 nnew = BER_BVISNULL( &new->a_vals[i] );
2347 /* If both are empty, stop looking */
2348 if ( nold && nnew ) {
2351 /* If they are different, stop looking */
2352 if ( nold != nnew ) {
2353 dni->wasChanged = 1;
2356 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2357 dni->wasChanged = 1;
2361 if ( dni->wasChanged ) break;
2363 if ( dni->wasChanged ) {
2364 dni->ads = op->o_tmpalloc( dni->attrs *
2365 sizeof(AttributeDescription *), op->o_tmpmemctx );
2367 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2368 dni->ads[i] = old->a_desc;
2374 } else if ( rs->sr_type == REP_RESULT ) {
2375 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2376 Debug( LDAP_DEBUG_ANY,
2377 "dn_callback : consistency error - "
2378 "entryUUID is not unique\n", 0, 0, 0 );
2382 return LDAP_SUCCESS;
2386 nonpresent_callback(
2390 syncinfo_t *si = op->o_callback->sc_private;
2393 struct berval* present_uuid = NULL;
2394 struct nonpresent_entry *np_entry;
2396 if ( rs->sr_type == REP_RESULT ) {
2397 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2398 si->si_presentlist = NULL;
2400 } else if ( rs->sr_type == REP_SEARCH ) {
2401 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2402 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2404 if ( a == NULL ) return 0;
2406 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2410 if ( present_uuid == NULL ) {
2411 np_entry = (struct nonpresent_entry *)
2412 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2413 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2414 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2415 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2418 avl_delete( &si->si_presentlist,
2419 &a->a_nvals[0], syncuuid_cmp );
2420 ch_free( present_uuid->bv_val );
2421 ch_free( present_uuid );
2424 return LDAP_SUCCESS;
2432 if ( rs->sr_err != LDAP_SUCCESS &&
2433 rs->sr_err != LDAP_REFERRAL &&
2434 rs->sr_err != LDAP_ALREADY_EXISTS &&
2435 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2436 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2438 Debug( LDAP_DEBUG_ANY,
2439 "null_callback : error code 0x%x\n",
2442 return LDAP_SUCCESS;
2445 static struct berval *
2446 slap_uuidstr_from_normalized(
2447 struct berval* uuidstr,
2448 struct berval* normalized,
2452 unsigned char nibble;
2455 if ( normalized == NULL ) return NULL;
2456 if ( normalized->bv_len != 16 ) return NULL;
2461 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2462 if ( new == NULL ) {
2469 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2470 if ( new != uuidstr ) {
2471 slap_sl_free( new, ctx );
2476 for ( i = 0; i < 16; i++ ) {
2477 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2478 new->bv_val[(i<<1)+d] = '-';
2482 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2483 if ( nibble < 10 ) {
2484 new->bv_val[(i<<1)+d] = nibble + '0';
2486 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2489 nibble = (normalized->bv_val[i]) & 0xF;
2490 if ( nibble < 10 ) {
2491 new->bv_val[(i<<1)+d+1] = nibble + '0';
2493 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2497 new->bv_val[new->bv_len] = '\0';
2502 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2504 const struct berval *uuid1 = v_uuid1;
2505 const struct berval *uuid2 = v_uuid2;
2506 int rc = uuid1->bv_len - uuid2->bv_len;
2507 if ( rc ) return rc;
2508 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2512 avl_ber_bvfree( void *v_bv )
2514 struct berval *bv = (struct berval *)v_bv;
2516 if( v_bv == NULL ) return;
2517 if ( !BER_BVISNULL( bv ) ) {
2518 ch_free( bv->bv_val );
2520 ch_free( (char *) bv );
2524 syncinfo_free( syncinfo_t *sie )
2526 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2527 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2528 ch_free( sie->si_provideruri.bv_val );
2531 bindconf_free( &sie->si_bindconf );
2533 if ( sie->si_filterstr.bv_val ) {
2534 ch_free( sie->si_filterstr.bv_val );
2536 if ( sie->si_base.bv_val ) {
2537 ch_free( sie->si_base.bv_val );
2539 if ( sie->si_attrs ) {
2541 while ( sie->si_attrs[i] != NULL ) {
2542 ch_free( sie->si_attrs[i] );
2545 ch_free( sie->si_attrs );
2547 if ( sie->si_exattrs ) {
2549 while ( sie->si_exattrs[i] != NULL ) {
2550 ch_free( sie->si_exattrs[i] );
2553 ch_free( sie->si_exattrs );
2555 if ( sie->si_anlist ) {
2557 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2558 ch_free( sie->si_anlist[i].an_name.bv_val );
2561 ch_free( sie->si_anlist );
2563 if ( sie->si_exanlist ) {
2565 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2566 ch_free( sie->si_exanlist[i].an_name.bv_val );
2569 ch_free( sie->si_exanlist );
2571 if ( sie->si_retryinterval ) {
2572 ch_free( sie->si_retryinterval );
2574 if ( sie->si_retrynum ) {
2575 ch_free( sie->si_retrynum );
2577 if ( sie->si_retrynum_init ) {
2578 ch_free( sie->si_retrynum_init );
2580 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2581 if ( sie->si_presentlist ) {
2582 avl_free( sie->si_presentlist, avl_ber_bvfree );
2585 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2587 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2588 struct nonpresent_entry* npe;
2589 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2590 LDAP_LIST_REMOVE( npe, npe_link );
2591 if ( npe->npe_name ) {
2592 if ( npe->npe_name->bv_val ) {
2593 ch_free( npe->npe_name->bv_val );
2595 ch_free( npe->npe_name );
2597 if ( npe->npe_nname ) {
2598 if ( npe->npe_nname->bv_val ) {
2599 ch_free( npe->npe_nname->bv_val );
2601 ch_free( npe->npe_nname );
2610 /* NOTE: used & documented in slapd.conf(5) */
2612 #define PROVIDERSTR "provider"
2613 #define SCHEMASTR "schemachecking"
2614 #define FILTERSTR "filter"
2615 #define SEARCHBASESTR "searchbase"
2616 #define SCOPESTR "scope"
2617 #define ATTRSONLYSTR "attrsonly"
2618 #define ATTRSSTR "attrs"
2619 #define TYPESTR "type"
2620 #define INTERVALSTR "interval"
2621 #define RETRYSTR "retry"
2622 #define SLIMITSTR "sizelimit"
2623 #define TLIMITSTR "timelimit"
2624 #define SYNCDATASTR "syncdata"
2626 /* FIXME: undocumented */
2627 #define LOGBASESTR "logbase"
2628 #define LOGFILTERSTR "logfilter"
2629 #define OLDAUTHCSTR "bindprincipal"
2630 #define EXATTRSSTR "exattrs"
2631 #define MANAGEDSAITSTR "manageDSAit"
2634 #define LASTMODSTR "lastmod"
2635 #define LMGENSTR "gen"
2636 #define LMNOSTR "no"
2637 #define LMREQSTR "req"
2638 #define SRVTABSTR "srvtab"
2639 #define SUFFIXSTR "suffix"
2642 #define GOT_ID 0x0001
2643 #define GOT_PROVIDER 0x0002
2646 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2652 { BER_BVC("base"), LDAP_SCOPE_BASE },
2653 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2654 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2655 #ifdef LDAP_SCOPE_SUBORDINATE
2656 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2657 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2659 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2660 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2664 static slap_verbmasks datamodes[] = {
2665 { BER_BVC("default"), SYNCDATA_DEFAULT },
2666 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2667 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2672 parse_syncrepl_line(
2682 for ( i = 1; i < cargc; i++ ) {
2683 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2684 STRLENOF( IDSTR "=" ) ) )
2687 /* '\0' string terminator accounts for '=' */
2688 val = cargv[ i ] + STRLENOF( IDSTR "=" );
2690 if ( tmp >= 1000 || tmp < 0 ) {
2691 fprintf( stderr, "Error: parse_syncrepl_line: "
2692 "syncrepl id %d is out of range [0..999]\n", tmp );
2697 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2698 STRLENOF( PROVIDERSTR "=" ) ) )
2700 val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2701 ber_str2bv( val, 0, 1, &si->si_provideruri );
2702 gots |= GOT_PROVIDER;
2703 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2704 STRLENOF( SCHEMASTR "=" ) ) )
2706 val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2707 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2708 si->si_schemachecking = 1;
2709 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2710 si->si_schemachecking = 0;
2712 si->si_schemachecking = 1;
2714 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2715 STRLENOF( FILTERSTR "=" ) ) )
2717 val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2718 if ( si->si_filterstr.bv_val )
2719 ch_free( si->si_filterstr.bv_val );
2720 ber_str2bv( val, 0, 1, &si->si_filterstr );
2721 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2722 STRLENOF( LOGFILTERSTR "=" ) ) )
2724 val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2725 if ( si->si_logfilterstr.bv_val )
2726 ch_free( si->si_logfilterstr.bv_val );
2727 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2728 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2729 STRLENOF( SEARCHBASESTR "=" ) ) )
2734 val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2735 if ( si->si_base.bv_val ) {
2736 ch_free( si->si_base.bv_val );
2738 ber_str2bv( val, 0, 0, &bv );
2739 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2740 if ( rc != LDAP_SUCCESS ) {
2741 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2742 val, rc, ldap_err2string( rc ) );
2745 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2746 STRLENOF( LOGBASESTR "=" ) ) )
2751 val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2752 if ( si->si_logbase.bv_val ) {
2753 ch_free( si->si_logbase.bv_val );
2755 ber_str2bv( val, 0, 0, &bv );
2756 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2757 if ( rc != LDAP_SUCCESS ) {
2758 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2759 val, rc, ldap_err2string( rc ) );
2762 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2763 STRLENOF( SCOPESTR "=" ) ) )
2766 val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2767 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2768 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2769 si->si_scope = scopes[j].val;
2773 if ( BER_BVISNULL(&scopes[j].key) ) {
2774 fprintf( stderr, "Error: parse_syncrepl_line: "
2775 "unknown scope \"%s\"\n", val);
2778 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2779 STRLENOF( ATTRSONLYSTR "=" ) ) )
2781 si->si_attrsonly = 1;
2782 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2783 STRLENOF( ATTRSSTR "=" ) ) )
2785 val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2786 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2788 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2789 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2790 if ( si->si_anlist == NULL ) {
2791 ch_free( attr_fname );
2794 si->si_anfile = attr_fname;
2796 char *str, *s, *next;
2797 char delimstr[] = " ,\t";
2798 str = ch_strdup( val );
2799 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2801 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2803 if ( strlen(s) == 1 && *s == '*' ) {
2804 si->si_allattrs = 1;
2805 *(val + ( s - str )) = delimstr[0];
2807 if ( strlen(s) == 1 && *s == '+' ) {
2808 si->si_allopattrs = 1;
2809 *(val + ( s - str )) = delimstr[0];
2813 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2814 if ( si->si_anlist == NULL ) {
2818 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2819 STRLENOF( EXATTRSSTR "=" ) ) )
2821 val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2822 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2824 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2825 si->si_exanlist = file2anlist(
2826 si->si_exanlist, attr_fname, " ,\t" );
2827 if ( si->si_exanlist == NULL ) {
2828 ch_free( attr_fname );
2831 ch_free( attr_fname );
2833 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2834 if ( si->si_exanlist == NULL ) {
2838 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2839 STRLENOF( TYPESTR "=" ) ) )
2841 val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2842 if ( !strncasecmp( val, "refreshOnly",
2843 STRLENOF("refreshOnly") ))
2845 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2846 } else if ( !strncasecmp( val, "refreshAndPersist",
2847 STRLENOF("refreshAndPersist") ))
2849 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2850 si->si_interval = 60;
2852 fprintf( stderr, "Error: parse_syncrepl_line: "
2853 "unknown sync type \"%s\"\n", val);
2856 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2857 STRLENOF( INTERVALSTR "=" ) ) )
2859 val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2860 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2861 si->si_interval = 0;
2869 hstr = strchr( dstr, ':' );
2870 if ( hstr == NULL ) {
2871 fprintf( stderr, "Error: parse_syncrepl_line: "
2872 "invalid interval \"%s\"\n", val );
2876 mstr = strchr( hstr, ':' );
2877 if ( mstr == NULL ) {
2878 fprintf( stderr, "Error: parse_syncrepl_line: "
2879 "invalid interval \"%s\"\n", val );
2883 sstr = strchr( mstr, ':' );
2884 if ( sstr == NULL ) {
2885 fprintf( stderr, "Error: parse_syncrepl_line: "
2886 "invalid interval \"%s\"\n", val );
2895 if (( hh > 24 ) || ( hh < 0 ) ||
2896 ( mm > 60 ) || ( mm < 0 ) ||
2897 ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2898 fprintf( stderr, "Error: parse_syncrepl_line: "
2899 "invalid interval \"%s\"\n", val );
2902 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2904 if ( si->si_interval < 0 ) {
2905 fprintf( stderr, "Error: parse_syncrepl_line: "
2906 "invalid interval \"%ld\"\n",
2907 (long) si->si_interval);
2910 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2911 STRLENOF( RETRYSTR "=" ) ) )
2916 val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2917 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2918 retry_list[0] = NULL;
2920 slap_str2clist( &retry_list, val, " ,\t" );
2922 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2926 "Error: incomplete syncrepl retry list\n" );
2927 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2928 ch_free( retry_list[k] );
2930 ch_free( retry_list );
2931 exit( EXIT_FAILURE );
2933 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2934 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2935 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2936 for ( j = 0; j < n; j++ ) {
2937 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2938 if ( *retry_list[j*2+1] == '+' ) {
2939 si->si_retrynum_init[j] = -1;
2940 si->si_retrynum[j] = -1;
2944 si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2945 si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2948 si->si_retrynum_init[j] = -2;
2949 si->si_retrynum[j] = -2;
2950 si->si_retryinterval[j] = 0;
2952 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2953 ch_free( retry_list[k] );
2955 ch_free( retry_list );
2956 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2957 STRLENOF( MANAGEDSAITSTR "=" ) ) )
2959 val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2960 si->si_manageDSAit = atoi( val );
2961 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2962 STRLENOF( SLIMITSTR "=") ) )
2964 val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2965 si->si_slimit = atoi( val );
2966 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2967 STRLENOF( TLIMITSTR "=" ) ) )
2969 val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2970 si->si_tlimit = atoi( val );
2971 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2972 STRLENOF( SYNCDATASTR "=" ) ) )
2974 val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2975 si->si_syncdata = verb_to_mask( val, datamodes );
2976 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2977 fprintf( stderr, "Error: parse_syncrepl_line: "
2978 "unknown keyword \"%s\"\n", cargv[ i ] );
2983 if ( gots != GOT_ALL ) {
2985 "Error: Malformed \"syncrepl\" line in slapd config file" );
3002 if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
3003 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
3004 "required for syncrepl\n", be->be_type, 0, 0 );
3007 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3010 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3014 si->si_bindconf.sb_tls = SB_TLS_OFF;
3015 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3016 si->si_schemachecking = 0;
3017 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3018 &si->si_filterstr );
3019 si->si_base.bv_val = NULL;
3020 si->si_scope = LDAP_SCOPE_SUBTREE;
3021 si->si_attrsonly = 0;
3022 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3023 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3024 si->si_attrs = NULL;
3025 si->si_allattrs = 0;
3026 si->si_allopattrs = 0;
3027 si->si_exattrs = NULL;
3028 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3029 si->si_interval = 86400;
3030 si->si_retryinterval = NULL;
3031 si->si_retrynum_init = NULL;
3032 si->si_retrynum = NULL;
3033 si->si_manageDSAit = 0;
3037 si->si_presentlist = NULL;
3038 LDAP_LIST_INIT( &si->si_nonpresentlist );
3039 ldap_pvt_thread_mutex_init( &si->si_mutex );
3041 rc = parse_syncrepl_line( cargv, cargc, si );
3045 init_syncrepl( si );
3046 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3047 do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3052 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3053 syncinfo_free( si );
3056 Debug( LDAP_DEBUG_CONFIG,
3057 "Config: ** successfully added syncrepl \"%s\"\n",
3058 BER_BVISNULL( &si->si_provideruri ) ?
3059 "(null)" : si->si_provideruri.bv_val, 0, 0 );
3060 if ( !si->si_schemachecking ) {
3061 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3063 be->be_syncinfo = si;
3069 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3072 char buf[BUFSIZ*2], *ptr;
3075 bindconf_unparse( &si->si_bindconf, &bc );
3077 ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3078 si->si_rid, si->si_provideruri.bv_val );
3079 if ( !BER_BVISNULL( &bc )) {
3080 ptr = lutil_strcopy( ptr, bc.bv_val );
3083 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3084 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3085 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3088 if ( !BER_BVISNULL( &si->si_base )) {
3089 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3090 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3093 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3094 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3095 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3098 if ( !BER_BVISNULL( &si->si_logbase )) {
3099 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3100 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3103 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3104 if ( si->si_scope == scopes[i].val ) {
3105 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3106 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3110 if ( si->si_attrsonly ) {
3111 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3113 if ( si->si_anfile ) {
3114 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3115 ptr = lutil_strcopy( ptr, si->si_anfile );
3116 } else if ( si->si_allattrs || si->si_allopattrs ||
3117 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3119 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3121 ptr = anlist_unparse( si->si_anlist, ptr );
3122 if ( si->si_allattrs ) {
3123 if ( old != ptr ) *ptr++ = ',';
3126 if ( si->si_allopattrs ) {
3127 if ( old != ptr ) *ptr++ = ',';
3132 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3133 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3134 ptr = anlist_unparse( si->si_exanlist, ptr );
3136 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3137 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3139 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3140 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3141 "refreshAndPersist" : "refreshOnly" );
3143 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3146 dd = si->si_interval;
3153 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3154 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3155 } else if ( si->si_retryinterval ) {
3157 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3158 for (i=0; si->si_retryinterval[i]; i++) {
3159 if ( space ) *ptr++ = ' ';
3161 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3162 if ( si->si_retrynum_init[i] == -1 )
3165 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3170 if ( si->si_slimit ) {
3171 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3172 ptr += sprintf( ptr, "%d", si->si_slimit );
3175 if ( si->si_tlimit ) {
3176 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3177 ptr += sprintf( ptr, "%d", si->si_tlimit );
3180 if ( si->si_syncdata ) {
3181 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3182 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3183 ptr = lutil_strcopy( ptr, bc.bv_val );
3186 bc.bv_len = ptr - buf;
3188 ber_dupbv( bv, &bc );
3192 syncrepl_config(ConfigArgs *c) {
3193 if (c->op == SLAP_CONFIG_EMIT) {
3194 if ( c->be->be_syncinfo ) {
3196 syncrepl_unparse( c->be->be_syncinfo, &bv );
3197 ber_bvarray_add( &c->rvalue_vals, &bv );
3201 } else if ( c->op == LDAP_MOD_DELETE ) {
3204 if ( c->be->be_syncinfo ) {
3205 re = c->be->be_syncinfo->si_re;
3207 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3208 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3209 ldap_pvt_runqueue_remove( &slapd_rq, re );
3211 syncinfo_free( c->be->be_syncinfo );
3212 c->be->be_syncinfo = NULL;
3214 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3217 if(SLAP_SHADOW(c->be)) {
3218 Debug(LDAP_DEBUG_ANY, "%s: "
3219 "syncrepl: database already shadowed.\n",
3222 } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3225 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);