1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 #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 );
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 void 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 /* Init connection to master */
427 rc = ldap_initialize( &si->si_ld, si->si_bindconf.sb_uri.bv_val );
428 if ( rc != LDAP_SUCCESS ) {
429 Debug( LDAP_DEBUG_ANY,
430 "do_syncrep1: ldap_initialize failed (%s)\n",
431 si->si_bindconf.sb_uri.bv_val, 0, 0 );
435 op->o_protocol = LDAP_VERSION3;
436 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
440 if ( si->si_bindconf.sb_tls ) {
441 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
442 if( rc != LDAP_SUCCESS ) {
443 Debug( LDAP_DEBUG_ANY,
444 "%s: ldap_start_tls failed (%d)\n",
445 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
447 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
451 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
452 #ifdef HAVE_CYRUS_SASL
455 if ( si->si_bindconf.sb_secprops != NULL ) {
456 rc = ldap_set_option( si->si_ld,
457 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
459 if( rc != LDAP_OPT_SUCCESS ) {
460 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
461 "(%s,SECPROPS,\"%s\") failed!\n",
462 si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops, 0 );
467 defaults = lutil_sasl_defaults( si->si_ld,
468 si->si_bindconf.sb_saslmech.bv_val,
469 si->si_bindconf.sb_realm.bv_val,
470 si->si_bindconf.sb_authcId.bv_val,
471 si->si_bindconf.sb_cred.bv_val,
472 si->si_bindconf.sb_authzId.bv_val );
474 rc = ldap_sasl_interactive_bind_s( si->si_ld,
475 si->si_bindconf.sb_binddn.bv_val,
476 si->si_bindconf.sb_saslmech.bv_val,
482 lutil_sasl_freedefs( defaults );
484 /* FIXME: different error behaviors according to
486 * 2) on err policy : exit, retry, backoff ...
488 if ( rc != LDAP_SUCCESS ) {
489 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
491 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
492 "ldap_sasl_interactive_bind_s failed (%d)\n",
495 /* FIXME (see above comment) */
496 /* if Kerberos credentials cache is not active, retry */
497 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
498 rc == LDAP_LOCAL_ERROR )
500 rc = LDAP_SERVER_DOWN;
505 #else /* HAVE_CYRUS_SASL */
506 /* Should never get here, we trapped this at config time */
508 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
513 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
514 rc = ldap_sasl_bind_s( si->si_ld,
515 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
516 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
517 if ( rc != LDAP_SUCCESS ) {
518 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
519 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
524 /* Set SSF to strongest of TLS, SASL SSFs */
527 op->o_transport_ssf = 0;
529 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
530 == LDAP_SUCCESS && ssl != NULL )
532 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
534 #endif /* HAVE_TLS */
535 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
536 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
537 ? op->o_sasl_ssf : op->o_tls_ssf;
540 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
541 /* get contextCSN shadow replica from database */
542 BerVarray csn = NULL;
544 assert( si->si_rid < 1000 );
545 op->o_req_ndn = op->o_bd->be_nsuffix[0];
546 op->o_req_dn = op->o_req_ndn;
548 /* try to read stored contextCSN */
549 backend_attribute( op, NULL, &op->o_req_ndn,
550 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
552 ch_free( si->si_syncCookie.ctxcsn.bv_val );
553 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
554 ber_bvarray_free_x( csn, op->o_tmpmemctx );
557 si->si_syncCookie.rid = si->si_rid;
559 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
560 if ( si->si_rid == sc->rid ) {
561 cmdline_cookie_found = 1;
566 if ( cmdline_cookie_found ) {
567 /* cookie is supplied in the command line */
569 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
571 /* ctxcsn wasn't parsed yet, do it now */
572 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
573 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
574 /* if cmdline cookie does not have ctxcsn */
575 /* component, set it to an initial value */
576 slap_init_sync_cookie_ctxcsn( sc );
578 slap_sync_cookie_free( &si->si_syncCookie, 0 );
579 slap_dup_sync_cookie( &si->si_syncCookie, sc );
580 slap_sync_cookie_free( sc, 1 );
583 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
584 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
587 rc = ldap_sync_search( si, op->o_tmpmemctx );
589 if( rc != LDAP_SUCCESS ) {
590 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
591 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
597 ldap_unbind_ext( si->si_ld, NULL, NULL );
610 LDAPControl **rctrls = NULL;
613 BerElementBuffer berbuf;
614 BerElement *ber = (BerElement *)&berbuf;
616 LDAPMessage *res = NULL;
617 LDAPMessage *msg = NULL;
620 struct berval *retdata = NULL;
625 struct berval syncUUID = BER_BVNULL;
626 struct sync_cookie syncCookie = { BER_BVNULL };
627 struct sync_cookie syncCookie_req = { BER_BVNULL };
628 struct berval cookie = BER_BVNULL;
636 Modifications *modlist = NULL;
641 struct timeval *tout_p = NULL;
642 struct timeval tout = { 0, 0 };
644 int refreshDeletes = 0;
646 BerVarray syncUUIDs = NULL;
649 if ( slapd_shutdown ) {
654 ber_init2( ber, NULL, LBER_USE_DER );
655 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
657 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
659 psub = &si->si_be->be_nsuffix[0];
661 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
663 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
669 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
670 tout_p, &res )) > 0 )
672 if ( slapd_shutdown ) {
676 for( msg = ldap_first_message( si->si_ld, res );
678 msg = ldap_next_message( si->si_ld, msg ) )
680 if ( slapd_shutdown ) {
684 switch( ldap_msgtype( msg ) ) {
685 case LDAP_RES_SEARCH_ENTRY:
686 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
687 /* we can't work without the control */
689 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
690 "got search entry without "
691 "control\n", 0, 0, 0 );
696 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
697 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
698 /* FIXME: what if syncUUID is NULL or empty?
699 * (happens with back-sql...) */
700 if ( BER_BVISEMPTY( &syncUUID ) ) {
701 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
702 "got empty syncUUID\n", 0, 0, 0 );
703 ldap_controls_free( rctrls );
707 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
708 ber_scanf( ber, /*"{"*/ "m}", &cookie );
709 if ( !BER_BVISNULL( &cookie ) ) {
710 ch_free( syncCookie.octet_str.bv_val );
711 ber_dupbv( &syncCookie.octet_str, &cookie );
713 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
715 slap_parse_sync_cookie( &syncCookie, NULL );
718 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
721 if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
722 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
723 syncrepl_updateCookie( si, op, psub, &syncCookie );
725 } else if ( syncrepl_message_to_entry( si, op, msg,
726 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
727 rc_efree = syncrepl_entry( si, op, entry, &modlist,
728 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
729 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
731 syncrepl_updateCookie( si, op, psub, &syncCookie );
734 ldap_controls_free( rctrls );
736 slap_mods_free( modlist, 1 );
738 if ( rc_efree && entry ) {
744 case LDAP_RES_SEARCH_REFERENCE:
745 Debug( LDAP_DEBUG_ANY,
746 "do_syncrep2: reference received error\n", 0, 0, 0 );
749 case LDAP_RES_SEARCH_RESULT:
750 Debug( LDAP_DEBUG_SYNC,
751 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
752 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
754 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
755 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
756 /* map old result code to registered code */
757 err = LDAP_SYNC_REFRESH_REQUIRED;
760 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
761 if ( si->si_logstate == SYNCLOG_LOGGING ) {
762 si->si_logstate = SYNCLOG_FALLBACK;
769 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
771 ber_scanf( ber, "{" /*"}"*/);
772 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 ) == LDAP_TAG_REFRESHDELETES )
785 ber_scanf( ber, "b", &refreshDeletes );
787 ber_scanf( ber, /*"{"*/ "}" );
789 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
791 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
794 value_match( &match, slap_schema.si_ad_entryCSN,
795 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
796 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
797 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
800 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
801 match < 0 && err == LDAP_SUCCESS )
803 syncrepl_updateCookie( si, op, psub, &syncCookie );
806 ldap_controls_free( rctrls );
808 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
809 /* FIXME : different error behaviors according to
810 * 1) err code : LDAP_BUSY ...
811 * 2) on err policy : stop service, stop sync, retry
813 if ( refreshDeletes == 0 && match < 0 &&
814 err == LDAP_SUCCESS )
816 syncrepl_del_nonpresent( op, si, NULL );
818 avl_free( si->si_presentlist, avl_ber_bvfree );
819 si->si_presentlist = NULL;
822 if ( err == LDAP_SUCCESS
823 && si->si_logstate == SYNCLOG_FALLBACK ) {
824 si->si_logstate = SYNCLOG_LOGGING;
825 rc = LDAP_SYNC_REFRESH_REQUIRED;
832 case LDAP_RES_INTERMEDIATE:
833 rc = ldap_parse_intermediate( si->si_ld, msg,
834 &retoid, &retdata, NULL, 0 );
835 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
836 ber_init2( ber, retdata, LBER_USE_DER );
838 switch ( si_tag = ber_peek_tag( ber, &len )) {
840 case LDAP_TAG_SYNC_NEW_COOKIE:
841 Debug( LDAP_DEBUG_SYNC,
842 "do_syncrep2: %s - %s%s\n",
843 "LDAP_RES_INTERMEDIATE",
844 "NEW_COOKIE", "\n" );
845 ber_scanf( ber, "tm", &tag, &cookie );
847 case LDAP_TAG_SYNC_REFRESH_DELETE:
848 case LDAP_TAG_SYNC_REFRESH_PRESENT:
849 Debug( LDAP_DEBUG_SYNC,
850 "do_syncrep2: %s - %s%s\n",
851 "LDAP_RES_INTERMEDIATE",
852 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
853 "REFRESH_PRESENT" : "REFRESH_DELETE",
855 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
856 si->si_refreshDelete = 1;
858 si->si_refreshPresent = 1;
860 ber_scanf( ber, "t{" /*"}"*/, &tag );
861 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
863 ber_scanf( ber, "m", &cookie );
864 if ( !BER_BVISNULL( &cookie ) ) {
865 ch_free( syncCookie.octet_str.bv_val );
866 ber_dupbv( &syncCookie.octet_str, &cookie );
868 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
870 slap_parse_sync_cookie( &syncCookie, NULL );
873 if ( ber_peek_tag( ber, &len ) ==
874 LDAP_TAG_REFRESHDONE )
876 ber_scanf( ber, "b", &refreshDone );
878 ber_scanf( ber, /*"{"*/ "}" );
880 case LDAP_TAG_SYNC_ID_SET:
881 Debug( LDAP_DEBUG_SYNC,
882 "do_syncrep2: %s - %s%s\n",
883 "LDAP_RES_INTERMEDIATE",
886 ber_scanf( ber, "t{" /*"}"*/, &tag );
887 if ( ber_peek_tag( ber, &len ) ==
888 LDAP_TAG_SYNC_COOKIE )
890 ber_scanf( ber, "m", &cookie );
891 if ( !BER_BVISNULL( &cookie ) ) {
892 ch_free( syncCookie.octet_str.bv_val );
893 ber_dupbv( &syncCookie.octet_str, &cookie );
895 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
897 slap_parse_sync_cookie( &syncCookie, NULL );
900 if ( ber_peek_tag( ber, &len ) ==
901 LDAP_TAG_REFRESHDELETES )
903 ber_scanf( ber, "b", &refreshDeletes );
905 ber_scanf( ber, "[W]", &syncUUIDs );
906 ber_scanf( ber, /*"{"*/ "}" );
907 if ( refreshDeletes ) {
908 syncrepl_del_nonpresent( op, si, syncUUIDs );
909 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
911 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
912 struct berval *syncuuid_bv;
913 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
914 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
915 avl_insert( &si->si_presentlist,
916 (caddr_t) syncuuid_bv,
917 syncuuid_cmp, avl_dup_error );
919 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
923 Debug( LDAP_DEBUG_ANY,
924 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
925 (long) si_tag, 0, 0 );
926 ldap_memfree( retoid );
927 ber_bvfree( retdata );
931 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
933 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
936 value_match( &match, slap_schema.si_ad_entryCSN,
937 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
938 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
939 &syncCookie_req.ctxcsn,
940 &syncCookie.ctxcsn, &text );
943 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
946 syncrepl_updateCookie( si, op, psub, &syncCookie);
949 if ( si->si_refreshPresent == 1 ) {
951 syncrepl_del_nonpresent( op, si, NULL );
955 ldap_memfree( retoid );
956 ber_bvfree( retdata );
960 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
961 "unknown intermediate response (%d)\n",
963 ldap_memfree( retoid );
964 ber_bvfree( retdata );
970 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
971 "unknown message\n", 0, 0, 0 );
975 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
976 slap_sync_cookie_free( &syncCookie_req, 0 );
977 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
978 slap_sync_cookie_free( &syncCookie, 0 );
988 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
989 errstr = ldap_err2string( rc );
991 Debug( LDAP_DEBUG_ANY,
992 "do_syncrep2 : %s\n", errstr, 0, 0 );
996 slap_sync_cookie_free( &syncCookie, 0 );
997 slap_sync_cookie_free( &syncCookie_req, 0 );
999 if ( res ) ldap_msgfree( res );
1001 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1002 if ( si->si_conn_setup ) {
1004 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1005 connection_client_stop( s );
1006 si->si_conn_setup = 0;
1008 ldap_unbind_ext( si->si_ld, NULL, NULL );
1020 struct re_s* rtask = arg;
1021 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1022 Connection conn = {0};
1023 OperationBuffer opbuf;
1025 int rc = LDAP_SUCCESS;
1031 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1036 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1038 switch( abs( si->si_type )) {
1039 case LDAP_SYNC_REFRESH_ONLY:
1040 case LDAP_SYNC_REFRESH_AND_PERSIST:
1043 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1047 if ( slapd_shutdown ) {
1049 if ( si->si_conn_setup ) {
1050 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1051 connection_client_stop( s );
1052 si->si_conn_setup = 0;
1054 ldap_unbind_ext( si->si_ld, NULL, NULL );
1057 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1061 op = (Operation *) &opbuf;
1062 connection_fake_init( &conn, op, ctx );
1064 /* use global malloc for now */
1065 op->o_tmpmemctx = NULL;
1066 op->o_tmpmfuncs = &ch_mfuncs;
1068 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1069 op->o_bd = be = si->si_be;
1070 op->o_dn = op->o_bd->be_rootdn;
1071 op->o_ndn = op->o_bd->be_rootndn;
1073 /* Establish session, do search */
1075 si->si_refreshDelete = 0;
1076 si->si_refreshPresent = 0;
1077 rc = do_syncrep1( op, si );
1081 /* Process results */
1082 if ( rc == LDAP_SUCCESS ) {
1083 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1085 rc = do_syncrep2( op, si );
1086 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1087 rc = ldap_sync_search( si, op->o_tmpmemctx );
1091 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1092 /* If we succeeded, enable the connection for further listening.
1093 * If we failed, tear down the connection and reschedule.
1095 if ( rc == LDAP_SUCCESS ) {
1096 if ( si->si_conn_setup ) {
1097 connection_client_enable( s );
1099 rc = connection_client_setup( s, do_syncrepl, arg );
1101 si->si_conn_setup = 1;
1103 } else if ( si->si_conn_setup ) {
1107 if ( rc == -2 ) rc = 0;
1111 /* At this point, we have 4 cases:
1112 * 1) for any hard failure, give up and remove this task
1113 * 2) for ServerDown, reschedule this task to run
1114 * 3) for Refresh and Success, reschedule to run
1115 * 4) for Persist and Success, reschedule to defer
1117 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1119 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1120 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1124 connection_client_stop( s );
1127 if ( rc == LDAP_SUCCESS ) {
1128 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1131 rtask->interval.tv_sec = si->si_interval;
1132 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1133 if ( si->si_retrynum ) {
1134 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1135 si->si_retrynum[i] = si->si_retrynum_init[i];
1137 si->si_retrynum[i] = RETRYNUM_TAIL;
1140 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1141 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1145 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1146 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1147 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1148 if ( si->si_retrynum[i] > 0 )
1149 si->si_retrynum[i]--;
1150 rtask->interval.tv_sec = si->si_retryinterval[i];
1151 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1152 slap_wake_listener();
1156 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1157 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1162 static slap_verbmasks modops[] = {
1163 { BER_BVC("add"), LDAP_REQ_ADD },
1164 { BER_BVC("delete"), LDAP_REQ_DELETE },
1165 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1166 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1170 static Modifications *
1171 syncrepl_accesslog_mods(
1178 AttributeDescription *ad;
1179 struct berval bv, bv2;
1181 Modifications *mod = NULL, *modlist = NULL, **modtail;
1186 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1190 colon = ber_bvchr( &bv, ':' );
1192 continue; /* invalid */
1193 bv.bv_len = colon - bv.bv_val;
1194 if ( slap_bv2ad( &bv, &ad, &text )) {
1198 /* Ignore dynamically generated attrs */
1199 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1201 /* Ignore excluded attrs */
1202 if ( ldap_charray_inlist( si->si_exattrs,
1203 ad->ad_type->sat_cname.bv_val ))
1207 case '+': op = LDAP_MOD_ADD; break;
1208 case '-': op = LDAP_MOD_DELETE; break;
1209 case '=': op = LDAP_MOD_REPLACE; break;
1210 case '#': op = LDAP_MOD_INCREMENT; break;
1214 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1215 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1218 mod->sml_next = NULL;
1220 mod->sml_type = ad->ad_cname;
1221 mod->sml_values = NULL;
1222 mod->sml_nvalues = NULL;
1225 modtail = &mod->sml_next;
1227 if ( colon[2] == ' ' ) {
1228 bv.bv_val = colon + 3;
1229 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1230 ber_dupbv( &bv2, &bv );
1231 ber_bvarray_add( &mod->sml_values, &bv2 );
1237 static Modifications *
1238 syncrepl_changelog_mods(
1243 return NULL; /* FIXME */
1247 syncrepl_message_to_op(
1253 BerElement *ber = NULL;
1254 Modifications *modlist = NULL;
1256 SlapReply rs = { REP_RESULT };
1257 slap_callback cb = { NULL, null_callback, NULL, NULL };
1260 char txtbuf[SLAP_TEXT_BUFLEN];
1261 size_t textlen = sizeof txtbuf;
1263 struct berval bdn, dn = BER_BVNULL, ndn;
1264 struct berval bv, *bvals = NULL;
1265 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1266 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1267 psup = BER_BVNULL, nsup = BER_BVNULL;
1268 int rc, deleteOldRdn = 0;
1270 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1271 Debug( LDAP_DEBUG_ANY,
1272 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1276 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1281 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1283 if ( rc != LDAP_SUCCESS ) {
1284 Debug( LDAP_DEBUG_ANY,
1285 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1289 op->o_tag = LBER_DEFAULT;
1291 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1293 if ( bv.bv_val == NULL )
1296 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1298 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1299 ber_dupbv( &op->o_req_dn, &dn );
1300 ber_dupbv( &op->o_req_ndn, &ndn );
1301 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1302 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1303 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1304 int i = verb_to_mask( bvals[0].bv_val, modops );
1306 Debug( LDAP_DEBUG_ANY,
1307 "syncrepl_message_to_op : unknown op %s",
1308 bvals[0].bv_val, 0, 0 );
1313 op->o_tag = modops[i].mask;
1314 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1315 /* Parse attribute into modlist */
1316 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1317 modlist = syncrepl_accesslog_mods( si, bvals );
1319 modlist = syncrepl_changelog_mods( si, bvals );
1320 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1322 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1323 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1325 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1327 } else if ( !ber_bvstrcasecmp( &bv,
1328 &slap_schema.si_ad_entryCSN->ad_cname )) {
1329 slap_queue_csn( op, bvals );
1334 /* If we didn't get a mod type or a target DN, bail out */
1335 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1340 op->o_callback = &cb;
1342 switch( op->o_tag ) {
1344 case LDAP_REQ_MODIFY:
1345 /* If we didn't get required data, bail */
1346 if ( !modlist ) goto done;
1348 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1350 if ( rc != LDAP_SUCCESS ) {
1351 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1356 if ( op->o_tag == LDAP_REQ_ADD ) {
1357 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1358 op->ora_e->e_name = op->o_req_dn;
1359 op->ora_e->e_nname = op->o_req_ndn;
1360 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1361 if( rc != LDAP_SUCCESS ) {
1362 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1365 rc = op->o_bd->be_add( op, &rs );
1367 be_entry_release_w( op, op->ora_e );
1369 op->orm_modlist = modlist;
1370 rc = op->o_bd->be_modify( op, &rs );
1373 case LDAP_REQ_MODRDN:
1374 if ( BER_BVISNULL( &rdn )) goto done;
1376 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1378 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1380 if ( !BER_BVISNULL( &sup )) {
1381 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1383 op->orr_newSup = &psup;
1384 op->orr_nnewSup = ⊅
1386 op->orr_newrdn = prdn;
1387 op->orr_nnewrdn = nrdn;
1388 op->orr_deleteoldrdn = deleteOldRdn;
1389 rc = op->o_bd->be_modrdn( op, &rs );
1391 case LDAP_REQ_DELETE:
1392 rc = op->o_bd->be_delete( op, &rs );
1396 slap_graduate_commit_csn( op );
1398 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1399 if ( !BER_BVISNULL( &rdn )) {
1400 if ( !BER_BVISNULL( &nsup ))
1401 ch_free( nsup.bv_val );
1402 if ( !BER_BVISNULL( &psup ))
1403 ch_free( psup.bv_val );
1404 if ( !BER_BVISNULL( &nrdn ))
1405 ch_free( nrdn.bv_val );
1406 if ( !BER_BVISNULL( &prdn ))
1407 ch_free( prdn.bv_val );
1409 ber_free ( ber, 0 );
1414 syncrepl_message_to_entry(
1418 Modifications **modlist,
1424 BerElement *ber = NULL;
1427 Modifications **modtail = modlist;
1430 char txtbuf[SLAP_TEXT_BUFLEN];
1431 size_t textlen = sizeof txtbuf;
1433 struct berval bdn = {0, NULL}, dn, ndn;
1438 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1439 Debug( LDAP_DEBUG_ANY,
1440 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1444 op->o_tag = LDAP_REQ_ADD;
1446 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1448 if ( rc != LDAP_SUCCESS ) {
1449 Debug( LDAP_DEBUG_ANY,
1450 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1454 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1455 ber_dupbv( &op->o_req_dn, &dn );
1456 ber_dupbv( &op->o_req_ndn, &ndn );
1457 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1458 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1460 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1463 return LDAP_SUCCESS;
1466 if ( entry == NULL ) {
1470 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1472 e->e_name = op->o_req_dn;
1473 e->e_nname = op->o_req_ndn;
1475 while ( ber_remaining( ber ) ) {
1476 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1477 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1482 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1484 mod->sml_op = LDAP_MOD_REPLACE;
1486 mod->sml_next = NULL;
1487 mod->sml_desc = NULL;
1488 mod->sml_type = tmp.sml_type;
1489 mod->sml_values = tmp.sml_values;
1490 mod->sml_nvalues = NULL;
1493 modtail = &mod->sml_next;
1496 if ( *modlist == NULL ) {
1497 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1503 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1505 if ( rc != LDAP_SUCCESS ) {
1506 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1511 /* Strip out dynamically generated attrs */
1512 for ( modtail = modlist; *modtail ; ) {
1514 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1515 *modtail = mod->sml_next;
1516 slap_mod_free( &mod->sml_mod, 0 );
1519 modtail = &mod->sml_next;
1523 /* Strip out attrs in exattrs list */
1524 for ( modtail = modlist; *modtail ; ) {
1526 if ( ldap_charray_inlist( si->si_exattrs,
1527 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1528 *modtail = mod->sml_next;
1529 slap_mod_free( &mod->sml_mod, 0 );
1532 modtail = &mod->sml_next;
1536 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1537 if( rc != LDAP_SUCCESS ) {
1538 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1543 ber_free ( ber, 0 );
1544 if ( rc != LDAP_SUCCESS ) {
1554 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1556 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1557 * entry if a previous refresh was interrupted before sending us a new
1558 * context state. We try to compare the new entry to the existing entry
1559 * and ignore the new entry if they are the same.
1561 * Also, we may get an update where the entryDN has changed, due to
1562 * a ModDn on the provider. We detect this as well, so we can issue
1563 * the corresponding operation locally.
1565 * In the case of a modify, we get a list of all the attributes
1566 * in the original entry. Rather than deleting the entry and re-adding it,
1567 * we issue a Modify request that deletes all the attributes and adds all
1568 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1571 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1572 * a ModDN we will issue both operations on the local database.
1574 typedef struct dninfo {
1578 int renamed; /* Was an existing entry renamed? */
1579 int wasChanged; /* are the attributes changed? */
1580 int attrs; /* how many attribute types are in the ads list */
1581 AttributeDescription **ads;
1589 Modifications** modlist,
1591 struct berval* syncUUID,
1592 struct sync_cookie* syncCookie_req,
1593 struct berval* syncCSN )
1595 Backend *be = op->o_bd;
1596 slap_callback cb = { NULL, NULL, NULL, NULL };
1597 struct berval *syncuuid_bv = NULL;
1598 struct berval syncUUID_strrep = BER_BVNULL;
1599 struct berval uuid_bv = BER_BVNULL;
1601 SlapReply rs_search = {REP_RESULT};
1602 SlapReply rs_delete = {REP_RESULT};
1603 SlapReply rs_add = {REP_RESULT};
1604 SlapReply rs_modify = {REP_RESULT};
1606 #ifdef LDAP_COMP_MATCH
1607 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1609 AttributeAssertion ava = { NULL, BER_BVNULL };
1611 int rc = LDAP_SUCCESS;
1612 int ret = LDAP_SUCCESS;
1614 struct berval pdn = BER_BVNULL;
1618 switch( syncstate ) {
1619 case LDAP_SYNC_PRESENT:
1620 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1622 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1625 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1627 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1629 case LDAP_SYNC_DELETE:
1630 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1632 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1634 case LDAP_SYNC_MODIFY:
1635 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1637 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1640 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1642 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1645 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1646 if ( !si->si_refreshPresent ) {
1647 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1648 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1649 syncuuid_cmp, avl_dup_error );
1653 if ( syncstate == LDAP_SYNC_PRESENT ) {
1655 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1656 if ( entry == NULL ) {
1661 f.f_choice = LDAP_FILTER_EQUALITY;
1663 ava.aa_desc = slap_schema.si_ad_entryUUID;
1664 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1665 ava.aa_value = *syncUUID;
1666 op->ors_filter = &f;
1668 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1669 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1670 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1671 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1672 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1673 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1674 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1675 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1677 op->o_tag = LDAP_REQ_SEARCH;
1678 op->ors_scope = LDAP_SCOPE_SUBTREE;
1679 op->ors_deref = LDAP_DEREF_NEVER;
1681 /* get the entry for this UUID */
1682 op->o_req_dn = si->si_base;
1683 op->o_req_ndn = si->si_base;
1685 op->o_time = slap_get_time();
1686 op->ors_tlimit = SLAP_NO_LIMIT;
1689 op->ors_attrs = slap_anlist_all_attributes;
1690 op->ors_attrsonly = 0;
1692 /* set callback function */
1693 op->o_callback = &cb;
1694 cb.sc_response = dn_callback;
1695 cb.sc_private = &dni;
1696 dni.new_entry = entry;
1698 if ( limits_check( op, &rs_search ) == 0 ) {
1699 rc = be->be_search( op, &rs_search );
1700 Debug( LDAP_DEBUG_SYNC,
1701 "syncrepl_entry: %s (%d)\n",
1702 "be_search", rc, 0 );
1705 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1706 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1709 cb.sc_response = null_callback;
1712 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1713 Debug( LDAP_DEBUG_SYNC,
1714 "syncrepl_entry: %s\n",
1715 entry->e_name.bv_val, 0, 0 );
1717 Debug( LDAP_DEBUG_SYNC,
1718 "syncrepl_entry: %s\n",
1719 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1722 if ( syncstate != LDAP_SYNC_DELETE ) {
1723 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1726 /* add if missing */
1727 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1728 &syncUUID_strrep, syncUUID );
1730 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1731 /* replace only if necessary */
1732 if ( a->a_nvals != a->a_vals ) {
1733 ber_memfree( a->a_nvals[0].bv_val );
1734 ber_dupbv( &a->a_nvals[0], syncUUID );
1736 ber_memfree( a->a_vals[0].bv_val );
1737 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1741 switch ( syncstate ) {
1743 case LDAP_SYNC_MODIFY:
1745 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1747 op->o_csn = a->a_vals[0];
1750 if ( BER_BVISNULL( &dni.dn )) {
1752 op->o_req_dn = entry->e_name;
1753 op->o_req_ndn = entry->e_nname;
1754 op->o_tag = LDAP_REQ_ADD;
1757 rc = be->be_add( op, &rs_add );
1758 Debug( LDAP_DEBUG_SYNC,
1759 "syncrepl_entry: %s (%d)\n",
1761 switch ( rs_add.sr_err ) {
1763 be_entry_release_w( op, entry );
1768 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1769 * only if the suffix entry is not present */
1770 case LDAP_NO_SUCH_OBJECT:
1771 syncrepl_add_glue( op, entry );
1775 /* if an entry was added via syncrepl_add_glue(),
1776 * it likely has no entryUUID, so the previous
1777 * be_search() doesn't find it. In this case,
1778 * give syncrepl a chance to modify it. Also
1779 * allow for entries that were recreated with the
1780 * same DN but a different entryUUID.
1782 case LDAP_ALREADY_EXISTS:
1784 Operation op2 = *op;
1785 SlapReply rs2 = { 0 };
1786 slap_callback cb2 = { 0 };
1788 op2.o_tag = LDAP_REQ_SEARCH;
1789 op2.o_req_dn = entry->e_name;
1790 op2.o_req_ndn = entry->e_nname;
1791 op2.ors_scope = LDAP_SCOPE_BASE;
1792 op2.ors_deref = LDAP_DEREF_NEVER;
1793 op2.ors_attrs = slap_anlist_all_attributes;
1794 op2.ors_attrsonly = 0;
1795 op2.ors_limit = NULL;
1797 op2.ors_tlimit = SLAP_NO_LIMIT;
1799 f.f_choice = LDAP_FILTER_PRESENT;
1800 f.f_desc = slap_schema.si_ad_objectClass;
1801 op2.ors_filter = &f;
1802 op2.ors_filterstr = generic_filterstr;
1804 op2.o_callback = &cb2;
1805 cb2.sc_response = dn_callback;
1806 cb2.sc_private = &dni;
1808 be->be_search( &op2, &rs2 );
1816 Debug( LDAP_DEBUG_ANY,
1817 "syncrepl_entry : be_add failed (%d)\n",
1818 rs_add.sr_err, 0, 0 );
1825 op->o_req_dn = dni.dn;
1826 op->o_req_ndn = dni.ndn;
1827 if ( dni.renamed ) {
1828 struct berval noldp, newp, nnewp;
1830 op->o_tag = LDAP_REQ_MODRDN;
1831 dnRdn( &entry->e_name, &op->orr_newrdn );
1832 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1834 dnParent( &dni.ndn, &noldp );
1835 dnParent( &entry->e_nname, &nnewp );
1836 if ( !dn_match( &noldp, &newp )) {
1837 dnParent( &entry->e_name, &newp );
1838 op->orr_newSup = &newp;
1839 op->orr_nnewSup = &nnewp;
1841 op->orr_deleteoldrdn = 0;
1842 rc = be->be_modrdn( op, &rs_modify );
1843 Debug( LDAP_DEBUG_SYNC,
1844 "syncrepl_entry: %s (%d)\n",
1845 "be_modrdn", rc, 0 );
1846 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1847 op->o_req_dn = entry->e_name;
1848 op->o_req_ndn = entry->e_nname;
1854 if ( dni.wasChanged ) {
1855 Modifications *mod, *modhead = NULL;
1856 Modifications *modtail = NULL;
1859 op->o_tag = LDAP_REQ_MODIFY;
1861 assert( *modlist != NULL );
1863 /* Delete all the old attrs */
1864 for ( i = 0; i < dni.attrs; i++ ) {
1865 mod = ch_malloc( sizeof( Modifications ) );
1866 mod->sml_op = LDAP_MOD_DELETE;
1868 mod->sml_desc = dni.ads[i];
1869 mod->sml_type = mod->sml_desc->ad_cname;
1870 mod->sml_values = NULL;
1871 mod->sml_nvalues = NULL;
1872 if ( !modhead ) modhead = mod;
1874 modtail->sml_next = mod;
1879 /* Append passed in list to ours */
1881 modtail->sml_next = *modlist;
1887 /* Find end of this list */
1888 for ( ; mod != NULL; mod = mod->sml_next ) {
1892 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1893 mod->sml_op = LDAP_MOD_REPLACE;
1895 mod->sml_desc = slap_schema.si_ad_entryUUID;
1896 mod->sml_type = mod->sml_desc->ad_cname;
1897 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1898 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1899 ber_dupbv( &uuid_bv, syncUUID );
1900 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1901 modtail->sml_next = mod;
1903 op->o_tag = LDAP_REQ_MODIFY;
1904 op->orm_modlist = *modlist;
1906 rc = be->be_modify( op, &rs_modify );
1907 Debug( LDAP_DEBUG_SYNC,
1908 "syncrepl_entry: %s (%d)\n",
1909 "be_modify", rc, 0 );
1910 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1911 Debug( LDAP_DEBUG_ANY,
1912 "syncrepl_entry : be_modify failed (%d)\n",
1913 rs_modify.sr_err, 0, 0 );
1918 case LDAP_SYNC_DELETE :
1919 if ( !BER_BVISNULL( &dni.dn )) {
1920 op->o_req_dn = dni.dn;
1921 op->o_req_ndn = dni.ndn;
1922 op->o_tag = LDAP_REQ_DELETE;
1923 rc = be->be_delete( op, &rs_delete );
1924 Debug( LDAP_DEBUG_SYNC,
1925 "syncrepl_entry: %s (%d)\n",
1926 "be_delete", rc, 0 );
1928 while ( rs_delete.sr_err == LDAP_SUCCESS
1929 && op->o_delete_glue_parent ) {
1930 op->o_delete_glue_parent = 0;
1931 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1932 slap_callback cb = { NULL };
1933 cb.sc_response = slap_null_cb;
1934 dnParent( &op->o_req_ndn, &pdn );
1936 op->o_req_ndn = pdn;
1937 op->o_callback = &cb;
1938 op->o_bd->be_delete( op, &rs_delete );
1948 Debug( LDAP_DEBUG_ANY,
1949 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1955 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1956 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1957 BER_BVZERO( &syncUUID_strrep );
1960 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1962 if ( !BER_BVISNULL( &dni.ndn ) ) {
1963 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1965 if ( !BER_BVISNULL( &dni.dn ) ) {
1966 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1968 BER_BVZERO( &op->o_csn );
1972 static struct berval gcbva[] = {
1978 #define NP_DELETE_ONE 2
1981 syncrepl_del_nonpresent(
1986 Backend* be = op->o_bd;
1987 slap_callback cb = { NULL };
1988 SlapReply rs_search = {REP_RESULT};
1989 SlapReply rs_delete = {REP_RESULT};
1990 SlapReply rs_modify = {REP_RESULT};
1991 struct nonpresent_entry *np_list, *np_prev;
1993 AttributeName an[2];
1995 struct berval pdn = BER_BVNULL;
1997 op->o_req_dn = si->si_base;
1998 op->o_req_ndn = si->si_base;
2000 cb.sc_response = nonpresent_callback;
2003 op->o_callback = &cb;
2004 op->o_tag = LDAP_REQ_SEARCH;
2005 op->ors_scope = si->si_scope;
2006 op->ors_deref = LDAP_DEREF_NEVER;
2007 op->o_time = slap_get_time();
2008 op->ors_tlimit = SLAP_NO_LIMIT;
2013 #ifdef LDAP_COMP_MATCH
2014 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2016 AttributeAssertion eq = { NULL, BER_BVNULL };
2020 op->ors_attrsonly = 1;
2021 op->ors_attrs = slap_anlist_no_attrs;
2022 op->ors_limit = NULL;
2023 op->ors_filter = &uf;
2026 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2028 uf.f_choice = LDAP_FILTER_EQUALITY;
2029 si->si_refreshDelete |= NP_DELETE_ONE;
2031 for (i=0; uuids[i].bv_val; i++) {
2033 uf.f_av_value = uuids[i];
2034 rc = be->be_search( op, &rs_search );
2036 si->si_refreshDelete ^= NP_DELETE_ONE;
2038 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2039 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2040 an[0].an_desc = slap_schema.si_ad_entryUUID;
2042 op->ors_slimit = SLAP_NO_LIMIT;
2043 op->ors_attrsonly = 0;
2044 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2045 op->ors_filterstr = si->si_filterstr;
2046 op->o_nocaching = 1;
2048 if ( limits_check( op, &rs_search ) == 0 ) {
2049 rc = be->be_search( op, &rs_search );
2051 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2054 op->o_nocaching = 0;
2056 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2058 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2060 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2061 while ( np_list != NULL ) {
2062 LDAP_LIST_REMOVE( np_list, npe_link );
2064 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2065 op->o_tag = LDAP_REQ_DELETE;
2066 op->o_callback = &cb;
2067 cb.sc_response = null_callback;
2069 op->o_req_dn = *np_prev->npe_name;
2070 op->o_req_ndn = *np_prev->npe_nname;
2071 rc = op->o_bd->be_delete( op, &rs_delete );
2073 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2074 Modifications mod1, mod2;
2075 mod1.sml_op = LDAP_MOD_REPLACE;
2077 mod1.sml_desc = slap_schema.si_ad_objectClass;
2078 mod1.sml_type = mod1.sml_desc->ad_cname;
2079 mod1.sml_values = &gcbva[0];
2080 mod1.sml_nvalues = NULL;
2081 mod1.sml_next = &mod2;
2083 mod2.sml_op = LDAP_MOD_REPLACE;
2085 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2086 mod2.sml_type = mod2.sml_desc->ad_cname;
2087 mod2.sml_values = &gcbva[1];
2088 mod2.sml_nvalues = NULL;
2089 mod2.sml_next = NULL;
2091 op->o_tag = LDAP_REQ_MODIFY;
2092 op->orm_modlist = &mod1;
2094 rc = be->be_modify( op, &rs_modify );
2097 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2098 op->o_delete_glue_parent ) {
2099 op->o_delete_glue_parent = 0;
2100 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2101 slap_callback cb = { NULL };
2102 cb.sc_response = slap_null_cb;
2103 dnParent( &op->o_req_ndn, &pdn );
2105 op->o_req_ndn = pdn;
2106 op->o_callback = &cb;
2107 /* give it a root privil ? */
2108 op->o_bd->be_delete( op, &rs_delete );
2114 op->o_delete_glue_parent = 0;
2116 ber_bvfree( np_prev->npe_name );
2117 ber_bvfree( np_prev->npe_nname );
2121 slap_graduate_commit_csn( op );
2132 Backend *be = op->o_bd;
2133 slap_callback cb = { NULL };
2138 struct berval dn = BER_BVNULL;
2139 struct berval ndn = BER_BVNULL;
2141 SlapReply rs_add = {REP_RESULT};
2142 struct berval ptr, nptr;
2145 op->o_tag = LDAP_REQ_ADD;
2146 op->o_callback = &cb;
2147 cb.sc_response = null_callback;
2148 cb.sc_private = NULL;
2153 /* count RDNs in suffix */
2154 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2155 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2157 ptr.bv_len -= comma - ptr.bv_val;
2167 /* Start with BE suffix */
2169 for ( i = 0; i < suffrdns; i++ ) {
2170 comma = ber_bvrchr( &ptr, ',' );
2171 if ( comma != NULL ) {
2172 ptr.bv_len = comma - ptr.bv_val;
2179 if ( !BER_BVISEMPTY( &ptr ) ) {
2180 dn.bv_len -= ptr.bv_len + 1;
2181 dn.bv_val += ptr.bv_len + 1;
2184 /* the normalizedDNs are always the same length, no counting
2188 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2189 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2190 ndn.bv_len = be->be_nsuffix[0].bv_len;
2192 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2198 while ( ndn.bv_val > e->e_nname.bv_val ) {
2199 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2200 ber_dupbv( &glue->e_name, &dn );
2201 ber_dupbv( &glue->e_nname, &ndn );
2203 a = ch_calloc( 1, sizeof( Attribute ));
2204 a->a_desc = slap_schema.si_ad_objectClass;
2206 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2207 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2208 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2209 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2211 a->a_nvals = a->a_vals;
2213 a->a_next = glue->e_attrs;
2216 a = ch_calloc( 1, sizeof( Attribute ));
2217 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2219 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2220 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2221 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2223 a->a_nvals = a->a_vals;
2225 a->a_next = glue->e_attrs;
2228 op->o_req_dn = glue->e_name;
2229 op->o_req_ndn = glue->e_nname;
2231 rc = be->be_add ( op, &rs_add );
2232 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2233 be_entry_release_w( op, glue );
2235 /* incl. ALREADY EXIST */
2239 /* Move to next child */
2240 comma = ber_bvrchr( &ptr, ',' );
2241 if ( comma == NULL ) {
2244 ptr.bv_len = comma - ptr.bv_val;
2246 dn.bv_val = ++comma;
2247 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2249 comma = ber_bvrchr( &nptr, ',' );
2250 assert( comma != NULL );
2251 nptr.bv_len = comma - nptr.bv_val;
2253 ndn.bv_val = ++comma;
2254 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2257 op->o_req_dn = e->e_name;
2258 op->o_req_ndn = e->e_nname;
2260 rc = be->be_add ( op, &rs_add );
2261 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2262 be_entry_release_w( op, e );
2271 syncrepl_updateCookie(
2275 struct sync_cookie *syncCookie )
2277 Backend *be = op->o_bd;
2278 Modifications mod = { { 0 } };
2279 struct berval vals[ 2 ];
2283 slap_callback cb = { NULL };
2284 SlapReply rs_modify = {REP_RESULT};
2286 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2287 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2289 mod.sml_op = LDAP_MOD_REPLACE;
2290 mod.sml_desc = slap_schema.si_ad_contextCSN;
2291 mod.sml_type = mod.sml_desc->ad_cname;
2292 mod.sml_values = vals;
2293 vals[0] = si->si_syncCookie.ctxcsn;
2294 vals[1].bv_val = NULL;
2297 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2299 op->o_tag = LDAP_REQ_MODIFY;
2301 assert( si->si_rid < 1000 );
2303 cb.sc_response = null_callback;
2306 op->o_callback = &cb;
2307 op->o_req_dn = op->o_bd->be_suffix[0];
2308 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2310 /* update contextCSN */
2311 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2312 op->orm_modlist = &mod;
2313 rc = be->be_modify( op, &rs_modify );
2316 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2317 Debug( LDAP_DEBUG_ANY,
2318 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2321 slap_graduate_commit_csn( op );
2331 dninfo *dni = op->o_callback->sc_private;
2333 if ( rs->sr_type == REP_SEARCH ) {
2334 if ( !BER_BVISNULL( &dni->dn ) ) {
2335 Debug( LDAP_DEBUG_ANY,
2336 "dn_callback : consistency error - "
2337 "entryUUID is not unique\n", 0, 0, 0 );
2339 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2340 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2341 /* If there is a new entry, see if it differs from the old.
2342 * We compare the non-normalized values so that cosmetic changes
2343 * in the provider are always propagated.
2345 if ( dni->new_entry ) {
2346 Attribute *old, *new;
2349 /* Did the DN change? Note that we don't explicitly try to
2350 * discover if the deleteOldRdn argument applies here. It
2351 * would save an unnecessary Modify if we detected it, but
2352 * that's a fair amount of trouble to compare the two attr
2353 * lists in detail. (Just test normalized DN; we ignore
2354 * insignificant changes here.)
2356 if ( !dn_match( &rs->sr_entry->e_nname,
2357 &dni->new_entry->e_nname ) )
2362 for ( i = 0, old = rs->sr_entry->e_attrs;
2364 i++, old = old->a_next )
2369 /* We assume that attributes are saved in the same order
2370 * in the remote and local databases. So if we walk through
2371 * the attributeDescriptions one by one they should match in
2372 * lock step. If not, we signal a change. Otherwise we test
2375 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2377 old = old->a_next, new = new->a_next )
2379 if ( old->a_desc != new->a_desc ) {
2380 dni->wasChanged = 1;
2383 for ( i = 0; ; i++ ) {
2385 nold = BER_BVISNULL( &old->a_vals[i] );
2386 nnew = BER_BVISNULL( &new->a_vals[i] );
2387 /* If both are empty, stop looking */
2388 if ( nold && nnew ) {
2391 /* If they are different, stop looking */
2392 if ( nold != nnew ) {
2393 dni->wasChanged = 1;
2396 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2397 dni->wasChanged = 1;
2401 if ( dni->wasChanged ) break;
2403 if ( dni->wasChanged ) {
2404 dni->ads = op->o_tmpalloc( dni->attrs *
2405 sizeof(AttributeDescription *), op->o_tmpmemctx );
2407 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2408 dni->ads[i] = old->a_desc;
2414 } else if ( rs->sr_type == REP_RESULT ) {
2415 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2416 Debug( LDAP_DEBUG_ANY,
2417 "dn_callback : consistency error - "
2418 "entryUUID is not unique\n", 0, 0, 0 );
2422 return LDAP_SUCCESS;
2426 nonpresent_callback(
2430 syncinfo_t *si = op->o_callback->sc_private;
2433 struct berval* present_uuid = NULL;
2434 struct nonpresent_entry *np_entry;
2436 if ( rs->sr_type == REP_RESULT ) {
2437 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2438 si->si_presentlist = NULL;
2440 } else if ( rs->sr_type == REP_SEARCH ) {
2441 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2442 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2444 if ( a == NULL ) return 0;
2446 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2450 if ( present_uuid == NULL ) {
2451 np_entry = (struct nonpresent_entry *)
2452 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2453 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2454 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2455 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2458 avl_delete( &si->si_presentlist,
2459 &a->a_nvals[0], syncuuid_cmp );
2460 ch_free( present_uuid->bv_val );
2461 ch_free( present_uuid );
2464 return LDAP_SUCCESS;
2472 if ( rs->sr_err != LDAP_SUCCESS &&
2473 rs->sr_err != LDAP_REFERRAL &&
2474 rs->sr_err != LDAP_ALREADY_EXISTS &&
2475 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2476 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2478 Debug( LDAP_DEBUG_ANY,
2479 "null_callback : error code 0x%x\n",
2482 return LDAP_SUCCESS;
2485 static struct berval *
2486 slap_uuidstr_from_normalized(
2487 struct berval* uuidstr,
2488 struct berval* normalized,
2492 unsigned char nibble;
2495 if ( normalized == NULL ) return NULL;
2496 if ( normalized->bv_len != 16 ) return NULL;
2501 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2502 if ( new == NULL ) {
2509 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2510 if ( new != uuidstr ) {
2511 slap_sl_free( new, ctx );
2516 for ( i = 0; i < 16; i++ ) {
2517 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2518 new->bv_val[(i<<1)+d] = '-';
2522 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2523 if ( nibble < 10 ) {
2524 new->bv_val[(i<<1)+d] = nibble + '0';
2526 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2529 nibble = (normalized->bv_val[i]) & 0xF;
2530 if ( nibble < 10 ) {
2531 new->bv_val[(i<<1)+d+1] = nibble + '0';
2533 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2537 new->bv_val[new->bv_len] = '\0';
2542 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2544 const struct berval *uuid1 = v_uuid1;
2545 const struct berval *uuid2 = v_uuid2;
2546 int rc = uuid1->bv_len - uuid2->bv_len;
2547 if ( rc ) return rc;
2548 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2552 avl_ber_bvfree( void *v_bv )
2554 struct berval *bv = (struct berval *)v_bv;
2556 if( v_bv == NULL ) return;
2557 if ( !BER_BVISNULL( bv ) ) {
2558 ch_free( bv->bv_val );
2560 ch_free( (char *) bv );
2564 syncinfo_free( syncinfo_t *sie )
2566 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2568 bindconf_free( &sie->si_bindconf );
2570 if ( sie->si_filterstr.bv_val ) {
2571 ch_free( sie->si_filterstr.bv_val );
2573 if ( sie->si_base.bv_val ) {
2574 ch_free( sie->si_base.bv_val );
2576 if ( sie->si_attrs ) {
2578 while ( sie->si_attrs[i] != NULL ) {
2579 ch_free( sie->si_attrs[i] );
2582 ch_free( sie->si_attrs );
2584 if ( sie->si_exattrs ) {
2586 while ( sie->si_exattrs[i] != NULL ) {
2587 ch_free( sie->si_exattrs[i] );
2590 ch_free( sie->si_exattrs );
2592 if ( sie->si_anlist ) {
2594 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2595 ch_free( sie->si_anlist[i].an_name.bv_val );
2598 ch_free( sie->si_anlist );
2600 if ( sie->si_exanlist ) {
2602 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2603 ch_free( sie->si_exanlist[i].an_name.bv_val );
2606 ch_free( sie->si_exanlist );
2608 if ( sie->si_retryinterval ) {
2609 ch_free( sie->si_retryinterval );
2611 if ( sie->si_retrynum ) {
2612 ch_free( sie->si_retrynum );
2614 if ( sie->si_retrynum_init ) {
2615 ch_free( sie->si_retrynum_init );
2617 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2618 if ( sie->si_presentlist ) {
2619 avl_free( sie->si_presentlist, avl_ber_bvfree );
2622 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2624 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2625 struct nonpresent_entry* npe;
2626 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2627 LDAP_LIST_REMOVE( npe, npe_link );
2628 if ( npe->npe_name ) {
2629 if ( npe->npe_name->bv_val ) {
2630 ch_free( npe->npe_name->bv_val );
2632 ch_free( npe->npe_name );
2634 if ( npe->npe_nname ) {
2635 if ( npe->npe_nname->bv_val ) {
2636 ch_free( npe->npe_nname->bv_val );
2638 ch_free( npe->npe_nname );
2647 /* NOTE: used & documented in slapd.conf(5) */
2649 #define PROVIDERSTR "provider"
2650 #define SCHEMASTR "schemachecking"
2651 #define FILTERSTR "filter"
2652 #define SEARCHBASESTR "searchbase"
2653 #define SCOPESTR "scope"
2654 #define ATTRSONLYSTR "attrsonly"
2655 #define ATTRSSTR "attrs"
2656 #define TYPESTR "type"
2657 #define INTERVALSTR "interval"
2658 #define RETRYSTR "retry"
2659 #define SLIMITSTR "sizelimit"
2660 #define TLIMITSTR "timelimit"
2661 #define SYNCDATASTR "syncdata"
2663 /* FIXME: undocumented */
2664 #define LOGBASESTR "logbase"
2665 #define LOGFILTERSTR "logfilter"
2666 #define OLDAUTHCSTR "bindprincipal"
2667 #define EXATTRSSTR "exattrs"
2668 #define MANAGEDSAITSTR "manageDSAit"
2671 #define LASTMODSTR "lastmod"
2672 #define LMGENSTR "gen"
2673 #define LMNOSTR "no"
2674 #define LMREQSTR "req"
2675 #define SRVTABSTR "srvtab"
2676 #define SUFFIXSTR "suffix"
2679 #define GOT_ID 0x0001
2680 #define GOT_PROVIDER 0x0002
2683 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2689 { BER_BVC("base"), LDAP_SCOPE_BASE },
2690 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2691 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2692 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2693 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2694 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2695 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2699 static slap_verbmasks datamodes[] = {
2700 { BER_BVC("default"), SYNCDATA_DEFAULT },
2701 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2702 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2707 parse_syncrepl_line(
2715 for ( i = 1; i < c->argc; i++ ) {
2716 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2717 STRLENOF( IDSTR "=" ) ) )
2720 /* '\0' string terminator accounts for '=' */
2721 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2722 if ( lutil_atoi( &tmp, val ) != 0 ) {
2723 snprintf( c->msg, sizeof( c->msg ),
2724 "Error: parse_syncrepl_line: "
2725 "unable to parse syncrepl id \"%s\"", val );
2726 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2729 if ( tmp >= 1000 || tmp < 0 ) {
2730 snprintf( c->msg, sizeof( c->msg ),
2731 "Error: parse_syncrepl_line: "
2732 "syncrepl id %d is out of range [0..999]", tmp );
2733 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2738 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2739 STRLENOF( PROVIDERSTR "=" ) ) )
2741 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2742 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2743 gots |= GOT_PROVIDER;
2744 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2745 STRLENOF( SCHEMASTR "=" ) ) )
2747 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2748 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2749 si->si_schemachecking = 1;
2750 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2751 si->si_schemachecking = 0;
2753 si->si_schemachecking = 1;
2755 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2756 STRLENOF( FILTERSTR "=" ) ) )
2758 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2759 if ( si->si_filterstr.bv_val )
2760 ch_free( si->si_filterstr.bv_val );
2761 ber_str2bv( val, 0, 1, &si->si_filterstr );
2762 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2763 STRLENOF( LOGFILTERSTR "=" ) ) )
2765 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2766 if ( si->si_logfilterstr.bv_val )
2767 ch_free( si->si_logfilterstr.bv_val );
2768 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2769 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2770 STRLENOF( SEARCHBASESTR "=" ) ) )
2775 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2776 if ( si->si_base.bv_val ) {
2777 ch_free( si->si_base.bv_val );
2779 ber_str2bv( val, 0, 0, &bv );
2780 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2781 if ( rc != LDAP_SUCCESS ) {
2782 snprintf( c->msg, sizeof( c->msg ),
2783 "Invalid base DN \"%s\": %d (%s)",
2784 val, rc, ldap_err2string( rc ) );
2785 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2788 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2789 STRLENOF( LOGBASESTR "=" ) ) )
2794 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2795 if ( si->si_logbase.bv_val ) {
2796 ch_free( si->si_logbase.bv_val );
2798 ber_str2bv( val, 0, 0, &bv );
2799 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2800 if ( rc != LDAP_SUCCESS ) {
2801 snprintf( c->msg, sizeof( c->msg ),
2802 "Invalid logbase DN \"%s\": %d (%s)",
2803 val, rc, ldap_err2string( rc ) );
2804 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2807 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2808 STRLENOF( SCOPESTR "=" ) ) )
2811 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2812 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2813 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2814 si->si_scope = scopes[j].val;
2818 if ( BER_BVISNULL(&scopes[j].key) ) {
2819 snprintf( c->msg, sizeof( c->msg ),
2820 "Error: parse_syncrepl_line: "
2821 "unknown scope \"%s\"", val);
2822 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2825 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR "=",
2826 STRLENOF( ATTRSONLYSTR "=" ) ) )
2828 si->si_attrsonly = 1;
2829 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2830 STRLENOF( ATTRSSTR "=" ) ) )
2832 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2833 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2835 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2836 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2837 if ( si->si_anlist == NULL ) {
2838 ch_free( attr_fname );
2841 si->si_anfile = attr_fname;
2843 char *str, *s, *next;
2844 char delimstr[] = " ,\t";
2845 str = ch_strdup( val );
2846 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2848 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2850 if ( strlen(s) == 1 && *s == '*' ) {
2851 si->si_allattrs = 1;
2852 *(val + ( s - str )) = delimstr[0];
2854 if ( strlen(s) == 1 && *s == '+' ) {
2855 si->si_allopattrs = 1;
2856 *(val + ( s - str )) = delimstr[0];
2860 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2861 if ( si->si_anlist == NULL ) {
2865 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2866 STRLENOF( EXATTRSSTR "=" ) ) )
2868 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2869 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2871 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2872 si->si_exanlist = file2anlist(
2873 si->si_exanlist, attr_fname, " ,\t" );
2874 if ( si->si_exanlist == NULL ) {
2875 ch_free( attr_fname );
2878 ch_free( attr_fname );
2880 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2881 if ( si->si_exanlist == NULL ) {
2885 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2886 STRLENOF( TYPESTR "=" ) ) )
2888 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2889 if ( !strncasecmp( val, "refreshOnly",
2890 STRLENOF("refreshOnly") ) )
2892 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2893 } else if ( !strncasecmp( val, "refreshAndPersist",
2894 STRLENOF("refreshAndPersist") ) )
2896 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2897 si->si_interval = 60;
2899 snprintf( c->msg, sizeof( c->msg ),
2900 "Error: parse_syncrepl_line: "
2901 "unknown sync type \"%s\"", val);
2902 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2905 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2906 STRLENOF( INTERVALSTR "=" ) ) )
2908 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2909 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2910 si->si_interval = 0;
2911 } else if ( strchr( val, ':' ) != NULL ) {
2912 char *next, *ptr = val;
2913 unsigned dd, hh, mm, ss;
2914 dd = strtoul( ptr, &next, 10 );
2915 if ( next == ptr || next[0] != ':' ) {
2916 snprintf( c->msg, sizeof( c->msg ),
2917 "Error: parse_syncrepl_line: "
2918 "invalid interval \"%s\", unable to parse days", val );
2919 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2923 hh = strtoul( ptr, &next, 10 );
2924 if ( next == ptr || next[0] != ':' || hh > 24 ) {
2925 snprintf( c->msg, sizeof( c->msg ),
2926 "Error: parse_syncrepl_line: "
2927 "invalid interval \"%s\", unable to parse hours", val );
2928 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2932 mm = strtoul( ptr, &next, 10 );
2933 if ( next == ptr || next[0] != ':' || mm > 60 ) {
2934 snprintf( c->msg, sizeof( c->msg ),
2935 "Error: parse_syncrepl_line: "
2936 "invalid interval \"%s\", unable to parse minutes", val );
2937 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2941 ss = strtoul( ptr, &next, 10 );
2942 if ( next == ptr || next[0] != '\0' || ss > 60 ) {
2943 snprintf( c->msg, sizeof( c->msg ),
2944 "Error: parse_syncrepl_line: "
2945 "invalid interval \"%s\", unable to parse seconds", val );
2946 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2949 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2953 if ( lutil_parse_time( val, &t ) != 0 ) {
2954 snprintf( c->msg, sizeof( c->msg ),
2955 "Error: parse_syncrepl_line: "
2956 "invalid interval \"%s\"", val );
2957 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2960 si->si_interval = (time_t)t;
2962 if ( si->si_interval < 0 ) {
2963 snprintf( c->msg, sizeof( c->msg ),
2964 "Error: parse_syncrepl_line: "
2965 "invalid interval \"%ld\"",
2966 (long) si->si_interval);
2967 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2970 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
2971 STRLENOF( RETRYSTR "=" ) ) )
2976 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
2977 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2978 retry_list[0] = NULL;
2980 slap_str2clist( &retry_list, val, " ,\t" );
2982 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2985 snprintf( c->msg, sizeof( c->msg ),
2986 "Error: incomplete syncrepl retry list" );
2987 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2988 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2989 ch_free( retry_list[k] );
2991 ch_free( retry_list );
2994 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2995 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2996 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2997 for ( j = 0; j < n; j++ ) {
2999 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3000 snprintf( c->msg, sizeof( c->msg ),
3001 "Error: invalid retry interval \"%s\" (#%d)",
3002 retry_list[j*2], j );
3003 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3004 /* do some cleanup */
3007 si->si_retryinterval[j] = (time_t)t;
3008 if ( *retry_list[j*2+1] == '+' ) {
3009 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3010 si->si_retrynum[j] = RETRYNUM_FOREVER;
3014 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0 ) {
3015 snprintf( c->msg, sizeof( c->msg ),
3016 "Error: invalid initial retry number \"%s\" (#%d)",
3017 retry_list[j*2+1], j );
3018 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3019 /* do some cleanup */
3022 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0 ) {
3023 snprintf( c->msg, sizeof( c->msg ),
3024 "Error: invalid retry number \"%s\" (#%d)",
3025 retry_list[j*2+1], j );
3026 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3027 /* do some cleanup */
3032 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3033 si->si_retrynum[j] = RETRYNUM_TAIL;
3034 si->si_retryinterval[j] = 0;
3036 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3037 ch_free( retry_list[k] );
3039 ch_free( retry_list );
3040 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3041 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3043 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3044 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3045 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3047 snprintf( c->msg, sizeof( c->msg ),
3048 "invalid manageDSAit value \"%s\".\n",
3050 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3053 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3054 STRLENOF( SLIMITSTR "=") ) )
3056 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3057 if ( lutil_atoi( &si->si_slimit, val ) != 0 ) {
3058 snprintf( c->msg, sizeof( c->msg ),
3059 "invalid size limit value \"%s\".\n",
3061 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3064 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3065 STRLENOF( TLIMITSTR "=" ) ) )
3067 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3068 if ( lutil_atoi( &si->si_tlimit, val ) != 0 ) {
3069 snprintf( c->msg, sizeof( c->msg ),
3070 "invalid time limit value \"%s\".\n",
3072 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3075 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3076 STRLENOF( SYNCDATASTR "=" ) ) )
3078 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3079 si->si_syncdata = verb_to_mask( val, datamodes );
3080 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3081 snprintf( c->msg, sizeof( c->msg ),
3082 "Error: parse_syncrepl_line: "
3083 "unknown keyword \"%s\"\n", c->argv[ i ] );
3084 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3089 if ( gots != GOT_ALL ) {
3090 snprintf( c->msg, sizeof( c->msg ),
3091 "Error: Malformed \"syncrepl\" line in slapd config file" );
3092 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3106 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3107 Debug( LDAP_DEBUG_ANY, "%s: database %s does not support operations "
3108 "required for syncrepl\n", c->log, c->be->be_type, 0 );
3111 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3114 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3118 si->si_bindconf.sb_tls = SB_TLS_OFF;
3119 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3120 si->si_schemachecking = 0;
3121 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3122 &si->si_filterstr );
3123 si->si_base.bv_val = NULL;
3124 si->si_scope = LDAP_SCOPE_SUBTREE;
3125 si->si_attrsonly = 0;
3126 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3127 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3128 si->si_attrs = NULL;
3129 si->si_allattrs = 0;
3130 si->si_allopattrs = 0;
3131 si->si_exattrs = NULL;
3132 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3133 si->si_interval = 86400;
3134 si->si_retryinterval = NULL;
3135 si->si_retrynum_init = NULL;
3136 si->si_retrynum = NULL;
3137 si->si_manageDSAit = 0;
3140 si->si_conn_setup = 0;
3142 si->si_presentlist = NULL;
3143 LDAP_LIST_INIT( &si->si_nonpresentlist );
3144 ldap_pvt_thread_mutex_init( &si->si_mutex );
3146 rc = parse_syncrepl_line( c, si );
3150 init_syncrepl( si );
3151 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3152 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3157 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3158 syncinfo_free( si );
3161 Debug( LDAP_DEBUG_CONFIG,
3162 "Config: ** successfully added syncrepl \"%s\"\n",
3163 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3164 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3165 if ( !si->si_schemachecking ) {
3166 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3168 c->be->be_syncinfo = si;
3174 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3176 struct berval bc, uri;
3177 char buf[BUFSIZ*2], *ptr;
3180 /* temporarily inhibit bindconf from printing URI */
3181 uri = si->si_bindconf.sb_uri;
3182 BER_BVZERO( &si->si_bindconf.sb_uri );
3183 bindconf_unparse( &si->si_bindconf, &bc );
3184 si->si_bindconf.sb_uri = uri;
3187 ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03ld " PROVIDERSTR "=%s",
3188 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3189 if ( !BER_BVISNULL( &bc )) {
3190 ptr = lutil_strcopy( ptr, bc.bv_val );
3193 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3194 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3195 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3198 if ( !BER_BVISNULL( &si->si_base )) {
3199 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3200 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3203 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3204 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3205 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3208 if ( !BER_BVISNULL( &si->si_logbase )) {
3209 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3210 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3213 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3214 if ( si->si_scope == scopes[i].val ) {
3215 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3216 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3220 if ( si->si_attrsonly ) {
3221 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3223 if ( si->si_anfile ) {
3224 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3225 ptr = lutil_strcopy( ptr, si->si_anfile );
3226 } else if ( si->si_allattrs || si->si_allopattrs ||
3227 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3229 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3231 ptr = anlist_unparse( si->si_anlist, ptr );
3232 if ( si->si_allattrs ) {
3233 if ( old != ptr ) *ptr++ = ',';
3236 if ( si->si_allopattrs ) {
3237 if ( old != ptr ) *ptr++ = ',';
3242 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3243 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3244 ptr = anlist_unparse( si->si_exanlist, ptr );
3246 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3247 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3249 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3250 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3251 "refreshAndPersist" : "refreshOnly" );
3253 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3256 dd = si->si_interval;
3263 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3264 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3265 } else if ( si->si_retryinterval ) {
3267 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3268 for (i=0; si->si_retryinterval[i]; i++) {
3269 if ( space ) *ptr++ = ' ';
3271 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3272 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3275 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3280 if ( si->si_slimit ) {
3281 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3282 ptr += sprintf( ptr, "%d", si->si_slimit );
3285 if ( si->si_tlimit ) {
3286 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3287 ptr += sprintf( ptr, "%d", si->si_tlimit );
3290 if ( si->si_syncdata ) {
3291 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3292 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3293 ptr = lutil_strcopy( ptr, bc.bv_val );
3296 bc.bv_len = ptr - buf;
3298 ber_dupbv( bv, &bc );
3302 syncrepl_config( ConfigArgs *c )
3304 if (c->op == SLAP_CONFIG_EMIT) {
3305 if ( c->be->be_syncinfo ) {
3307 syncrepl_unparse( c->be->be_syncinfo, &bv );
3308 ber_bvarray_add( &c->rvalue_vals, &bv );
3312 } else if ( c->op == LDAP_MOD_DELETE ) {
3315 if ( c->be->be_syncinfo ) {
3316 re = c->be->be_syncinfo->si_re;
3318 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3319 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3320 ldap_pvt_runqueue_remove( &slapd_rq, re );
3322 syncinfo_free( c->be->be_syncinfo );
3323 c->be->be_syncinfo = NULL;
3325 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3328 if ( SLAP_SHADOW( c->be ) ) {
3329 Debug(LDAP_DEBUG_ANY, "%s: "
3330 "syncrepl: database already shadowed.\n",
3333 } else if ( add_syncrepl( c ) ) {
3336 return config_sync_shadow( c );