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-2006 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 #define RETRYNUM_FOREVER (-1) /* retry forever */
48 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
49 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
50 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
52 typedef struct syncinfo_s {
53 struct slap_backend_db *si_be;
56 slap_bindconf si_bindconf;
57 struct berval si_base;
58 struct berval si_logbase;
59 struct berval si_filterstr;
60 struct berval si_logfilterstr;
64 AttributeName *si_anlist;
65 AttributeName *si_exanlist;
70 int si_schemachecking;
71 int si_type; /* the active type */
72 int si_ctype; /* the configured type */
74 time_t *si_retryinterval;
75 int *si_retrynum_init;
77 struct sync_cookie si_syncCookie;
82 int si_refreshPresent;
86 Avlnode *si_presentlist;
88 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
89 ldap_pvt_thread_mutex_t si_mutex;
92 static int syncuuid_cmp( const void *, const void * );
93 static void avl_ber_bvfree( void * );
94 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
95 static int syncrepl_message_to_op(
96 syncinfo_t *, Operation *, LDAPMessage * );
97 static int syncrepl_message_to_entry(
98 syncinfo_t *, Operation *, LDAPMessage *,
99 Modifications **, Entry **, int );
100 static int syncrepl_entry(
101 syncinfo_t *, Operation*, Entry*,
102 Modifications**,int, struct berval*,
103 struct sync_cookie *,
105 static int syncrepl_updateCookie(
106 syncinfo_t *, Operation *, struct berval *,
107 struct sync_cookie * );
108 static struct berval * slap_uuidstr_from_normalized(
109 struct berval *, struct berval *, void * );
111 /* callback functions */
112 static int dn_callback( struct slap_op *, struct slap_rep * );
113 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
114 static int null_callback( struct slap_op *, struct slap_rep * );
116 static AttributeDescription *sync_descs[4];
119 init_syncrepl(syncinfo_t *si)
122 char **attrs, **exattrs;
124 if ( !sync_descs[0] ) {
125 sync_descs[0] = slap_schema.si_ad_objectClass;
126 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
127 sync_descs[2] = slap_schema.si_ad_entryCSN;
128 sync_descs[3] = NULL;
131 if ( si->si_allattrs && si->si_allopattrs )
134 attrs = anlist2attrs( si->si_anlist );
137 if ( si->si_allattrs ) {
140 if ( !is_at_operational( at_find( attrs[i] ))) {
141 for ( j = i; attrs[j] != NULL; j++ ) {
144 attrs[j] = attrs[j+1];
150 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
151 attrs[i] = ch_strdup("*");
154 } else if ( si->si_allopattrs ) {
157 if ( is_at_operational( at_find( attrs[i] ))) {
158 for ( j = i; attrs[j] != NULL; j++ ) {
161 attrs[j] = attrs[j+1];
167 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
168 attrs[i] = ch_strdup("+");
172 for ( i = 0; sync_descs[i] != NULL; i++ ) {
175 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
176 for ( k = j; attrs[k] != NULL; k++ ) {
179 attrs[k] = attrs[k+1];
187 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
189 if ( si->si_allopattrs ) {
190 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
192 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
195 if ( attrs == NULL ) {
196 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
200 if ( si->si_allopattrs ) {
201 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
203 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
204 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
212 if ( si->si_allattrs == si->si_allopattrs ) {
213 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
214 attrs[i++] = ch_strdup( "*" );
215 attrs[i++] = ch_strdup( "+" );
216 } else if ( si->si_allattrs && !si->si_allopattrs ) {
217 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
218 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
219 attrs[i++] = ch_strdup( "*" );
220 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
221 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
223 } else if ( !si->si_allattrs && si->si_allopattrs ) {
224 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
225 attrs[i++] = ch_strdup( "+" );
226 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
231 si->si_attrs = attrs;
233 exattrs = anlist2attrs( si->si_exanlist );
236 for ( n = 0; exattrs[n] != NULL; n++ ) ;
238 for ( i = 0; sync_descs[i] != NULL; i++ ) {
240 while ( exattrs[j] != NULL ) {
241 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
242 ch_free( exattrs[j] );
243 for ( k = j; exattrs[k] != NULL; k++ ) {
244 exattrs[k] = exattrs[k+1];
252 for ( i = 0; exattrs[i] != NULL; i++ ) {
253 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
255 if ( ( oc = si->si_anlist[j].an_oc ) ) {
257 while ( oc->soc_required[k] ) {
258 if ( !strcmp( exattrs[i],
259 oc->soc_required[k]->sat_cname.bv_val )) {
260 ch_free( exattrs[i] );
261 for ( l = i; exattrs[l]; l++ ) {
262 exattrs[l] = exattrs[l+1];
272 for ( i = 0; exattrs[i] != NULL; i++ ) ;
275 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
278 si->si_exattrs = exattrs;
281 typedef struct logschema {
283 struct berval ls_req;
284 struct berval ls_mod;
285 struct berval ls_newRdn;
286 struct berval ls_delRdn;
287 struct berval ls_newSup;
290 static logschema changelog_sc = {
292 BER_BVC("changeType"),
295 BER_BVC("deleteOldRDN"),
296 BER_BVC("newSuperior")
299 static logschema accesslog_sc = {
303 BER_BVC("reqNewRDN"),
304 BER_BVC("reqDeleteOldRDN"),
305 BER_BVC("reqNewSuperior")
313 BerElementBuffer berbuf;
314 BerElement *ber = (BerElement *)&berbuf;
315 LDAPControl c[2], *ctrls[3];
316 struct timeval timeout;
321 char **attrs, *lattrs[8];
326 /* setup LDAP SYNC control */
327 ber_init2( ber, NULL, LBER_USE_DER );
328 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
330 /* If we're using a log but we have no state, then fallback to
331 * normal mode for a full refresh.
333 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
334 si->si_logstate = SYNCLOG_FALLBACK;
336 /* Use the log parameters if we're in log mode */
337 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
339 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
343 lattrs[0] = ls->ls_dn.bv_val;
344 lattrs[1] = ls->ls_req.bv_val;
345 lattrs[2] = ls->ls_mod.bv_val;
346 lattrs[3] = ls->ls_newRdn.bv_val;
347 lattrs[4] = ls->ls_delRdn.bv_val;
348 lattrs[5] = ls->ls_newSup.bv_val;
349 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
353 base = si->si_logbase.bv_val;
354 filter = si->si_logfilterstr.bv_val;
357 scope = LDAP_SCOPE_SUBTREE;
360 base = si->si_base.bv_val;
361 filter = si->si_filterstr.bv_val;
362 attrs = si->si_attrs;
363 attrsonly = si->si_attrsonly;
364 scope = si->si_scope;
366 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
367 si->si_type = LDAP_SYNC_REFRESH_ONLY;
369 si->si_type = si->si_ctype;
372 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
374 ber_printf( ber, "{eOb}",
375 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
377 ber_printf( ber, "{eb}",
378 abs(si->si_type), rhint );
381 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
386 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
387 c[0].ldctl_iscritical = si->si_type < 0;
390 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
391 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
392 c[1].ldctl_value = si->si_bindconf.sb_authzId;
393 c[1].ldctl_iscritical = 1;
400 timeout.tv_sec = si->si_tlimit;
403 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
404 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
405 si->si_slimit, &msgid );
416 int cmdline_cookie_found = 0;
418 struct sync_cookie *sc = NULL;
424 psub = &si->si_be->be_nsuffix[0];
426 rc = slap_client_connect( &si->si_ld, &si->si_bindconf, LDAP_VERSION3 );
427 if ( rc != LDAP_SUCCESS ) {
430 op->o_protocol = LDAP_VERSION3;
432 /* Set SSF to strongest of TLS, SASL SSFs */
435 op->o_transport_ssf = 0;
437 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
438 == LDAP_SUCCESS && ssl != NULL )
440 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
442 #endif /* HAVE_TLS */
443 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
444 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
445 ? op->o_sasl_ssf : op->o_tls_ssf;
448 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
449 /* get contextCSN shadow replica from database */
450 BerVarray csn = NULL;
452 assert( si->si_rid < 1000 );
453 op->o_req_ndn = op->o_bd->be_nsuffix[0];
454 op->o_req_dn = op->o_req_ndn;
456 /* try to read stored contextCSN */
457 backend_attribute( op, NULL, &op->o_req_ndn,
458 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
460 ch_free( si->si_syncCookie.ctxcsn.bv_val );
461 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
462 ber_bvarray_free_x( csn, op->o_tmpmemctx );
465 si->si_syncCookie.rid = si->si_rid;
467 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
468 if ( si->si_rid == sc->rid ) {
469 cmdline_cookie_found = 1;
474 if ( cmdline_cookie_found ) {
475 /* cookie is supplied in the command line */
477 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
479 /* ctxcsn wasn't parsed yet, do it now */
480 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
481 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
482 /* if cmdline cookie does not have ctxcsn */
483 /* component, set it to an initial value */
484 slap_init_sync_cookie_ctxcsn( sc );
486 slap_sync_cookie_free( &si->si_syncCookie, 0 );
487 slap_dup_sync_cookie( &si->si_syncCookie, sc );
488 slap_sync_cookie_free( sc, 1 );
491 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
492 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
495 rc = ldap_sync_search( si, op->o_tmpmemctx );
497 if( rc != LDAP_SUCCESS ) {
498 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
499 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
505 ldap_unbind_ext( si->si_ld, NULL, NULL );
518 LDAPControl **rctrls = NULL;
521 BerElementBuffer berbuf;
522 BerElement *ber = (BerElement *)&berbuf;
524 LDAPMessage *res = NULL;
525 LDAPMessage *msg = NULL;
528 struct berval *retdata = NULL;
533 struct berval syncUUID = BER_BVNULL;
534 struct sync_cookie syncCookie = { BER_BVNULL };
535 struct sync_cookie syncCookie_req = { BER_BVNULL };
536 struct berval cookie = BER_BVNULL;
542 Modifications *modlist = NULL;
547 struct timeval *tout_p = NULL;
548 struct timeval tout = { 0, 0 };
550 int refreshDeletes = 0;
552 BerVarray syncUUIDs = NULL;
555 if ( slapd_shutdown ) {
560 ber_init2( ber, NULL, LBER_USE_DER );
561 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
563 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
565 psub = &si->si_be->be_nsuffix[0];
567 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
569 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
575 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
576 tout_p, &res )) > 0 )
578 if ( slapd_shutdown ) {
582 for( msg = ldap_first_message( si->si_ld, res );
584 msg = ldap_next_message( si->si_ld, msg ) )
586 if ( slapd_shutdown ) {
590 switch( ldap_msgtype( msg ) ) {
591 case LDAP_RES_SEARCH_ENTRY:
592 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
593 /* we can't work without the control */
595 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
596 "got search entry without "
597 "control\n", 0, 0, 0 );
602 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
603 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
604 /* FIXME: what if syncUUID is NULL or empty?
605 * (happens with back-sql...) */
606 if ( BER_BVISEMPTY( &syncUUID ) ) {
607 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
608 "got empty syncUUID\n", 0, 0, 0 );
609 ldap_controls_free( rctrls );
613 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
614 ber_scanf( ber, /*"{"*/ "m}", &cookie );
615 if ( !BER_BVISNULL( &cookie ) ) {
616 ch_free( syncCookie.octet_str.bv_val );
617 ber_dupbv( &syncCookie.octet_str, &cookie );
619 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
621 slap_parse_sync_cookie( &syncCookie, NULL );
625 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
627 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
628 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
629 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
631 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
632 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
633 if (( rc = syncrepl_entry( si, op, entry, &modlist,
634 syncstate, &syncUUID, &syncCookie_req,
635 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
636 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
637 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
640 ldap_controls_free( rctrls );
642 slap_mods_free( modlist, 1 );
648 case LDAP_RES_SEARCH_REFERENCE:
649 Debug( LDAP_DEBUG_ANY,
650 "do_syncrep2: reference received error\n", 0, 0, 0 );
653 case LDAP_RES_SEARCH_RESULT:
654 Debug( LDAP_DEBUG_SYNC,
655 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
656 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
658 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
659 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
660 /* map old result code to registered code */
661 err = LDAP_SYNC_REFRESH_REQUIRED;
664 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
665 if ( si->si_logstate == SYNCLOG_LOGGING ) {
666 si->si_logstate = SYNCLOG_FALLBACK;
673 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
675 ber_scanf( ber, "{" /*"}"*/);
676 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
677 ber_scanf( ber, "m", &cookie );
678 if ( !BER_BVISNULL( &cookie ) ) {
679 ch_free( syncCookie.octet_str.bv_val );
680 ber_dupbv( &syncCookie.octet_str, &cookie);
682 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
684 slap_parse_sync_cookie( &syncCookie, NULL );
687 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
689 ber_scanf( ber, "b", &refreshDeletes );
691 ber_scanf( ber, /*"{"*/ "}" );
693 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
695 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
698 value_match( &match, slap_schema.si_ad_entryCSN,
699 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
700 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
701 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
705 ldap_controls_free( rctrls );
707 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
708 /* FIXME : different error behaviors according to
709 * 1) err code : LDAP_BUSY ...
710 * 2) on err policy : stop service, stop sync, retry
712 if ( refreshDeletes == 0 && match < 0 &&
713 err == LDAP_SUCCESS )
715 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
717 avl_free( si->si_presentlist, avl_ber_bvfree );
718 si->si_presentlist = NULL;
721 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
722 match < 0 && err == LDAP_SUCCESS )
724 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
726 if ( err == LDAP_SUCCESS
727 && si->si_logstate == SYNCLOG_FALLBACK ) {
728 si->si_logstate = SYNCLOG_LOGGING;
729 rc = LDAP_SYNC_REFRESH_REQUIRED;
736 case LDAP_RES_INTERMEDIATE:
737 rc = ldap_parse_intermediate( si->si_ld, msg,
738 &retoid, &retdata, NULL, 0 );
739 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
740 ber_init2( ber, retdata, LBER_USE_DER );
742 switch ( si_tag = ber_peek_tag( ber, &len )) {
744 case LDAP_TAG_SYNC_NEW_COOKIE:
745 Debug( LDAP_DEBUG_SYNC,
746 "do_syncrep2: %s - %s%s\n",
747 "LDAP_RES_INTERMEDIATE",
748 "NEW_COOKIE", "\n" );
749 ber_scanf( ber, "tm", &tag, &cookie );
751 case LDAP_TAG_SYNC_REFRESH_DELETE:
752 case LDAP_TAG_SYNC_REFRESH_PRESENT:
753 Debug( LDAP_DEBUG_SYNC,
754 "do_syncrep2: %s - %s%s\n",
755 "LDAP_RES_INTERMEDIATE",
756 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
757 "REFRESH_PRESENT" : "REFRESH_DELETE",
759 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
760 si->si_refreshDelete = 1;
762 si->si_refreshPresent = 1;
764 ber_scanf( ber, "t{" /*"}"*/, &tag );
765 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
767 ber_scanf( ber, "m", &cookie );
768 if ( !BER_BVISNULL( &cookie ) ) {
769 ch_free( syncCookie.octet_str.bv_val );
770 ber_dupbv( &syncCookie.octet_str, &cookie );
772 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
774 slap_parse_sync_cookie( &syncCookie, NULL );
777 if ( ber_peek_tag( ber, &len ) ==
778 LDAP_TAG_REFRESHDONE )
780 ber_scanf( ber, "b", &refreshDone );
782 ber_scanf( ber, /*"{"*/ "}" );
784 case LDAP_TAG_SYNC_ID_SET:
785 Debug( LDAP_DEBUG_SYNC,
786 "do_syncrep2: %s - %s%s\n",
787 "LDAP_RES_INTERMEDIATE",
790 ber_scanf( ber, "t{" /*"}"*/, &tag );
791 if ( ber_peek_tag( ber, &len ) ==
792 LDAP_TAG_SYNC_COOKIE )
794 ber_scanf( ber, "m", &cookie );
795 if ( !BER_BVISNULL( &cookie ) ) {
796 ch_free( syncCookie.octet_str.bv_val );
797 ber_dupbv( &syncCookie.octet_str, &cookie );
799 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
801 slap_parse_sync_cookie( &syncCookie, NULL );
804 if ( ber_peek_tag( ber, &len ) ==
805 LDAP_TAG_REFRESHDELETES )
807 ber_scanf( ber, "b", &refreshDeletes );
809 ber_scanf( ber, "[W]", &syncUUIDs );
810 ber_scanf( ber, /*"{"*/ "}" );
811 if ( refreshDeletes ) {
812 syncrepl_del_nonpresent( op, si, syncUUIDs,
813 &syncCookie.ctxcsn );
814 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
816 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
817 struct berval *syncuuid_bv;
818 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
819 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
820 avl_insert( &si->si_presentlist,
821 (caddr_t) syncuuid_bv,
822 syncuuid_cmp, avl_dup_error );
824 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
826 slap_sync_cookie_free( &syncCookie, 0 );
829 Debug( LDAP_DEBUG_ANY,
830 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
831 (long) si_tag, 0, 0 );
832 ldap_memfree( retoid );
833 ber_bvfree( retdata );
837 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
839 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
842 value_match( &match, slap_schema.si_ad_entryCSN,
843 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
844 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
845 &syncCookie_req.ctxcsn,
846 &syncCookie.ctxcsn, &text );
850 if ( si->si_refreshPresent == 1 ) {
851 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
854 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
856 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
860 ldap_memfree( retoid );
861 ber_bvfree( retdata );
865 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
866 "unknown intermediate response (%d)\n",
868 ldap_memfree( retoid );
869 ber_bvfree( retdata );
875 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
876 "unknown message\n", 0, 0, 0 );
880 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
881 slap_sync_cookie_free( &syncCookie_req, 0 );
882 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
883 slap_sync_cookie_free( &syncCookie, 0 );
893 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
894 errstr = ldap_err2string( rc );
896 Debug( LDAP_DEBUG_ANY,
897 "do_syncrep2 : %s\n", errstr, 0, 0 );
901 slap_sync_cookie_free( &syncCookie, 0 );
902 slap_sync_cookie_free( &syncCookie_req, 0 );
904 if ( res ) ldap_msgfree( res );
906 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
907 if ( si->si_conn_setup ) {
909 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
910 connection_client_stop( s );
911 si->si_conn_setup = 0;
913 ldap_unbind_ext( si->si_ld, NULL, NULL );
925 struct re_s* rtask = arg;
926 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
927 Connection conn = {0};
928 OperationBuffer opbuf;
930 int rc = LDAP_SUCCESS;
936 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
941 ldap_pvt_thread_mutex_lock( &si->si_mutex );
943 switch( abs( si->si_type )) {
944 case LDAP_SYNC_REFRESH_ONLY:
945 case LDAP_SYNC_REFRESH_AND_PERSIST:
948 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
952 if ( slapd_shutdown ) {
954 if ( si->si_conn_setup ) {
955 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
956 connection_client_stop( s );
957 si->si_conn_setup = 0;
959 ldap_unbind_ext( si->si_ld, NULL, NULL );
962 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
966 op = (Operation *) &opbuf;
967 connection_fake_init( &conn, op, ctx );
969 /* use global malloc for now */
970 op->o_tmpmemctx = NULL;
971 op->o_tmpmfuncs = &ch_mfuncs;
973 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
974 op->o_bd = be = si->si_be;
975 op->o_dn = op->o_bd->be_rootdn;
976 op->o_ndn = op->o_bd->be_rootndn;
978 /* Establish session, do search */
980 si->si_refreshDelete = 0;
981 si->si_refreshPresent = 0;
982 rc = do_syncrep1( op, si );
986 /* Process results */
987 if ( rc == LDAP_SUCCESS ) {
988 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
990 rc = do_syncrep2( op, si );
991 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
992 rc = ldap_sync_search( si, op->o_tmpmemctx );
996 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
997 /* If we succeeded, enable the connection for further listening.
998 * If we failed, tear down the connection and reschedule.
1000 if ( rc == LDAP_SUCCESS ) {
1001 if ( si->si_conn_setup ) {
1002 connection_client_enable( s );
1004 rc = connection_client_setup( s, do_syncrepl, arg );
1006 si->si_conn_setup = 1;
1008 } else if ( si->si_conn_setup ) {
1012 if ( rc == -2 ) rc = 0;
1016 /* At this point, we have 4 cases:
1017 * 1) for any hard failure, give up and remove this task
1018 * 2) for ServerDown, reschedule this task to run
1019 * 3) for Refresh and Success, reschedule to run
1020 * 4) for Persist and Success, reschedule to defer
1022 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1024 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1025 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1029 connection_client_stop( s );
1032 if ( rc == LDAP_SUCCESS ) {
1033 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1036 rtask->interval.tv_sec = si->si_interval;
1037 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1038 if ( si->si_retrynum ) {
1039 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1040 si->si_retrynum[i] = si->si_retrynum_init[i];
1042 si->si_retrynum[i] = RETRYNUM_TAIL;
1045 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1046 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1050 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1051 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1052 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1053 if ( si->si_retrynum[i] > 0 )
1054 si->si_retrynum[i]--;
1055 rtask->interval.tv_sec = si->si_retryinterval[i];
1056 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1057 slap_wake_listener();
1061 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1062 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1067 static slap_verbmasks modops[] = {
1068 { BER_BVC("add"), LDAP_REQ_ADD },
1069 { BER_BVC("delete"), LDAP_REQ_DELETE },
1070 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1071 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1075 static Modifications *
1076 syncrepl_accesslog_mods(
1083 AttributeDescription *ad;
1084 struct berval bv, bv2;
1086 Modifications *mod = NULL, *modlist = NULL, **modtail;
1091 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1095 colon = ber_bvchr( &bv, ':' );
1097 continue; /* invalid */
1098 bv.bv_len = colon - bv.bv_val;
1099 if ( slap_bv2ad( &bv, &ad, &text )) {
1103 /* Ignore dynamically generated attrs */
1104 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1106 /* Ignore excluded attrs */
1107 if ( ldap_charray_inlist( si->si_exattrs,
1108 ad->ad_type->sat_cname.bv_val ))
1112 case '+': op = LDAP_MOD_ADD; break;
1113 case '-': op = LDAP_MOD_DELETE; break;
1114 case '=': op = LDAP_MOD_REPLACE; break;
1115 case '#': op = LDAP_MOD_INCREMENT; break;
1119 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1120 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1123 mod->sml_next = NULL;
1125 mod->sml_type = ad->ad_cname;
1126 mod->sml_values = NULL;
1127 mod->sml_nvalues = NULL;
1130 modtail = &mod->sml_next;
1132 if ( colon[2] == ' ' ) {
1133 bv.bv_val = colon + 3;
1134 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1135 ber_dupbv( &bv2, &bv );
1136 ber_bvarray_add( &mod->sml_values, &bv2 );
1142 static Modifications *
1143 syncrepl_changelog_mods(
1148 return NULL; /* FIXME */
1152 syncrepl_message_to_op(
1158 BerElement *ber = NULL;
1159 Modifications *modlist = NULL;
1161 SlapReply rs = { REP_RESULT };
1162 slap_callback cb = { NULL, null_callback, NULL, NULL };
1165 char txtbuf[SLAP_TEXT_BUFLEN];
1166 size_t textlen = sizeof txtbuf;
1168 struct berval bdn, dn = BER_BVNULL, ndn;
1169 struct berval bv, *bvals = NULL;
1170 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1171 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1172 psup = BER_BVNULL, nsup = BER_BVNULL;
1173 int rc, deleteOldRdn = 0;
1175 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1176 Debug( LDAP_DEBUG_ANY,
1177 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1181 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1186 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1188 if ( rc != LDAP_SUCCESS ) {
1189 Debug( LDAP_DEBUG_ANY,
1190 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1194 op->o_tag = LBER_DEFAULT;
1196 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1198 if ( bv.bv_val == NULL )
1201 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1203 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1204 ber_dupbv( &op->o_req_dn, &dn );
1205 ber_dupbv( &op->o_req_ndn, &ndn );
1206 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1207 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1208 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1209 int i = verb_to_mask( bvals[0].bv_val, modops );
1211 Debug( LDAP_DEBUG_ANY,
1212 "syncrepl_message_to_op : unknown op %s",
1213 bvals[0].bv_val, 0, 0 );
1218 op->o_tag = modops[i].mask;
1219 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1220 /* Parse attribute into modlist */
1221 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1222 modlist = syncrepl_accesslog_mods( si, bvals );
1224 modlist = syncrepl_changelog_mods( si, bvals );
1225 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1227 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1228 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1230 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1232 } else if ( !ber_bvstrcasecmp( &bv,
1233 &slap_schema.si_ad_entryCSN->ad_cname )) {
1234 slap_queue_csn( op, bvals );
1239 /* If we didn't get a mod type or a target DN, bail out */
1240 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1245 op->o_callback = &cb;
1246 slap_op_time( &op->o_time, &op->o_tincr );
1248 switch( op->o_tag ) {
1250 case LDAP_REQ_MODIFY:
1251 /* If we didn't get required data, bail */
1252 if ( !modlist ) goto done;
1254 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1256 if ( rc != LDAP_SUCCESS ) {
1257 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1262 if ( op->o_tag == LDAP_REQ_ADD ) {
1263 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1264 op->ora_e->e_name = op->o_req_dn;
1265 op->ora_e->e_nname = op->o_req_ndn;
1266 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1267 if( rc != LDAP_SUCCESS ) {
1268 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1271 rc = op->o_bd->be_add( op, &rs );
1272 Debug( LDAP_DEBUG_SYNC,
1273 "syncrepl_message_to_op: be_add %s (%d)\n",
1274 op->o_req_dn.bv_val, rc, 0 );
1276 be_entry_release_w( op, op->ora_e );
1278 op->orm_modlist = modlist;
1279 rc = op->o_bd->be_modify( op, &rs );
1280 Debug( LDAP_DEBUG_SYNC,
1281 "syncrepl_message_to_op: be_modify %s (%d)\n",
1282 op->o_req_dn.bv_val, rc, 0 );
1285 case LDAP_REQ_MODRDN:
1286 if ( BER_BVISNULL( &rdn )) goto done;
1288 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1290 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1292 if ( !BER_BVISNULL( &sup )) {
1293 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1295 op->orr_newSup = &psup;
1296 op->orr_nnewSup = ⊅
1298 op->orr_newrdn = prdn;
1299 op->orr_nnewrdn = nrdn;
1300 op->orr_deleteoldrdn = deleteOldRdn;
1301 op->orr_modlist = NULL;
1302 if ( slap_modrdn2mods( op, &rs ))
1304 rc = op->o_bd->be_modrdn( op, &rs );
1305 slap_mods_free( op->orr_modlist, 1 );
1306 Debug( LDAP_DEBUG_SYNC,
1307 "syncrepl_message_to_op: be_modrdn %s (%d)\n",
1308 op->o_req_dn.bv_val, rc, 0 );
1310 case LDAP_REQ_DELETE:
1311 rc = op->o_bd->be_delete( op, &rs );
1312 Debug( LDAP_DEBUG_SYNC,
1313 "syncrepl_message_to_op: be_delete %s (%d)\n",
1314 op->o_req_dn.bv_val, rc, 0 );
1318 slap_graduate_commit_csn( op );
1320 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1321 if ( !BER_BVISNULL( &rdn )) {
1322 if ( !BER_BVISNULL( &nsup ))
1323 ch_free( nsup.bv_val );
1324 if ( !BER_BVISNULL( &psup ))
1325 ch_free( psup.bv_val );
1326 if ( !BER_BVISNULL( &nrdn ))
1327 ch_free( nrdn.bv_val );
1328 if ( !BER_BVISNULL( &prdn ))
1329 ch_free( prdn.bv_val );
1331 ber_free ( ber, 0 );
1336 syncrepl_message_to_entry(
1340 Modifications **modlist,
1346 BerElement *ber = NULL;
1349 Modifications **modtail = modlist;
1352 char txtbuf[SLAP_TEXT_BUFLEN];
1353 size_t textlen = sizeof txtbuf;
1355 struct berval bdn = {0, NULL}, dn, ndn;
1360 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1361 Debug( LDAP_DEBUG_ANY,
1362 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1366 op->o_tag = LDAP_REQ_ADD;
1368 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1370 if ( rc != LDAP_SUCCESS ) {
1371 Debug( LDAP_DEBUG_ANY,
1372 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1376 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1377 ber_dupbv( &op->o_req_dn, &dn );
1378 ber_dupbv( &op->o_req_ndn, &ndn );
1379 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1380 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1382 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1385 return LDAP_SUCCESS;
1388 if ( entry == NULL ) {
1392 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1393 e->e_name = op->o_req_dn;
1394 e->e_nname = op->o_req_ndn;
1396 while ( ber_remaining( ber ) ) {
1397 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1398 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1403 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1405 mod->sml_op = LDAP_MOD_REPLACE;
1407 mod->sml_next = NULL;
1408 mod->sml_desc = NULL;
1409 mod->sml_type = tmp.sml_type;
1410 mod->sml_values = tmp.sml_values;
1411 mod->sml_nvalues = NULL;
1414 modtail = &mod->sml_next;
1417 if ( *modlist == NULL ) {
1418 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1424 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1426 if ( rc != LDAP_SUCCESS ) {
1427 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1432 /* Strip out dynamically generated attrs */
1433 for ( modtail = modlist; *modtail ; ) {
1435 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1436 *modtail = mod->sml_next;
1437 slap_mod_free( &mod->sml_mod, 0 );
1440 modtail = &mod->sml_next;
1444 /* Strip out attrs in exattrs list */
1445 for ( modtail = modlist; *modtail ; ) {
1447 if ( ldap_charray_inlist( si->si_exattrs,
1448 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1449 *modtail = mod->sml_next;
1450 slap_mod_free( &mod->sml_mod, 0 );
1453 modtail = &mod->sml_next;
1457 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1458 if( rc != LDAP_SUCCESS ) {
1459 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1464 ber_free ( ber, 0 );
1465 if ( rc != LDAP_SUCCESS ) {
1476 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1478 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1479 * entry if a previous refresh was interrupted before sending us a new
1480 * context state. We try to compare the new entry to the existing entry
1481 * and ignore the new entry if they are the same.
1483 * Also, we may get an update where the entryDN has changed, due to
1484 * a ModDn on the provider. We detect this as well, so we can issue
1485 * the corresponding operation locally.
1487 * In the case of a modify, we get a list of all the attributes
1488 * in the original entry. Rather than deleting the entry and re-adding it,
1489 * we issue a Modify request that deletes all the attributes and adds all
1490 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1493 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1494 * a ModDN we will issue both operations on the local database.
1496 typedef struct dninfo {
1500 int renamed; /* Was an existing entry renamed? */
1501 int wasChanged; /* are the attributes changed? */
1502 int attrs; /* how many attribute types are in the ads list */
1503 AttributeDescription **ads;
1511 Modifications** modlist,
1513 struct berval* syncUUID,
1514 struct sync_cookie* syncCookie_req,
1515 struct berval* syncCSN )
1517 Backend *be = op->o_bd;
1518 slap_callback cb = { NULL, NULL, NULL, NULL };
1519 struct berval *syncuuid_bv = NULL;
1520 struct berval syncUUID_strrep = BER_BVNULL;
1521 struct berval uuid_bv = BER_BVNULL;
1523 SlapReply rs_search = {REP_RESULT};
1524 SlapReply rs_delete = {REP_RESULT};
1525 SlapReply rs_add = {REP_RESULT};
1526 SlapReply rs_modify = {REP_RESULT};
1528 #ifdef LDAP_COMP_MATCH
1529 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1531 AttributeAssertion ava = { NULL, BER_BVNULL };
1533 int rc = LDAP_SUCCESS;
1535 struct berval pdn = BER_BVNULL;
1539 switch( syncstate ) {
1540 case LDAP_SYNC_PRESENT:
1541 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1543 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1546 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1548 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1550 case LDAP_SYNC_DELETE:
1551 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1553 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1555 case LDAP_SYNC_MODIFY:
1556 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1558 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1561 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1563 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1566 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1567 if ( !si->si_refreshPresent ) {
1568 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1569 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1570 syncuuid_cmp, avl_dup_error );
1574 if ( syncstate == LDAP_SYNC_PRESENT ) {
1576 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1577 if ( entry == NULL ) {
1582 f.f_choice = LDAP_FILTER_EQUALITY;
1584 ava.aa_desc = slap_schema.si_ad_entryUUID;
1585 ava.aa_value = *syncUUID;
1587 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1588 if ( syncuuid_bv ) {
1589 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: inserted UUID %s\n",
1590 syncUUID_strrep.bv_val, 0, 0 );
1592 op->ors_filter = &f;
1594 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1595 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1596 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1597 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1598 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1599 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1600 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1601 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1603 op->o_tag = LDAP_REQ_SEARCH;
1604 op->ors_scope = LDAP_SCOPE_SUBTREE;
1605 op->ors_deref = LDAP_DEREF_NEVER;
1607 /* get the entry for this UUID */
1608 op->o_req_dn = si->si_base;
1609 op->o_req_ndn = si->si_base;
1611 op->o_time = slap_get_time();
1612 op->ors_tlimit = SLAP_NO_LIMIT;
1615 op->ors_attrs = slap_anlist_all_attributes;
1616 op->ors_attrsonly = 0;
1618 /* set callback function */
1619 op->o_callback = &cb;
1620 cb.sc_response = dn_callback;
1621 cb.sc_private = &dni;
1622 dni.new_entry = entry;
1624 if ( limits_check( op, &rs_search ) == 0 ) {
1625 rc = be->be_search( op, &rs_search );
1626 Debug( LDAP_DEBUG_SYNC,
1627 "syncrepl_entry: %s (%d)\n",
1628 "be_search", rc, 0 );
1631 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1632 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1635 cb.sc_response = null_callback;
1638 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1639 Debug( LDAP_DEBUG_SYNC,
1640 "syncrepl_entry: %s\n",
1641 entry->e_name.bv_val, 0, 0 );
1643 Debug( LDAP_DEBUG_SYNC,
1644 "syncrepl_entry: %s\n",
1645 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1648 if ( syncstate != LDAP_SYNC_DELETE ) {
1649 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1652 /* add if missing */
1653 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1654 &syncUUID_strrep, syncUUID );
1656 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1657 /* replace only if necessary */
1658 if ( a->a_nvals != a->a_vals ) {
1659 ber_memfree( a->a_nvals[0].bv_val );
1660 ber_dupbv( &a->a_nvals[0], syncUUID );
1662 ber_memfree( a->a_vals[0].bv_val );
1663 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1665 /* Don't save the contextCSN on the inooming context entry,
1666 * we'll write it when syncrepl_updateCookie eventually
1667 * gets called. (ITS#4622)
1669 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1670 &be->be_nsuffix[0] )) {
1672 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1674 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1683 slap_op_time( &op->o_time, &op->o_tincr );
1684 switch ( syncstate ) {
1686 case LDAP_SYNC_MODIFY:
1688 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1690 /* FIXME: op->o_csn is assumed to be
1691 * on the thread's slab; this needs
1692 * to be cleared ASAP.
1693 * What happens if already present?
1695 assert( BER_BVISNULL( &op->o_csn ) );
1696 op->o_csn = a->a_vals[0];
1700 if ( BER_BVISNULL( &dni.dn )) {
1702 op->o_req_dn = entry->e_name;
1703 op->o_req_ndn = entry->e_nname;
1704 op->o_tag = LDAP_REQ_ADD;
1707 rc = be->be_add( op, &rs_add );
1708 Debug( LDAP_DEBUG_SYNC,
1709 "syncrepl_entry: %s (%d)\n",
1711 switch ( rs_add.sr_err ) {
1713 be_entry_release_w( op, entry );
1718 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1719 * only if the suffix entry is not present */
1720 case LDAP_NO_SUCH_OBJECT:
1721 rc = syncrepl_add_glue( op, entry );
1725 /* if an entry was added via syncrepl_add_glue(),
1726 * it likely has no entryUUID, so the previous
1727 * be_search() doesn't find it. In this case,
1728 * give syncrepl a chance to modify it. Also
1729 * allow for entries that were recreated with the
1730 * same DN but a different entryUUID.
1732 case LDAP_ALREADY_EXISTS:
1734 Operation op2 = *op;
1735 SlapReply rs2 = { 0 };
1736 slap_callback cb2 = { 0 };
1738 op2.o_tag = LDAP_REQ_SEARCH;
1739 op2.o_req_dn = entry->e_name;
1740 op2.o_req_ndn = entry->e_nname;
1741 op2.ors_scope = LDAP_SCOPE_BASE;
1742 op2.ors_deref = LDAP_DEREF_NEVER;
1743 op2.ors_attrs = slap_anlist_all_attributes;
1744 op2.ors_attrsonly = 0;
1745 op2.ors_limit = NULL;
1747 op2.ors_tlimit = SLAP_NO_LIMIT;
1749 f.f_choice = LDAP_FILTER_PRESENT;
1750 f.f_desc = slap_schema.si_ad_objectClass;
1751 op2.ors_filter = &f;
1752 op2.ors_filterstr = generic_filterstr;
1754 op2.o_callback = &cb2;
1755 cb2.sc_response = dn_callback;
1756 cb2.sc_private = &dni;
1758 rc = be->be_search( &op2, &rs2 );
1759 if ( rc ) goto done;
1762 slap_op_time( &op->o_time, &op->o_tincr );
1768 Debug( LDAP_DEBUG_ANY,
1769 "syncrepl_entry : be_add failed (%d)\n",
1770 rs_add.sr_err, 0, 0 );
1776 op->o_req_dn = dni.dn;
1777 op->o_req_ndn = dni.ndn;
1778 if ( dni.renamed ) {
1779 struct berval noldp, newp, nnewp;
1781 op->o_tag = LDAP_REQ_MODRDN;
1782 dnRdn( &entry->e_name, &op->orr_newrdn );
1783 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1785 dnParent( &dni.ndn, &noldp );
1786 dnParent( &entry->e_nname, &nnewp );
1787 if ( !dn_match( &noldp, &nnewp )) {
1788 dnParent( &entry->e_name, &newp );
1789 op->orr_newSup = &newp;
1790 op->orr_nnewSup = &nnewp;
1792 op->orr_deleteoldrdn = 0;
1793 op->orr_modlist = NULL;
1794 if (( rc = slap_modrdn2mods( op, &rs_modify ))) {
1798 /* RDNs must be NUL-terminated for back-ldap */
1799 noldp = op->orr_newrdn;
1800 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
1801 noldp = op->orr_nnewrdn;
1802 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
1804 rc = be->be_modrdn( op, &rs_modify );
1805 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
1806 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
1808 slap_mods_free( op->orr_modlist, 1 );
1809 Debug( LDAP_DEBUG_SYNC,
1810 "syncrepl_entry: %s (%d)\n",
1811 "be_modrdn", rc, 0 );
1812 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1813 op->o_req_dn = entry->e_name;
1814 op->o_req_ndn = entry->e_nname;
1818 if ( dni.wasChanged )
1819 slap_op_time( &op->o_time, &op->o_tincr );
1821 if ( dni.wasChanged ) {
1822 Modifications *mod, *modhead = NULL;
1823 Modifications *modtail = NULL;
1826 op->o_tag = LDAP_REQ_MODIFY;
1828 assert( *modlist != NULL );
1830 /* Delete all the old attrs */
1831 for ( i = 0; i < dni.attrs; i++ ) {
1832 mod = ch_malloc( sizeof( Modifications ) );
1833 mod->sml_op = LDAP_MOD_DELETE;
1835 mod->sml_desc = dni.ads[i];
1836 mod->sml_type = mod->sml_desc->ad_cname;
1837 mod->sml_values = NULL;
1838 mod->sml_nvalues = NULL;
1839 if ( !modhead ) modhead = mod;
1841 modtail->sml_next = mod;
1846 /* Append passed in list to ours */
1848 modtail->sml_next = *modlist;
1854 /* Find end of this list */
1855 for ( ; mod != NULL; mod = mod->sml_next ) {
1859 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1860 mod->sml_op = LDAP_MOD_REPLACE;
1862 mod->sml_desc = slap_schema.si_ad_entryUUID;
1863 mod->sml_type = mod->sml_desc->ad_cname;
1864 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1865 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1866 ber_dupbv( &uuid_bv, syncUUID );
1867 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1868 modtail->sml_next = mod;
1870 op->o_tag = LDAP_REQ_MODIFY;
1871 op->orm_modlist = *modlist;
1873 rc = be->be_modify( op, &rs_modify );
1874 Debug( LDAP_DEBUG_SYNC,
1875 "syncrepl_entry: %s (%d)\n",
1876 "be_modify", rc, 0 );
1877 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1878 Debug( LDAP_DEBUG_ANY,
1879 "syncrepl_entry : be_modify failed (%d)\n",
1880 rs_modify.sr_err, 0, 0 );
1884 case LDAP_SYNC_DELETE :
1885 if ( !BER_BVISNULL( &dni.dn )) {
1886 op->o_req_dn = dni.dn;
1887 op->o_req_ndn = dni.ndn;
1888 op->o_tag = LDAP_REQ_DELETE;
1889 rc = be->be_delete( op, &rs_delete );
1890 Debug( LDAP_DEBUG_SYNC,
1891 "syncrepl_entry: %s (%d)\n",
1892 "be_delete", rc, 0 );
1894 while ( rs_delete.sr_err == LDAP_SUCCESS
1895 && op->o_delete_glue_parent ) {
1896 op->o_delete_glue_parent = 0;
1897 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1898 slap_callback cb = { NULL };
1899 cb.sc_response = slap_null_cb;
1900 dnParent( &op->o_req_ndn, &pdn );
1902 op->o_req_ndn = pdn;
1903 op->o_callback = &cb;
1904 op->o_bd->be_delete( op, &rs_delete );
1913 Debug( LDAP_DEBUG_ANY,
1914 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1919 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1920 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1921 BER_BVZERO( &syncUUID_strrep );
1924 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1926 if ( !BER_BVISNULL( &dni.ndn ) ) {
1927 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1929 if ( !BER_BVISNULL( &dni.dn ) ) {
1930 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1933 entry_free( entry );
1934 BER_BVZERO( &op->o_csn );
1938 static struct berval gcbva[] = {
1944 #define NP_DELETE_ONE 2
1947 syncrepl_del_nonpresent(
1951 struct berval *cookiecsn )
1953 Backend* be = op->o_bd;
1954 slap_callback cb = { NULL };
1955 SlapReply rs_search = {REP_RESULT};
1956 SlapReply rs_delete = {REP_RESULT};
1957 SlapReply rs_modify = {REP_RESULT};
1958 struct nonpresent_entry *np_list, *np_prev;
1960 AttributeName an[2];
1962 struct berval pdn = BER_BVNULL;
1965 op->o_req_dn = si->si_base;
1966 op->o_req_ndn = si->si_base;
1968 cb.sc_response = nonpresent_callback;
1971 op->o_callback = &cb;
1972 op->o_tag = LDAP_REQ_SEARCH;
1973 op->ors_scope = si->si_scope;
1974 op->ors_deref = LDAP_DEREF_NEVER;
1975 op->o_time = slap_get_time();
1976 op->ors_tlimit = SLAP_NO_LIMIT;
1981 #ifdef LDAP_COMP_MATCH
1982 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1984 AttributeAssertion eq = { NULL, BER_BVNULL };
1988 op->ors_attrsonly = 1;
1989 op->ors_attrs = slap_anlist_no_attrs;
1990 op->ors_limit = NULL;
1991 op->ors_filter = &uf;
1994 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1996 uf.f_choice = LDAP_FILTER_EQUALITY;
1997 si->si_refreshDelete |= NP_DELETE_ONE;
1999 for (i=0; uuids[i].bv_val; i++) {
2001 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2003 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2004 uf.f_av_value = uuids[i];
2005 rc = be->be_search( op, &rs_search );
2006 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2008 si->si_refreshDelete ^= NP_DELETE_ONE;
2010 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2011 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2012 an[0].an_desc = slap_schema.si_ad_entryUUID;
2014 op->ors_slimit = SLAP_NO_LIMIT;
2015 op->ors_attrsonly = 0;
2016 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2017 op->ors_filterstr = si->si_filterstr;
2018 op->o_nocaching = 1;
2020 if ( limits_check( op, &rs_search ) == 0 ) {
2021 rc = be->be_search( op, &rs_search );
2023 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2026 op->o_nocaching = 0;
2028 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2030 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2033 csn = si->si_syncCookie.ctxcsn;
2035 slap_queue_csn( op, &csn );
2037 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2038 while ( np_list != NULL ) {
2039 LDAP_LIST_REMOVE( np_list, npe_link );
2041 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2042 op->o_tag = LDAP_REQ_DELETE;
2043 op->o_callback = &cb;
2044 cb.sc_response = null_callback;
2046 op->o_req_dn = *np_prev->npe_name;
2047 op->o_req_ndn = *np_prev->npe_nname;
2048 rc = op->o_bd->be_delete( op, &rs_delete );
2049 Debug( LDAP_DEBUG_SYNC,
2050 "syncrepl_del_nonpresent: be_delete %s (%d)\n",
2051 op->o_req_dn.bv_val, rc, 0 );
2053 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2054 Modifications mod1, mod2;
2055 mod1.sml_op = LDAP_MOD_REPLACE;
2057 mod1.sml_desc = slap_schema.si_ad_objectClass;
2058 mod1.sml_type = mod1.sml_desc->ad_cname;
2059 mod1.sml_values = &gcbva[0];
2060 mod1.sml_nvalues = NULL;
2061 mod1.sml_next = &mod2;
2063 mod2.sml_op = LDAP_MOD_REPLACE;
2065 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2066 mod2.sml_type = mod2.sml_desc->ad_cname;
2067 mod2.sml_values = &gcbva[1];
2068 mod2.sml_nvalues = NULL;
2069 mod2.sml_next = NULL;
2071 op->o_tag = LDAP_REQ_MODIFY;
2072 op->orm_modlist = &mod1;
2074 rc = be->be_modify( op, &rs_modify );
2077 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2078 op->o_delete_glue_parent ) {
2079 op->o_delete_glue_parent = 0;
2080 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2081 slap_callback cb = { NULL };
2082 cb.sc_response = slap_null_cb;
2083 dnParent( &op->o_req_ndn, &pdn );
2085 op->o_req_ndn = pdn;
2086 op->o_callback = &cb;
2087 /* give it a root privil ? */
2088 op->o_bd->be_delete( op, &rs_delete );
2094 op->o_delete_glue_parent = 0;
2096 ber_bvfree( np_prev->npe_name );
2097 ber_bvfree( np_prev->npe_nname );
2101 slap_graduate_commit_csn( op );
2103 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2104 BER_BVZERO( &op->o_csn );
2115 Backend *be = op->o_bd;
2116 slap_callback cb = { NULL };
2121 struct berval dn = BER_BVNULL;
2122 struct berval ndn = BER_BVNULL;
2124 SlapReply rs_add = {REP_RESULT};
2125 struct berval ptr, nptr;
2128 op->o_tag = LDAP_REQ_ADD;
2129 op->o_callback = &cb;
2130 cb.sc_response = null_callback;
2131 cb.sc_private = NULL;
2136 /* count RDNs in suffix */
2137 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2138 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2140 ptr.bv_len -= comma - ptr.bv_val;
2150 /* Start with BE suffix */
2152 for ( i = 0; i < suffrdns; i++ ) {
2153 comma = ber_bvrchr( &ptr, ',' );
2154 if ( comma != NULL ) {
2155 ptr.bv_len = comma - ptr.bv_val;
2162 if ( !BER_BVISEMPTY( &ptr ) ) {
2163 dn.bv_len -= ptr.bv_len + 1;
2164 dn.bv_val += ptr.bv_len + 1;
2167 /* the normalizedDNs are always the same length, no counting
2171 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2172 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2173 ndn.bv_len = be->be_nsuffix[0].bv_len;
2175 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2181 while ( ndn.bv_val > e->e_nname.bv_val ) {
2182 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2183 ber_dupbv( &glue->e_name, &dn );
2184 ber_dupbv( &glue->e_nname, &ndn );
2186 a = ch_calloc( 1, sizeof( Attribute ));
2187 a->a_desc = slap_schema.si_ad_objectClass;
2189 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2190 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2191 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2192 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2194 a->a_nvals = a->a_vals;
2196 a->a_next = glue->e_attrs;
2199 a = ch_calloc( 1, sizeof( Attribute ));
2200 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2202 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2203 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2204 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2206 a->a_nvals = a->a_vals;
2208 a->a_next = glue->e_attrs;
2211 op->o_req_dn = glue->e_name;
2212 op->o_req_ndn = glue->e_nname;
2214 rc = be->be_add ( op, &rs_add );
2215 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2216 be_entry_release_w( op, glue );
2218 /* incl. ALREADY EXIST */
2220 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2226 /* Move to next child */
2227 comma = ber_bvrchr( &ptr, ',' );
2228 if ( comma == NULL ) {
2231 ptr.bv_len = comma - ptr.bv_val;
2233 dn.bv_val = ++comma;
2234 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2236 comma = ber_bvrchr( &nptr, ',' );
2237 assert( comma != NULL );
2238 nptr.bv_len = comma - nptr.bv_val;
2240 ndn.bv_val = ++comma;
2241 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2244 op->o_req_dn = e->e_name;
2245 op->o_req_ndn = e->e_nname;
2247 rc = be->be_add ( op, &rs_add );
2248 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2249 be_entry_release_w( op, e );
2258 syncrepl_updateCookie(
2262 struct sync_cookie *syncCookie )
2264 Backend *be = op->o_bd;
2265 Modifications mod = { { 0 } };
2266 struct berval vals[ 2 ];
2270 slap_callback cb = { NULL };
2271 SlapReply rs_modify = {REP_RESULT};
2273 mod.sml_op = LDAP_MOD_REPLACE;
2274 mod.sml_desc = slap_schema.si_ad_contextCSN;
2275 mod.sml_type = mod.sml_desc->ad_cname;
2276 mod.sml_values = vals;
2277 vals[0] = syncCookie->ctxcsn;
2278 BER_BVZERO( &vals[1] );
2280 slap_queue_csn( op, &syncCookie->ctxcsn );
2282 op->o_tag = LDAP_REQ_MODIFY;
2284 assert( si->si_rid < 1000 );
2286 cb.sc_response = null_callback;
2289 op->o_callback = &cb;
2290 op->o_req_dn = op->o_bd->be_suffix[0];
2291 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2293 /* update contextCSN */
2294 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2295 op->orm_modlist = &mod;
2296 rc = be->be_modify( op, &rs_modify );
2299 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2300 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2301 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2303 Debug( LDAP_DEBUG_ANY,
2304 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2307 slap_graduate_commit_csn( op );
2309 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2310 BER_BVZERO( &op->o_csn );
2320 dninfo *dni = op->o_callback->sc_private;
2322 if ( rs->sr_type == REP_SEARCH ) {
2323 if ( !BER_BVISNULL( &dni->dn ) ) {
2324 Debug( LDAP_DEBUG_ANY,
2325 "dn_callback : consistency error - "
2326 "entryUUID is not unique\n", 0, 0, 0 );
2328 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2329 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2330 /* If there is a new entry, see if it differs from the old.
2331 * We compare the non-normalized values so that cosmetic changes
2332 * in the provider are always propagated.
2334 if ( dni->new_entry ) {
2335 Attribute *old, *new;
2338 /* Did the DN change? Note that we don't explicitly try to
2339 * discover if the deleteOldRdn argument applies here. It
2340 * would save an unnecessary Modify if we detected it, but
2341 * that's a fair amount of trouble to compare the two attr
2342 * lists in detail. (Just test normalized DN; we ignore
2343 * insignificant changes here.)
2345 if ( !dn_match( &rs->sr_entry->e_nname,
2346 &dni->new_entry->e_nname ) )
2351 for ( i = 0, old = rs->sr_entry->e_attrs;
2353 i++, old = old->a_next )
2358 /* We assume that attributes are saved in the same order
2359 * in the remote and local databases. So if we walk through
2360 * the attributeDescriptions one by one they should match in
2361 * lock step. If not, we signal a change. Otherwise we test
2364 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2366 old = old->a_next, new = new->a_next )
2368 if ( old->a_desc != new->a_desc ) {
2369 dni->wasChanged = 1;
2372 for ( i = 0; ; i++ ) {
2374 nold = BER_BVISNULL( &old->a_vals[i] );
2375 nnew = BER_BVISNULL( &new->a_vals[i] );
2376 /* If both are empty, stop looking */
2377 if ( nold && nnew ) {
2380 /* If they are different, stop looking */
2381 if ( nold != nnew ) {
2382 dni->wasChanged = 1;
2385 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2386 dni->wasChanged = 1;
2390 if ( dni->wasChanged ) break;
2392 if ( dni->wasChanged ) {
2393 dni->ads = op->o_tmpalloc( dni->attrs *
2394 sizeof(AttributeDescription *), op->o_tmpmemctx );
2396 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2397 dni->ads[i] = old->a_desc;
2403 } else if ( rs->sr_type == REP_RESULT ) {
2404 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2405 Debug( LDAP_DEBUG_ANY,
2406 "dn_callback : consistency error - "
2407 "entryUUID is not unique\n", 0, 0, 0 );
2411 return LDAP_SUCCESS;
2415 nonpresent_callback(
2419 syncinfo_t *si = op->o_callback->sc_private;
2422 struct berval* present_uuid = NULL;
2423 struct nonpresent_entry *np_entry;
2425 if ( rs->sr_type == REP_RESULT ) {
2426 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2427 si->si_presentlist = NULL;
2429 } else if ( rs->sr_type == REP_SEARCH ) {
2430 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2431 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2434 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2437 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: UUID %s, dn %s, %sfound\n",
2438 a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val, present_uuid ? "" : "not " );
2440 if ( a == NULL ) return 0;
2443 if ( present_uuid == NULL ) {
2444 np_entry = (struct nonpresent_entry *)
2445 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2446 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2447 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2448 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2451 avl_delete( &si->si_presentlist,
2452 &a->a_nvals[0], syncuuid_cmp );
2453 ch_free( present_uuid->bv_val );
2454 ch_free( present_uuid );
2457 return LDAP_SUCCESS;
2465 if ( rs->sr_err != LDAP_SUCCESS &&
2466 rs->sr_err != LDAP_REFERRAL &&
2467 rs->sr_err != LDAP_ALREADY_EXISTS &&
2468 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2469 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2471 Debug( LDAP_DEBUG_ANY,
2472 "null_callback : error code 0x%x\n",
2475 return LDAP_SUCCESS;
2478 static struct berval *
2479 slap_uuidstr_from_normalized(
2480 struct berval* uuidstr,
2481 struct berval* normalized,
2485 unsigned char nibble;
2488 if ( normalized == NULL ) return NULL;
2489 if ( normalized->bv_len != 16 ) return NULL;
2494 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2495 if ( new == NULL ) {
2502 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2503 if ( new != uuidstr ) {
2504 slap_sl_free( new, ctx );
2509 for ( i = 0; i < 16; i++ ) {
2510 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2511 new->bv_val[(i<<1)+d] = '-';
2515 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2516 if ( nibble < 10 ) {
2517 new->bv_val[(i<<1)+d] = nibble + '0';
2519 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2522 nibble = (normalized->bv_val[i]) & 0xF;
2523 if ( nibble < 10 ) {
2524 new->bv_val[(i<<1)+d+1] = nibble + '0';
2526 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2530 new->bv_val[new->bv_len] = '\0';
2535 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2537 const struct berval *uuid1 = v_uuid1;
2538 const struct berval *uuid2 = v_uuid2;
2539 int rc = uuid1->bv_len - uuid2->bv_len;
2540 if ( rc ) return rc;
2541 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2545 avl_ber_bvfree( void *v_bv )
2547 struct berval *bv = (struct berval *)v_bv;
2549 if( v_bv == NULL ) return;
2550 if ( !BER_BVISNULL( bv ) ) {
2551 ch_free( bv->bv_val );
2553 ch_free( (char *) bv );
2557 syncinfo_free( syncinfo_t *sie )
2560 if ( sie->si_conn_setup ) {
2562 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2563 connection_client_stop( s );
2564 sie->si_conn_setup = 0;
2566 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2569 /* re-fetch it, in case it was already removed */
2570 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2572 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2573 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2574 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2577 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2579 bindconf_free( &sie->si_bindconf );
2581 if ( sie->si_filterstr.bv_val ) {
2582 ch_free( sie->si_filterstr.bv_val );
2584 if ( sie->si_base.bv_val ) {
2585 ch_free( sie->si_base.bv_val );
2587 if ( sie->si_attrs ) {
2589 while ( sie->si_attrs[i] != NULL ) {
2590 ch_free( sie->si_attrs[i] );
2593 ch_free( sie->si_attrs );
2595 if ( sie->si_exattrs ) {
2597 while ( sie->si_exattrs[i] != NULL ) {
2598 ch_free( sie->si_exattrs[i] );
2601 ch_free( sie->si_exattrs );
2603 if ( sie->si_anlist ) {
2605 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2606 ch_free( sie->si_anlist[i].an_name.bv_val );
2609 ch_free( sie->si_anlist );
2611 if ( sie->si_exanlist ) {
2613 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2614 ch_free( sie->si_exanlist[i].an_name.bv_val );
2617 ch_free( sie->si_exanlist );
2619 if ( sie->si_retryinterval ) {
2620 ch_free( sie->si_retryinterval );
2622 if ( sie->si_retrynum ) {
2623 ch_free( sie->si_retrynum );
2625 if ( sie->si_retrynum_init ) {
2626 ch_free( sie->si_retrynum_init );
2628 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2629 if ( sie->si_presentlist ) {
2630 avl_free( sie->si_presentlist, avl_ber_bvfree );
2632 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2633 struct nonpresent_entry* npe;
2634 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2635 LDAP_LIST_REMOVE( npe, npe_link );
2636 if ( npe->npe_name ) {
2637 if ( npe->npe_name->bv_val ) {
2638 ch_free( npe->npe_name->bv_val );
2640 ch_free( npe->npe_name );
2642 if ( npe->npe_nname ) {
2643 if ( npe->npe_nname->bv_val ) {
2644 ch_free( npe->npe_nname->bv_val );
2646 ch_free( npe->npe_nname );
2655 /* NOTE: used & documented in slapd.conf(5) */
2657 #define PROVIDERSTR "provider"
2658 #define SCHEMASTR "schemachecking"
2659 #define FILTERSTR "filter"
2660 #define SEARCHBASESTR "searchbase"
2661 #define SCOPESTR "scope"
2662 #define ATTRSONLYSTR "attrsonly"
2663 #define ATTRSSTR "attrs"
2664 #define TYPESTR "type"
2665 #define INTERVALSTR "interval"
2666 #define RETRYSTR "retry"
2667 #define SLIMITSTR "sizelimit"
2668 #define TLIMITSTR "timelimit"
2669 #define SYNCDATASTR "syncdata"
2671 /* FIXME: undocumented */
2672 #define LOGBASESTR "logbase"
2673 #define LOGFILTERSTR "logfilter"
2674 #define OLDAUTHCSTR "bindprincipal"
2675 #define EXATTRSSTR "exattrs"
2676 #define MANAGEDSAITSTR "manageDSAit"
2679 #define LASTMODSTR "lastmod"
2680 #define LMGENSTR "gen"
2681 #define LMNOSTR "no"
2682 #define LMREQSTR "req"
2683 #define SRVTABSTR "srvtab"
2684 #define SUFFIXSTR "suffix"
2687 #define GOT_ID 0x0001
2688 #define GOT_PROVIDER 0x0002
2689 #define GOT_BASE 0x0004
2692 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2698 { BER_BVC("base"), LDAP_SCOPE_BASE },
2699 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2700 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2701 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2702 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
2703 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2704 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2705 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2709 static slap_verbmasks datamodes[] = {
2710 { BER_BVC("default"), SYNCDATA_DEFAULT },
2711 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2712 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2717 parse_syncrepl_line(
2725 for ( i = 1; i < c->argc; i++ ) {
2726 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2727 STRLENOF( IDSTR "=" ) ) )
2730 /* '\0' string terminator accounts for '=' */
2731 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2732 if ( lutil_atoi( &tmp, val ) != 0 ) {
2733 snprintf( c->msg, sizeof( c->msg ),
2734 "Error: parse_syncrepl_line: "
2735 "unable to parse syncrepl id \"%s\"", val );
2736 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2739 if ( tmp >= 1000 || tmp < 0 ) {
2740 snprintf( c->msg, sizeof( c->msg ),
2741 "Error: parse_syncrepl_line: "
2742 "syncrepl id %d is out of range [0..999]", tmp );
2743 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2748 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2749 STRLENOF( PROVIDERSTR "=" ) ) )
2751 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2752 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2753 gots |= GOT_PROVIDER;
2754 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2755 STRLENOF( SCHEMASTR "=" ) ) )
2757 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2758 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2759 si->si_schemachecking = 1;
2760 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2761 si->si_schemachecking = 0;
2763 si->si_schemachecking = 1;
2765 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2766 STRLENOF( FILTERSTR "=" ) ) )
2768 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2769 if ( si->si_filterstr.bv_val )
2770 ch_free( si->si_filterstr.bv_val );
2771 ber_str2bv( val, 0, 1, &si->si_filterstr );
2772 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2773 STRLENOF( LOGFILTERSTR "=" ) ) )
2775 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2776 if ( si->si_logfilterstr.bv_val )
2777 ch_free( si->si_logfilterstr.bv_val );
2778 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2779 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2780 STRLENOF( SEARCHBASESTR "=" ) ) )
2785 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2786 if ( si->si_base.bv_val ) {
2787 ch_free( si->si_base.bv_val );
2789 ber_str2bv( val, 0, 0, &bv );
2790 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2791 if ( rc != LDAP_SUCCESS ) {
2792 snprintf( c->msg, sizeof( c->msg ),
2793 "Invalid base DN \"%s\": %d (%s)",
2794 val, rc, ldap_err2string( rc ) );
2795 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2799 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2800 STRLENOF( LOGBASESTR "=" ) ) )
2805 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2806 if ( si->si_logbase.bv_val ) {
2807 ch_free( si->si_logbase.bv_val );
2809 ber_str2bv( val, 0, 0, &bv );
2810 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2811 if ( rc != LDAP_SUCCESS ) {
2812 snprintf( c->msg, sizeof( c->msg ),
2813 "Invalid logbase DN \"%s\": %d (%s)",
2814 val, rc, ldap_err2string( rc ) );
2815 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2818 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2819 STRLENOF( SCOPESTR "=" ) ) )
2822 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2823 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2824 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2825 si->si_scope = scopes[j].val;
2829 if ( BER_BVISNULL(&scopes[j].key) ) {
2830 snprintf( c->msg, sizeof( c->msg ),
2831 "Error: parse_syncrepl_line: "
2832 "unknown scope \"%s\"", val);
2833 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2836 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2837 STRLENOF( ATTRSONLYSTR ) ) )
2839 si->si_attrsonly = 1;
2840 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2841 STRLENOF( ATTRSSTR "=" ) ) )
2843 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2844 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2846 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2847 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2848 if ( si->si_anlist == NULL ) {
2849 ch_free( attr_fname );
2852 si->si_anfile = attr_fname;
2854 char *str, *s, *next;
2855 char delimstr[] = " ,\t";
2856 str = ch_strdup( val );
2857 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2859 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2861 if ( strlen(s) == 1 && *s == '*' ) {
2862 si->si_allattrs = 1;
2863 *(val + ( s - str )) = delimstr[0];
2865 if ( strlen(s) == 1 && *s == '+' ) {
2866 si->si_allopattrs = 1;
2867 *(val + ( s - str )) = delimstr[0];
2871 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2872 if ( si->si_anlist == NULL ) {
2876 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2877 STRLENOF( EXATTRSSTR "=" ) ) )
2879 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2880 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2882 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2883 si->si_exanlist = file2anlist(
2884 si->si_exanlist, attr_fname, " ,\t" );
2885 if ( si->si_exanlist == NULL ) {
2886 ch_free( attr_fname );
2889 ch_free( attr_fname );
2891 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2892 if ( si->si_exanlist == NULL ) {
2896 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2897 STRLENOF( TYPESTR "=" ) ) )
2899 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2900 if ( !strncasecmp( val, "refreshOnly",
2901 STRLENOF("refreshOnly") ) )
2903 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2904 } else if ( !strncasecmp( val, "refreshAndPersist",
2905 STRLENOF("refreshAndPersist") ) )
2907 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2908 si->si_interval = 60;
2910 snprintf( c->msg, sizeof( c->msg ),
2911 "Error: parse_syncrepl_line: "
2912 "unknown sync type \"%s\"", val);
2913 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2916 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2917 STRLENOF( INTERVALSTR "=" ) ) )
2919 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2920 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2921 si->si_interval = 0;
2922 } else if ( strchr( val, ':' ) != NULL ) {
2923 char *next, *ptr = val;
2924 unsigned dd, hh, mm, ss;
2926 /* NOTE: the test for ptr[ 0 ] == '-'
2927 * should go before the call to strtoul() */
2928 dd = strtoul( ptr, &next, 10 );
2929 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
2930 snprintf( c->msg, sizeof( c->msg ),
2931 "Error: parse_syncrepl_line: "
2932 "invalid interval \"%s\", unable to parse days", val );
2933 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2937 hh = strtoul( ptr, &next, 10 );
2938 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
2939 snprintf( c->msg, sizeof( c->msg ),
2940 "Error: parse_syncrepl_line: "
2941 "invalid interval \"%s\", unable to parse hours", val );
2942 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2946 mm = strtoul( ptr, &next, 10 );
2947 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
2948 snprintf( c->msg, sizeof( c->msg ),
2949 "Error: parse_syncrepl_line: "
2950 "invalid interval \"%s\", unable to parse minutes", val );
2951 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2955 ss = strtoul( ptr, &next, 10 );
2956 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
2957 snprintf( c->msg, sizeof( c->msg ),
2958 "Error: parse_syncrepl_line: "
2959 "invalid interval \"%s\", unable to parse seconds", val );
2960 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2963 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2967 if ( lutil_parse_time( val, &t ) != 0 ) {
2968 snprintf( c->msg, sizeof( c->msg ),
2969 "Error: parse_syncrepl_line: "
2970 "invalid interval \"%s\"", val );
2971 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2974 si->si_interval = (time_t)t;
2976 if ( si->si_interval < 0 ) {
2977 snprintf( c->msg, sizeof( c->msg ),
2978 "Error: parse_syncrepl_line: "
2979 "invalid interval \"%ld\"",
2980 (long) si->si_interval);
2981 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2984 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
2985 STRLENOF( RETRYSTR "=" ) ) )
2990 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
2991 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2992 retry_list[0] = NULL;
2994 slap_str2clist( &retry_list, val, " ,\t" );
2996 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2999 snprintf( c->msg, sizeof( c->msg ),
3000 "Error: incomplete syncrepl retry list" );
3001 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3002 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3003 ch_free( retry_list[k] );
3005 ch_free( retry_list );
3008 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3009 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3010 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3011 for ( j = 0; j < n; j++ ) {
3013 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3014 snprintf( c->msg, sizeof( c->msg ),
3015 "Error: invalid retry interval \"%s\" (#%d)",
3016 retry_list[j*2], j );
3017 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3018 /* do some cleanup */
3021 si->si_retryinterval[j] = (time_t)t;
3022 if ( *retry_list[j*2+1] == '+' ) {
3023 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3024 si->si_retrynum[j] = RETRYNUM_FOREVER;
3028 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3029 || si->si_retrynum_init[j] <= 0 )
3031 snprintf( c->msg, sizeof( c->msg ),
3032 "Error: invalid initial retry number \"%s\" (#%d)",
3033 retry_list[j*2+1], j );
3034 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3035 /* do some cleanup */
3038 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3039 || si->si_retrynum[j] <= 0 )
3041 snprintf( c->msg, sizeof( c->msg ),
3042 "Error: invalid retry number \"%s\" (#%d)",
3043 retry_list[j*2+1], j );
3044 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3045 /* do some cleanup */
3050 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3051 si->si_retrynum[j] = RETRYNUM_TAIL;
3052 si->si_retryinterval[j] = 0;
3054 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3055 ch_free( retry_list[k] );
3057 ch_free( retry_list );
3058 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3059 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3061 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3062 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3063 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3065 snprintf( c->msg, sizeof( c->msg ),
3066 "invalid manageDSAit value \"%s\".\n",
3068 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3071 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3072 STRLENOF( SLIMITSTR "=") ) )
3074 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3075 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3078 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3079 snprintf( c->msg, sizeof( c->msg ),
3080 "invalid size limit value \"%s\".\n",
3082 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3085 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3086 STRLENOF( TLIMITSTR "=" ) ) )
3088 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3089 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3092 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3093 snprintf( c->msg, sizeof( c->msg ),
3094 "invalid time limit value \"%s\".\n",
3096 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3099 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3100 STRLENOF( SYNCDATASTR "=" ) ) )
3102 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3103 si->si_syncdata = verb_to_mask( val, datamodes );
3104 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3105 snprintf( c->msg, sizeof( c->msg ),
3106 "Error: parse_syncrepl_line: "
3107 "unknown keyword \"%s\"\n", c->argv[ i ] );
3108 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3113 if ( gots != GOT_ALL ) {
3114 snprintf( c->msg, sizeof( c->msg ),
3115 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3116 gots & GOT_ID ? "" : " "IDSTR,
3117 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3118 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3119 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3133 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3134 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3135 "operations required for syncrepl", c->be->be_type );
3136 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3139 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3140 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3141 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3144 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3147 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3151 si->si_bindconf.sb_tls = SB_TLS_OFF;
3152 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3153 si->si_schemachecking = 0;
3154 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3155 &si->si_filterstr );
3156 si->si_base.bv_val = NULL;
3157 si->si_scope = LDAP_SCOPE_SUBTREE;
3158 si->si_attrsonly = 0;
3159 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3160 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3161 si->si_attrs = NULL;
3162 si->si_allattrs = 0;
3163 si->si_allopattrs = 0;
3164 si->si_exattrs = NULL;
3165 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3166 si->si_interval = 86400;
3167 si->si_retryinterval = NULL;
3168 si->si_retrynum_init = NULL;
3169 si->si_retrynum = NULL;
3170 si->si_manageDSAit = 0;
3173 si->si_conn_setup = 0;
3175 si->si_presentlist = NULL;
3176 LDAP_LIST_INIT( &si->si_nonpresentlist );
3177 ldap_pvt_thread_mutex_init( &si->si_mutex );
3179 rc = parse_syncrepl_line( c, si );
3183 init_syncrepl( si );
3184 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3185 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3190 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3191 syncinfo_free( si );
3194 Debug( LDAP_DEBUG_CONFIG,
3195 "Config: ** successfully added syncrepl \"%s\"\n",
3196 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3197 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3198 if ( !si->si_schemachecking ) {
3199 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3201 c->be->be_syncinfo = si;
3207 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3209 struct berval bc, uri;
3210 char buf[BUFSIZ*2], *ptr;
3213 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3217 /* temporarily inhibit bindconf from printing URI */
3218 uri = si->si_bindconf.sb_uri;
3219 BER_BVZERO( &si->si_bindconf.sb_uri );
3220 bindconf_unparse( &si->si_bindconf, &bc );
3221 si->si_bindconf.sb_uri = uri;
3224 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3225 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3226 if ( ptr - buf >= sizeof( buf ) ) return;
3227 if ( !BER_BVISNULL( &bc )) {
3228 if ( WHATSLEFT <= bc.bv_len ) {
3232 ptr = lutil_strcopy( ptr, bc.bv_val );
3235 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3236 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3237 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3238 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3241 if ( !BER_BVISNULL( &si->si_base )) {
3242 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3243 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3244 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3247 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3248 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3249 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3250 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3253 if ( !BER_BVISNULL( &si->si_logbase )) {
3254 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3255 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3256 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3259 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3260 if ( si->si_scope == scopes[i].val ) {
3261 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3262 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3263 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3267 if ( si->si_attrsonly ) {
3268 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3269 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3271 if ( si->si_anfile ) {
3272 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3273 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3274 ptr = lutil_strcopy( ptr, si->si_anfile );
3276 } else if ( si->si_allattrs || si->si_allopattrs ||
3277 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3281 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3282 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3284 /* FIXME: add check for overflow */
3285 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3286 if ( si->si_allattrs ) {
3287 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3288 if ( old != ptr ) *ptr++ = ',';
3291 if ( si->si_allopattrs ) {
3292 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3293 if ( old != ptr ) *ptr++ = ',';
3298 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3299 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3300 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3301 /* FIXME: add check for overflow */
3302 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3304 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3305 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3306 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3308 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3309 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3310 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3311 "refreshAndPersist" : "refreshOnly" );
3313 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3316 dd = si->si_interval;
3323 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3324 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3325 if ( ptr - buf >= sizeof( buf ) ) return;
3326 } else if ( si->si_retryinterval ) {
3328 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3329 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3330 for (i=0; si->si_retryinterval[i]; i++) {
3331 if ( space ) *ptr++ = ' ';
3333 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3334 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3337 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3339 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3343 if ( si->si_slimit ) {
3344 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3345 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3346 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3349 if ( si->si_tlimit ) {
3350 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3351 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3352 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3355 if ( si->si_syncdata ) {
3356 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3357 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3358 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3359 ptr = lutil_strcopy( ptr, bc.bv_val );
3362 bc.bv_len = ptr - buf;
3364 ber_dupbv( bv, &bc );
3368 syncrepl_config( ConfigArgs *c )
3370 if (c->op == SLAP_CONFIG_EMIT) {
3371 if ( c->be->be_syncinfo ) {
3373 syncrepl_unparse( c->be->be_syncinfo, &bv );
3374 ber_bvarray_add( &c->rvalue_vals, &bv );
3378 } else if ( c->op == LDAP_MOD_DELETE ) {
3379 if ( c->be->be_syncinfo ) {
3380 syncinfo_free( c->be->be_syncinfo );
3381 c->be->be_syncinfo = NULL;
3383 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3386 if ( SLAP_SHADOW( c->be ) ) {
3387 Debug(LDAP_DEBUG_ANY, "%s: "
3388 "syncrepl: database already shadowed.\n",
3391 } else if ( add_syncrepl( c ) ) {
3394 return config_sync_shadow( c );