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;
82 Avlnode *si_presentlist;
84 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
85 ldap_pvt_thread_mutex_t si_mutex;
88 static int syncuuid_cmp( const void *, const void * );
89 static void avl_ber_bvfree( void * );
90 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
91 static int syncrepl_message_to_op(
92 syncinfo_t *, Operation *, LDAPMessage * );
93 static int syncrepl_message_to_entry(
94 syncinfo_t *, Operation *, LDAPMessage *,
95 Modifications **, Entry **, int );
96 static int syncrepl_entry(
97 syncinfo_t *, Operation*, Entry*,
98 Modifications**,int, struct berval*,
101 static void syncrepl_updateCookie(
102 syncinfo_t *, Operation *, struct berval *,
103 struct sync_cookie * );
104 static struct berval * slap_uuidstr_from_normalized(
105 struct berval *, struct berval *, void * );
107 /* callback functions */
108 static int dn_callback( struct slap_op *, struct slap_rep * );
109 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
110 static int null_callback( struct slap_op *, struct slap_rep * );
112 static AttributeDescription *sync_descs[4];
115 init_syncrepl(syncinfo_t *si)
118 char **attrs, **exattrs;
120 if ( !sync_descs[0] ) {
121 sync_descs[0] = slap_schema.si_ad_objectClass;
122 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
123 sync_descs[2] = slap_schema.si_ad_entryCSN;
124 sync_descs[3] = NULL;
127 if ( si->si_allattrs && si->si_allopattrs )
130 attrs = anlist2attrs( si->si_anlist );
133 if ( si->si_allattrs ) {
136 if ( !is_at_operational( at_find( attrs[i] ))) {
137 for ( j = i; attrs[j] != NULL; j++ ) {
140 attrs[j] = attrs[j+1];
146 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
147 attrs[i] = ch_strdup("*");
150 } else if ( si->si_allopattrs ) {
153 if ( is_at_operational( at_find( attrs[i] ))) {
154 for ( j = i; attrs[j] != NULL; j++ ) {
157 attrs[j] = attrs[j+1];
163 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
164 attrs[i] = ch_strdup("+");
168 for ( i = 0; sync_descs[i] != NULL; i++ ) {
171 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
172 for ( k = j; attrs[k] != NULL; k++ ) {
175 attrs[k] = attrs[k+1];
183 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
185 if ( si->si_allopattrs ) {
186 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
188 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
191 if ( attrs == NULL ) {
192 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
196 if ( si->si_allopattrs ) {
197 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
199 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
200 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
208 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( "+" );
212 } else if ( si->si_allattrs && !si->si_allopattrs ) {
213 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
214 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
215 attrs[i++] = ch_strdup( "*" );
216 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
217 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
219 } else if ( !si->si_allattrs && si->si_allopattrs ) {
220 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
221 attrs[i++] = ch_strdup( "+" );
222 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
227 si->si_attrs = attrs;
229 exattrs = anlist2attrs( si->si_exanlist );
232 for ( n = 0; exattrs[n] != NULL; n++ ) ;
234 for ( i = 0; sync_descs[i] != NULL; i++ ) {
236 while ( exattrs[j] != NULL ) {
237 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
238 ch_free( exattrs[j] );
239 for ( k = j; exattrs[k] != NULL; k++ ) {
240 exattrs[k] = exattrs[k+1];
248 for ( i = 0; exattrs[i] != NULL; i++ ) {
249 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
251 if ( ( oc = si->si_anlist[j].an_oc ) ) {
253 while ( oc->soc_required[k] ) {
254 if ( !strcmp( exattrs[i],
255 oc->soc_required[k]->sat_cname.bv_val )) {
256 ch_free( exattrs[i] );
257 for ( l = i; exattrs[l]; l++ ) {
258 exattrs[l] = exattrs[l+1];
268 for ( i = 0; exattrs[i] != NULL; i++ ) ;
271 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
274 si->si_exattrs = exattrs;
277 typedef struct logschema {
279 struct berval ls_req;
280 struct berval ls_mod;
281 struct berval ls_newRdn;
282 struct berval ls_delRdn;
283 struct berval ls_newSup;
286 static logschema changelog_sc = {
288 BER_BVC("changeType"),
291 BER_BVC("deleteOldRDN"),
292 BER_BVC("newSuperior")
295 static logschema accesslog_sc = {
299 BER_BVC("reqNewRDN"),
300 BER_BVC("reqDeleteOldRDN"),
301 BER_BVC("reqNewSuperior")
309 BerElementBuffer berbuf;
310 BerElement *ber = (BerElement *)&berbuf;
311 LDAPControl c[2], *ctrls[3];
312 struct timeval timeout;
317 char **attrs, *lattrs[8];
322 /* setup LDAP SYNC control */
323 ber_init2( ber, NULL, LBER_USE_DER );
324 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
326 /* If we're using a log but we have no state, then fallback to
327 * normal mode for a full refresh.
329 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
330 si->si_logstate = SYNCLOG_FALLBACK;
332 /* Use the log parameters if we're in log mode */
333 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
335 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
339 lattrs[0] = ls->ls_dn.bv_val;
340 lattrs[1] = ls->ls_req.bv_val;
341 lattrs[2] = ls->ls_mod.bv_val;
342 lattrs[3] = ls->ls_newRdn.bv_val;
343 lattrs[4] = ls->ls_delRdn.bv_val;
344 lattrs[5] = ls->ls_newSup.bv_val;
345 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
349 base = si->si_logbase.bv_val;
350 filter = si->si_logfilterstr.bv_val;
353 scope = LDAP_SCOPE_ONELEVEL;
356 base = si->si_base.bv_val;
357 filter = si->si_filterstr.bv_val;
358 attrs = si->si_attrs;
359 attrsonly = si->si_attrsonly;
360 scope = si->si_scope;
362 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
363 si->si_type = LDAP_SYNC_REFRESH_ONLY;
365 si->si_type = si->si_ctype;
368 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
370 ber_printf( ber, "{eOb}",
371 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
373 ber_printf( ber, "{eb}",
374 abs(si->si_type), rhint );
377 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
382 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
383 c[0].ldctl_iscritical = si->si_type < 0;
386 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
387 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
388 c[1].ldctl_value = si->si_bindconf.sb_authzId;
389 c[1].ldctl_iscritical = 1;
396 timeout.tv_sec = si->si_tlimit;
399 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
400 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
401 si->si_slimit, &msgid );
412 int cmdline_cookie_found = 0;
414 struct sync_cookie *sc = NULL;
420 psub = &si->si_be->be_nsuffix[0];
422 /* Init connection to master */
423 rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
424 if ( rc != LDAP_SUCCESS ) {
425 Debug( LDAP_DEBUG_ANY,
426 "do_syncrep1: ldap_initialize failed (%s)\n",
427 si->si_provideruri.bv_val, 0, 0 );
431 op->o_protocol = LDAP_VERSION3;
432 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
436 if ( si->si_bindconf.sb_tls ) {
437 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
438 if( rc != LDAP_SUCCESS ) {
439 Debug( LDAP_DEBUG_ANY,
440 "%s: ldap_start_tls failed (%d)\n",
441 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
443 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
447 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
448 #ifdef HAVE_CYRUS_SASL
451 if ( si->si_bindconf.sb_secprops != NULL ) {
452 rc = ldap_set_option( si->si_ld,
453 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
455 if( rc != LDAP_OPT_SUCCESS ) {
456 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
457 "(%s,SECPROPS,\"%s\") failed!\n",
458 si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
463 defaults = lutil_sasl_defaults( si->si_ld,
464 si->si_bindconf.sb_saslmech.bv_val,
465 si->si_bindconf.sb_realm.bv_val,
466 si->si_bindconf.sb_authcId.bv_val,
467 si->si_bindconf.sb_cred.bv_val,
468 si->si_bindconf.sb_authzId.bv_val );
470 rc = ldap_sasl_interactive_bind_s( si->si_ld,
471 si->si_bindconf.sb_binddn.bv_val,
472 si->si_bindconf.sb_saslmech.bv_val,
478 lutil_sasl_freedefs( defaults );
480 /* FIXME: different error behaviors according to
482 * 2) on err policy : exit, retry, backoff ...
484 if ( rc != LDAP_SUCCESS ) {
485 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
487 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
488 "ldap_sasl_interactive_bind_s failed (%d)\n",
491 /* FIXME (see above comment) */
492 /* if Kerberos credentials cache is not active, retry */
493 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
494 rc == LDAP_LOCAL_ERROR )
496 rc = LDAP_SERVER_DOWN;
501 #else /* HAVE_CYRUS_SASL */
502 /* Should never get here, we trapped this at config time */
504 fprintf( stderr, "not compiled with SASL support\n" );
509 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
510 rc = ldap_sasl_bind_s( si->si_ld,
511 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
512 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
513 if ( rc != LDAP_SUCCESS ) {
514 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
515 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
520 /* Set SSF to strongest of TLS, SASL SSFs */
523 op->o_transport_ssf = 0;
525 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
526 == LDAP_SUCCESS && ssl != NULL )
528 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
530 #endif /* HAVE_TLS */
531 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
532 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
533 ? op->o_sasl_ssf : op->o_tls_ssf;
536 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
537 /* get contextCSN shadow replica from database */
538 BerVarray csn = NULL;
540 assert( si->si_rid < 1000 );
541 op->o_req_ndn = op->o_bd->be_nsuffix[0];
542 op->o_req_dn = op->o_req_ndn;
544 /* try to read stored contextCSN */
545 backend_attribute( op, NULL, &op->o_req_ndn,
546 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
548 ch_free( si->si_syncCookie.ctxcsn.bv_val );
549 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
550 ber_bvarray_free_x( csn, op->o_tmpmemctx );
553 si->si_syncCookie.rid = si->si_rid;
555 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
556 if ( si->si_rid == sc->rid ) {
557 cmdline_cookie_found = 1;
562 if ( cmdline_cookie_found ) {
563 /* cookie is supplied in the command line */
565 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
567 /* ctxcsn wasn't parsed yet, do it now */
568 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
569 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
570 /* if cmdline cookie does not have ctxcsn */
571 /* component, set it to an initial value */
572 slap_init_sync_cookie_ctxcsn( sc );
574 slap_sync_cookie_free( &si->si_syncCookie, 0 );
575 slap_dup_sync_cookie( &si->si_syncCookie, sc );
576 slap_sync_cookie_free( sc, 1 );
579 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
580 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
583 rc = ldap_sync_search( si, op->o_tmpmemctx );
585 if( rc != LDAP_SUCCESS ) {
586 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
587 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
593 ldap_unbind_ext( si->si_ld, NULL, NULL );
606 LDAPControl **rctrls = NULL;
609 BerElementBuffer berbuf;
610 BerElement *ber = (BerElement *)&berbuf;
612 LDAPMessage *res = NULL;
613 LDAPMessage *msg = NULL;
616 struct berval *retdata = NULL;
621 struct berval syncUUID = BER_BVNULL;
622 struct sync_cookie syncCookie = { BER_BVNULL };
623 struct sync_cookie syncCookie_req = { BER_BVNULL };
624 struct berval cookie = BER_BVNULL;
632 Modifications *modlist = NULL;
637 struct timeval *tout_p = NULL;
638 struct timeval tout = { 0, 0 };
640 int refreshDeletes = 0;
642 BerVarray syncUUIDs = NULL;
645 if ( slapd_shutdown ) {
650 ber_init2( ber, NULL, LBER_USE_DER );
651 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
653 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
655 psub = &si->si_be->be_nsuffix[0];
657 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
659 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
665 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
666 tout_p, &res )) > 0 )
668 if ( slapd_shutdown ) {
672 for( msg = ldap_first_message( si->si_ld, res );
674 msg = ldap_next_message( si->si_ld, msg ) )
676 if ( slapd_shutdown ) {
680 switch( ldap_msgtype( msg ) ) {
681 case LDAP_RES_SEARCH_ENTRY:
682 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
683 /* we can't work without the control */
685 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
686 "got search entry without "
687 "control\n", 0, 0, 0 );
692 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
693 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
694 /* FIXME: what if syncUUID is NULL or empty?
695 * (happens with back-sql...) */
696 if ( BER_BVISEMPTY( &syncUUID ) ) {
697 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
698 "got empty syncUUID\n", 0, 0, 0 );
702 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
703 ber_scanf( ber, /*"{"*/ "m}", &cookie );
704 if ( !BER_BVISNULL( &cookie ) ) {
705 ch_free( syncCookie.octet_str.bv_val );
706 ber_dupbv( &syncCookie.octet_str, &cookie );
708 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
710 slap_parse_sync_cookie( &syncCookie, NULL );
713 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
716 if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
717 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
718 syncrepl_updateCookie( si, op, psub, &syncCookie );
720 } else if ( syncrepl_message_to_entry( si, op, msg,
721 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
722 rc_efree = syncrepl_entry( si, op, entry, &modlist,
723 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
724 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
726 syncrepl_updateCookie( si, op, psub, &syncCookie );
729 ldap_controls_free( rctrls );
731 slap_mods_free( modlist, 1 );
733 if ( rc_efree && entry ) {
739 case LDAP_RES_SEARCH_REFERENCE:
740 Debug( LDAP_DEBUG_ANY,
741 "do_syncrep2: reference received error\n", 0, 0, 0 );
744 case LDAP_RES_SEARCH_RESULT:
745 Debug( LDAP_DEBUG_SYNC,
746 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
747 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
749 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
750 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
751 /* map old result code to registered code */
752 err = LDAP_SYNC_REFRESH_REQUIRED;
755 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
756 if ( si->si_logstate == SYNCLOG_LOGGING ) {
757 si->si_logstate = SYNCLOG_FALLBACK;
764 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
766 ber_scanf( ber, "{" /*"}"*/);
767 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
768 ber_scanf( ber, "m", &cookie );
769 if ( !BER_BVISNULL( &cookie ) ) {
770 ch_free( syncCookie.octet_str.bv_val );
771 ber_dupbv( &syncCookie.octet_str, &cookie);
773 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
775 slap_parse_sync_cookie( &syncCookie, NULL );
778 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
780 ber_scanf( ber, "b", &refreshDeletes );
782 ber_scanf( ber, /*"{"*/ "}" );
784 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
786 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
789 value_match( &match, slap_schema.si_ad_entryCSN,
790 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
791 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
792 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
795 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
796 match < 0 && err == LDAP_SUCCESS )
798 syncrepl_updateCookie( si, op, psub, &syncCookie );
801 ldap_controls_free( rctrls );
803 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
804 /* FIXME : different error behaviors according to
805 * 1) err code : LDAP_BUSY ...
806 * 2) on err policy : stop service, stop sync, retry
808 if ( refreshDeletes == 0 && match < 0 &&
809 err == LDAP_SUCCESS )
811 syncrepl_del_nonpresent( op, si, NULL );
813 avl_free( si->si_presentlist, avl_ber_bvfree );
814 si->si_presentlist = NULL;
817 if ( err == LDAP_SUCCESS
818 && si->si_logstate == SYNCLOG_FALLBACK ) {
819 si->si_logstate = SYNCLOG_LOGGING;
820 rc = LDAP_SYNC_REFRESH_REQUIRED;
827 case LDAP_RES_INTERMEDIATE:
828 rc = ldap_parse_intermediate( si->si_ld, msg,
829 &retoid, &retdata, NULL, 0 );
830 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
831 ber_init2( ber, retdata, LBER_USE_DER );
833 switch ( si_tag = ber_peek_tag( ber, &len )) {
835 case LDAP_TAG_SYNC_NEW_COOKIE:
836 Debug( LDAP_DEBUG_SYNC,
837 "do_syncrep2: %s - %s%s\n",
838 "LDAP_RES_INTERMEDIATE",
839 "NEW_COOKIE", "\n" );
840 ber_scanf( ber, "tm", &tag, &cookie );
842 case LDAP_TAG_SYNC_REFRESH_DELETE:
843 case LDAP_TAG_SYNC_REFRESH_PRESENT:
844 Debug( LDAP_DEBUG_SYNC,
845 "do_syncrep2: %s - %s%s\n",
846 "LDAP_RES_INTERMEDIATE",
847 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
848 "REFRESH_PRESENT" : "REFRESH_DELETE",
850 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
851 si->si_refreshDelete = 1;
853 si->si_refreshPresent = 1;
855 ber_scanf( ber, "t{" /*"}"*/, &tag );
856 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
858 ber_scanf( ber, "m", &cookie );
859 if ( !BER_BVISNULL( &cookie ) ) {
860 ch_free( syncCookie.octet_str.bv_val );
861 ber_dupbv( &syncCookie.octet_str, &cookie );
863 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
865 slap_parse_sync_cookie( &syncCookie, NULL );
868 if ( ber_peek_tag( ber, &len ) ==
869 LDAP_TAG_REFRESHDONE )
871 ber_scanf( ber, "b", &refreshDone );
873 ber_scanf( ber, /*"{"*/ "}" );
875 case LDAP_TAG_SYNC_ID_SET:
876 Debug( LDAP_DEBUG_SYNC,
877 "do_syncrep2: %s - %s%s\n",
878 "LDAP_RES_INTERMEDIATE",
881 ber_scanf( ber, "t{" /*"}"*/, &tag );
882 if ( ber_peek_tag( ber, &len ) ==
883 LDAP_TAG_SYNC_COOKIE )
885 ber_scanf( ber, "m", &cookie );
886 if ( !BER_BVISNULL( &cookie ) ) {
887 ch_free( syncCookie.octet_str.bv_val );
888 ber_dupbv( &syncCookie.octet_str, &cookie );
890 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
892 slap_parse_sync_cookie( &syncCookie, NULL );
895 if ( ber_peek_tag( ber, &len ) ==
896 LDAP_TAG_REFRESHDELETES )
898 ber_scanf( ber, "b", &refreshDeletes );
900 ber_scanf( ber, "[W]", &syncUUIDs );
901 ber_scanf( ber, /*"{"*/ "}" );
902 if ( refreshDeletes ) {
903 syncrepl_del_nonpresent( op, si, syncUUIDs );
904 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
906 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
907 struct berval *syncuuid_bv;
908 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
909 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
910 avl_insert( &si->si_presentlist,
911 (caddr_t) syncuuid_bv,
912 syncuuid_cmp, avl_dup_error );
914 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
918 Debug( LDAP_DEBUG_ANY,
919 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
920 (long) si_tag, 0, 0 );
921 ldap_memfree( retoid );
922 ber_bvfree( retdata );
926 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
928 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
931 value_match( &match, slap_schema.si_ad_entryCSN,
932 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
933 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
934 &syncCookie_req.ctxcsn,
935 &syncCookie.ctxcsn, &text );
938 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
941 syncrepl_updateCookie( si, op, psub, &syncCookie);
944 if ( si->si_refreshPresent == 1 ) {
946 syncrepl_del_nonpresent( op, si, NULL );
950 ldap_memfree( retoid );
951 ber_bvfree( retdata );
955 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
956 "unknown intermediate response (%d)\n",
958 ldap_memfree( retoid );
959 ber_bvfree( retdata );
965 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
966 "unknown message\n", 0, 0, 0 );
970 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
971 slap_sync_cookie_free( &syncCookie_req, 0 );
972 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
973 slap_sync_cookie_free( &syncCookie, 0 );
983 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
984 errstr = ldap_err2string( rc );
986 Debug( LDAP_DEBUG_ANY,
987 "do_syncrep2 : %s\n", errstr, 0, 0 );
991 slap_sync_cookie_free( &syncCookie, 0 );
992 slap_sync_cookie_free( &syncCookie_req, 0 );
994 if ( res ) ldap_msgfree( res );
996 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
997 if ( si->si_conn_setup ) {
999 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1000 connection_client_stop( s );
1001 si->si_conn_setup = 0;
1003 ldap_unbind_ext( si->si_ld, NULL, NULL );
1015 struct re_s* rtask = arg;
1016 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1017 Connection conn = {0};
1018 OperationBuffer opbuf;
1020 int rc = LDAP_SUCCESS;
1026 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1031 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1033 switch( abs( si->si_type )) {
1034 case LDAP_SYNC_REFRESH_ONLY:
1035 case LDAP_SYNC_REFRESH_AND_PERSIST:
1038 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1042 if ( slapd_shutdown ) {
1044 if ( si->si_conn_setup ) {
1045 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1046 connection_client_stop( s );
1047 si->si_conn_setup = 0;
1049 ldap_unbind_ext( si->si_ld, NULL, NULL );
1052 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1056 op = (Operation *) &opbuf;
1057 connection_fake_init( &conn, op, ctx );
1059 /* use global malloc for now */
1060 op->o_tmpmemctx = NULL;
1061 op->o_tmpmfuncs = &ch_mfuncs;
1063 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1064 op->o_bd = be = si->si_be;
1065 op->o_dn = op->o_bd->be_rootdn;
1066 op->o_ndn = op->o_bd->be_rootndn;
1068 /* Establish session, do search */
1070 si->si_refreshDelete = 0;
1071 si->si_refreshPresent = 0;
1072 rc = do_syncrep1( op, si );
1076 /* Process results */
1077 if ( rc == LDAP_SUCCESS ) {
1078 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1080 rc = do_syncrep2( op, si );
1081 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1082 rc = ldap_sync_search( si, op->o_tmpmemctx );
1086 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1087 /* If we succeeded, enable the connection for further listening.
1088 * If we failed, tear down the connection and reschedule.
1090 if ( rc == LDAP_SUCCESS ) {
1091 if ( si->si_conn_setup ) {
1092 connection_client_enable( s );
1094 rc = connection_client_setup( s, do_syncrepl, arg );
1096 si->si_conn_setup = 1;
1098 } else if ( si->si_conn_setup ) {
1102 if ( rc == -2 ) rc = 0;
1106 /* At this point, we have 4 cases:
1107 * 1) for any hard failure, give up and remove this task
1108 * 2) for ServerDown, reschedule this task to run
1109 * 3) for Refresh and Success, reschedule to run
1110 * 4) for Persist and Success, reschedule to defer
1112 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1114 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1115 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1119 connection_client_stop( s );
1122 if ( rc == LDAP_SUCCESS ) {
1123 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1126 rtask->interval.tv_sec = si->si_interval;
1127 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1128 if ( si->si_retrynum ) {
1129 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
1130 si->si_retrynum[i] = si->si_retrynum_init[i];
1132 si->si_retrynum[i] = -2;
1135 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1136 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
1140 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1141 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1142 } else if ( si->si_retrynum[i] >= -1 ) {
1143 if ( si->si_retrynum[i] > 0 )
1144 si->si_retrynum[i]--;
1145 rtask->interval.tv_sec = si->si_retryinterval[i];
1146 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1147 slap_wake_listener();
1151 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1152 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1157 static slap_verbmasks modops[] = {
1158 { BER_BVC("add"), LDAP_REQ_ADD },
1159 { BER_BVC("delete"), LDAP_REQ_DELETE },
1160 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1161 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1165 static Modifications *
1166 syncrepl_accesslog_mods(
1173 AttributeDescription *ad;
1174 struct berval bv, bv2;
1176 Modifications *mod = NULL, *modlist = NULL, **modtail;
1181 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1185 colon = strchr( bv.bv_val, ':' );
1187 continue; /* invalid */
1188 bv.bv_len = colon - bv.bv_val;
1189 if ( slap_bv2ad( &bv, &ad, &text )) {
1193 /* Ignore dynamically generated attrs */
1194 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1196 /* Ignore excluded attrs */
1197 if ( ldap_charray_inlist( si->si_exattrs,
1198 ad->ad_type->sat_cname.bv_val ))
1202 case '+': op = LDAP_MOD_ADD; break;
1203 case '-': op = LDAP_MOD_DELETE; break;
1204 case '=': op = LDAP_MOD_REPLACE; break;
1205 case '#': op = LDAP_MOD_INCREMENT; break;
1209 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1210 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1213 mod->sml_next = NULL;
1215 mod->sml_type = ad->ad_cname;
1216 mod->sml_values = NULL;
1217 mod->sml_nvalues = NULL;
1220 modtail = &mod->sml_next;
1222 if ( colon[2] == ' ' ) {
1223 bv.bv_val = colon + 3;
1224 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1225 ber_dupbv( &bv2, &bv );
1226 ber_bvarray_add( &mod->sml_values, &bv2 );
1232 static Modifications *
1233 syncrepl_changelog_mods(
1238 return NULL; /* FIXME */
1242 syncrepl_message_to_op(
1248 BerElement *ber = NULL;
1249 Modifications *modlist = NULL;
1251 SlapReply rs = { REP_RESULT };
1252 slap_callback cb = { NULL, null_callback, NULL, NULL };
1255 char txtbuf[SLAP_TEXT_BUFLEN];
1256 size_t textlen = sizeof txtbuf;
1258 struct berval bdn, dn = BER_BVNULL, ndn;
1259 struct berval bv, *bvals = NULL;
1260 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1261 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1262 psup = BER_BVNULL, nsup = BER_BVNULL;
1263 int rc, deleteOldRdn = 0;
1265 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1266 Debug( LDAP_DEBUG_ANY,
1267 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1271 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1276 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1278 if ( rc != LDAP_SUCCESS ) {
1279 Debug( LDAP_DEBUG_ANY,
1280 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1284 op->o_tag = LBER_DEFAULT;
1286 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1288 if ( bv.bv_val == NULL )
1291 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1293 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1294 ber_dupbv( &op->o_req_dn, &dn );
1295 ber_dupbv( &op->o_req_ndn, &ndn );
1296 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1297 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1298 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1299 int i = verb_to_mask( bvals[0].bv_val, modops );
1301 Debug( LDAP_DEBUG_ANY,
1302 "syncrepl_message_to_op : unknown op %s",
1303 bvals[0].bv_val, 0, 0 );
1308 op->o_tag = modops[i].mask;
1309 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1310 /* Parse attribute into modlist */
1311 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1312 modlist = syncrepl_accesslog_mods( si, bvals );
1314 modlist = syncrepl_changelog_mods( si, bvals );
1315 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1317 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1318 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1320 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1322 } else if ( !ber_bvstrcasecmp( &bv,
1323 &slap_schema.si_ad_entryCSN->ad_cname )) {
1324 slap_queue_csn( op, bvals );
1329 /* If we didn't get a mod type or a target DN, bail out */
1330 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1335 op->o_callback = &cb;
1337 switch( op->o_tag ) {
1339 case LDAP_REQ_MODIFY:
1340 /* If we didn't get required data, bail */
1341 if ( !modlist ) goto done;
1343 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1345 if ( rc != LDAP_SUCCESS ) {
1346 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1351 if ( op->o_tag == LDAP_REQ_ADD ) {
1352 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1353 op->ora_e->e_name = op->o_req_dn;
1354 op->ora_e->e_nname = op->o_req_ndn;
1355 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1356 if( rc != LDAP_SUCCESS ) {
1357 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1360 rc = op->o_bd->be_add( op, &rs );
1362 be_entry_release_w( op, op->ora_e );
1364 op->orm_modlist = modlist;
1365 rc = op->o_bd->be_modify( op, &rs );
1368 case LDAP_REQ_MODRDN:
1369 if ( BER_BVISNULL( &rdn )) goto done;
1371 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1373 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1375 if ( !BER_BVISNULL( &sup )) {
1376 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1378 op->orr_newSup = &psup;
1379 op->orr_nnewSup = ⊅
1381 op->orr_newrdn = prdn;
1382 op->orr_nnewrdn = nrdn;
1383 op->orr_deleteoldrdn = deleteOldRdn;
1384 rc = op->o_bd->be_modrdn( op, &rs );
1386 case LDAP_REQ_DELETE:
1387 rc = op->o_bd->be_delete( op, &rs );
1391 slap_graduate_commit_csn( op );
1393 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1394 if ( !BER_BVISNULL( &rdn )) {
1395 if ( !BER_BVISNULL( &nsup ))
1396 ch_free( nsup.bv_val );
1397 if ( !BER_BVISNULL( &psup ))
1398 ch_free( psup.bv_val );
1399 if ( !BER_BVISNULL( &nrdn ))
1400 ch_free( nrdn.bv_val );
1401 if ( !BER_BVISNULL( &prdn ))
1402 ch_free( prdn.bv_val );
1404 ber_free ( ber, 0 );
1409 syncrepl_message_to_entry(
1413 Modifications **modlist,
1419 BerElement *ber = NULL;
1422 Modifications **modtail = modlist;
1425 char txtbuf[SLAP_TEXT_BUFLEN];
1426 size_t textlen = sizeof txtbuf;
1428 struct berval bdn = {0, NULL}, dn, ndn;
1433 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1434 Debug( LDAP_DEBUG_ANY,
1435 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1439 op->o_tag = LDAP_REQ_ADD;
1441 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1443 if ( rc != LDAP_SUCCESS ) {
1444 Debug( LDAP_DEBUG_ANY,
1445 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1449 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1450 ber_dupbv( &op->o_req_dn, &dn );
1451 ber_dupbv( &op->o_req_ndn, &ndn );
1452 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1453 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1455 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1458 return LDAP_SUCCESS;
1461 if ( entry == NULL ) {
1465 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1467 e->e_name = op->o_req_dn;
1468 e->e_nname = op->o_req_ndn;
1470 while ( ber_remaining( ber ) ) {
1471 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1472 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1477 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1479 mod->sml_op = LDAP_MOD_REPLACE;
1481 mod->sml_next = NULL;
1482 mod->sml_desc = NULL;
1483 mod->sml_type = tmp.sml_type;
1484 mod->sml_values = tmp.sml_values;
1485 mod->sml_nvalues = NULL;
1488 modtail = &mod->sml_next;
1491 if ( *modlist == NULL ) {
1492 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1498 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1500 if ( rc != LDAP_SUCCESS ) {
1501 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1506 /* Strip out dynamically generated attrs */
1507 for ( modtail = modlist; *modtail ; ) {
1509 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1510 *modtail = mod->sml_next;
1511 slap_mod_free( &mod->sml_mod, 0 );
1514 modtail = &mod->sml_next;
1518 /* Strip out attrs in exattrs list */
1519 for ( modtail = modlist; *modtail ; ) {
1521 if ( ldap_charray_inlist( si->si_exattrs,
1522 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1523 *modtail = mod->sml_next;
1524 slap_mod_free( &mod->sml_mod, 0 );
1527 modtail = &mod->sml_next;
1531 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1532 if( rc != LDAP_SUCCESS ) {
1533 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1538 ber_free ( ber, 0 );
1539 if ( rc != LDAP_SUCCESS ) {
1549 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1551 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1552 * entry if a previous refresh was interrupted before sending us a new
1553 * context state. We try to compare the new entry to the existing entry
1554 * and ignore the new entry if they are the same.
1556 * Also, we may get an update where the entryDN has changed, due to
1557 * a ModDn on the provider. We detect this as well, so we can issue
1558 * the corresponding operation locally.
1560 * In the case of a modify, we get a list of all the attributes
1561 * in the original entry. Rather than deleting the entry and re-adding it,
1562 * we issue a Modify request that deletes all the attributes and adds all
1563 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1566 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1567 * a ModDN we will issue both operations on the local database.
1569 typedef struct dninfo {
1573 int renamed; /* Was an existing entry renamed? */
1574 int wasChanged; /* are the attributes changed? */
1575 int attrs; /* how many attribute types are in the ads list */
1576 AttributeDescription **ads;
1584 Modifications** modlist,
1586 struct berval* syncUUID,
1587 struct sync_cookie* syncCookie_req,
1588 struct berval* syncCSN )
1590 Backend *be = op->o_bd;
1591 slap_callback cb = { NULL, NULL, NULL, NULL };
1592 struct berval *syncuuid_bv = NULL;
1593 struct berval syncUUID_strrep = BER_BVNULL;
1594 struct berval uuid_bv = BER_BVNULL;
1596 SlapReply rs_search = {REP_RESULT};
1597 SlapReply rs_delete = {REP_RESULT};
1598 SlapReply rs_add = {REP_RESULT};
1599 SlapReply rs_modify = {REP_RESULT};
1601 #ifdef LDAP_COMP_MATCH
1602 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1604 AttributeAssertion ava = { NULL, BER_BVNULL };
1606 int rc = LDAP_SUCCESS;
1607 int ret = LDAP_SUCCESS;
1609 struct berval pdn = BER_BVNULL;
1613 switch( syncstate ) {
1614 case LDAP_SYNC_PRESENT:
1615 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1617 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1620 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1622 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1624 case LDAP_SYNC_DELETE:
1625 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1627 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1629 case LDAP_SYNC_MODIFY:
1630 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1632 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1635 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1637 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1640 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1641 if ( !si->si_refreshPresent ) {
1642 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1643 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1644 syncuuid_cmp, avl_dup_error );
1648 if ( syncstate == LDAP_SYNC_PRESENT ) {
1650 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1651 if ( entry == NULL ) {
1656 f.f_choice = LDAP_FILTER_EQUALITY;
1658 ava.aa_desc = slap_schema.si_ad_entryUUID;
1659 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1660 ava.aa_value = *syncUUID;
1661 op->ors_filter = &f;
1663 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1664 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1665 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1666 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1667 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1668 syncUUID->bv_val, syncUUID->bv_len );
1669 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1670 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1672 op->o_tag = LDAP_REQ_SEARCH;
1673 op->ors_scope = LDAP_SCOPE_SUBTREE;
1674 op->ors_deref = LDAP_DEREF_NEVER;
1676 /* get the entry for this UUID */
1677 op->o_req_dn = si->si_base;
1678 op->o_req_ndn = si->si_base;
1680 op->o_time = slap_get_time();
1681 op->ors_tlimit = SLAP_NO_LIMIT;
1684 op->ors_attrs = slap_anlist_all_attributes;
1685 op->ors_attrsonly = 0;
1687 /* set callback function */
1688 op->o_callback = &cb;
1689 cb.sc_response = dn_callback;
1690 cb.sc_private = &dni;
1691 dni.new_entry = entry;
1693 if ( limits_check( op, &rs_search ) == 0 ) {
1694 rc = be->be_search( op, &rs_search );
1695 Debug( LDAP_DEBUG_SYNC,
1696 "syncrepl_entry: %s (%d)\n",
1697 "be_search", rc, 0 );
1700 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1701 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1704 cb.sc_response = null_callback;
1707 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1708 Debug( LDAP_DEBUG_SYNC,
1709 "syncrepl_entry: %s\n",
1710 entry->e_name.bv_val, 0, 0 );
1712 Debug( LDAP_DEBUG_SYNC,
1713 "syncrepl_entry: %s\n",
1714 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1717 if ( syncstate != LDAP_SYNC_DELETE ) {
1718 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1721 /* add if missing */
1722 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1723 &syncUUID_strrep, syncUUID );
1725 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1726 /* replace only if necessary */
1727 if ( a->a_nvals != a->a_vals ) {
1728 ber_memfree( a->a_nvals[0].bv_val );
1729 ber_dupbv( &a->a_nvals[0], syncUUID );
1731 ber_memfree( a->a_vals[0].bv_val );
1732 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1736 switch ( syncstate ) {
1738 case LDAP_SYNC_MODIFY:
1740 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1742 op->o_csn = a->a_vals[0];
1745 if ( BER_BVISNULL( &dni.dn )) {
1747 op->o_req_dn = entry->e_name;
1748 op->o_req_ndn = entry->e_nname;
1749 op->o_tag = LDAP_REQ_ADD;
1752 rc = be->be_add( op, &rs_add );
1753 Debug( LDAP_DEBUG_SYNC,
1754 "syncrepl_entry: %s (%d)\n",
1756 switch ( rs_add.sr_err ) {
1758 be_entry_release_w( op, entry );
1763 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1764 * only if the suffix entry is not present */
1765 case LDAP_NO_SUCH_OBJECT:
1766 syncrepl_add_glue( op, entry );
1770 /* if an entry was added via syncrepl_add_glue(),
1771 * it likely has no entryUUID, so the previous
1772 * be_search() doesn't find it. In this case,
1773 * give syncrepl a chance to modify it. Also
1774 * allow for entries that were recreated with the
1775 * same DN but a different entryUUID.
1777 case LDAP_ALREADY_EXISTS:
1779 Operation op2 = *op;
1780 SlapReply rs2 = { 0 };
1781 slap_callback cb2 = { 0 };
1783 op2.o_tag = LDAP_REQ_SEARCH;
1784 op2.o_req_dn = entry->e_name;
1785 op2.o_req_ndn = entry->e_nname;
1786 op2.ors_scope = LDAP_SCOPE_BASE;
1787 op2.ors_deref = LDAP_DEREF_NEVER;
1788 op2.ors_attrs = slap_anlist_all_attributes;
1789 op2.ors_attrsonly = 0;
1790 op2.ors_limit = NULL;
1792 op2.ors_tlimit = SLAP_NO_LIMIT;
1794 f.f_choice = LDAP_FILTER_PRESENT;
1795 f.f_desc = slap_schema.si_ad_objectClass;
1796 op2.ors_filter = &f;
1797 op2.ors_filterstr = generic_filterstr;
1799 op2.o_callback = &cb2;
1800 cb2.sc_response = dn_callback;
1801 cb2.sc_private = &dni;
1803 be->be_search( &op2, &rs2 );
1811 Debug( LDAP_DEBUG_ANY,
1812 "syncrepl_entry : be_add failed (%d)\n",
1813 rs_add.sr_err, 0, 0 );
1820 op->o_req_dn = dni.dn;
1821 op->o_req_ndn = dni.ndn;
1822 if ( dni.renamed ) {
1823 struct berval noldp, newp, nnewp;
1825 op->o_tag = LDAP_REQ_MODRDN;
1826 dnRdn( &entry->e_name, &op->orr_newrdn );
1827 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1829 dnParent( &dni.ndn, &noldp );
1830 dnParent( &entry->e_nname, &nnewp );
1831 if ( !dn_match( &noldp, &newp )) {
1832 dnParent( &entry->e_name, &newp );
1833 op->orr_newSup = &newp;
1834 op->orr_nnewSup = &nnewp;
1836 op->orr_deleteoldrdn = 0;
1837 rc = be->be_modrdn( op, &rs_modify );
1838 Debug( LDAP_DEBUG_SYNC,
1839 "syncrepl_entry: %s (%d)\n",
1840 "be_modrdn", rc, 0 );
1841 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1842 op->o_req_dn = entry->e_name;
1843 op->o_req_ndn = entry->e_nname;
1849 if ( dni.wasChanged ) {
1850 Modifications *mod, *modhead = NULL;
1851 Modifications *modtail = NULL;
1854 op->o_tag = LDAP_REQ_MODIFY;
1856 assert( *modlist != NULL );
1858 /* Delete all the old attrs */
1859 for ( i = 0; i < dni.attrs; i++ ) {
1860 mod = ch_malloc( sizeof( Modifications ) );
1861 mod->sml_op = LDAP_MOD_DELETE;
1863 mod->sml_desc = dni.ads[i];
1864 mod->sml_type = mod->sml_desc->ad_cname;
1865 mod->sml_values = NULL;
1866 mod->sml_nvalues = NULL;
1867 if ( !modhead ) modhead = mod;
1869 modtail->sml_next = mod;
1874 /* Append passed in list to ours */
1876 modtail->sml_next = *modlist;
1882 /* Find end of this list */
1883 for ( ; mod != NULL; mod = mod->sml_next ) {
1887 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1888 mod->sml_op = LDAP_MOD_REPLACE;
1890 mod->sml_desc = slap_schema.si_ad_entryUUID;
1891 mod->sml_type = mod->sml_desc->ad_cname;
1892 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1893 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1894 ber_dupbv( &uuid_bv, syncUUID );
1895 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1896 modtail->sml_next = mod;
1898 op->o_tag = LDAP_REQ_MODIFY;
1899 op->orm_modlist = *modlist;
1901 rc = be->be_modify( op, &rs_modify );
1902 Debug( LDAP_DEBUG_SYNC,
1903 "syncrepl_entry: %s (%d)\n",
1904 "be_modify", rc, 0 );
1905 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1906 Debug( LDAP_DEBUG_ANY,
1907 "syncrepl_entry : be_modify failed (%d)\n",
1908 rs_modify.sr_err, 0, 0 );
1913 case LDAP_SYNC_DELETE :
1914 if ( !BER_BVISNULL( &dni.dn )) {
1915 op->o_req_dn = dni.dn;
1916 op->o_req_ndn = dni.ndn;
1917 op->o_tag = LDAP_REQ_DELETE;
1918 rc = be->be_delete( op, &rs_delete );
1919 Debug( LDAP_DEBUG_SYNC,
1920 "syncrepl_entry: %s (%d)\n",
1921 "be_delete", rc, 0 );
1923 while ( rs_delete.sr_err == LDAP_SUCCESS
1924 && op->o_delete_glue_parent ) {
1925 op->o_delete_glue_parent = 0;
1926 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1927 slap_callback cb = { NULL };
1928 cb.sc_response = slap_null_cb;
1929 dnParent( &op->o_req_ndn, &pdn );
1931 op->o_req_ndn = pdn;
1932 op->o_callback = &cb;
1933 op->o_bd->be_delete( op, &rs_delete );
1943 Debug( LDAP_DEBUG_ANY,
1944 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1950 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1951 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1952 BER_BVZERO( &syncUUID_strrep );
1955 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1957 if ( !BER_BVISNULL( &dni.ndn ) ) {
1958 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1960 if ( !BER_BVISNULL( &dni.dn ) ) {
1961 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1963 BER_BVZERO( &op->o_csn );
1967 static struct berval gcbva[] = {
1973 #define NP_DELETE_ONE 2
1976 syncrepl_del_nonpresent(
1981 Backend* be = op->o_bd;
1982 slap_callback cb = { NULL };
1983 SlapReply rs_search = {REP_RESULT};
1984 SlapReply rs_delete = {REP_RESULT};
1985 SlapReply rs_modify = {REP_RESULT};
1986 struct nonpresent_entry *np_list, *np_prev;
1988 AttributeName an[2];
1990 struct berval pdn = BER_BVNULL;
1992 op->o_req_dn = si->si_base;
1993 op->o_req_ndn = si->si_base;
1995 cb.sc_response = nonpresent_callback;
1998 op->o_callback = &cb;
1999 op->o_tag = LDAP_REQ_SEARCH;
2000 op->ors_scope = si->si_scope;
2001 op->ors_deref = LDAP_DEREF_NEVER;
2002 op->o_time = slap_get_time();
2003 op->ors_tlimit = SLAP_NO_LIMIT;
2008 #ifdef LDAP_COMP_MATCH
2009 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2011 AttributeAssertion eq = { NULL, BER_BVNULL };
2015 op->ors_attrsonly = 1;
2016 op->ors_attrs = slap_anlist_no_attrs;
2017 op->ors_limit = NULL;
2018 op->ors_filter = &uf;
2021 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2023 uf.f_choice = LDAP_FILTER_EQUALITY;
2024 si->si_refreshDelete |= NP_DELETE_ONE;
2026 for (i=0; uuids[i].bv_val; i++) {
2028 uf.f_av_value = uuids[i];
2029 rc = be->be_search( op, &rs_search );
2031 si->si_refreshDelete ^= NP_DELETE_ONE;
2033 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2034 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2035 an[0].an_desc = slap_schema.si_ad_entryUUID;
2037 op->ors_slimit = SLAP_NO_LIMIT;
2038 op->ors_attrsonly = 0;
2039 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2040 op->ors_filterstr = si->si_filterstr;
2041 op->o_nocaching = 1;
2043 if ( limits_check( op, &rs_search ) == 0 ) {
2044 rc = be->be_search( op, &rs_search );
2046 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2049 op->o_nocaching = 0;
2051 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2053 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2055 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2056 while ( np_list != NULL ) {
2057 LDAP_LIST_REMOVE( np_list, npe_link );
2059 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2060 op->o_tag = LDAP_REQ_DELETE;
2061 op->o_callback = &cb;
2062 cb.sc_response = null_callback;
2064 op->o_req_dn = *np_prev->npe_name;
2065 op->o_req_ndn = *np_prev->npe_nname;
2066 rc = op->o_bd->be_delete( op, &rs_delete );
2068 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2069 Modifications mod1, mod2;
2070 mod1.sml_op = LDAP_MOD_REPLACE;
2072 mod1.sml_desc = slap_schema.si_ad_objectClass;
2073 mod1.sml_type = mod1.sml_desc->ad_cname;
2074 mod1.sml_values = &gcbva[0];
2075 mod1.sml_nvalues = NULL;
2076 mod1.sml_next = &mod2;
2078 mod2.sml_op = LDAP_MOD_REPLACE;
2080 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2081 mod2.sml_type = mod2.sml_desc->ad_cname;
2082 mod2.sml_values = &gcbva[1];
2083 mod2.sml_nvalues = NULL;
2084 mod2.sml_next = NULL;
2086 op->o_tag = LDAP_REQ_MODIFY;
2087 op->orm_modlist = &mod1;
2089 rc = be->be_modify( op, &rs_modify );
2092 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2093 op->o_delete_glue_parent ) {
2094 op->o_delete_glue_parent = 0;
2095 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2096 slap_callback cb = { NULL };
2097 cb.sc_response = slap_null_cb;
2098 dnParent( &op->o_req_ndn, &pdn );
2100 op->o_req_ndn = pdn;
2101 op->o_callback = &cb;
2102 /* give it a root privil ? */
2103 op->o_bd->be_delete( op, &rs_delete );
2109 op->o_delete_glue_parent = 0;
2111 ber_bvfree( np_prev->npe_name );
2112 ber_bvfree( np_prev->npe_nname );
2116 slap_graduate_commit_csn( op );
2127 Backend *be = op->o_bd;
2128 slap_callback cb = { NULL };
2133 struct berval dn = {0, NULL};
2134 struct berval ndn = {0, NULL};
2136 SlapReply rs_add = {REP_RESULT};
2139 op->o_tag = LDAP_REQ_ADD;
2140 op->o_callback = &cb;
2141 cb.sc_response = null_callback;
2142 cb.sc_private = NULL;
2147 /* count RDNs in suffix */
2148 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2149 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2159 /* Start with BE suffix */
2160 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2161 comma = strrchr( dn.bv_val, ',' );
2162 if ( ptr ) *ptr = ',';
2163 if ( comma ) *comma = '\0';
2168 dn.bv_len -= ptr - dn.bv_val;
2171 /* the normalizedDNs are always the same length, no counting
2174 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2175 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2176 ndn.bv_len = be->be_nsuffix[0].bv_len;
2179 while ( ndn.bv_val > e->e_nname.bv_val ) {
2180 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2181 ber_dupbv( &glue->e_name, &dn );
2182 ber_dupbv( &glue->e_nname, &ndn );
2184 a = ch_calloc( 1, sizeof( Attribute ));
2185 a->a_desc = slap_schema.si_ad_objectClass;
2187 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2188 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2189 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2190 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2192 a->a_nvals = a->a_vals;
2194 a->a_next = glue->e_attrs;
2197 a = ch_calloc( 1, sizeof( Attribute ));
2198 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2200 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2201 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2202 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2204 a->a_nvals = a->a_vals;
2206 a->a_next = glue->e_attrs;
2209 op->o_req_dn = glue->e_name;
2210 op->o_req_ndn = glue->e_nname;
2212 rc = be->be_add ( op, &rs_add );
2213 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2214 be_entry_release_w( op, glue );
2216 /* incl. ALREADY EXIST */
2220 /* Move to next child */
2221 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2224 if ( ptr == e->e_name.bv_val ) break;
2226 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2227 for( ptr = ndn.bv_val-2;
2228 ptr > e->e_nname.bv_val && *ptr != ',';
2234 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2237 op->o_req_dn = e->e_name;
2238 op->o_req_ndn = e->e_nname;
2240 rc = be->be_add ( op, &rs_add );
2241 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2242 be_entry_release_w( op, e );
2251 syncrepl_updateCookie(
2255 struct sync_cookie *syncCookie )
2257 Backend *be = op->o_bd;
2258 Modifications mod = { { 0 } };
2259 struct berval vals[ 2 ];
2263 slap_callback cb = { NULL };
2264 SlapReply rs_modify = {REP_RESULT};
2266 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2267 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2269 mod.sml_op = LDAP_MOD_REPLACE;
2270 mod.sml_desc = slap_schema.si_ad_contextCSN;
2271 mod.sml_type = mod.sml_desc->ad_cname;
2272 mod.sml_values = vals;
2273 vals[0] = si->si_syncCookie.ctxcsn;
2274 vals[1].bv_val = NULL;
2277 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2279 op->o_tag = LDAP_REQ_MODIFY;
2281 assert( si->si_rid < 1000 );
2283 cb.sc_response = null_callback;
2286 op->o_callback = &cb;
2287 op->o_req_dn = op->o_bd->be_suffix[0];
2288 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2290 /* update contextCSN */
2291 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2292 op->orm_modlist = &mod;
2293 rc = be->be_modify( op, &rs_modify );
2296 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2297 Debug( LDAP_DEBUG_ANY,
2298 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2301 slap_graduate_commit_csn( op );
2311 dninfo *dni = op->o_callback->sc_private;
2313 if ( rs->sr_type == REP_SEARCH ) {
2314 if ( !BER_BVISNULL( &dni->dn ) ) {
2315 Debug( LDAP_DEBUG_ANY,
2316 "dn_callback : consistency error - "
2317 "entryUUID is not unique\n", 0, 0, 0 );
2319 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2320 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2321 /* If there is a new entry, see if it differs from the old.
2322 * We compare the non-normalized values so that cosmetic changes
2323 * in the provider are always propagated.
2325 if ( dni->new_entry ) {
2326 Attribute *old, *new;
2329 /* Did the DN change? Note that we don't explicitly try to
2330 * discover if the deleteOldRdn argument applies here. It
2331 * would save an unnecessary Modify if we detected it, but
2332 * that's a fair amount of trouble to compare the two attr
2333 * lists in detail. (Just test normalized DN; we ignore
2334 * insignificant changes here.)
2336 if ( !dn_match( &rs->sr_entry->e_nname,
2337 &dni->new_entry->e_nname ) )
2342 for ( i = 0, old = rs->sr_entry->e_attrs;
2344 i++, old = old->a_next )
2349 /* We assume that attributes are saved in the same order
2350 * in the remote and local databases. So if we walk through
2351 * the attributeDescriptions one by one they should match in
2352 * lock step. If not, we signal a change. Otherwise we test
2355 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2357 old = old->a_next, new = new->a_next )
2359 if ( old->a_desc != new->a_desc ) {
2360 dni->wasChanged = 1;
2363 for ( i = 0; ; i++ ) {
2365 nold = BER_BVISNULL( &old->a_vals[i] );
2366 nnew = BER_BVISNULL( &new->a_vals[i] );
2367 /* If both are empty, stop looking */
2368 if ( nold && nnew ) {
2371 /* If they are different, stop looking */
2372 if ( nold != nnew ) {
2373 dni->wasChanged = 1;
2376 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2377 dni->wasChanged = 1;
2381 if ( dni->wasChanged ) break;
2383 if ( dni->wasChanged ) {
2384 dni->ads = op->o_tmpalloc( dni->attrs *
2385 sizeof(AttributeDescription *), op->o_tmpmemctx );
2387 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2388 dni->ads[i] = old->a_desc;
2394 } else if ( rs->sr_type == REP_RESULT ) {
2395 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2396 Debug( LDAP_DEBUG_ANY,
2397 "dn_callback : consistency error - "
2398 "entryUUID is not unique\n", 0, 0, 0 );
2402 return LDAP_SUCCESS;
2406 nonpresent_callback(
2410 syncinfo_t *si = op->o_callback->sc_private;
2413 struct berval* present_uuid = NULL;
2414 struct nonpresent_entry *np_entry;
2416 if ( rs->sr_type == REP_RESULT ) {
2417 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2418 si->si_presentlist = NULL;
2420 } else if ( rs->sr_type == REP_SEARCH ) {
2421 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2422 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2424 if ( a == NULL ) return 0;
2426 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2430 if ( present_uuid == NULL ) {
2431 np_entry = (struct nonpresent_entry *)
2432 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2433 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2434 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2435 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2438 avl_delete( &si->si_presentlist,
2439 &a->a_nvals[0], syncuuid_cmp );
2440 ch_free( present_uuid->bv_val );
2441 ch_free( present_uuid );
2444 return LDAP_SUCCESS;
2452 if ( rs->sr_err != LDAP_SUCCESS &&
2453 rs->sr_err != LDAP_REFERRAL &&
2454 rs->sr_err != LDAP_ALREADY_EXISTS &&
2455 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2456 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2458 Debug( LDAP_DEBUG_ANY,
2459 "null_callback : error code 0x%x\n",
2462 return LDAP_SUCCESS;
2465 static struct berval *
2466 slap_uuidstr_from_normalized(
2467 struct berval* uuidstr,
2468 struct berval* normalized,
2472 unsigned char nibble;
2475 if ( normalized == NULL ) return NULL;
2476 if ( normalized->bv_len != 16 ) return NULL;
2481 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2482 if ( new == NULL ) {
2489 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2490 if ( new != uuidstr ) {
2491 slap_sl_free( new, ctx );
2496 for ( i = 0; i < 16; i++ ) {
2497 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2498 new->bv_val[(i<<1)+d] = '-';
2502 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2503 if ( nibble < 10 ) {
2504 new->bv_val[(i<<1)+d] = nibble + '0';
2506 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2509 nibble = (normalized->bv_val[i]) & 0xF;
2510 if ( nibble < 10 ) {
2511 new->bv_val[(i<<1)+d+1] = nibble + '0';
2513 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2517 new->bv_val[new->bv_len] = '\0';
2522 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2524 const struct berval *uuid1 = v_uuid1;
2525 const struct berval *uuid2 = v_uuid2;
2526 int rc = uuid1->bv_len - uuid2->bv_len;
2527 if ( rc ) return rc;
2528 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2532 avl_ber_bvfree( void *v_bv )
2534 struct berval *bv = (struct berval *)v_bv;
2536 if( v_bv == NULL ) return;
2537 if ( !BER_BVISNULL( bv ) ) {
2538 ch_free( bv->bv_val );
2540 ch_free( (char *) bv );
2544 syncinfo_free( syncinfo_t *sie )
2546 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2547 if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2548 ch_free( sie->si_provideruri.bv_val );
2551 bindconf_free( &sie->si_bindconf );
2553 if ( sie->si_filterstr.bv_val ) {
2554 ch_free( sie->si_filterstr.bv_val );
2556 if ( sie->si_base.bv_val ) {
2557 ch_free( sie->si_base.bv_val );
2559 if ( sie->si_attrs ) {
2561 while ( sie->si_attrs[i] != NULL ) {
2562 ch_free( sie->si_attrs[i] );
2565 ch_free( sie->si_attrs );
2567 if ( sie->si_exattrs ) {
2569 while ( sie->si_exattrs[i] != NULL ) {
2570 ch_free( sie->si_exattrs[i] );
2573 ch_free( sie->si_exattrs );
2575 if ( sie->si_anlist ) {
2577 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2578 ch_free( sie->si_anlist[i].an_name.bv_val );
2581 ch_free( sie->si_anlist );
2583 if ( sie->si_exanlist ) {
2585 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2586 ch_free( sie->si_exanlist[i].an_name.bv_val );
2589 ch_free( sie->si_exanlist );
2591 if ( sie->si_retryinterval ) {
2592 ch_free( sie->si_retryinterval );
2594 if ( sie->si_retrynum ) {
2595 ch_free( sie->si_retrynum );
2597 if ( sie->si_retrynum_init ) {
2598 ch_free( sie->si_retrynum_init );
2600 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2601 if ( sie->si_presentlist ) {
2602 avl_free( sie->si_presentlist, avl_ber_bvfree );
2605 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2607 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2608 struct nonpresent_entry* npe;
2609 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2610 LDAP_LIST_REMOVE( npe, npe_link );
2611 if ( npe->npe_name ) {
2612 if ( npe->npe_name->bv_val ) {
2613 ch_free( npe->npe_name->bv_val );
2615 ch_free( npe->npe_name );
2617 if ( npe->npe_nname ) {
2618 if ( npe->npe_nname->bv_val ) {
2619 ch_free( npe->npe_nname->bv_val );
2621 ch_free( npe->npe_nname );
2630 /* NOTE: used & documented in slapd.conf(5) */
2632 #define PROVIDERSTR "provider"
2633 #define SCHEMASTR "schemachecking"
2634 #define FILTERSTR "filter"
2635 #define SEARCHBASESTR "searchbase"
2636 #define SCOPESTR "scope"
2637 #define ATTRSONLYSTR "attrsonly"
2638 #define ATTRSSTR "attrs"
2639 #define TYPESTR "type"
2640 #define INTERVALSTR "interval"
2641 #define RETRYSTR "retry"
2642 #define SLIMITSTR "sizelimit"
2643 #define TLIMITSTR "timelimit"
2644 #define SYNCDATASTR "syncdata"
2646 /* FIXME: undocumented */
2647 #define LOGBASESTR "logbase"
2648 #define LOGFILTERSTR "logfilter"
2649 #define OLDAUTHCSTR "bindprincipal"
2650 #define EXATTRSSTR "exattrs"
2651 #define MANAGEDSAITSTR "manageDSAit"
2654 #define LASTMODSTR "lastmod"
2655 #define LMGENSTR "gen"
2656 #define LMNOSTR "no"
2657 #define LMREQSTR "req"
2658 #define SRVTABSTR "srvtab"
2659 #define SUFFIXSTR "suffix"
2662 #define GOT_ID 0x0001
2663 #define GOT_PROVIDER 0x0002
2666 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2672 { BER_BVC("base"), LDAP_SCOPE_BASE },
2673 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2674 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2675 #ifdef LDAP_SCOPE_SUBORDINATE
2676 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2677 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2679 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2680 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2684 static slap_verbmasks datamodes[] = {
2685 { BER_BVC("default"), SYNCDATA_DEFAULT },
2686 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2687 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2692 parse_syncrepl_line(
2702 for ( i = 1; i < cargc; i++ ) {
2703 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2704 STRLENOF( IDSTR "=" ) ) )
2707 /* '\0' string terminator accounts for '=' */
2708 val = cargv[ i ] + STRLENOF( IDSTR "=" );
2710 if ( tmp >= 1000 || tmp < 0 ) {
2711 fprintf( stderr, "Error: parse_syncrepl_line: "
2712 "syncrepl id %d is out of range [0..999]\n", tmp );
2717 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2718 STRLENOF( PROVIDERSTR "=" ) ) )
2720 val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2721 ber_str2bv( val, 0, 1, &si->si_provideruri );
2722 gots |= GOT_PROVIDER;
2723 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2724 STRLENOF( SCHEMASTR "=" ) ) )
2726 val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2727 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2728 si->si_schemachecking = 1;
2729 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2730 si->si_schemachecking = 0;
2732 si->si_schemachecking = 1;
2734 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2735 STRLENOF( FILTERSTR "=" ) ) )
2737 val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2738 if ( si->si_filterstr.bv_val )
2739 ch_free( si->si_filterstr.bv_val );
2740 ber_str2bv( val, 0, 1, &si->si_filterstr );
2741 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2742 STRLENOF( LOGFILTERSTR "=" ) ) )
2744 val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2745 if ( si->si_logfilterstr.bv_val )
2746 ch_free( si->si_logfilterstr.bv_val );
2747 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2748 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2749 STRLENOF( SEARCHBASESTR "=" ) ) )
2754 val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2755 if ( si->si_base.bv_val ) {
2756 ch_free( si->si_base.bv_val );
2758 ber_str2bv( val, 0, 0, &bv );
2759 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2760 if ( rc != LDAP_SUCCESS ) {
2761 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2762 val, rc, ldap_err2string( rc ) );
2765 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2766 STRLENOF( LOGBASESTR "=" ) ) )
2771 val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2772 if ( si->si_logbase.bv_val ) {
2773 ch_free( si->si_logbase.bv_val );
2775 ber_str2bv( val, 0, 0, &bv );
2776 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2777 if ( rc != LDAP_SUCCESS ) {
2778 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2779 val, rc, ldap_err2string( rc ) );
2782 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2783 STRLENOF( SCOPESTR "=" ) ) )
2786 val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2787 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2788 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2789 si->si_scope = scopes[j].val;
2793 if ( BER_BVISNULL(&scopes[j].key) ) {
2794 fprintf( stderr, "Error: parse_syncrepl_line: "
2795 "unknown scope \"%s\"\n", val);
2798 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2799 STRLENOF( ATTRSONLYSTR "=" ) ) )
2801 si->si_attrsonly = 1;
2802 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2803 STRLENOF( ATTRSSTR "=" ) ) )
2805 val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2806 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2808 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2809 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2810 if ( si->si_anlist == NULL ) {
2811 ch_free( attr_fname );
2814 si->si_anfile = attr_fname;
2816 char *str, *s, *next;
2817 char delimstr[] = " ,\t";
2818 str = ch_strdup( val );
2819 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2821 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2823 if ( strlen(s) == 1 && *s == '*' ) {
2824 si->si_allattrs = 1;
2825 *(val + ( s - str )) = delimstr[0];
2827 if ( strlen(s) == 1 && *s == '+' ) {
2828 si->si_allopattrs = 1;
2829 *(val + ( s - str )) = delimstr[0];
2833 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2834 if ( si->si_anlist == NULL ) {
2838 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2839 STRLENOF( EXATTRSSTR "=" ) ) )
2841 val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2842 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2844 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2845 si->si_exanlist = file2anlist(
2846 si->si_exanlist, attr_fname, " ,\t" );
2847 if ( si->si_exanlist == NULL ) {
2848 ch_free( attr_fname );
2851 ch_free( attr_fname );
2853 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2854 if ( si->si_exanlist == NULL ) {
2858 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2859 STRLENOF( TYPESTR "=" ) ) )
2861 val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2862 if ( !strncasecmp( val, "refreshOnly",
2863 STRLENOF("refreshOnly") ))
2865 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2866 } else if ( !strncasecmp( val, "refreshAndPersist",
2867 STRLENOF("refreshAndPersist") ))
2869 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2870 si->si_interval = 60;
2872 fprintf( stderr, "Error: parse_syncrepl_line: "
2873 "unknown sync type \"%s\"\n", val);
2876 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2877 STRLENOF( INTERVALSTR "=" ) ) )
2879 val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2880 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2881 si->si_interval = 0;
2889 hstr = strchr( dstr, ':' );
2890 if ( hstr == NULL ) {
2891 fprintf( stderr, "Error: parse_syncrepl_line: "
2892 "invalid interval \"%s\"\n", val );
2896 mstr = strchr( hstr, ':' );
2897 if ( mstr == NULL ) {
2898 fprintf( stderr, "Error: parse_syncrepl_line: "
2899 "invalid interval \"%s\"\n", val );
2903 sstr = strchr( mstr, ':' );
2904 if ( sstr == NULL ) {
2905 fprintf( stderr, "Error: parse_syncrepl_line: "
2906 "invalid interval \"%s\"\n", val );
2915 if (( hh > 24 ) || ( hh < 0 ) ||
2916 ( mm > 60 ) || ( mm < 0 ) ||
2917 ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2918 fprintf( stderr, "Error: parse_syncrepl_line: "
2919 "invalid interval \"%s\"\n", val );
2922 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2924 if ( si->si_interval < 0 ) {
2925 fprintf( stderr, "Error: parse_syncrepl_line: "
2926 "invalid interval \"%ld\"\n",
2927 (long) si->si_interval);
2930 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2931 STRLENOF( RETRYSTR "=" ) ) )
2936 val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2937 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2938 retry_list[0] = NULL;
2940 slap_str2clist( &retry_list, val, " ,\t" );
2942 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2946 "Error: incomplete syncrepl retry list\n" );
2947 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2948 ch_free( retry_list[k] );
2950 ch_free( retry_list );
2951 exit( EXIT_FAILURE );
2953 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2954 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2955 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2956 for ( j = 0; j < n; j++ ) {
2957 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2958 if ( *retry_list[j*2+1] == '+' ) {
2959 si->si_retrynum_init[j] = -1;
2960 si->si_retrynum[j] = -1;
2964 si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2965 si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2968 si->si_retrynum_init[j] = -2;
2969 si->si_retrynum[j] = -2;
2970 si->si_retryinterval[j] = 0;
2972 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2973 ch_free( retry_list[k] );
2975 ch_free( retry_list );
2976 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2977 STRLENOF( MANAGEDSAITSTR "=" ) ) )
2979 val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2980 si->si_manageDSAit = atoi( val );
2981 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2982 STRLENOF( SLIMITSTR "=") ) )
2984 val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2985 si->si_slimit = atoi( val );
2986 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2987 STRLENOF( TLIMITSTR "=" ) ) )
2989 val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2990 si->si_tlimit = atoi( val );
2991 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2992 STRLENOF( SYNCDATASTR "=" ) ) )
2994 val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2995 si->si_syncdata = verb_to_mask( val, datamodes );
2996 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2997 fprintf( stderr, "Error: parse_syncrepl_line: "
2998 "unknown keyword \"%s\"\n", cargv[ i ] );
3003 if ( gots != GOT_ALL ) {
3005 "Error: Malformed \"syncrepl\" line in slapd config file" );
3022 if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
3023 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
3024 "required for syncrepl\n", be->be_type, 0, 0 );
3027 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3030 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3034 si->si_bindconf.sb_tls = SB_TLS_OFF;
3035 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3036 si->si_schemachecking = 0;
3037 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3038 &si->si_filterstr );
3039 si->si_base.bv_val = NULL;
3040 si->si_scope = LDAP_SCOPE_SUBTREE;
3041 si->si_attrsonly = 0;
3042 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3043 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3044 si->si_attrs = NULL;
3045 si->si_allattrs = 0;
3046 si->si_allopattrs = 0;
3047 si->si_exattrs = NULL;
3048 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3049 si->si_interval = 86400;
3050 si->si_retryinterval = NULL;
3051 si->si_retrynum_init = NULL;
3052 si->si_retrynum = NULL;
3053 si->si_manageDSAit = 0;
3056 si->si_conn_setup = 0;
3058 si->si_presentlist = NULL;
3059 LDAP_LIST_INIT( &si->si_nonpresentlist );
3060 ldap_pvt_thread_mutex_init( &si->si_mutex );
3062 rc = parse_syncrepl_line( cargv, cargc, si );
3066 init_syncrepl( si );
3067 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3068 do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3073 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3074 syncinfo_free( si );
3077 Debug( LDAP_DEBUG_CONFIG,
3078 "Config: ** successfully added syncrepl \"%s\"\n",
3079 BER_BVISNULL( &si->si_provideruri ) ?
3080 "(null)" : si->si_provideruri.bv_val, 0, 0 );
3081 if ( !si->si_schemachecking ) {
3082 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3084 be->be_syncinfo = si;
3090 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3093 char buf[BUFSIZ*2], *ptr;
3096 bindconf_unparse( &si->si_bindconf, &bc );
3098 ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3099 si->si_rid, si->si_provideruri.bv_val );
3100 if ( !BER_BVISNULL( &bc )) {
3101 ptr = lutil_strcopy( ptr, bc.bv_val );
3104 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3105 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3106 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3109 if ( !BER_BVISNULL( &si->si_base )) {
3110 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3111 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3114 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3115 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3116 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3119 if ( !BER_BVISNULL( &si->si_logbase )) {
3120 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3121 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3124 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3125 if ( si->si_scope == scopes[i].val ) {
3126 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3127 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3131 if ( si->si_attrsonly ) {
3132 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3134 if ( si->si_anfile ) {
3135 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3136 ptr = lutil_strcopy( ptr, si->si_anfile );
3137 } else if ( si->si_allattrs || si->si_allopattrs ||
3138 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3140 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3142 ptr = anlist_unparse( si->si_anlist, ptr );
3143 if ( si->si_allattrs ) {
3144 if ( old != ptr ) *ptr++ = ',';
3147 if ( si->si_allopattrs ) {
3148 if ( old != ptr ) *ptr++ = ',';
3153 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3154 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3155 ptr = anlist_unparse( si->si_exanlist, ptr );
3157 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3158 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3160 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3161 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3162 "refreshAndPersist" : "refreshOnly" );
3164 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3167 dd = si->si_interval;
3174 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3175 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3176 } else if ( si->si_retryinterval ) {
3178 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3179 for (i=0; si->si_retryinterval[i]; i++) {
3180 if ( space ) *ptr++ = ' ';
3182 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3183 if ( si->si_retrynum_init[i] == -1 )
3186 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3191 if ( si->si_slimit ) {
3192 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3193 ptr += sprintf( ptr, "%d", si->si_slimit );
3196 if ( si->si_tlimit ) {
3197 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3198 ptr += sprintf( ptr, "%d", si->si_tlimit );
3201 if ( si->si_syncdata ) {
3202 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3203 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3204 ptr = lutil_strcopy( ptr, bc.bv_val );
3207 bc.bv_len = ptr - buf;
3209 ber_dupbv( bv, &bc );
3213 syncrepl_config(ConfigArgs *c) {
3214 if (c->op == SLAP_CONFIG_EMIT) {
3215 if ( c->be->be_syncinfo ) {
3217 syncrepl_unparse( c->be->be_syncinfo, &bv );
3218 ber_bvarray_add( &c->rvalue_vals, &bv );
3222 } else if ( c->op == LDAP_MOD_DELETE ) {
3225 if ( c->be->be_syncinfo ) {
3226 re = c->be->be_syncinfo->si_re;
3228 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3229 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3230 ldap_pvt_runqueue_remove( &slapd_rq, re );
3232 syncinfo_free( c->be->be_syncinfo );
3233 c->be->be_syncinfo = NULL;
3235 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3238 if(SLAP_SHADOW(c->be)) {
3239 Debug(LDAP_DEBUG_ANY, "%s: "
3240 "syncrepl: database already shadowed.\n",
3243 } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3246 SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);