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_alloc();
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 ) {
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_alloc();
2183 ber_dupbv( &glue->e_name, &dn );
2184 ber_dupbv( &glue->e_nname, &ndn );
2186 a = attr_alloc( slap_schema.si_ad_objectClass );
2188 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2189 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2190 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2191 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2193 a->a_nvals = a->a_vals;
2195 a->a_next = glue->e_attrs;
2198 a = attr_alloc( 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 */
2218 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2224 /* Move to next child */
2225 comma = ber_bvrchr( &ptr, ',' );
2226 if ( comma == NULL ) {
2229 ptr.bv_len = comma - ptr.bv_val;
2231 dn.bv_val = ++comma;
2232 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2234 comma = ber_bvrchr( &nptr, ',' );
2235 assert( comma != NULL );
2236 nptr.bv_len = comma - nptr.bv_val;
2238 ndn.bv_val = ++comma;
2239 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2242 op->o_req_dn = e->e_name;
2243 op->o_req_ndn = e->e_nname;
2245 rc = be->be_add ( op, &rs_add );
2246 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2247 be_entry_release_w( op, e );
2256 syncrepl_updateCookie(
2260 struct sync_cookie *syncCookie )
2262 Backend *be = op->o_bd;
2263 Modifications mod = { { 0 } };
2264 struct berval vals[ 2 ];
2268 slap_callback cb = { NULL };
2269 SlapReply rs_modify = {REP_RESULT};
2271 mod.sml_op = LDAP_MOD_REPLACE;
2272 mod.sml_desc = slap_schema.si_ad_contextCSN;
2273 mod.sml_type = mod.sml_desc->ad_cname;
2274 mod.sml_values = vals;
2275 vals[0] = syncCookie->ctxcsn;
2276 BER_BVZERO( &vals[1] );
2278 slap_queue_csn( op, &syncCookie->ctxcsn );
2280 op->o_tag = LDAP_REQ_MODIFY;
2282 assert( si->si_rid < 1000 );
2284 cb.sc_response = null_callback;
2287 op->o_callback = &cb;
2288 op->o_req_dn = op->o_bd->be_suffix[0];
2289 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2291 /* update contextCSN */
2292 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2293 op->orm_modlist = &mod;
2294 rc = be->be_modify( op, &rs_modify );
2297 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2298 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2299 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2301 Debug( LDAP_DEBUG_ANY,
2302 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2305 slap_graduate_commit_csn( op );
2307 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2308 BER_BVZERO( &op->o_csn );
2318 dninfo *dni = op->o_callback->sc_private;
2320 if ( rs->sr_type == REP_SEARCH ) {
2321 if ( !BER_BVISNULL( &dni->dn ) ) {
2322 Debug( LDAP_DEBUG_ANY,
2323 "dn_callback : consistency error - "
2324 "entryUUID is not unique\n", 0, 0, 0 );
2326 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2327 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2328 /* If there is a new entry, see if it differs from the old.
2329 * We compare the non-normalized values so that cosmetic changes
2330 * in the provider are always propagated.
2332 if ( dni->new_entry ) {
2333 Attribute *old, *new;
2336 /* Did the DN change? Note that we don't explicitly try to
2337 * discover if the deleteOldRdn argument applies here. It
2338 * would save an unnecessary Modify if we detected it, but
2339 * that's a fair amount of trouble to compare the two attr
2340 * lists in detail. (Just test normalized DN; we ignore
2341 * insignificant changes here.)
2343 if ( !dn_match( &rs->sr_entry->e_nname,
2344 &dni->new_entry->e_nname ) )
2349 for ( i = 0, old = rs->sr_entry->e_attrs;
2351 i++, old = old->a_next )
2356 /* We assume that attributes are saved in the same order
2357 * in the remote and local databases. So if we walk through
2358 * the attributeDescriptions one by one they should match in
2359 * lock step. If not, we signal a change. Otherwise we test
2362 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2364 old = old->a_next, new = new->a_next )
2366 if ( old->a_desc != new->a_desc ) {
2367 dni->wasChanged = 1;
2370 for ( i = 0; ; i++ ) {
2372 nold = BER_BVISNULL( &old->a_vals[i] );
2373 nnew = BER_BVISNULL( &new->a_vals[i] );
2374 /* If both are empty, stop looking */
2375 if ( nold && nnew ) {
2378 /* If they are different, stop looking */
2379 if ( nold != nnew ) {
2380 dni->wasChanged = 1;
2383 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2384 dni->wasChanged = 1;
2388 if ( dni->wasChanged ) break;
2390 if ( dni->wasChanged ) {
2391 dni->ads = op->o_tmpalloc( dni->attrs *
2392 sizeof(AttributeDescription *), op->o_tmpmemctx );
2394 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2395 dni->ads[i] = old->a_desc;
2401 } else if ( rs->sr_type == REP_RESULT ) {
2402 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2403 Debug( LDAP_DEBUG_ANY,
2404 "dn_callback : consistency error - "
2405 "entryUUID is not unique\n", 0, 0, 0 );
2409 return LDAP_SUCCESS;
2413 nonpresent_callback(
2417 syncinfo_t *si = op->o_callback->sc_private;
2420 struct berval* present_uuid = NULL;
2421 struct nonpresent_entry *np_entry;
2423 if ( rs->sr_type == REP_RESULT ) {
2424 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2425 si->si_presentlist = NULL;
2427 } else if ( rs->sr_type == REP_SEARCH ) {
2428 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2429 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2432 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2435 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: UUID %s, dn %s, %sfound\n",
2436 a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val, present_uuid ? "" : "not " );
2438 if ( a == NULL ) return 0;
2441 if ( present_uuid == NULL ) {
2442 np_entry = (struct nonpresent_entry *)
2443 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2444 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2445 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2446 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2449 avl_delete( &si->si_presentlist,
2450 &a->a_nvals[0], syncuuid_cmp );
2451 ch_free( present_uuid->bv_val );
2452 ch_free( present_uuid );
2455 return LDAP_SUCCESS;
2463 if ( rs->sr_err != LDAP_SUCCESS &&
2464 rs->sr_err != LDAP_REFERRAL &&
2465 rs->sr_err != LDAP_ALREADY_EXISTS &&
2466 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2467 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2469 Debug( LDAP_DEBUG_ANY,
2470 "null_callback : error code 0x%x\n",
2473 return LDAP_SUCCESS;
2476 static struct berval *
2477 slap_uuidstr_from_normalized(
2478 struct berval* uuidstr,
2479 struct berval* normalized,
2483 unsigned char nibble;
2486 if ( normalized == NULL ) return NULL;
2487 if ( normalized->bv_len != 16 ) return NULL;
2492 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2493 if ( new == NULL ) {
2500 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2501 if ( new != uuidstr ) {
2502 slap_sl_free( new, ctx );
2507 for ( i = 0; i < 16; i++ ) {
2508 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2509 new->bv_val[(i<<1)+d] = '-';
2513 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2514 if ( nibble < 10 ) {
2515 new->bv_val[(i<<1)+d] = nibble + '0';
2517 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2520 nibble = (normalized->bv_val[i]) & 0xF;
2521 if ( nibble < 10 ) {
2522 new->bv_val[(i<<1)+d+1] = nibble + '0';
2524 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2528 new->bv_val[new->bv_len] = '\0';
2533 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2535 const struct berval *uuid1 = v_uuid1;
2536 const struct berval *uuid2 = v_uuid2;
2537 int rc = uuid1->bv_len - uuid2->bv_len;
2538 if ( rc ) return rc;
2539 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2543 avl_ber_bvfree( void *v_bv )
2545 struct berval *bv = (struct berval *)v_bv;
2547 if( v_bv == NULL ) return;
2548 if ( !BER_BVISNULL( bv ) ) {
2549 ch_free( bv->bv_val );
2551 ch_free( (char *) bv );
2555 syncinfo_free( syncinfo_t *sie )
2558 if ( sie->si_conn_setup ) {
2560 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2561 connection_client_stop( s );
2562 sie->si_conn_setup = 0;
2564 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2567 /* re-fetch it, in case it was already removed */
2568 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2570 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2571 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2572 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2575 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2577 bindconf_free( &sie->si_bindconf );
2579 if ( sie->si_filterstr.bv_val ) {
2580 ch_free( sie->si_filterstr.bv_val );
2582 if ( sie->si_base.bv_val ) {
2583 ch_free( sie->si_base.bv_val );
2585 if ( sie->si_attrs ) {
2587 while ( sie->si_attrs[i] != NULL ) {
2588 ch_free( sie->si_attrs[i] );
2591 ch_free( sie->si_attrs );
2593 if ( sie->si_exattrs ) {
2595 while ( sie->si_exattrs[i] != NULL ) {
2596 ch_free( sie->si_exattrs[i] );
2599 ch_free( sie->si_exattrs );
2601 if ( sie->si_anlist ) {
2603 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2604 ch_free( sie->si_anlist[i].an_name.bv_val );
2607 ch_free( sie->si_anlist );
2609 if ( sie->si_exanlist ) {
2611 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2612 ch_free( sie->si_exanlist[i].an_name.bv_val );
2615 ch_free( sie->si_exanlist );
2617 if ( sie->si_retryinterval ) {
2618 ch_free( sie->si_retryinterval );
2620 if ( sie->si_retrynum ) {
2621 ch_free( sie->si_retrynum );
2623 if ( sie->si_retrynum_init ) {
2624 ch_free( sie->si_retrynum_init );
2626 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2627 if ( sie->si_presentlist ) {
2628 avl_free( sie->si_presentlist, avl_ber_bvfree );
2630 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2631 struct nonpresent_entry* npe;
2632 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2633 LDAP_LIST_REMOVE( npe, npe_link );
2634 if ( npe->npe_name ) {
2635 if ( npe->npe_name->bv_val ) {
2636 ch_free( npe->npe_name->bv_val );
2638 ch_free( npe->npe_name );
2640 if ( npe->npe_nname ) {
2641 if ( npe->npe_nname->bv_val ) {
2642 ch_free( npe->npe_nname->bv_val );
2644 ch_free( npe->npe_nname );
2653 /* NOTE: used & documented in slapd.conf(5) */
2655 #define PROVIDERSTR "provider"
2656 #define SCHEMASTR "schemachecking"
2657 #define FILTERSTR "filter"
2658 #define SEARCHBASESTR "searchbase"
2659 #define SCOPESTR "scope"
2660 #define ATTRSONLYSTR "attrsonly"
2661 #define ATTRSSTR "attrs"
2662 #define TYPESTR "type"
2663 #define INTERVALSTR "interval"
2664 #define RETRYSTR "retry"
2665 #define SLIMITSTR "sizelimit"
2666 #define TLIMITSTR "timelimit"
2667 #define SYNCDATASTR "syncdata"
2669 /* FIXME: undocumented */
2670 #define LOGBASESTR "logbase"
2671 #define LOGFILTERSTR "logfilter"
2672 #define OLDAUTHCSTR "bindprincipal"
2673 #define EXATTRSSTR "exattrs"
2674 #define MANAGEDSAITSTR "manageDSAit"
2677 #define LASTMODSTR "lastmod"
2678 #define LMGENSTR "gen"
2679 #define LMNOSTR "no"
2680 #define LMREQSTR "req"
2681 #define SRVTABSTR "srvtab"
2682 #define SUFFIXSTR "suffix"
2685 #define GOT_ID 0x0001
2686 #define GOT_PROVIDER 0x0002
2687 #define GOT_BASE 0x0004
2690 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2696 { BER_BVC("base"), LDAP_SCOPE_BASE },
2697 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2698 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2699 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2700 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
2701 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2702 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2703 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2707 static slap_verbmasks datamodes[] = {
2708 { BER_BVC("default"), SYNCDATA_DEFAULT },
2709 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2710 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2715 parse_syncrepl_line(
2723 for ( i = 1; i < c->argc; i++ ) {
2724 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2725 STRLENOF( IDSTR "=" ) ) )
2728 /* '\0' string terminator accounts for '=' */
2729 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2730 if ( lutil_atoi( &tmp, val ) != 0 ) {
2731 snprintf( c->msg, sizeof( c->msg ),
2732 "Error: parse_syncrepl_line: "
2733 "unable to parse syncrepl id \"%s\"", val );
2734 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2737 if ( tmp >= 1000 || tmp < 0 ) {
2738 snprintf( c->msg, sizeof( c->msg ),
2739 "Error: parse_syncrepl_line: "
2740 "syncrepl id %d is out of range [0..999]", tmp );
2741 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2746 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2747 STRLENOF( PROVIDERSTR "=" ) ) )
2749 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2750 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2751 gots |= GOT_PROVIDER;
2752 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2753 STRLENOF( SCHEMASTR "=" ) ) )
2755 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2756 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2757 si->si_schemachecking = 1;
2758 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2759 si->si_schemachecking = 0;
2761 si->si_schemachecking = 1;
2763 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2764 STRLENOF( FILTERSTR "=" ) ) )
2766 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2767 if ( si->si_filterstr.bv_val )
2768 ch_free( si->si_filterstr.bv_val );
2769 ber_str2bv( val, 0, 1, &si->si_filterstr );
2770 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2771 STRLENOF( LOGFILTERSTR "=" ) ) )
2773 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2774 if ( si->si_logfilterstr.bv_val )
2775 ch_free( si->si_logfilterstr.bv_val );
2776 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2777 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2778 STRLENOF( SEARCHBASESTR "=" ) ) )
2783 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2784 if ( si->si_base.bv_val ) {
2785 ch_free( si->si_base.bv_val );
2787 ber_str2bv( val, 0, 0, &bv );
2788 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2789 if ( rc != LDAP_SUCCESS ) {
2790 snprintf( c->msg, sizeof( c->msg ),
2791 "Invalid base DN \"%s\": %d (%s)",
2792 val, rc, ldap_err2string( rc ) );
2793 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2797 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2798 STRLENOF( LOGBASESTR "=" ) ) )
2803 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2804 if ( si->si_logbase.bv_val ) {
2805 ch_free( si->si_logbase.bv_val );
2807 ber_str2bv( val, 0, 0, &bv );
2808 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2809 if ( rc != LDAP_SUCCESS ) {
2810 snprintf( c->msg, sizeof( c->msg ),
2811 "Invalid logbase DN \"%s\": %d (%s)",
2812 val, rc, ldap_err2string( rc ) );
2813 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2816 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2817 STRLENOF( SCOPESTR "=" ) ) )
2820 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2821 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2822 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2823 si->si_scope = scopes[j].val;
2827 if ( BER_BVISNULL(&scopes[j].key) ) {
2828 snprintf( c->msg, sizeof( c->msg ),
2829 "Error: parse_syncrepl_line: "
2830 "unknown scope \"%s\"", val);
2831 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2834 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2835 STRLENOF( ATTRSONLYSTR ) ) )
2837 si->si_attrsonly = 1;
2838 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2839 STRLENOF( ATTRSSTR "=" ) ) )
2841 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2842 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2844 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2845 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2846 if ( si->si_anlist == NULL ) {
2847 ch_free( attr_fname );
2850 si->si_anfile = attr_fname;
2852 char *str, *s, *next;
2853 char delimstr[] = " ,\t";
2854 str = ch_strdup( val );
2855 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2857 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2859 if ( strlen(s) == 1 && *s == '*' ) {
2860 si->si_allattrs = 1;
2861 *(val + ( s - str )) = delimstr[0];
2863 if ( strlen(s) == 1 && *s == '+' ) {
2864 si->si_allopattrs = 1;
2865 *(val + ( s - str )) = delimstr[0];
2869 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2870 if ( si->si_anlist == NULL ) {
2874 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2875 STRLENOF( EXATTRSSTR "=" ) ) )
2877 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2878 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2880 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2881 si->si_exanlist = file2anlist(
2882 si->si_exanlist, attr_fname, " ,\t" );
2883 if ( si->si_exanlist == NULL ) {
2884 ch_free( attr_fname );
2887 ch_free( attr_fname );
2889 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2890 if ( si->si_exanlist == NULL ) {
2894 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2895 STRLENOF( TYPESTR "=" ) ) )
2897 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2898 if ( !strncasecmp( val, "refreshOnly",
2899 STRLENOF("refreshOnly") ) )
2901 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2902 } else if ( !strncasecmp( val, "refreshAndPersist",
2903 STRLENOF("refreshAndPersist") ) )
2905 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2906 si->si_interval = 60;
2908 snprintf( c->msg, sizeof( c->msg ),
2909 "Error: parse_syncrepl_line: "
2910 "unknown sync type \"%s\"", val);
2911 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2914 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2915 STRLENOF( INTERVALSTR "=" ) ) )
2917 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2918 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2919 si->si_interval = 0;
2920 } else if ( strchr( val, ':' ) != NULL ) {
2921 char *next, *ptr = val;
2922 unsigned dd, hh, mm, ss;
2924 /* NOTE: the test for ptr[ 0 ] == '-'
2925 * should go before the call to strtoul() */
2926 dd = strtoul( ptr, &next, 10 );
2927 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
2928 snprintf( c->msg, sizeof( c->msg ),
2929 "Error: parse_syncrepl_line: "
2930 "invalid interval \"%s\", unable to parse days", val );
2931 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2935 hh = strtoul( ptr, &next, 10 );
2936 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
2937 snprintf( c->msg, sizeof( c->msg ),
2938 "Error: parse_syncrepl_line: "
2939 "invalid interval \"%s\", unable to parse hours", val );
2940 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2944 mm = strtoul( ptr, &next, 10 );
2945 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
2946 snprintf( c->msg, sizeof( c->msg ),
2947 "Error: parse_syncrepl_line: "
2948 "invalid interval \"%s\", unable to parse minutes", val );
2949 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2953 ss = strtoul( ptr, &next, 10 );
2954 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
2955 snprintf( c->msg, sizeof( c->msg ),
2956 "Error: parse_syncrepl_line: "
2957 "invalid interval \"%s\", unable to parse seconds", val );
2958 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2961 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2965 if ( lutil_parse_time( val, &t ) != 0 ) {
2966 snprintf( c->msg, sizeof( c->msg ),
2967 "Error: parse_syncrepl_line: "
2968 "invalid interval \"%s\"", val );
2969 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2972 si->si_interval = (time_t)t;
2974 if ( si->si_interval < 0 ) {
2975 snprintf( c->msg, sizeof( c->msg ),
2976 "Error: parse_syncrepl_line: "
2977 "invalid interval \"%ld\"",
2978 (long) si->si_interval);
2979 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2982 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
2983 STRLENOF( RETRYSTR "=" ) ) )
2988 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
2989 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2990 retry_list[0] = NULL;
2992 slap_str2clist( &retry_list, val, " ,\t" );
2994 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2997 snprintf( c->msg, sizeof( c->msg ),
2998 "Error: incomplete syncrepl retry list" );
2999 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3000 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3001 ch_free( retry_list[k] );
3003 ch_free( retry_list );
3006 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3007 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3008 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3009 for ( j = 0; j < n; j++ ) {
3011 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3012 snprintf( c->msg, sizeof( c->msg ),
3013 "Error: invalid retry interval \"%s\" (#%d)",
3014 retry_list[j*2], j );
3015 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3016 /* do some cleanup */
3019 si->si_retryinterval[j] = (time_t)t;
3020 if ( *retry_list[j*2+1] == '+' ) {
3021 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3022 si->si_retrynum[j] = RETRYNUM_FOREVER;
3026 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3027 || si->si_retrynum_init[j] <= 0 )
3029 snprintf( c->msg, sizeof( c->msg ),
3030 "Error: invalid initial retry number \"%s\" (#%d)",
3031 retry_list[j*2+1], j );
3032 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3033 /* do some cleanup */
3036 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3037 || si->si_retrynum[j] <= 0 )
3039 snprintf( c->msg, sizeof( c->msg ),
3040 "Error: invalid retry number \"%s\" (#%d)",
3041 retry_list[j*2+1], j );
3042 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3043 /* do some cleanup */
3048 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3049 si->si_retrynum[j] = RETRYNUM_TAIL;
3050 si->si_retryinterval[j] = 0;
3052 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3053 ch_free( retry_list[k] );
3055 ch_free( retry_list );
3056 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3057 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3059 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3060 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3061 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3063 snprintf( c->msg, sizeof( c->msg ),
3064 "invalid manageDSAit value \"%s\".\n",
3066 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3069 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3070 STRLENOF( SLIMITSTR "=") ) )
3072 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3073 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3076 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3077 snprintf( c->msg, sizeof( c->msg ),
3078 "invalid size limit value \"%s\".\n",
3080 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3083 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3084 STRLENOF( TLIMITSTR "=" ) ) )
3086 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3087 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3090 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3091 snprintf( c->msg, sizeof( c->msg ),
3092 "invalid time limit value \"%s\".\n",
3094 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3097 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3098 STRLENOF( SYNCDATASTR "=" ) ) )
3100 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3101 si->si_syncdata = verb_to_mask( val, datamodes );
3102 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3103 snprintf( c->msg, sizeof( c->msg ),
3104 "Error: parse_syncrepl_line: "
3105 "unknown keyword \"%s\"\n", c->argv[ i ] );
3106 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3111 if ( gots != GOT_ALL ) {
3112 snprintf( c->msg, sizeof( c->msg ),
3113 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3114 gots & GOT_ID ? "" : " "IDSTR,
3115 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3116 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3117 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3131 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3132 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3133 "operations required for syncrepl", c->be->be_type );
3134 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3137 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3138 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3139 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3142 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3145 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3149 si->si_bindconf.sb_tls = SB_TLS_OFF;
3150 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3151 si->si_schemachecking = 0;
3152 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3153 &si->si_filterstr );
3154 si->si_base.bv_val = NULL;
3155 si->si_scope = LDAP_SCOPE_SUBTREE;
3156 si->si_attrsonly = 0;
3157 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3158 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3159 si->si_attrs = NULL;
3160 si->si_allattrs = 0;
3161 si->si_allopattrs = 0;
3162 si->si_exattrs = NULL;
3163 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3164 si->si_interval = 86400;
3165 si->si_retryinterval = NULL;
3166 si->si_retrynum_init = NULL;
3167 si->si_retrynum = NULL;
3168 si->si_manageDSAit = 0;
3171 si->si_conn_setup = 0;
3173 si->si_presentlist = NULL;
3174 LDAP_LIST_INIT( &si->si_nonpresentlist );
3175 ldap_pvt_thread_mutex_init( &si->si_mutex );
3177 rc = parse_syncrepl_line( c, si );
3181 init_syncrepl( si );
3182 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3183 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3188 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3189 syncinfo_free( si );
3192 Debug( LDAP_DEBUG_CONFIG,
3193 "Config: ** successfully added syncrepl \"%s\"\n",
3194 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3195 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3196 if ( !si->si_schemachecking ) {
3197 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3199 c->be->be_syncinfo = si;
3205 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3207 struct berval bc, uri;
3208 char buf[BUFSIZ*2], *ptr;
3211 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3215 /* temporarily inhibit bindconf from printing URI */
3216 uri = si->si_bindconf.sb_uri;
3217 BER_BVZERO( &si->si_bindconf.sb_uri );
3218 bindconf_unparse( &si->si_bindconf, &bc );
3219 si->si_bindconf.sb_uri = uri;
3222 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3223 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3224 if ( ptr - buf >= sizeof( buf ) ) return;
3225 if ( !BER_BVISNULL( &bc )) {
3226 if ( WHATSLEFT <= bc.bv_len ) {
3230 ptr = lutil_strcopy( ptr, bc.bv_val );
3233 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3234 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3235 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3236 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3239 if ( !BER_BVISNULL( &si->si_base )) {
3240 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3241 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3242 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3245 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3246 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3247 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3248 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3251 if ( !BER_BVISNULL( &si->si_logbase )) {
3252 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3253 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3254 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3257 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3258 if ( si->si_scope == scopes[i].val ) {
3259 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3260 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3261 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3265 if ( si->si_attrsonly ) {
3266 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3267 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3269 if ( si->si_anfile ) {
3270 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3271 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3272 ptr = lutil_strcopy( ptr, si->si_anfile );
3274 } else if ( si->si_allattrs || si->si_allopattrs ||
3275 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3279 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3280 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3282 /* FIXME: add check for overflow */
3283 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3284 if ( si->si_allattrs ) {
3285 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3286 if ( old != ptr ) *ptr++ = ',';
3289 if ( si->si_allopattrs ) {
3290 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3291 if ( old != ptr ) *ptr++ = ',';
3296 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3297 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3298 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3299 /* FIXME: add check for overflow */
3300 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3302 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3303 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3304 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3306 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3307 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3308 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3309 "refreshAndPersist" : "refreshOnly" );
3311 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3314 dd = si->si_interval;
3321 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3322 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3323 if ( ptr - buf >= sizeof( buf ) ) return;
3324 } else if ( si->si_retryinterval ) {
3326 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3327 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3328 for (i=0; si->si_retryinterval[i]; i++) {
3329 if ( space ) *ptr++ = ' ';
3331 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3332 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3335 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3337 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3341 if ( si->si_slimit ) {
3342 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3343 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3344 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3347 if ( si->si_tlimit ) {
3348 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3349 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3350 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3353 if ( si->si_syncdata ) {
3354 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3355 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3356 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3357 ptr = lutil_strcopy( ptr, bc.bv_val );
3360 bc.bv_len = ptr - buf;
3362 ber_dupbv( bv, &bc );
3366 syncrepl_config( ConfigArgs *c )
3368 if (c->op == SLAP_CONFIG_EMIT) {
3369 if ( c->be->be_syncinfo ) {
3371 syncrepl_unparse( c->be->be_syncinfo, &bv );
3372 ber_bvarray_add( &c->rvalue_vals, &bv );
3376 } else if ( c->op == LDAP_MOD_DELETE ) {
3377 if ( c->be->be_syncinfo ) {
3378 syncinfo_free( c->be->be_syncinfo );
3379 c->be->be_syncinfo = NULL;
3381 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3384 if ( SLAP_SHADOW( c->be ) ) {
3385 Debug(LDAP_DEBUG_ANY, "%s: "
3386 "syncrepl: database already shadowed.\n",
3389 } else if ( add_syncrepl( c ) ) {
3392 return config_sync_shadow( c );