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 */
596 /* NOTE: make sure we use the right one;
597 * a better approach would be to run thru
598 * the whole list and take care of all */
599 rctrlp = ldap_find_control( LDAP_CONTROL_SYNC_STATE, rctrls );
601 if ( rctrlp == NULL ) {
602 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
603 "got search entry without "
604 "Sync State control\n", 0, 0, 0 );
608 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
609 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
610 /* FIXME: what if syncUUID is NULL or empty?
611 * (happens with back-sql...) */
612 if ( BER_BVISEMPTY( &syncUUID ) ) {
613 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
614 "got empty syncUUID\n", 0, 0, 0 );
615 ldap_controls_free( rctrls );
619 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
620 ber_scanf( ber, /*"{"*/ "m}", &cookie );
621 if ( !BER_BVISNULL( &cookie ) ) {
622 ch_free( syncCookie.octet_str.bv_val );
623 ber_dupbv( &syncCookie.octet_str, &cookie );
625 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
627 slap_parse_sync_cookie( &syncCookie, NULL );
631 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
633 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
634 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
635 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
637 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
638 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
639 if (( rc = syncrepl_entry( si, op, entry, &modlist,
640 syncstate, &syncUUID, &syncCookie_req,
641 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
642 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
643 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
646 ldap_controls_free( rctrls );
648 slap_mods_free( modlist, 1 );
654 case LDAP_RES_SEARCH_REFERENCE:
655 Debug( LDAP_DEBUG_ANY,
656 "do_syncrep2: reference received error\n", 0, 0, 0 );
659 case LDAP_RES_SEARCH_RESULT:
660 Debug( LDAP_DEBUG_SYNC,
661 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
662 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
664 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
665 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
666 /* map old result code to registered code */
667 err = LDAP_SYNC_REFRESH_REQUIRED;
670 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
671 if ( si->si_logstate == SYNCLOG_LOGGING ) {
672 si->si_logstate = SYNCLOG_FALLBACK;
679 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
681 ber_scanf( ber, "{" /*"}"*/);
682 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
683 ber_scanf( ber, "m", &cookie );
684 if ( !BER_BVISNULL( &cookie ) ) {
685 ch_free( syncCookie.octet_str.bv_val );
686 ber_dupbv( &syncCookie.octet_str, &cookie);
688 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
690 slap_parse_sync_cookie( &syncCookie, NULL );
693 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
695 ber_scanf( ber, "b", &refreshDeletes );
697 ber_scanf( ber, /*"{"*/ "}" );
699 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
701 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
704 value_match( &match, slap_schema.si_ad_entryCSN,
705 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
706 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
707 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
711 ldap_controls_free( rctrls );
713 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
714 /* FIXME : different error behaviors according to
715 * 1) err code : LDAP_BUSY ...
716 * 2) on err policy : stop service, stop sync, retry
718 if ( refreshDeletes == 0 && match < 0 &&
719 err == LDAP_SUCCESS )
721 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
723 avl_free( si->si_presentlist, avl_ber_bvfree );
724 si->si_presentlist = NULL;
727 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
728 match < 0 && err == LDAP_SUCCESS )
730 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
732 if ( err == LDAP_SUCCESS
733 && si->si_logstate == SYNCLOG_FALLBACK ) {
734 si->si_logstate = SYNCLOG_LOGGING;
735 rc = LDAP_SYNC_REFRESH_REQUIRED;
742 case LDAP_RES_INTERMEDIATE:
743 rc = ldap_parse_intermediate( si->si_ld, msg,
744 &retoid, &retdata, NULL, 0 );
745 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
746 ber_init2( ber, retdata, LBER_USE_DER );
748 switch ( si_tag = ber_peek_tag( ber, &len )) {
750 case LDAP_TAG_SYNC_NEW_COOKIE:
751 Debug( LDAP_DEBUG_SYNC,
752 "do_syncrep2: %s - %s%s\n",
753 "LDAP_RES_INTERMEDIATE",
754 "NEW_COOKIE", "\n" );
755 ber_scanf( ber, "tm", &tag, &cookie );
757 case LDAP_TAG_SYNC_REFRESH_DELETE:
758 case LDAP_TAG_SYNC_REFRESH_PRESENT:
759 Debug( LDAP_DEBUG_SYNC,
760 "do_syncrep2: %s - %s%s\n",
761 "LDAP_RES_INTERMEDIATE",
762 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
763 "REFRESH_PRESENT" : "REFRESH_DELETE",
765 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
766 si->si_refreshDelete = 1;
768 si->si_refreshPresent = 1;
770 ber_scanf( ber, "t{" /*"}"*/, &tag );
771 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
773 ber_scanf( ber, "m", &cookie );
774 if ( !BER_BVISNULL( &cookie ) ) {
775 ch_free( syncCookie.octet_str.bv_val );
776 ber_dupbv( &syncCookie.octet_str, &cookie );
778 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
780 slap_parse_sync_cookie( &syncCookie, NULL );
783 if ( ber_peek_tag( ber, &len ) ==
784 LDAP_TAG_REFRESHDONE )
786 ber_scanf( ber, "b", &refreshDone );
788 ber_scanf( ber, /*"{"*/ "}" );
790 case LDAP_TAG_SYNC_ID_SET:
791 Debug( LDAP_DEBUG_SYNC,
792 "do_syncrep2: %s - %s%s\n",
793 "LDAP_RES_INTERMEDIATE",
796 ber_scanf( ber, "t{" /*"}"*/, &tag );
797 if ( ber_peek_tag( ber, &len ) ==
798 LDAP_TAG_SYNC_COOKIE )
800 ber_scanf( ber, "m", &cookie );
801 if ( !BER_BVISNULL( &cookie ) ) {
802 ch_free( syncCookie.octet_str.bv_val );
803 ber_dupbv( &syncCookie.octet_str, &cookie );
805 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
807 slap_parse_sync_cookie( &syncCookie, NULL );
810 if ( ber_peek_tag( ber, &len ) ==
811 LDAP_TAG_REFRESHDELETES )
813 ber_scanf( ber, "b", &refreshDeletes );
815 ber_scanf( ber, "[W]", &syncUUIDs );
816 ber_scanf( ber, /*"{"*/ "}" );
817 if ( refreshDeletes ) {
818 syncrepl_del_nonpresent( op, si, syncUUIDs,
819 &syncCookie.ctxcsn );
820 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
823 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
824 struct berval *syncuuid_bv;
825 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
826 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
827 avl_insert( &si->si_presentlist,
828 (caddr_t) syncuuid_bv,
829 syncuuid_cmp, avl_dup_error );
831 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
833 slap_sync_cookie_free( &syncCookie, 0 );
836 Debug( LDAP_DEBUG_ANY,
837 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
838 (long) si_tag, 0, 0 );
839 ldap_memfree( retoid );
840 ber_bvfree( retdata );
844 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
846 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
849 value_match( &match, slap_schema.si_ad_entryCSN,
850 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
851 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
852 &syncCookie_req.ctxcsn,
853 &syncCookie.ctxcsn, &text );
857 if ( si->si_refreshPresent == 1 ) {
858 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
861 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
863 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
867 ldap_memfree( retoid );
868 ber_bvfree( retdata );
872 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
873 "unknown intermediate response (%d)\n",
875 ldap_memfree( retoid );
876 ber_bvfree( retdata );
882 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
883 "unknown message\n", 0, 0, 0 );
887 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
888 slap_sync_cookie_free( &syncCookie_req, 0 );
889 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
890 slap_sync_cookie_free( &syncCookie, 0 );
900 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
901 errstr = ldap_err2string( rc );
903 Debug( LDAP_DEBUG_ANY,
904 "do_syncrep2 : %s\n", errstr, 0, 0 );
908 slap_sync_cookie_free( &syncCookie, 0 );
909 slap_sync_cookie_free( &syncCookie_req, 0 );
911 if ( res ) ldap_msgfree( res );
913 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
914 if ( si->si_conn_setup ) {
916 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
917 connection_client_stop( s );
918 si->si_conn_setup = 0;
920 ldap_unbind_ext( si->si_ld, NULL, NULL );
932 struct re_s* rtask = arg;
933 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
934 Connection conn = {0};
935 OperationBuffer opbuf;
937 int rc = LDAP_SUCCESS;
943 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
948 ldap_pvt_thread_mutex_lock( &si->si_mutex );
950 switch( abs( si->si_type )) {
951 case LDAP_SYNC_REFRESH_ONLY:
952 case LDAP_SYNC_REFRESH_AND_PERSIST:
955 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
959 if ( slapd_shutdown ) {
961 if ( si->si_conn_setup ) {
962 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
963 connection_client_stop( s );
964 si->si_conn_setup = 0;
966 ldap_unbind_ext( si->si_ld, NULL, NULL );
969 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
973 op = (Operation *) &opbuf;
974 connection_fake_init( &conn, op, ctx );
976 /* use global malloc for now */
977 op->o_tmpmemctx = NULL;
978 op->o_tmpmfuncs = &ch_mfuncs;
980 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
981 op->o_bd = be = si->si_be;
982 op->o_dn = op->o_bd->be_rootdn;
983 op->o_ndn = op->o_bd->be_rootndn;
985 /* Establish session, do search */
987 si->si_refreshDelete = 0;
988 si->si_refreshPresent = 0;
989 rc = do_syncrep1( op, si );
993 /* Process results */
994 if ( rc == LDAP_SUCCESS ) {
995 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
997 rc = do_syncrep2( op, si );
998 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
999 rc = ldap_sync_search( si, op->o_tmpmemctx );
1003 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1004 /* If we succeeded, enable the connection for further listening.
1005 * If we failed, tear down the connection and reschedule.
1007 if ( rc == LDAP_SUCCESS ) {
1008 if ( si->si_conn_setup ) {
1009 connection_client_enable( s );
1011 rc = connection_client_setup( s, do_syncrepl, arg );
1013 si->si_conn_setup = 1;
1015 } else if ( si->si_conn_setup ) {
1019 if ( rc == -2 ) rc = 0;
1023 /* At this point, we have 4 cases:
1024 * 1) for any hard failure, give up and remove this task
1025 * 2) for ServerDown, reschedule this task to run
1026 * 3) for Refresh and Success, reschedule to run
1027 * 4) for Persist and Success, reschedule to defer
1029 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1031 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1032 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1036 connection_client_stop( s );
1039 if ( rc == LDAP_SUCCESS ) {
1040 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1043 rtask->interval.tv_sec = si->si_interval;
1044 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1045 if ( si->si_retrynum ) {
1046 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1047 si->si_retrynum[i] = si->si_retrynum_init[i];
1049 si->si_retrynum[i] = RETRYNUM_TAIL;
1052 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1053 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1057 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1058 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1059 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1060 if ( si->si_retrynum[i] > 0 )
1061 si->si_retrynum[i]--;
1062 rtask->interval.tv_sec = si->si_retryinterval[i];
1063 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1064 slap_wake_listener();
1068 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1069 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1074 static slap_verbmasks modops[] = {
1075 { BER_BVC("add"), LDAP_REQ_ADD },
1076 { BER_BVC("delete"), LDAP_REQ_DELETE },
1077 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1078 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1082 static Modifications *
1083 syncrepl_accesslog_mods(
1090 AttributeDescription *ad;
1091 struct berval bv, bv2;
1093 Modifications *mod = NULL, *modlist = NULL, **modtail;
1098 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1102 colon = ber_bvchr( &bv, ':' );
1104 continue; /* invalid */
1105 bv.bv_len = colon - bv.bv_val;
1106 if ( slap_bv2ad( &bv, &ad, &text )) {
1110 /* Ignore dynamically generated attrs */
1111 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1113 /* Ignore excluded attrs */
1114 if ( ldap_charray_inlist( si->si_exattrs,
1115 ad->ad_type->sat_cname.bv_val ))
1119 case '+': op = LDAP_MOD_ADD; break;
1120 case '-': op = LDAP_MOD_DELETE; break;
1121 case '=': op = LDAP_MOD_REPLACE; break;
1122 case '#': op = LDAP_MOD_INCREMENT; break;
1126 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1127 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1130 mod->sml_next = NULL;
1132 mod->sml_type = ad->ad_cname;
1133 mod->sml_values = NULL;
1134 mod->sml_nvalues = NULL;
1137 modtail = &mod->sml_next;
1139 if ( colon[2] == ' ' ) {
1140 bv.bv_val = colon + 3;
1141 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1142 ber_dupbv( &bv2, &bv );
1143 ber_bvarray_add( &mod->sml_values, &bv2 );
1149 static Modifications *
1150 syncrepl_changelog_mods(
1155 return NULL; /* FIXME */
1159 syncrepl_message_to_op(
1165 BerElement *ber = NULL;
1166 Modifications *modlist = NULL;
1168 SlapReply rs = { REP_RESULT };
1169 slap_callback cb = { NULL, null_callback, NULL, NULL };
1172 char txtbuf[SLAP_TEXT_BUFLEN];
1173 size_t textlen = sizeof txtbuf;
1175 struct berval bdn, dn = BER_BVNULL, ndn;
1176 struct berval bv, *bvals = NULL;
1177 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1178 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1179 psup = BER_BVNULL, nsup = BER_BVNULL;
1180 int rc, deleteOldRdn = 0;
1182 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1183 Debug( LDAP_DEBUG_ANY,
1184 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1188 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1193 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1195 if ( rc != LDAP_SUCCESS ) {
1196 Debug( LDAP_DEBUG_ANY,
1197 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1201 op->o_tag = LBER_DEFAULT;
1203 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1205 if ( bv.bv_val == NULL )
1208 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1210 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1211 ber_dupbv( &op->o_req_dn, &dn );
1212 ber_dupbv( &op->o_req_ndn, &ndn );
1213 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1214 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1215 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1216 int i = verb_to_mask( bvals[0].bv_val, modops );
1218 Debug( LDAP_DEBUG_ANY,
1219 "syncrepl_message_to_op : unknown op %s",
1220 bvals[0].bv_val, 0, 0 );
1225 op->o_tag = modops[i].mask;
1226 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1227 /* Parse attribute into modlist */
1228 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1229 modlist = syncrepl_accesslog_mods( si, bvals );
1231 modlist = syncrepl_changelog_mods( si, bvals );
1232 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1234 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1235 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1237 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1239 } else if ( !ber_bvstrcasecmp( &bv,
1240 &slap_schema.si_ad_entryCSN->ad_cname )) {
1241 slap_queue_csn( op, bvals );
1246 /* If we didn't get a mod type or a target DN, bail out */
1247 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1252 op->o_callback = &cb;
1253 slap_op_time( &op->o_time, &op->o_tincr );
1255 switch( op->o_tag ) {
1257 case LDAP_REQ_MODIFY:
1258 /* If we didn't get required data, bail */
1259 if ( !modlist ) goto done;
1261 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1263 if ( rc != LDAP_SUCCESS ) {
1264 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1269 if ( op->o_tag == LDAP_REQ_ADD ) {
1270 op->ora_e = entry_alloc();
1271 op->ora_e->e_name = op->o_req_dn;
1272 op->ora_e->e_nname = op->o_req_ndn;
1273 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1274 if( rc != LDAP_SUCCESS ) {
1275 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1278 rc = op->o_bd->be_add( op, &rs );
1279 Debug( LDAP_DEBUG_SYNC,
1280 "syncrepl_message_to_op: be_add %s (%d)\n",
1281 op->o_req_dn.bv_val, rc, 0 );
1283 be_entry_release_w( op, op->ora_e );
1285 op->orm_modlist = modlist;
1286 rc = op->o_bd->be_modify( op, &rs );
1287 Debug( LDAP_DEBUG_SYNC,
1288 "syncrepl_message_to_op: be_modify %s (%d)\n",
1289 op->o_req_dn.bv_val, rc, 0 );
1292 case LDAP_REQ_MODRDN:
1293 if ( BER_BVISNULL( &rdn )) goto done;
1295 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1297 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1299 if ( !BER_BVISNULL( &sup )) {
1300 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1302 op->orr_newSup = &psup;
1303 op->orr_nnewSup = ⊅
1305 op->orr_newrdn = prdn;
1306 op->orr_nnewrdn = nrdn;
1307 op->orr_deleteoldrdn = deleteOldRdn;
1308 op->orr_modlist = NULL;
1309 if ( slap_modrdn2mods( op, &rs ))
1311 rc = op->o_bd->be_modrdn( op, &rs );
1312 slap_mods_free( op->orr_modlist, 1 );
1313 Debug( LDAP_DEBUG_SYNC,
1314 "syncrepl_message_to_op: be_modrdn %s (%d)\n",
1315 op->o_req_dn.bv_val, rc, 0 );
1317 case LDAP_REQ_DELETE:
1318 rc = op->o_bd->be_delete( op, &rs );
1319 Debug( LDAP_DEBUG_SYNC,
1320 "syncrepl_message_to_op: be_delete %s (%d)\n",
1321 op->o_req_dn.bv_val, rc, 0 );
1325 slap_graduate_commit_csn( op );
1327 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1328 if ( !BER_BVISNULL( &rdn )) {
1329 if ( !BER_BVISNULL( &nsup ))
1330 ch_free( nsup.bv_val );
1331 if ( !BER_BVISNULL( &psup ))
1332 ch_free( psup.bv_val );
1333 if ( !BER_BVISNULL( &nrdn ))
1334 ch_free( nrdn.bv_val );
1335 if ( !BER_BVISNULL( &prdn ))
1336 ch_free( prdn.bv_val );
1338 ber_free ( ber, 0 );
1343 syncrepl_message_to_entry(
1347 Modifications **modlist,
1353 BerElement *ber = NULL;
1356 Modifications **modtail = modlist;
1359 char txtbuf[SLAP_TEXT_BUFLEN];
1360 size_t textlen = sizeof txtbuf;
1362 struct berval bdn = {0, NULL}, dn, ndn;
1367 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1368 Debug( LDAP_DEBUG_ANY,
1369 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1373 op->o_tag = LDAP_REQ_ADD;
1375 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1377 if ( rc != LDAP_SUCCESS ) {
1378 Debug( LDAP_DEBUG_ANY,
1379 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1383 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1384 ber_dupbv( &op->o_req_dn, &dn );
1385 ber_dupbv( &op->o_req_ndn, &ndn );
1386 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1387 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1389 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1392 return LDAP_SUCCESS;
1395 if ( entry == NULL ) {
1400 e->e_name = op->o_req_dn;
1401 e->e_nname = op->o_req_ndn;
1403 while ( ber_remaining( ber ) ) {
1404 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1405 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1410 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1412 mod->sml_op = LDAP_MOD_REPLACE;
1414 mod->sml_next = NULL;
1415 mod->sml_desc = NULL;
1416 mod->sml_type = tmp.sml_type;
1417 mod->sml_values = tmp.sml_values;
1418 mod->sml_nvalues = NULL;
1421 modtail = &mod->sml_next;
1424 if ( *modlist == NULL ) {
1425 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1431 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1433 if ( rc != LDAP_SUCCESS ) {
1434 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1439 /* Strip out dynamically generated attrs */
1440 for ( modtail = modlist; *modtail ; ) {
1442 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1443 *modtail = mod->sml_next;
1444 slap_mod_free( &mod->sml_mod, 0 );
1447 modtail = &mod->sml_next;
1451 /* Strip out attrs in exattrs list */
1452 for ( modtail = modlist; *modtail ; ) {
1454 if ( ldap_charray_inlist( si->si_exattrs,
1455 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1456 *modtail = mod->sml_next;
1457 slap_mod_free( &mod->sml_mod, 0 );
1460 modtail = &mod->sml_next;
1464 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1465 if( rc != LDAP_SUCCESS ) {
1466 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1471 ber_free ( ber, 0 );
1472 if ( rc != LDAP_SUCCESS ) {
1483 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1485 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1486 * entry if a previous refresh was interrupted before sending us a new
1487 * context state. We try to compare the new entry to the existing entry
1488 * and ignore the new entry if they are the same.
1490 * Also, we may get an update where the entryDN has changed, due to
1491 * a ModDn on the provider. We detect this as well, so we can issue
1492 * the corresponding operation locally.
1494 * In the case of a modify, we get a list of all the attributes
1495 * in the original entry. Rather than deleting the entry and re-adding it,
1496 * we issue a Modify request that deletes all the attributes and adds all
1497 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1500 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1501 * a ModDN we will issue both operations on the local database.
1503 typedef struct dninfo {
1507 int renamed; /* Was an existing entry renamed? */
1508 int wasChanged; /* are the attributes changed? */
1509 int attrs; /* how many attribute types are in the ads list */
1510 AttributeDescription **ads;
1518 Modifications** modlist,
1520 struct berval* syncUUID,
1521 struct sync_cookie* syncCookie_req,
1522 struct berval* syncCSN )
1524 Backend *be = op->o_bd;
1525 slap_callback cb = { NULL, NULL, NULL, NULL };
1526 struct berval *syncuuid_bv = NULL;
1527 struct berval syncUUID_strrep = BER_BVNULL;
1528 struct berval uuid_bv = BER_BVNULL;
1530 SlapReply rs_search = {REP_RESULT};
1531 SlapReply rs_delete = {REP_RESULT};
1532 SlapReply rs_add = {REP_RESULT};
1533 SlapReply rs_modify = {REP_RESULT};
1535 #ifdef LDAP_COMP_MATCH
1536 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1538 AttributeAssertion ava = { NULL, BER_BVNULL };
1540 int rc = LDAP_SUCCESS;
1542 struct berval pdn = BER_BVNULL;
1546 switch( syncstate ) {
1547 case LDAP_SYNC_PRESENT:
1548 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1550 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1553 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1555 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1557 case LDAP_SYNC_DELETE:
1558 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1560 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1562 case LDAP_SYNC_MODIFY:
1563 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1565 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1568 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1570 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1573 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1574 if ( !si->si_refreshPresent ) {
1575 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1576 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1577 syncuuid_cmp, avl_dup_error );
1581 if ( syncstate == LDAP_SYNC_PRESENT ) {
1583 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1584 if ( entry == NULL ) {
1589 f.f_choice = LDAP_FILTER_EQUALITY;
1591 ava.aa_desc = slap_schema.si_ad_entryUUID;
1592 ava.aa_value = *syncUUID;
1594 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1595 if ( syncuuid_bv ) {
1596 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: inserted UUID %s\n",
1597 syncUUID_strrep.bv_val, 0, 0 );
1599 op->ors_filter = &f;
1601 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1602 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1603 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1604 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1605 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1606 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1607 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1608 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1610 op->o_tag = LDAP_REQ_SEARCH;
1611 op->ors_scope = LDAP_SCOPE_SUBTREE;
1612 op->ors_deref = LDAP_DEREF_NEVER;
1614 /* get the entry for this UUID */
1615 op->o_req_dn = si->si_base;
1616 op->o_req_ndn = si->si_base;
1618 op->o_time = slap_get_time();
1619 op->ors_tlimit = SLAP_NO_LIMIT;
1622 op->ors_attrs = slap_anlist_all_attributes;
1623 op->ors_attrsonly = 0;
1625 /* set callback function */
1626 op->o_callback = &cb;
1627 cb.sc_response = dn_callback;
1628 cb.sc_private = &dni;
1629 dni.new_entry = entry;
1631 if ( limits_check( op, &rs_search ) == 0 ) {
1632 rc = be->be_search( op, &rs_search );
1633 Debug( LDAP_DEBUG_SYNC,
1634 "syncrepl_entry: %s (%d)\n",
1635 "be_search", rc, 0 );
1638 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1639 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1642 cb.sc_response = null_callback;
1645 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1646 Debug( LDAP_DEBUG_SYNC,
1647 "syncrepl_entry: %s\n",
1648 entry->e_name.bv_val, 0, 0 );
1650 Debug( LDAP_DEBUG_SYNC,
1651 "syncrepl_entry: %s\n",
1652 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1655 if ( syncstate != LDAP_SYNC_DELETE ) {
1656 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1659 /* add if missing */
1660 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1661 &syncUUID_strrep, syncUUID );
1663 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1664 /* replace only if necessary */
1665 if ( a->a_nvals != a->a_vals ) {
1666 ber_memfree( a->a_nvals[0].bv_val );
1667 ber_dupbv( &a->a_nvals[0], syncUUID );
1669 ber_memfree( a->a_vals[0].bv_val );
1670 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1672 /* Don't save the contextCSN on the inooming context entry,
1673 * we'll write it when syncrepl_updateCookie eventually
1674 * gets called. (ITS#4622)
1676 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1677 &be->be_nsuffix[0] )) {
1679 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1681 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1690 slap_op_time( &op->o_time, &op->o_tincr );
1691 switch ( syncstate ) {
1693 case LDAP_SYNC_MODIFY:
1695 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1697 /* FIXME: op->o_csn is assumed to be
1698 * on the thread's slab; this needs
1699 * to be cleared ASAP.
1700 * What happens if already present?
1702 assert( BER_BVISNULL( &op->o_csn ) );
1703 op->o_csn = a->a_vals[0];
1707 if ( BER_BVISNULL( &dni.dn )) {
1709 op->o_req_dn = entry->e_name;
1710 op->o_req_ndn = entry->e_nname;
1711 op->o_tag = LDAP_REQ_ADD;
1714 rc = be->be_add( op, &rs_add );
1715 Debug( LDAP_DEBUG_SYNC,
1716 "syncrepl_entry: %s (%d)\n",
1718 switch ( rs_add.sr_err ) {
1720 be_entry_release_w( op, entry );
1725 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1726 * only if the suffix entry is not present */
1727 case LDAP_NO_SUCH_OBJECT:
1728 rc = syncrepl_add_glue( op, entry );
1732 /* if an entry was added via syncrepl_add_glue(),
1733 * it likely has no entryUUID, so the previous
1734 * be_search() doesn't find it. In this case,
1735 * give syncrepl a chance to modify it. Also
1736 * allow for entries that were recreated with the
1737 * same DN but a different entryUUID.
1739 case LDAP_ALREADY_EXISTS:
1741 Operation op2 = *op;
1742 SlapReply rs2 = { 0 };
1743 slap_callback cb2 = { 0 };
1745 op2.o_tag = LDAP_REQ_SEARCH;
1746 op2.o_req_dn = entry->e_name;
1747 op2.o_req_ndn = entry->e_nname;
1748 op2.ors_scope = LDAP_SCOPE_BASE;
1749 op2.ors_deref = LDAP_DEREF_NEVER;
1750 op2.ors_attrs = slap_anlist_all_attributes;
1751 op2.ors_attrsonly = 0;
1752 op2.ors_limit = NULL;
1754 op2.ors_tlimit = SLAP_NO_LIMIT;
1756 f.f_choice = LDAP_FILTER_PRESENT;
1757 f.f_desc = slap_schema.si_ad_objectClass;
1758 op2.ors_filter = &f;
1759 op2.ors_filterstr = generic_filterstr;
1761 op2.o_callback = &cb2;
1762 cb2.sc_response = dn_callback;
1763 cb2.sc_private = &dni;
1765 rc = be->be_search( &op2, &rs2 );
1766 if ( rc ) goto done;
1769 slap_op_time( &op->o_time, &op->o_tincr );
1775 Debug( LDAP_DEBUG_ANY,
1776 "syncrepl_entry : be_add failed (%d)\n",
1777 rs_add.sr_err, 0, 0 );
1783 op->o_req_dn = dni.dn;
1784 op->o_req_ndn = dni.ndn;
1785 if ( dni.renamed ) {
1786 struct berval noldp, newp, nnewp;
1788 op->o_tag = LDAP_REQ_MODRDN;
1789 dnRdn( &entry->e_name, &op->orr_newrdn );
1790 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1792 dnParent( &dni.ndn, &noldp );
1793 dnParent( &entry->e_nname, &nnewp );
1794 if ( !dn_match( &noldp, &nnewp )) {
1795 dnParent( &entry->e_name, &newp );
1796 op->orr_newSup = &newp;
1797 op->orr_nnewSup = &nnewp;
1799 op->orr_deleteoldrdn = 0;
1800 op->orr_modlist = NULL;
1801 if (( rc = slap_modrdn2mods( op, &rs_modify ))) {
1805 /* RDNs must be NUL-terminated for back-ldap */
1806 noldp = op->orr_newrdn;
1807 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
1808 noldp = op->orr_nnewrdn;
1809 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
1811 rc = be->be_modrdn( op, &rs_modify );
1812 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
1813 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
1815 slap_mods_free( op->orr_modlist, 1 );
1816 Debug( LDAP_DEBUG_SYNC,
1817 "syncrepl_entry: %s (%d)\n",
1818 "be_modrdn", rc, 0 );
1819 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1820 op->o_req_dn = entry->e_name;
1821 op->o_req_ndn = entry->e_nname;
1825 if ( dni.wasChanged )
1826 slap_op_time( &op->o_time, &op->o_tincr );
1828 if ( dni.wasChanged ) {
1829 Modifications *mod, *modhead = NULL;
1830 Modifications *modtail = NULL;
1833 op->o_tag = LDAP_REQ_MODIFY;
1835 assert( *modlist != NULL );
1837 /* Delete all the old attrs */
1838 for ( i = 0; i < dni.attrs; i++ ) {
1839 mod = ch_malloc( sizeof( Modifications ) );
1840 mod->sml_op = LDAP_MOD_DELETE;
1842 mod->sml_desc = dni.ads[i];
1843 mod->sml_type = mod->sml_desc->ad_cname;
1844 mod->sml_values = NULL;
1845 mod->sml_nvalues = NULL;
1846 if ( !modhead ) modhead = mod;
1848 modtail->sml_next = mod;
1853 /* Append passed in list to ours */
1855 modtail->sml_next = *modlist;
1861 /* Find end of this list */
1862 for ( ; mod != NULL; mod = mod->sml_next ) {
1866 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1867 mod->sml_op = LDAP_MOD_REPLACE;
1869 mod->sml_desc = slap_schema.si_ad_entryUUID;
1870 mod->sml_type = mod->sml_desc->ad_cname;
1871 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1872 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1873 ber_dupbv( &uuid_bv, syncUUID );
1874 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1875 modtail->sml_next = mod;
1877 op->o_tag = LDAP_REQ_MODIFY;
1878 op->orm_modlist = *modlist;
1880 rc = be->be_modify( op, &rs_modify );
1881 Debug( LDAP_DEBUG_SYNC,
1882 "syncrepl_entry: %s (%d)\n",
1883 "be_modify", rc, 0 );
1884 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1885 Debug( LDAP_DEBUG_ANY,
1886 "syncrepl_entry : be_modify failed (%d)\n",
1887 rs_modify.sr_err, 0, 0 );
1891 case LDAP_SYNC_DELETE :
1892 if ( !BER_BVISNULL( &dni.dn )) {
1893 op->o_req_dn = dni.dn;
1894 op->o_req_ndn = dni.ndn;
1895 op->o_tag = LDAP_REQ_DELETE;
1896 rc = be->be_delete( op, &rs_delete );
1897 Debug( LDAP_DEBUG_SYNC,
1898 "syncrepl_entry: %s (%d)\n",
1899 "be_delete", rc, 0 );
1901 while ( rs_delete.sr_err == LDAP_SUCCESS
1902 && op->o_delete_glue_parent ) {
1903 op->o_delete_glue_parent = 0;
1904 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1905 slap_callback cb = { NULL };
1906 cb.sc_response = slap_null_cb;
1907 dnParent( &op->o_req_ndn, &pdn );
1909 op->o_req_ndn = pdn;
1910 op->o_callback = &cb;
1911 op->o_bd->be_delete( op, &rs_delete );
1920 Debug( LDAP_DEBUG_ANY,
1921 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1926 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1927 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1928 BER_BVZERO( &syncUUID_strrep );
1931 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1933 if ( !BER_BVISNULL( &dni.ndn ) ) {
1934 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1936 if ( !BER_BVISNULL( &dni.dn ) ) {
1937 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1940 entry_free( entry );
1941 BER_BVZERO( &op->o_csn );
1945 static struct berval gcbva[] = {
1951 #define NP_DELETE_ONE 2
1954 syncrepl_del_nonpresent(
1958 struct berval *cookiecsn )
1960 Backend* be = op->o_bd;
1961 slap_callback cb = { NULL };
1962 SlapReply rs_search = {REP_RESULT};
1963 SlapReply rs_delete = {REP_RESULT};
1964 SlapReply rs_modify = {REP_RESULT};
1965 struct nonpresent_entry *np_list, *np_prev;
1967 AttributeName an[2];
1969 struct berval pdn = BER_BVNULL;
1972 op->o_req_dn = si->si_base;
1973 op->o_req_ndn = si->si_base;
1975 cb.sc_response = nonpresent_callback;
1978 op->o_callback = &cb;
1979 op->o_tag = LDAP_REQ_SEARCH;
1980 op->ors_scope = si->si_scope;
1981 op->ors_deref = LDAP_DEREF_NEVER;
1982 op->o_time = slap_get_time();
1983 op->ors_tlimit = SLAP_NO_LIMIT;
1988 #ifdef LDAP_COMP_MATCH
1989 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1991 AttributeAssertion eq = { NULL, BER_BVNULL };
1995 op->ors_attrsonly = 1;
1996 op->ors_attrs = slap_anlist_no_attrs;
1997 op->ors_limit = NULL;
1998 op->ors_filter = &uf;
2001 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2003 uf.f_choice = LDAP_FILTER_EQUALITY;
2004 si->si_refreshDelete |= NP_DELETE_ONE;
2006 for (i=0; uuids[i].bv_val; i++) {
2008 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2010 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2011 uf.f_av_value = uuids[i];
2012 rc = be->be_search( op, &rs_search );
2013 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2015 si->si_refreshDelete ^= NP_DELETE_ONE;
2017 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2018 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2019 an[0].an_desc = slap_schema.si_ad_entryUUID;
2021 op->ors_slimit = SLAP_NO_LIMIT;
2022 op->ors_attrsonly = 0;
2023 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2024 op->ors_filterstr = si->si_filterstr;
2025 op->o_nocaching = 1;
2027 if ( limits_check( op, &rs_search ) == 0 ) {
2028 rc = be->be_search( op, &rs_search );
2030 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2033 op->o_nocaching = 0;
2035 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2037 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2040 csn = si->si_syncCookie.ctxcsn;
2042 slap_queue_csn( op, &csn );
2044 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2045 while ( np_list != NULL ) {
2046 LDAP_LIST_REMOVE( np_list, npe_link );
2048 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2049 op->o_tag = LDAP_REQ_DELETE;
2050 op->o_callback = &cb;
2051 cb.sc_response = null_callback;
2053 op->o_req_dn = *np_prev->npe_name;
2054 op->o_req_ndn = *np_prev->npe_nname;
2055 rc = op->o_bd->be_delete( op, &rs_delete );
2056 Debug( LDAP_DEBUG_SYNC,
2057 "syncrepl_del_nonpresent: be_delete %s (%d)\n",
2058 op->o_req_dn.bv_val, rc, 0 );
2060 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2061 Modifications mod1, mod2;
2062 mod1.sml_op = LDAP_MOD_REPLACE;
2064 mod1.sml_desc = slap_schema.si_ad_objectClass;
2065 mod1.sml_type = mod1.sml_desc->ad_cname;
2066 mod1.sml_values = &gcbva[0];
2067 mod1.sml_nvalues = NULL;
2068 mod1.sml_next = &mod2;
2070 mod2.sml_op = LDAP_MOD_REPLACE;
2072 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2073 mod2.sml_type = mod2.sml_desc->ad_cname;
2074 mod2.sml_values = &gcbva[1];
2075 mod2.sml_nvalues = NULL;
2076 mod2.sml_next = NULL;
2078 op->o_tag = LDAP_REQ_MODIFY;
2079 op->orm_modlist = &mod1;
2081 rc = be->be_modify( op, &rs_modify );
2084 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2085 op->o_delete_glue_parent ) {
2086 op->o_delete_glue_parent = 0;
2087 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2088 slap_callback cb = { NULL };
2089 cb.sc_response = slap_null_cb;
2090 dnParent( &op->o_req_ndn, &pdn );
2092 op->o_req_ndn = pdn;
2093 op->o_callback = &cb;
2094 /* give it a root privil ? */
2095 op->o_bd->be_delete( op, &rs_delete );
2101 op->o_delete_glue_parent = 0;
2103 ber_bvfree( np_prev->npe_name );
2104 ber_bvfree( np_prev->npe_nname );
2108 slap_graduate_commit_csn( op );
2110 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2111 BER_BVZERO( &op->o_csn );
2122 Backend *be = op->o_bd;
2123 slap_callback cb = { NULL };
2128 struct berval dn = BER_BVNULL;
2129 struct berval ndn = BER_BVNULL;
2131 SlapReply rs_add = {REP_RESULT};
2132 struct berval ptr, nptr;
2135 op->o_tag = LDAP_REQ_ADD;
2136 op->o_callback = &cb;
2137 cb.sc_response = null_callback;
2138 cb.sc_private = NULL;
2143 /* count RDNs in suffix */
2144 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2145 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2147 ptr.bv_len -= comma - ptr.bv_val;
2157 /* Start with BE suffix */
2159 for ( i = 0; i < suffrdns; i++ ) {
2160 comma = ber_bvrchr( &ptr, ',' );
2161 if ( comma != NULL ) {
2162 ptr.bv_len = comma - ptr.bv_val;
2169 if ( !BER_BVISEMPTY( &ptr ) ) {
2170 dn.bv_len -= ptr.bv_len + 1;
2171 dn.bv_val += ptr.bv_len + 1;
2174 /* the normalizedDNs are always the same length, no counting
2178 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2179 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2180 ndn.bv_len = be->be_nsuffix[0].bv_len;
2182 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2188 while ( ndn.bv_val > e->e_nname.bv_val ) {
2189 glue = entry_alloc();
2190 ber_dupbv( &glue->e_name, &dn );
2191 ber_dupbv( &glue->e_nname, &ndn );
2193 a = attr_alloc( slap_schema.si_ad_objectClass );
2195 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2196 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2197 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2198 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2200 a->a_nvals = a->a_vals;
2202 a->a_next = glue->e_attrs;
2205 a = attr_alloc( slap_schema.si_ad_structuralObjectClass );
2207 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2208 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2209 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2211 a->a_nvals = a->a_vals;
2213 a->a_next = glue->e_attrs;
2216 op->o_req_dn = glue->e_name;
2217 op->o_req_ndn = glue->e_nname;
2219 rc = be->be_add ( op, &rs_add );
2220 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2221 be_entry_release_w( op, glue );
2223 /* incl. ALREADY EXIST */
2225 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2231 /* Move to next child */
2232 comma = ber_bvrchr( &ptr, ',' );
2233 if ( comma == NULL ) {
2236 ptr.bv_len = comma - ptr.bv_val;
2238 dn.bv_val = ++comma;
2239 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2241 comma = ber_bvrchr( &nptr, ',' );
2242 assert( comma != NULL );
2243 nptr.bv_len = comma - nptr.bv_val;
2245 ndn.bv_val = ++comma;
2246 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2249 op->o_req_dn = e->e_name;
2250 op->o_req_ndn = e->e_nname;
2252 rc = be->be_add ( op, &rs_add );
2253 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2254 be_entry_release_w( op, e );
2263 syncrepl_updateCookie(
2267 struct sync_cookie *syncCookie )
2269 Backend *be = op->o_bd;
2270 Modifications mod = { { 0 } };
2271 struct berval vals[ 2 ];
2275 slap_callback cb = { NULL };
2276 SlapReply rs_modify = {REP_RESULT};
2278 mod.sml_op = LDAP_MOD_REPLACE;
2279 mod.sml_desc = slap_schema.si_ad_contextCSN;
2280 mod.sml_type = mod.sml_desc->ad_cname;
2281 mod.sml_values = vals;
2282 vals[0] = syncCookie->ctxcsn;
2283 BER_BVZERO( &vals[1] );
2285 slap_queue_csn( op, &syncCookie->ctxcsn );
2287 op->o_tag = LDAP_REQ_MODIFY;
2289 assert( si->si_rid < 1000 );
2291 cb.sc_response = null_callback;
2294 op->o_callback = &cb;
2295 op->o_req_dn = op->o_bd->be_suffix[0];
2296 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2298 /* update contextCSN */
2299 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2300 op->orm_modlist = &mod;
2301 rc = be->be_modify( op, &rs_modify );
2304 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2305 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2306 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2308 Debug( LDAP_DEBUG_ANY,
2309 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2312 slap_graduate_commit_csn( op );
2314 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2315 BER_BVZERO( &op->o_csn );
2325 dninfo *dni = op->o_callback->sc_private;
2327 if ( rs->sr_type == REP_SEARCH ) {
2328 if ( !BER_BVISNULL( &dni->dn ) ) {
2329 Debug( LDAP_DEBUG_ANY,
2330 "dn_callback : consistency error - "
2331 "entryUUID is not unique\n", 0, 0, 0 );
2333 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2334 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2335 /* If there is a new entry, see if it differs from the old.
2336 * We compare the non-normalized values so that cosmetic changes
2337 * in the provider are always propagated.
2339 if ( dni->new_entry ) {
2340 Attribute *old, *new;
2343 /* Did the DN change? Note that we don't explicitly try to
2344 * discover if the deleteOldRdn argument applies here. It
2345 * would save an unnecessary Modify if we detected it, but
2346 * that's a fair amount of trouble to compare the two attr
2347 * lists in detail. (Just test normalized DN; we ignore
2348 * insignificant changes here.)
2350 if ( !dn_match( &rs->sr_entry->e_nname,
2351 &dni->new_entry->e_nname ) )
2356 for ( i = 0, old = rs->sr_entry->e_attrs;
2358 i++, old = old->a_next )
2363 /* We assume that attributes are saved in the same order
2364 * in the remote and local databases. So if we walk through
2365 * the attributeDescriptions one by one they should match in
2366 * lock step. If not, we signal a change. Otherwise we test
2369 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2371 old = old->a_next, new = new->a_next )
2373 if ( old->a_desc != new->a_desc ) {
2374 dni->wasChanged = 1;
2377 for ( i = 0; ; i++ ) {
2379 nold = BER_BVISNULL( &old->a_vals[i] );
2380 nnew = BER_BVISNULL( &new->a_vals[i] );
2381 /* If both are empty, stop looking */
2382 if ( nold && nnew ) {
2385 /* If they are different, stop looking */
2386 if ( nold != nnew ) {
2387 dni->wasChanged = 1;
2390 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2391 dni->wasChanged = 1;
2395 if ( dni->wasChanged ) break;
2397 if ( dni->wasChanged ) {
2398 dni->ads = op->o_tmpalloc( dni->attrs *
2399 sizeof(AttributeDescription *), op->o_tmpmemctx );
2401 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2402 dni->ads[i] = old->a_desc;
2408 } else if ( rs->sr_type == REP_RESULT ) {
2409 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2410 Debug( LDAP_DEBUG_ANY,
2411 "dn_callback : consistency error - "
2412 "entryUUID is not unique\n", 0, 0, 0 );
2416 return LDAP_SUCCESS;
2420 nonpresent_callback(
2424 syncinfo_t *si = op->o_callback->sc_private;
2427 struct berval* present_uuid = NULL;
2428 struct nonpresent_entry *np_entry;
2430 if ( rs->sr_type == REP_RESULT ) {
2431 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2432 si->si_presentlist = NULL;
2434 } else if ( rs->sr_type == REP_SEARCH ) {
2435 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2436 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2439 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2442 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: UUID %s, dn %s, %sfound\n",
2443 a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val, present_uuid ? "" : "not " );
2445 if ( a == NULL ) return 0;
2448 if ( present_uuid == NULL ) {
2449 np_entry = (struct nonpresent_entry *)
2450 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2451 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2452 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2453 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2456 avl_delete( &si->si_presentlist,
2457 &a->a_nvals[0], syncuuid_cmp );
2458 ch_free( present_uuid->bv_val );
2459 ch_free( present_uuid );
2462 return LDAP_SUCCESS;
2470 if ( rs->sr_err != LDAP_SUCCESS &&
2471 rs->sr_err != LDAP_REFERRAL &&
2472 rs->sr_err != LDAP_ALREADY_EXISTS &&
2473 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2474 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2476 Debug( LDAP_DEBUG_ANY,
2477 "null_callback : error code 0x%x\n",
2480 return LDAP_SUCCESS;
2483 static struct berval *
2484 slap_uuidstr_from_normalized(
2485 struct berval* uuidstr,
2486 struct berval* normalized,
2490 unsigned char nibble;
2493 if ( normalized == NULL ) return NULL;
2494 if ( normalized->bv_len != 16 ) return NULL;
2499 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2500 if ( new == NULL ) {
2507 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2508 if ( new != uuidstr ) {
2509 slap_sl_free( new, ctx );
2514 for ( i = 0; i < 16; i++ ) {
2515 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2516 new->bv_val[(i<<1)+d] = '-';
2520 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2521 if ( nibble < 10 ) {
2522 new->bv_val[(i<<1)+d] = nibble + '0';
2524 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2527 nibble = (normalized->bv_val[i]) & 0xF;
2528 if ( nibble < 10 ) {
2529 new->bv_val[(i<<1)+d+1] = nibble + '0';
2531 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2535 new->bv_val[new->bv_len] = '\0';
2540 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2542 const struct berval *uuid1 = v_uuid1;
2543 const struct berval *uuid2 = v_uuid2;
2544 int rc = uuid1->bv_len - uuid2->bv_len;
2545 if ( rc ) return rc;
2546 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2550 avl_ber_bvfree( void *v_bv )
2552 struct berval *bv = (struct berval *)v_bv;
2554 if( v_bv == NULL ) return;
2555 if ( !BER_BVISNULL( bv ) ) {
2556 ch_free( bv->bv_val );
2558 ch_free( (char *) bv );
2562 syncinfo_free( syncinfo_t *sie )
2565 if ( sie->si_conn_setup ) {
2567 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2568 connection_client_stop( s );
2569 sie->si_conn_setup = 0;
2571 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2574 /* re-fetch it, in case it was already removed */
2575 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2577 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2578 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2579 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2582 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2584 bindconf_free( &sie->si_bindconf );
2586 if ( sie->si_filterstr.bv_val ) {
2587 ch_free( sie->si_filterstr.bv_val );
2589 if ( sie->si_base.bv_val ) {
2590 ch_free( sie->si_base.bv_val );
2592 if ( sie->si_attrs ) {
2594 while ( sie->si_attrs[i] != NULL ) {
2595 ch_free( sie->si_attrs[i] );
2598 ch_free( sie->si_attrs );
2600 if ( sie->si_exattrs ) {
2602 while ( sie->si_exattrs[i] != NULL ) {
2603 ch_free( sie->si_exattrs[i] );
2606 ch_free( sie->si_exattrs );
2608 if ( sie->si_anlist ) {
2610 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2611 ch_free( sie->si_anlist[i].an_name.bv_val );
2614 ch_free( sie->si_anlist );
2616 if ( sie->si_exanlist ) {
2618 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2619 ch_free( sie->si_exanlist[i].an_name.bv_val );
2622 ch_free( sie->si_exanlist );
2624 if ( sie->si_retryinterval ) {
2625 ch_free( sie->si_retryinterval );
2627 if ( sie->si_retrynum ) {
2628 ch_free( sie->si_retrynum );
2630 if ( sie->si_retrynum_init ) {
2631 ch_free( sie->si_retrynum_init );
2633 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2634 if ( sie->si_presentlist ) {
2635 avl_free( sie->si_presentlist, avl_ber_bvfree );
2637 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2638 struct nonpresent_entry* npe;
2639 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2640 LDAP_LIST_REMOVE( npe, npe_link );
2641 if ( npe->npe_name ) {
2642 if ( npe->npe_name->bv_val ) {
2643 ch_free( npe->npe_name->bv_val );
2645 ch_free( npe->npe_name );
2647 if ( npe->npe_nname ) {
2648 if ( npe->npe_nname->bv_val ) {
2649 ch_free( npe->npe_nname->bv_val );
2651 ch_free( npe->npe_nname );
2660 /* NOTE: used & documented in slapd.conf(5) */
2662 #define PROVIDERSTR "provider"
2663 #define SCHEMASTR "schemachecking"
2664 #define FILTERSTR "filter"
2665 #define SEARCHBASESTR "searchbase"
2666 #define SCOPESTR "scope"
2667 #define ATTRSONLYSTR "attrsonly"
2668 #define ATTRSSTR "attrs"
2669 #define TYPESTR "type"
2670 #define INTERVALSTR "interval"
2671 #define RETRYSTR "retry"
2672 #define SLIMITSTR "sizelimit"
2673 #define TLIMITSTR "timelimit"
2674 #define SYNCDATASTR "syncdata"
2676 /* FIXME: undocumented */
2677 #define LOGBASESTR "logbase"
2678 #define LOGFILTERSTR "logfilter"
2679 #define OLDAUTHCSTR "bindprincipal"
2680 #define EXATTRSSTR "exattrs"
2681 #define MANAGEDSAITSTR "manageDSAit"
2684 #define LASTMODSTR "lastmod"
2685 #define LMGENSTR "gen"
2686 #define LMNOSTR "no"
2687 #define LMREQSTR "req"
2688 #define SRVTABSTR "srvtab"
2689 #define SUFFIXSTR "suffix"
2692 #define GOT_ID 0x0001
2693 #define GOT_PROVIDER 0x0002
2694 #define GOT_BASE 0x0004
2697 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2703 { BER_BVC("base"), LDAP_SCOPE_BASE },
2704 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2705 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2706 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2707 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
2708 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2709 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2710 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2714 static slap_verbmasks datamodes[] = {
2715 { BER_BVC("default"), SYNCDATA_DEFAULT },
2716 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2717 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2722 parse_syncrepl_line(
2730 for ( i = 1; i < c->argc; i++ ) {
2731 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2732 STRLENOF( IDSTR "=" ) ) )
2735 /* '\0' string terminator accounts for '=' */
2736 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2737 if ( lutil_atoi( &tmp, val ) != 0 ) {
2738 snprintf( c->msg, sizeof( c->msg ),
2739 "Error: parse_syncrepl_line: "
2740 "unable to parse syncrepl id \"%s\"", val );
2741 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2744 if ( tmp >= 1000 || tmp < 0 ) {
2745 snprintf( c->msg, sizeof( c->msg ),
2746 "Error: parse_syncrepl_line: "
2747 "syncrepl id %d is out of range [0..999]", tmp );
2748 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2753 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2754 STRLENOF( PROVIDERSTR "=" ) ) )
2756 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2757 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2758 gots |= GOT_PROVIDER;
2759 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2760 STRLENOF( SCHEMASTR "=" ) ) )
2762 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2763 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2764 si->si_schemachecking = 1;
2765 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2766 si->si_schemachecking = 0;
2768 si->si_schemachecking = 1;
2770 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2771 STRLENOF( FILTERSTR "=" ) ) )
2773 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2774 if ( si->si_filterstr.bv_val )
2775 ch_free( si->si_filterstr.bv_val );
2776 ber_str2bv( val, 0, 1, &si->si_filterstr );
2777 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2778 STRLENOF( LOGFILTERSTR "=" ) ) )
2780 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2781 if ( si->si_logfilterstr.bv_val )
2782 ch_free( si->si_logfilterstr.bv_val );
2783 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2784 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2785 STRLENOF( SEARCHBASESTR "=" ) ) )
2790 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2791 if ( si->si_base.bv_val ) {
2792 ch_free( si->si_base.bv_val );
2794 ber_str2bv( val, 0, 0, &bv );
2795 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2796 if ( rc != LDAP_SUCCESS ) {
2797 snprintf( c->msg, sizeof( c->msg ),
2798 "Invalid base DN \"%s\": %d (%s)",
2799 val, rc, ldap_err2string( rc ) );
2800 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2804 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2805 STRLENOF( LOGBASESTR "=" ) ) )
2810 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2811 if ( si->si_logbase.bv_val ) {
2812 ch_free( si->si_logbase.bv_val );
2814 ber_str2bv( val, 0, 0, &bv );
2815 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2816 if ( rc != LDAP_SUCCESS ) {
2817 snprintf( c->msg, sizeof( c->msg ),
2818 "Invalid logbase DN \"%s\": %d (%s)",
2819 val, rc, ldap_err2string( rc ) );
2820 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2823 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2824 STRLENOF( SCOPESTR "=" ) ) )
2827 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2828 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2829 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2830 si->si_scope = scopes[j].val;
2834 if ( BER_BVISNULL(&scopes[j].key) ) {
2835 snprintf( c->msg, sizeof( c->msg ),
2836 "Error: parse_syncrepl_line: "
2837 "unknown scope \"%s\"", val);
2838 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2841 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2842 STRLENOF( ATTRSONLYSTR ) ) )
2844 si->si_attrsonly = 1;
2845 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2846 STRLENOF( ATTRSSTR "=" ) ) )
2848 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2849 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2851 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2852 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2853 if ( si->si_anlist == NULL ) {
2854 ch_free( attr_fname );
2857 si->si_anfile = attr_fname;
2859 char *str, *s, *next;
2860 char delimstr[] = " ,\t";
2861 str = ch_strdup( val );
2862 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2864 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2866 if ( strlen(s) == 1 && *s == '*' ) {
2867 si->si_allattrs = 1;
2868 *(val + ( s - str )) = delimstr[0];
2870 if ( strlen(s) == 1 && *s == '+' ) {
2871 si->si_allopattrs = 1;
2872 *(val + ( s - str )) = delimstr[0];
2876 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2877 if ( si->si_anlist == NULL ) {
2881 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2882 STRLENOF( EXATTRSSTR "=" ) ) )
2884 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2885 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2887 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2888 si->si_exanlist = file2anlist(
2889 si->si_exanlist, attr_fname, " ,\t" );
2890 if ( si->si_exanlist == NULL ) {
2891 ch_free( attr_fname );
2894 ch_free( attr_fname );
2896 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2897 if ( si->si_exanlist == NULL ) {
2901 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2902 STRLENOF( TYPESTR "=" ) ) )
2904 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2905 if ( !strncasecmp( val, "refreshOnly",
2906 STRLENOF("refreshOnly") ) )
2908 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2909 } else if ( !strncasecmp( val, "refreshAndPersist",
2910 STRLENOF("refreshAndPersist") ) )
2912 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2913 si->si_interval = 60;
2915 snprintf( c->msg, sizeof( c->msg ),
2916 "Error: parse_syncrepl_line: "
2917 "unknown sync type \"%s\"", val);
2918 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2921 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2922 STRLENOF( INTERVALSTR "=" ) ) )
2924 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2925 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2926 si->si_interval = 0;
2927 } else if ( strchr( val, ':' ) != NULL ) {
2928 char *next, *ptr = val;
2929 unsigned dd, hh, mm, ss;
2931 /* NOTE: the test for ptr[ 0 ] == '-'
2932 * should go before the call to strtoul() */
2933 dd = strtoul( ptr, &next, 10 );
2934 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
2935 snprintf( c->msg, sizeof( c->msg ),
2936 "Error: parse_syncrepl_line: "
2937 "invalid interval \"%s\", unable to parse days", val );
2938 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2942 hh = strtoul( ptr, &next, 10 );
2943 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
2944 snprintf( c->msg, sizeof( c->msg ),
2945 "Error: parse_syncrepl_line: "
2946 "invalid interval \"%s\", unable to parse hours", val );
2947 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2951 mm = strtoul( ptr, &next, 10 );
2952 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
2953 snprintf( c->msg, sizeof( c->msg ),
2954 "Error: parse_syncrepl_line: "
2955 "invalid interval \"%s\", unable to parse minutes", val );
2956 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2960 ss = strtoul( ptr, &next, 10 );
2961 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
2962 snprintf( c->msg, sizeof( c->msg ),
2963 "Error: parse_syncrepl_line: "
2964 "invalid interval \"%s\", unable to parse seconds", val );
2965 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2968 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2972 if ( lutil_parse_time( val, &t ) != 0 ) {
2973 snprintf( c->msg, sizeof( c->msg ),
2974 "Error: parse_syncrepl_line: "
2975 "invalid interval \"%s\"", val );
2976 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2979 si->si_interval = (time_t)t;
2981 if ( si->si_interval < 0 ) {
2982 snprintf( c->msg, sizeof( c->msg ),
2983 "Error: parse_syncrepl_line: "
2984 "invalid interval \"%ld\"",
2985 (long) si->si_interval);
2986 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2989 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
2990 STRLENOF( RETRYSTR "=" ) ) )
2995 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
2996 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2997 retry_list[0] = NULL;
2999 slap_str2clist( &retry_list, val, " ,\t" );
3001 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3004 snprintf( c->msg, sizeof( c->msg ),
3005 "Error: incomplete syncrepl retry list" );
3006 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3007 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3008 ch_free( retry_list[k] );
3010 ch_free( retry_list );
3013 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3014 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3015 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3016 for ( j = 0; j < n; j++ ) {
3018 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3019 snprintf( c->msg, sizeof( c->msg ),
3020 "Error: invalid retry interval \"%s\" (#%d)",
3021 retry_list[j*2], j );
3022 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3023 /* do some cleanup */
3026 si->si_retryinterval[j] = (time_t)t;
3027 if ( *retry_list[j*2+1] == '+' ) {
3028 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3029 si->si_retrynum[j] = RETRYNUM_FOREVER;
3033 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3034 || si->si_retrynum_init[j] <= 0 )
3036 snprintf( c->msg, sizeof( c->msg ),
3037 "Error: invalid initial retry number \"%s\" (#%d)",
3038 retry_list[j*2+1], j );
3039 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3040 /* do some cleanup */
3043 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3044 || si->si_retrynum[j] <= 0 )
3046 snprintf( c->msg, sizeof( c->msg ),
3047 "Error: invalid retry number \"%s\" (#%d)",
3048 retry_list[j*2+1], j );
3049 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3050 /* do some cleanup */
3055 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3056 si->si_retrynum[j] = RETRYNUM_TAIL;
3057 si->si_retryinterval[j] = 0;
3059 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3060 ch_free( retry_list[k] );
3062 ch_free( retry_list );
3063 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3064 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3066 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3067 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3068 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3070 snprintf( c->msg, sizeof( c->msg ),
3071 "invalid manageDSAit value \"%s\".\n",
3073 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3076 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3077 STRLENOF( SLIMITSTR "=") ) )
3079 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3080 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3083 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3084 snprintf( c->msg, sizeof( c->msg ),
3085 "invalid size limit value \"%s\".\n",
3087 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3090 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3091 STRLENOF( TLIMITSTR "=" ) ) )
3093 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3094 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3097 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3098 snprintf( c->msg, sizeof( c->msg ),
3099 "invalid time limit value \"%s\".\n",
3101 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3104 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3105 STRLENOF( SYNCDATASTR "=" ) ) )
3107 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3108 si->si_syncdata = verb_to_mask( val, datamodes );
3109 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3110 snprintf( c->msg, sizeof( c->msg ),
3111 "Error: parse_syncrepl_line: "
3112 "unknown keyword \"%s\"\n", c->argv[ i ] );
3113 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3118 if ( gots != GOT_ALL ) {
3119 snprintf( c->msg, sizeof( c->msg ),
3120 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3121 gots & GOT_ID ? "" : " "IDSTR,
3122 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3123 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3124 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3138 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3139 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3140 "operations required for syncrepl", c->be->be_type );
3141 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3144 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3145 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3146 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3149 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3152 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3156 si->si_bindconf.sb_tls = SB_TLS_OFF;
3157 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3158 si->si_schemachecking = 0;
3159 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3160 &si->si_filterstr );
3161 si->si_base.bv_val = NULL;
3162 si->si_scope = LDAP_SCOPE_SUBTREE;
3163 si->si_attrsonly = 0;
3164 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3165 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3166 si->si_attrs = NULL;
3167 si->si_allattrs = 0;
3168 si->si_allopattrs = 0;
3169 si->si_exattrs = NULL;
3170 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3171 si->si_interval = 86400;
3172 si->si_retryinterval = NULL;
3173 si->si_retrynum_init = NULL;
3174 si->si_retrynum = NULL;
3175 si->si_manageDSAit = 0;
3178 si->si_conn_setup = 0;
3180 si->si_presentlist = NULL;
3181 LDAP_LIST_INIT( &si->si_nonpresentlist );
3182 ldap_pvt_thread_mutex_init( &si->si_mutex );
3184 rc = parse_syncrepl_line( c, si );
3188 init_syncrepl( si );
3189 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3190 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3195 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3196 syncinfo_free( si );
3199 Debug( LDAP_DEBUG_CONFIG,
3200 "Config: ** successfully added syncrepl \"%s\"\n",
3201 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3202 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3203 if ( !si->si_schemachecking ) {
3204 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3206 c->be->be_syncinfo = si;
3212 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3214 struct berval bc, uri;
3215 char buf[BUFSIZ*2], *ptr;
3218 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3222 /* temporarily inhibit bindconf from printing URI */
3223 uri = si->si_bindconf.sb_uri;
3224 BER_BVZERO( &si->si_bindconf.sb_uri );
3225 bindconf_unparse( &si->si_bindconf, &bc );
3226 si->si_bindconf.sb_uri = uri;
3229 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3230 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3231 if ( ptr - buf >= sizeof( buf ) ) return;
3232 if ( !BER_BVISNULL( &bc )) {
3233 if ( WHATSLEFT <= bc.bv_len ) {
3237 ptr = lutil_strcopy( ptr, bc.bv_val );
3240 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3241 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3242 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3243 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3246 if ( !BER_BVISNULL( &si->si_base )) {
3247 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3248 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3249 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3252 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3253 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3254 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3255 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3258 if ( !BER_BVISNULL( &si->si_logbase )) {
3259 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3260 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3261 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3264 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3265 if ( si->si_scope == scopes[i].val ) {
3266 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3267 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3268 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3272 if ( si->si_attrsonly ) {
3273 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3274 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3276 if ( si->si_anfile ) {
3277 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3278 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3279 ptr = lutil_strcopy( ptr, si->si_anfile );
3281 } else if ( si->si_allattrs || si->si_allopattrs ||
3282 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3286 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3287 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3289 /* FIXME: add check for overflow */
3290 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3291 if ( si->si_allattrs ) {
3292 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3293 if ( old != ptr ) *ptr++ = ',';
3296 if ( si->si_allopattrs ) {
3297 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3298 if ( old != ptr ) *ptr++ = ',';
3303 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3304 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3305 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3306 /* FIXME: add check for overflow */
3307 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3309 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3310 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3311 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3313 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3314 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3315 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3316 "refreshAndPersist" : "refreshOnly" );
3318 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3321 dd = si->si_interval;
3328 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3329 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3330 if ( ptr - buf >= sizeof( buf ) ) return;
3331 } else if ( si->si_retryinterval ) {
3333 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3334 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3335 for (i=0; si->si_retryinterval[i]; i++) {
3336 if ( space ) *ptr++ = ' ';
3338 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3339 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3342 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3344 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3348 if ( si->si_slimit ) {
3349 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3350 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3351 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3354 if ( si->si_tlimit ) {
3355 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3356 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3357 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3360 if ( si->si_syncdata ) {
3361 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3362 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3363 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3364 ptr = lutil_strcopy( ptr, bc.bv_val );
3367 bc.bv_len = ptr - buf;
3369 ber_dupbv( bv, &bc );
3373 syncrepl_config( ConfigArgs *c )
3375 if (c->op == SLAP_CONFIG_EMIT) {
3376 if ( c->be->be_syncinfo ) {
3378 syncrepl_unparse( c->be->be_syncinfo, &bv );
3379 ber_bvarray_add( &c->rvalue_vals, &bv );
3383 } else if ( c->op == LDAP_MOD_DELETE ) {
3384 if ( c->be->be_syncinfo ) {
3385 syncinfo_free( c->be->be_syncinfo );
3386 c->be->be_syncinfo = NULL;
3388 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3391 if ( SLAP_SHADOW( c->be ) ) {
3392 Debug(LDAP_DEBUG_ANY, "%s: "
3393 "syncrepl: database already shadowed.\n",
3396 } else if ( add_syncrepl( c ) ) {
3399 return config_sync_shadow( c );