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;
89 Avlnode *si_presentlist;
91 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
92 ldap_pvt_thread_mutex_t si_mutex;
95 static int syncuuid_cmp( const void *, const void * );
96 static void avl_ber_bvfree( void * );
97 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
98 static int syncrepl_message_to_op(
99 syncinfo_t *, Operation *, LDAPMessage * );
100 static int syncrepl_message_to_entry(
101 syncinfo_t *, Operation *, LDAPMessage *,
102 Modifications **, Entry **, int );
103 static int syncrepl_entry(
104 syncinfo_t *, Operation*, Entry*,
105 Modifications**,int, struct berval*,
106 struct sync_cookie *,
108 static int syncrepl_updateCookie(
109 syncinfo_t *, Operation *, struct berval *,
110 struct sync_cookie * );
111 static struct berval * slap_uuidstr_from_normalized(
112 struct berval *, struct berval *, void * );
114 /* callback functions */
115 static int dn_callback( struct slap_op *, struct slap_rep * );
116 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
117 static int null_callback( struct slap_op *, struct slap_rep * );
119 static AttributeDescription *sync_descs[4];
122 init_syncrepl(syncinfo_t *si)
125 char **attrs, **exattrs;
127 if ( !sync_descs[0] ) {
128 sync_descs[0] = slap_schema.si_ad_objectClass;
129 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
130 sync_descs[2] = slap_schema.si_ad_entryCSN;
131 sync_descs[3] = NULL;
134 if ( si->si_allattrs && si->si_allopattrs )
137 attrs = anlist2attrs( si->si_anlist );
140 if ( si->si_allattrs ) {
143 if ( !is_at_operational( at_find( attrs[i] ))) {
144 for ( j = i; attrs[j] != NULL; j++ ) {
147 attrs[j] = attrs[j+1];
153 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
154 attrs[i] = ch_strdup("*");
157 } else if ( si->si_allopattrs ) {
160 if ( is_at_operational( at_find( attrs[i] ))) {
161 for ( j = i; attrs[j] != NULL; j++ ) {
164 attrs[j] = attrs[j+1];
170 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
171 attrs[i] = ch_strdup("+");
175 for ( i = 0; sync_descs[i] != NULL; i++ ) {
178 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
179 for ( k = j; attrs[k] != NULL; k++ ) {
182 attrs[k] = attrs[k+1];
190 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
192 if ( si->si_allopattrs ) {
193 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
195 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
198 if ( attrs == NULL ) {
199 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
203 if ( si->si_allopattrs ) {
204 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
206 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
207 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
215 if ( si->si_allattrs == si->si_allopattrs ) {
216 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
217 attrs[i++] = ch_strdup( "*" );
218 attrs[i++] = ch_strdup( "+" );
219 } else if ( si->si_allattrs && !si->si_allopattrs ) {
220 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
221 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
222 attrs[i++] = ch_strdup( "*" );
223 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
224 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
226 } else if ( !si->si_allattrs && si->si_allopattrs ) {
227 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
228 attrs[i++] = ch_strdup( "+" );
229 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
234 si->si_attrs = attrs;
236 exattrs = anlist2attrs( si->si_exanlist );
239 for ( n = 0; exattrs[n] != NULL; n++ ) ;
241 for ( i = 0; sync_descs[i] != NULL; i++ ) {
243 while ( exattrs[j] != NULL ) {
244 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
245 ch_free( exattrs[j] );
246 for ( k = j; exattrs[k] != NULL; k++ ) {
247 exattrs[k] = exattrs[k+1];
255 for ( i = 0; exattrs[i] != NULL; i++ ) {
256 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
258 if ( ( oc = si->si_anlist[j].an_oc ) ) {
260 while ( oc->soc_required[k] ) {
261 if ( !strcmp( exattrs[i],
262 oc->soc_required[k]->sat_cname.bv_val )) {
263 ch_free( exattrs[i] );
264 for ( l = i; exattrs[l]; l++ ) {
265 exattrs[l] = exattrs[l+1];
275 for ( i = 0; exattrs[i] != NULL; i++ ) ;
278 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
281 si->si_exattrs = exattrs;
284 typedef struct logschema {
286 struct berval ls_req;
287 struct berval ls_mod;
288 struct berval ls_newRdn;
289 struct berval ls_delRdn;
290 struct berval ls_newSup;
293 static logschema changelog_sc = {
295 BER_BVC("changeType"),
298 BER_BVC("deleteOldRDN"),
299 BER_BVC("newSuperior")
302 static logschema accesslog_sc = {
306 BER_BVC("reqNewRDN"),
307 BER_BVC("reqDeleteOldRDN"),
308 BER_BVC("reqNewSuperior")
316 BerElementBuffer berbuf;
317 BerElement *ber = (BerElement *)&berbuf;
318 LDAPControl c[2], *ctrls[3];
319 struct timeval timeout;
324 char **attrs, *lattrs[8];
329 /* setup LDAP SYNC control */
330 ber_init2( ber, NULL, LBER_USE_DER );
331 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
333 /* If we're using a log but we have no state, then fallback to
334 * normal mode for a full refresh.
336 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
337 si->si_logstate = SYNCLOG_FALLBACK;
339 /* Use the log parameters if we're in log mode */
340 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
342 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
346 lattrs[0] = ls->ls_dn.bv_val;
347 lattrs[1] = ls->ls_req.bv_val;
348 lattrs[2] = ls->ls_mod.bv_val;
349 lattrs[3] = ls->ls_newRdn.bv_val;
350 lattrs[4] = ls->ls_delRdn.bv_val;
351 lattrs[5] = ls->ls_newSup.bv_val;
352 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
356 base = si->si_logbase.bv_val;
357 filter = si->si_logfilterstr.bv_val;
360 scope = LDAP_SCOPE_SUBTREE;
363 base = si->si_base.bv_val;
364 filter = si->si_filterstr.bv_val;
365 attrs = si->si_attrs;
366 attrsonly = si->si_attrsonly;
367 scope = si->si_scope;
369 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
370 si->si_type = LDAP_SYNC_REFRESH_ONLY;
372 si->si_type = si->si_ctype;
375 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
377 ber_printf( ber, "{eOb}",
378 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
380 ber_printf( ber, "{eb}",
381 abs(si->si_type), rhint );
384 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
389 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
390 c[0].ldctl_iscritical = si->si_type < 0;
393 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
394 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
395 c[1].ldctl_value = si->si_bindconf.sb_authzId;
396 c[1].ldctl_iscritical = 1;
403 timeout.tv_sec = si->si_tlimit;
406 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
407 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
408 si->si_slimit, &msgid );
419 int cmdline_cookie_found = 0;
421 struct sync_cookie *sc = NULL;
427 psub = &si->si_be->be_nsuffix[0];
429 /* Init connection to master */
430 rc = ldap_initialize( &si->si_ld, si->si_bindconf.sb_uri.bv_val );
431 if ( rc != LDAP_SUCCESS ) {
432 Debug( LDAP_DEBUG_ANY,
433 "do_syncrep1: ldap_initialize failed (%s)\n",
434 si->si_bindconf.sb_uri.bv_val, 0, 0 );
438 op->o_protocol = LDAP_VERSION3;
439 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION,
440 (const void *)&op->o_protocol );
443 if ( si->si_check_tls ) {
444 si->si_check_tls = 0;
445 rc = bindconf_tls_set( &si->si_bindconf, si->si_ld );
446 } else if ( si->si_bindconf.sb_tls_ctx ) {
447 rc = ldap_set_option( si->si_ld, LDAP_OPT_X_TLS_CTX,
448 si->si_bindconf.sb_tls_ctx );
451 Debug( LDAP_DEBUG_ANY,
452 "do_syncrep1: TLS context initialization failed\n", 0, 0, 0 );
459 if ( si->si_bindconf.sb_tls ) {
460 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
461 if( rc != LDAP_SUCCESS ) {
462 Debug( LDAP_DEBUG_ANY,
463 "%s: ldap_start_tls failed (%d)\n",
464 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
466 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
470 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
471 #ifdef HAVE_CYRUS_SASL
474 if ( si->si_bindconf.sb_secprops != NULL ) {
475 rc = ldap_set_option( si->si_ld,
476 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
478 if( rc != LDAP_OPT_SUCCESS ) {
479 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
480 "(%s,SECPROPS,\"%s\") failed!\n",
481 si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops, 0 );
486 defaults = lutil_sasl_defaults( si->si_ld,
487 si->si_bindconf.sb_saslmech.bv_val,
488 si->si_bindconf.sb_realm.bv_val,
489 si->si_bindconf.sb_authcId.bv_val,
490 si->si_bindconf.sb_cred.bv_val,
491 si->si_bindconf.sb_authzId.bv_val );
493 rc = ldap_sasl_interactive_bind_s( si->si_ld,
494 si->si_bindconf.sb_binddn.bv_val,
495 si->si_bindconf.sb_saslmech.bv_val,
501 lutil_sasl_freedefs( defaults );
503 /* FIXME: different error behaviors according to
505 * 2) on err policy : exit, retry, backoff ...
507 if ( rc != LDAP_SUCCESS ) {
508 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
510 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
511 "ldap_sasl_interactive_bind_s failed (%d)\n",
514 /* FIXME (see above comment) */
515 /* if Kerberos credentials cache is not active, retry */
516 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
517 rc == LDAP_LOCAL_ERROR )
519 rc = LDAP_SERVER_DOWN;
524 #else /* HAVE_CYRUS_SASL */
525 /* Should never get here, we trapped this at config time */
527 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
532 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
533 rc = ldap_sasl_bind_s( si->si_ld,
534 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
535 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
536 if ( rc != LDAP_SUCCESS ) {
537 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
538 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
543 /* Set SSF to strongest of TLS, SASL SSFs */
546 op->o_transport_ssf = 0;
548 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
549 == LDAP_SUCCESS && ssl != NULL )
551 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
553 #endif /* HAVE_TLS */
554 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
555 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
556 ? op->o_sasl_ssf : op->o_tls_ssf;
559 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
560 /* get contextCSN shadow replica from database */
561 BerVarray csn = NULL;
563 assert( si->si_rid < 1000 );
564 op->o_req_ndn = op->o_bd->be_nsuffix[0];
565 op->o_req_dn = op->o_req_ndn;
567 /* try to read stored contextCSN */
568 backend_attribute( op, NULL, &op->o_req_ndn,
569 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
571 ch_free( si->si_syncCookie.ctxcsn.bv_val );
572 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
573 ber_bvarray_free_x( csn, op->o_tmpmemctx );
576 si->si_syncCookie.rid = si->si_rid;
578 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
579 if ( si->si_rid == sc->rid ) {
580 cmdline_cookie_found = 1;
585 if ( cmdline_cookie_found ) {
586 /* cookie is supplied in the command line */
588 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
590 /* ctxcsn wasn't parsed yet, do it now */
591 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
592 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
593 /* if cmdline cookie does not have ctxcsn */
594 /* component, set it to an initial value */
595 slap_init_sync_cookie_ctxcsn( sc );
597 slap_sync_cookie_free( &si->si_syncCookie, 0 );
598 slap_dup_sync_cookie( &si->si_syncCookie, sc );
599 slap_sync_cookie_free( sc, 1 );
602 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
603 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
606 rc = ldap_sync_search( si, op->o_tmpmemctx );
608 if( rc != LDAP_SUCCESS ) {
609 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
610 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
616 ldap_unbind_ext( si->si_ld, NULL, NULL );
629 LDAPControl **rctrls = NULL;
632 BerElementBuffer berbuf;
633 BerElement *ber = (BerElement *)&berbuf;
635 LDAPMessage *res = NULL;
636 LDAPMessage *msg = NULL;
639 struct berval *retdata = NULL;
644 struct berval syncUUID = BER_BVNULL;
645 struct sync_cookie syncCookie = { BER_BVNULL };
646 struct sync_cookie syncCookie_req = { BER_BVNULL };
647 struct berval cookie = BER_BVNULL;
653 Modifications *modlist = NULL;
658 struct timeval *tout_p = NULL;
659 struct timeval tout = { 0, 0 };
661 int refreshDeletes = 0;
663 BerVarray syncUUIDs = NULL;
666 if ( slapd_shutdown ) {
671 ber_init2( ber, NULL, LBER_USE_DER );
672 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
674 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
676 psub = &si->si_be->be_nsuffix[0];
678 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
680 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
686 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
687 tout_p, &res )) > 0 )
689 if ( slapd_shutdown ) {
693 for( msg = ldap_first_message( si->si_ld, res );
695 msg = ldap_next_message( si->si_ld, msg ) )
697 if ( slapd_shutdown ) {
701 switch( ldap_msgtype( msg ) ) {
702 case LDAP_RES_SEARCH_ENTRY:
703 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
704 /* we can't work without the control */
706 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
707 "got search entry without "
708 "control\n", 0, 0, 0 );
713 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
714 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
715 /* FIXME: what if syncUUID is NULL or empty?
716 * (happens with back-sql...) */
717 if ( BER_BVISEMPTY( &syncUUID ) ) {
718 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
719 "got empty syncUUID\n", 0, 0, 0 );
720 ldap_controls_free( rctrls );
724 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
725 ber_scanf( ber, /*"{"*/ "m}", &cookie );
726 if ( !BER_BVISNULL( &cookie ) ) {
727 ch_free( syncCookie.octet_str.bv_val );
728 ber_dupbv( &syncCookie.octet_str, &cookie );
730 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
732 slap_parse_sync_cookie( &syncCookie, NULL );
736 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
738 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
739 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
740 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
742 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
743 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
744 if (( rc = syncrepl_entry( si, op, entry, &modlist,
745 syncstate, &syncUUID, &syncCookie_req,
746 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
747 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
748 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
751 ldap_controls_free( rctrls );
753 slap_mods_free( modlist, 1 );
759 case LDAP_RES_SEARCH_REFERENCE:
760 Debug( LDAP_DEBUG_ANY,
761 "do_syncrep2: reference received error\n", 0, 0, 0 );
764 case LDAP_RES_SEARCH_RESULT:
765 Debug( LDAP_DEBUG_SYNC,
766 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
767 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
769 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
770 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
771 /* map old result code to registered code */
772 err = LDAP_SYNC_REFRESH_REQUIRED;
775 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
776 if ( si->si_logstate == SYNCLOG_LOGGING ) {
777 si->si_logstate = SYNCLOG_FALLBACK;
784 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
786 ber_scanf( ber, "{" /*"}"*/);
787 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
788 ber_scanf( ber, "m", &cookie );
789 if ( !BER_BVISNULL( &cookie ) ) {
790 ch_free( syncCookie.octet_str.bv_val );
791 ber_dupbv( &syncCookie.octet_str, &cookie);
793 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
795 slap_parse_sync_cookie( &syncCookie, NULL );
798 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
800 ber_scanf( ber, "b", &refreshDeletes );
802 ber_scanf( ber, /*"{"*/ "}" );
804 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
806 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
809 value_match( &match, slap_schema.si_ad_entryCSN,
810 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
811 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
812 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
815 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
816 match < 0 && err == LDAP_SUCCESS )
818 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
821 ldap_controls_free( rctrls );
823 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
824 /* FIXME : different error behaviors according to
825 * 1) err code : LDAP_BUSY ...
826 * 2) on err policy : stop service, stop sync, retry
828 if ( refreshDeletes == 0 && match < 0 &&
829 err == LDAP_SUCCESS )
831 syncrepl_del_nonpresent( op, si, NULL );
833 avl_free( si->si_presentlist, avl_ber_bvfree );
834 si->si_presentlist = NULL;
837 if ( err == LDAP_SUCCESS
838 && si->si_logstate == SYNCLOG_FALLBACK ) {
839 si->si_logstate = SYNCLOG_LOGGING;
840 rc = LDAP_SYNC_REFRESH_REQUIRED;
847 case LDAP_RES_INTERMEDIATE:
848 rc = ldap_parse_intermediate( si->si_ld, msg,
849 &retoid, &retdata, NULL, 0 );
850 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
851 ber_init2( ber, retdata, LBER_USE_DER );
853 switch ( si_tag = ber_peek_tag( ber, &len )) {
855 case LDAP_TAG_SYNC_NEW_COOKIE:
856 Debug( LDAP_DEBUG_SYNC,
857 "do_syncrep2: %s - %s%s\n",
858 "LDAP_RES_INTERMEDIATE",
859 "NEW_COOKIE", "\n" );
860 ber_scanf( ber, "tm", &tag, &cookie );
862 case LDAP_TAG_SYNC_REFRESH_DELETE:
863 case LDAP_TAG_SYNC_REFRESH_PRESENT:
864 Debug( LDAP_DEBUG_SYNC,
865 "do_syncrep2: %s - %s%s\n",
866 "LDAP_RES_INTERMEDIATE",
867 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
868 "REFRESH_PRESENT" : "REFRESH_DELETE",
870 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
871 si->si_refreshDelete = 1;
873 si->si_refreshPresent = 1;
875 ber_scanf( ber, "t{" /*"}"*/, &tag );
876 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
878 ber_scanf( ber, "m", &cookie );
879 if ( !BER_BVISNULL( &cookie ) ) {
880 ch_free( syncCookie.octet_str.bv_val );
881 ber_dupbv( &syncCookie.octet_str, &cookie );
883 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
885 slap_parse_sync_cookie( &syncCookie, NULL );
888 if ( ber_peek_tag( ber, &len ) ==
889 LDAP_TAG_REFRESHDONE )
891 ber_scanf( ber, "b", &refreshDone );
893 ber_scanf( ber, /*"{"*/ "}" );
895 case LDAP_TAG_SYNC_ID_SET:
896 Debug( LDAP_DEBUG_SYNC,
897 "do_syncrep2: %s - %s%s\n",
898 "LDAP_RES_INTERMEDIATE",
901 ber_scanf( ber, "t{" /*"}"*/, &tag );
902 if ( ber_peek_tag( ber, &len ) ==
903 LDAP_TAG_SYNC_COOKIE )
905 ber_scanf( ber, "m", &cookie );
906 if ( !BER_BVISNULL( &cookie ) ) {
907 ch_free( syncCookie.octet_str.bv_val );
908 ber_dupbv( &syncCookie.octet_str, &cookie );
910 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
912 slap_parse_sync_cookie( &syncCookie, NULL );
915 if ( ber_peek_tag( ber, &len ) ==
916 LDAP_TAG_REFRESHDELETES )
918 ber_scanf( ber, "b", &refreshDeletes );
920 ber_scanf( ber, "[W]", &syncUUIDs );
921 ber_scanf( ber, /*"{"*/ "}" );
922 if ( refreshDeletes ) {
923 syncrepl_del_nonpresent( op, si, syncUUIDs );
924 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
926 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
927 struct berval *syncuuid_bv;
928 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
929 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
930 avl_insert( &si->si_presentlist,
931 (caddr_t) syncuuid_bv,
932 syncuuid_cmp, avl_dup_error );
934 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
938 Debug( LDAP_DEBUG_ANY,
939 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
940 (long) si_tag, 0, 0 );
941 ldap_memfree( retoid );
942 ber_bvfree( retdata );
946 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
948 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
951 value_match( &match, slap_schema.si_ad_entryCSN,
952 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
953 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
954 &syncCookie_req.ctxcsn,
955 &syncCookie.ctxcsn, &text );
958 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
961 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
964 if ( si->si_refreshPresent == 1 ) {
966 syncrepl_del_nonpresent( op, si, NULL );
970 ldap_memfree( retoid );
971 ber_bvfree( retdata );
975 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
976 "unknown intermediate response (%d)\n",
978 ldap_memfree( retoid );
979 ber_bvfree( retdata );
985 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
986 "unknown message\n", 0, 0, 0 );
990 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
991 slap_sync_cookie_free( &syncCookie_req, 0 );
992 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
993 slap_sync_cookie_free( &syncCookie, 0 );
1003 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1004 errstr = ldap_err2string( rc );
1006 Debug( LDAP_DEBUG_ANY,
1007 "do_syncrep2 : %s\n", errstr, 0, 0 );
1011 slap_sync_cookie_free( &syncCookie, 0 );
1012 slap_sync_cookie_free( &syncCookie_req, 0 );
1014 if ( res ) ldap_msgfree( res );
1016 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1017 if ( si->si_conn_setup ) {
1019 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1020 connection_client_stop( s );
1021 si->si_conn_setup = 0;
1023 ldap_unbind_ext( si->si_ld, NULL, NULL );
1035 struct re_s* rtask = arg;
1036 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1037 Connection conn = {0};
1038 OperationBuffer opbuf;
1040 int rc = LDAP_SUCCESS;
1046 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1051 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1053 switch( abs( si->si_type )) {
1054 case LDAP_SYNC_REFRESH_ONLY:
1055 case LDAP_SYNC_REFRESH_AND_PERSIST:
1058 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1062 if ( slapd_shutdown ) {
1064 if ( si->si_conn_setup ) {
1065 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1066 connection_client_stop( s );
1067 si->si_conn_setup = 0;
1069 ldap_unbind_ext( si->si_ld, NULL, NULL );
1072 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1076 op = (Operation *) &opbuf;
1077 connection_fake_init( &conn, op, ctx );
1079 /* use global malloc for now */
1080 op->o_tmpmemctx = NULL;
1081 op->o_tmpmfuncs = &ch_mfuncs;
1083 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1084 op->o_bd = be = si->si_be;
1085 op->o_dn = op->o_bd->be_rootdn;
1086 op->o_ndn = op->o_bd->be_rootndn;
1088 /* Establish session, do search */
1090 si->si_refreshDelete = 0;
1091 si->si_refreshPresent = 0;
1092 rc = do_syncrep1( op, si );
1096 /* Process results */
1097 if ( rc == LDAP_SUCCESS ) {
1098 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1100 rc = do_syncrep2( op, si );
1101 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1102 rc = ldap_sync_search( si, op->o_tmpmemctx );
1106 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1107 /* If we succeeded, enable the connection for further listening.
1108 * If we failed, tear down the connection and reschedule.
1110 if ( rc == LDAP_SUCCESS ) {
1111 if ( si->si_conn_setup ) {
1112 connection_client_enable( s );
1114 rc = connection_client_setup( s, do_syncrepl, arg );
1116 si->si_conn_setup = 1;
1118 } else if ( si->si_conn_setup ) {
1122 if ( rc == -2 ) rc = 0;
1126 /* At this point, we have 4 cases:
1127 * 1) for any hard failure, give up and remove this task
1128 * 2) for ServerDown, reschedule this task to run
1129 * 3) for Refresh and Success, reschedule to run
1130 * 4) for Persist and Success, reschedule to defer
1132 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1134 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1135 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1139 connection_client_stop( s );
1142 if ( rc == LDAP_SUCCESS ) {
1143 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1146 rtask->interval.tv_sec = si->si_interval;
1147 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1148 if ( si->si_retrynum ) {
1149 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1150 si->si_retrynum[i] = si->si_retrynum_init[i];
1152 si->si_retrynum[i] = RETRYNUM_TAIL;
1155 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1156 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1160 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1161 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1162 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1163 if ( si->si_retrynum[i] > 0 )
1164 si->si_retrynum[i]--;
1165 rtask->interval.tv_sec = si->si_retryinterval[i];
1166 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1167 slap_wake_listener();
1171 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1172 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1177 static slap_verbmasks modops[] = {
1178 { BER_BVC("add"), LDAP_REQ_ADD },
1179 { BER_BVC("delete"), LDAP_REQ_DELETE },
1180 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1181 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1185 static Modifications *
1186 syncrepl_accesslog_mods(
1193 AttributeDescription *ad;
1194 struct berval bv, bv2;
1196 Modifications *mod = NULL, *modlist = NULL, **modtail;
1201 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1205 colon = ber_bvchr( &bv, ':' );
1207 continue; /* invalid */
1208 bv.bv_len = colon - bv.bv_val;
1209 if ( slap_bv2ad( &bv, &ad, &text )) {
1213 /* Ignore dynamically generated attrs */
1214 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1216 /* Ignore excluded attrs */
1217 if ( ldap_charray_inlist( si->si_exattrs,
1218 ad->ad_type->sat_cname.bv_val ))
1222 case '+': op = LDAP_MOD_ADD; break;
1223 case '-': op = LDAP_MOD_DELETE; break;
1224 case '=': op = LDAP_MOD_REPLACE; break;
1225 case '#': op = LDAP_MOD_INCREMENT; break;
1229 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1230 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1233 mod->sml_next = NULL;
1235 mod->sml_type = ad->ad_cname;
1236 mod->sml_values = NULL;
1237 mod->sml_nvalues = NULL;
1240 modtail = &mod->sml_next;
1242 if ( colon[2] == ' ' ) {
1243 bv.bv_val = colon + 3;
1244 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1245 ber_dupbv( &bv2, &bv );
1246 ber_bvarray_add( &mod->sml_values, &bv2 );
1252 static Modifications *
1253 syncrepl_changelog_mods(
1258 return NULL; /* FIXME */
1262 syncrepl_message_to_op(
1268 BerElement *ber = NULL;
1269 Modifications *modlist = NULL;
1271 SlapReply rs = { REP_RESULT };
1272 slap_callback cb = { NULL, null_callback, NULL, NULL };
1275 char txtbuf[SLAP_TEXT_BUFLEN];
1276 size_t textlen = sizeof txtbuf;
1278 struct berval bdn, dn = BER_BVNULL, ndn;
1279 struct berval bv, *bvals = NULL;
1280 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1281 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1282 psup = BER_BVNULL, nsup = BER_BVNULL;
1283 int rc, deleteOldRdn = 0;
1285 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1286 Debug( LDAP_DEBUG_ANY,
1287 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1291 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1296 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1298 if ( rc != LDAP_SUCCESS ) {
1299 Debug( LDAP_DEBUG_ANY,
1300 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1304 op->o_tag = LBER_DEFAULT;
1306 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1308 if ( bv.bv_val == NULL )
1311 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1313 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1314 ber_dupbv( &op->o_req_dn, &dn );
1315 ber_dupbv( &op->o_req_ndn, &ndn );
1316 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1317 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1318 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1319 int i = verb_to_mask( bvals[0].bv_val, modops );
1321 Debug( LDAP_DEBUG_ANY,
1322 "syncrepl_message_to_op : unknown op %s",
1323 bvals[0].bv_val, 0, 0 );
1328 op->o_tag = modops[i].mask;
1329 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1330 /* Parse attribute into modlist */
1331 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1332 modlist = syncrepl_accesslog_mods( si, bvals );
1334 modlist = syncrepl_changelog_mods( si, bvals );
1335 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1337 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1338 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1340 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1342 } else if ( !ber_bvstrcasecmp( &bv,
1343 &slap_schema.si_ad_entryCSN->ad_cname )) {
1344 slap_queue_csn( op, bvals );
1349 /* If we didn't get a mod type or a target DN, bail out */
1350 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1355 op->o_callback = &cb;
1357 switch( op->o_tag ) {
1359 case LDAP_REQ_MODIFY:
1360 /* If we didn't get required data, bail */
1361 if ( !modlist ) goto done;
1363 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1365 if ( rc != LDAP_SUCCESS ) {
1366 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1371 if ( op->o_tag == LDAP_REQ_ADD ) {
1372 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1373 op->ora_e->e_name = op->o_req_dn;
1374 op->ora_e->e_nname = op->o_req_ndn;
1375 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1376 if( rc != LDAP_SUCCESS ) {
1377 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1380 rc = op->o_bd->be_add( op, &rs );
1381 Debug( LDAP_DEBUG_SYNC,
1382 "syncrepl_message_to_op: be_add %s (%d)\n",
1383 op->o_req_dn.bv_val, rc, 0 );
1385 be_entry_release_w( op, op->ora_e );
1387 op->orm_modlist = modlist;
1388 rc = op->o_bd->be_modify( op, &rs );
1389 Debug( LDAP_DEBUG_SYNC,
1390 "syncrepl_message_to_op: be_modify %s (%d)\n",
1391 op->o_req_dn.bv_val, rc, 0 );
1394 case LDAP_REQ_MODRDN:
1395 if ( BER_BVISNULL( &rdn )) goto done;
1397 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1399 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1401 if ( !BER_BVISNULL( &sup )) {
1402 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1404 op->orr_newSup = &psup;
1405 op->orr_nnewSup = ⊅
1407 op->orr_newrdn = prdn;
1408 op->orr_nnewrdn = nrdn;
1409 op->orr_deleteoldrdn = deleteOldRdn;
1410 op->orr_modlist = NULL;
1411 if ( slap_modrdn2mods( op, &rs ))
1413 rc = op->o_bd->be_modrdn( op, &rs );
1414 slap_mods_free( op->orr_modlist, 1 );
1415 Debug( LDAP_DEBUG_SYNC,
1416 "syncrepl_message_to_op: be_modrdn %s (%d)\n",
1417 op->o_req_dn.bv_val, rc, 0 );
1419 case LDAP_REQ_DELETE:
1420 rc = op->o_bd->be_delete( op, &rs );
1421 Debug( LDAP_DEBUG_SYNC,
1422 "syncrepl_message_to_op: be_delete %s (%d)\n",
1423 op->o_req_dn.bv_val, rc, 0 );
1427 slap_graduate_commit_csn( op );
1429 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1430 if ( !BER_BVISNULL( &rdn )) {
1431 if ( !BER_BVISNULL( &nsup ))
1432 ch_free( nsup.bv_val );
1433 if ( !BER_BVISNULL( &psup ))
1434 ch_free( psup.bv_val );
1435 if ( !BER_BVISNULL( &nrdn ))
1436 ch_free( nrdn.bv_val );
1437 if ( !BER_BVISNULL( &prdn ))
1438 ch_free( prdn.bv_val );
1440 ber_free ( ber, 0 );
1445 syncrepl_message_to_entry(
1449 Modifications **modlist,
1455 BerElement *ber = NULL;
1458 Modifications **modtail = modlist;
1461 char txtbuf[SLAP_TEXT_BUFLEN];
1462 size_t textlen = sizeof txtbuf;
1464 struct berval bdn = {0, NULL}, dn, ndn;
1469 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1470 Debug( LDAP_DEBUG_ANY,
1471 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1475 op->o_tag = LDAP_REQ_ADD;
1477 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1479 if ( rc != LDAP_SUCCESS ) {
1480 Debug( LDAP_DEBUG_ANY,
1481 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1485 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1486 ber_dupbv( &op->o_req_dn, &dn );
1487 ber_dupbv( &op->o_req_ndn, &ndn );
1488 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1489 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1491 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1494 return LDAP_SUCCESS;
1497 if ( entry == NULL ) {
1501 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1502 e->e_name = op->o_req_dn;
1503 e->e_nname = op->o_req_ndn;
1505 while ( ber_remaining( ber ) ) {
1506 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1507 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1512 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1514 mod->sml_op = LDAP_MOD_REPLACE;
1516 mod->sml_next = NULL;
1517 mod->sml_desc = NULL;
1518 mod->sml_type = tmp.sml_type;
1519 mod->sml_values = tmp.sml_values;
1520 mod->sml_nvalues = NULL;
1523 modtail = &mod->sml_next;
1526 if ( *modlist == NULL ) {
1527 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1533 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1535 if ( rc != LDAP_SUCCESS ) {
1536 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1541 /* Strip out dynamically generated attrs */
1542 for ( modtail = modlist; *modtail ; ) {
1544 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1545 *modtail = mod->sml_next;
1546 slap_mod_free( &mod->sml_mod, 0 );
1549 modtail = &mod->sml_next;
1553 /* Strip out attrs in exattrs list */
1554 for ( modtail = modlist; *modtail ; ) {
1556 if ( ldap_charray_inlist( si->si_exattrs,
1557 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1558 *modtail = mod->sml_next;
1559 slap_mod_free( &mod->sml_mod, 0 );
1562 modtail = &mod->sml_next;
1566 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1567 if( rc != LDAP_SUCCESS ) {
1568 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1573 ber_free ( ber, 0 );
1574 if ( rc != LDAP_SUCCESS ) {
1585 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1587 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1588 * entry if a previous refresh was interrupted before sending us a new
1589 * context state. We try to compare the new entry to the existing entry
1590 * and ignore the new entry if they are the same.
1592 * Also, we may get an update where the entryDN has changed, due to
1593 * a ModDn on the provider. We detect this as well, so we can issue
1594 * the corresponding operation locally.
1596 * In the case of a modify, we get a list of all the attributes
1597 * in the original entry. Rather than deleting the entry and re-adding it,
1598 * we issue a Modify request that deletes all the attributes and adds all
1599 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1602 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1603 * a ModDN we will issue both operations on the local database.
1605 typedef struct dninfo {
1609 int renamed; /* Was an existing entry renamed? */
1610 int wasChanged; /* are the attributes changed? */
1611 int attrs; /* how many attribute types are in the ads list */
1612 AttributeDescription **ads;
1620 Modifications** modlist,
1622 struct berval* syncUUID,
1623 struct sync_cookie* syncCookie_req,
1624 struct berval* syncCSN )
1626 Backend *be = op->o_bd;
1627 slap_callback cb = { NULL, NULL, NULL, NULL };
1628 struct berval *syncuuid_bv = NULL;
1629 struct berval syncUUID_strrep = BER_BVNULL;
1630 struct berval uuid_bv = BER_BVNULL;
1632 SlapReply rs_search = {REP_RESULT};
1633 SlapReply rs_delete = {REP_RESULT};
1634 SlapReply rs_add = {REP_RESULT};
1635 SlapReply rs_modify = {REP_RESULT};
1637 #ifdef LDAP_COMP_MATCH
1638 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1640 AttributeAssertion ava = { NULL, BER_BVNULL };
1642 int rc = LDAP_SUCCESS;
1644 struct berval pdn = BER_BVNULL;
1648 switch( syncstate ) {
1649 case LDAP_SYNC_PRESENT:
1650 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1652 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1655 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1657 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1659 case LDAP_SYNC_DELETE:
1660 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1662 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1664 case LDAP_SYNC_MODIFY:
1665 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1667 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1670 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1672 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1675 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1676 if ( !si->si_refreshPresent ) {
1677 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1678 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1679 syncuuid_cmp, avl_dup_error );
1683 if ( syncstate == LDAP_SYNC_PRESENT ) {
1685 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1686 if ( entry == NULL ) {
1691 f.f_choice = LDAP_FILTER_EQUALITY;
1693 ava.aa_desc = slap_schema.si_ad_entryUUID;
1694 ava.aa_value = *syncUUID;
1696 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1697 if ( syncuuid_bv ) {
1698 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: inserted UUID %s\n",
1699 syncUUID_strrep.bv_val, 0, 0 );
1701 op->ors_filter = &f;
1703 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1704 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1705 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1706 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1707 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1708 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1709 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1710 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1712 op->o_tag = LDAP_REQ_SEARCH;
1713 op->ors_scope = LDAP_SCOPE_SUBTREE;
1714 op->ors_deref = LDAP_DEREF_NEVER;
1716 /* get the entry for this UUID */
1717 op->o_req_dn = si->si_base;
1718 op->o_req_ndn = si->si_base;
1720 op->o_time = slap_get_time();
1721 op->ors_tlimit = SLAP_NO_LIMIT;
1724 op->ors_attrs = slap_anlist_all_attributes;
1725 op->ors_attrsonly = 0;
1727 /* set callback function */
1728 op->o_callback = &cb;
1729 cb.sc_response = dn_callback;
1730 cb.sc_private = &dni;
1731 dni.new_entry = entry;
1733 if ( limits_check( op, &rs_search ) == 0 ) {
1734 rc = be->be_search( op, &rs_search );
1735 Debug( LDAP_DEBUG_SYNC,
1736 "syncrepl_entry: %s (%d)\n",
1737 "be_search", rc, 0 );
1740 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1741 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1744 cb.sc_response = null_callback;
1747 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1748 Debug( LDAP_DEBUG_SYNC,
1749 "syncrepl_entry: %s\n",
1750 entry->e_name.bv_val, 0, 0 );
1752 Debug( LDAP_DEBUG_SYNC,
1753 "syncrepl_entry: %s\n",
1754 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1757 if ( syncstate != LDAP_SYNC_DELETE ) {
1758 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1761 /* add if missing */
1762 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1763 &syncUUID_strrep, syncUUID );
1765 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1766 /* replace only if necessary */
1767 if ( a->a_nvals != a->a_vals ) {
1768 ber_memfree( a->a_nvals[0].bv_val );
1769 ber_dupbv( &a->a_nvals[0], syncUUID );
1771 ber_memfree( a->a_vals[0].bv_val );
1772 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1776 switch ( syncstate ) {
1778 case LDAP_SYNC_MODIFY:
1780 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1782 /* FIXME: op->o_csn is assumed to be
1783 * on the thread's slab; this needs
1784 * to be cleared ASAP.
1785 * What happens if already present?
1787 assert( BER_BVISNULL( &op->o_csn ) );
1788 op->o_csn = a->a_vals[0];
1792 if ( BER_BVISNULL( &dni.dn )) {
1794 op->o_req_dn = entry->e_name;
1795 op->o_req_ndn = entry->e_nname;
1796 op->o_tag = LDAP_REQ_ADD;
1799 rc = be->be_add( op, &rs_add );
1800 Debug( LDAP_DEBUG_SYNC,
1801 "syncrepl_entry: %s (%d)\n",
1803 switch ( rs_add.sr_err ) {
1805 be_entry_release_w( op, entry );
1810 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1811 * only if the suffix entry is not present */
1812 case LDAP_NO_SUCH_OBJECT:
1813 rc = syncrepl_add_glue( op, entry );
1817 /* if an entry was added via syncrepl_add_glue(),
1818 * it likely has no entryUUID, so the previous
1819 * be_search() doesn't find it. In this case,
1820 * give syncrepl a chance to modify it. Also
1821 * allow for entries that were recreated with the
1822 * same DN but a different entryUUID.
1824 case LDAP_ALREADY_EXISTS:
1826 Operation op2 = *op;
1827 SlapReply rs2 = { 0 };
1828 slap_callback cb2 = { 0 };
1830 op2.o_tag = LDAP_REQ_SEARCH;
1831 op2.o_req_dn = entry->e_name;
1832 op2.o_req_ndn = entry->e_nname;
1833 op2.ors_scope = LDAP_SCOPE_BASE;
1834 op2.ors_deref = LDAP_DEREF_NEVER;
1835 op2.ors_attrs = slap_anlist_all_attributes;
1836 op2.ors_attrsonly = 0;
1837 op2.ors_limit = NULL;
1839 op2.ors_tlimit = SLAP_NO_LIMIT;
1841 f.f_choice = LDAP_FILTER_PRESENT;
1842 f.f_desc = slap_schema.si_ad_objectClass;
1843 op2.ors_filter = &f;
1844 op2.ors_filterstr = generic_filterstr;
1846 op2.o_callback = &cb2;
1847 cb2.sc_response = dn_callback;
1848 cb2.sc_private = &dni;
1850 rc = be->be_search( &op2, &rs2 );
1851 if ( rc ) goto done;
1859 Debug( LDAP_DEBUG_ANY,
1860 "syncrepl_entry : be_add failed (%d)\n",
1861 rs_add.sr_err, 0, 0 );
1867 op->o_req_dn = dni.dn;
1868 op->o_req_ndn = dni.ndn;
1869 if ( dni.renamed ) {
1870 struct berval noldp, newp, nnewp;
1872 op->o_tag = LDAP_REQ_MODRDN;
1873 dnRdn( &entry->e_name, &op->orr_newrdn );
1874 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1876 dnParent( &dni.ndn, &noldp );
1877 dnParent( &entry->e_nname, &nnewp );
1878 if ( !dn_match( &noldp, &nnewp )) {
1879 dnParent( &entry->e_name, &newp );
1880 op->orr_newSup = &newp;
1881 op->orr_nnewSup = &nnewp;
1883 op->orr_deleteoldrdn = 0;
1884 op->orr_modlist = NULL;
1885 if (( rc = slap_modrdn2mods( op, &rs_modify ))) {
1888 rc = be->be_modrdn( op, &rs_modify );
1889 slap_mods_free( op->orr_modlist, 1 );
1890 Debug( LDAP_DEBUG_SYNC,
1891 "syncrepl_entry: %s (%d)\n",
1892 "be_modrdn", rc, 0 );
1893 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1894 op->o_req_dn = entry->e_name;
1895 op->o_req_ndn = entry->e_nname;
1900 if ( dni.wasChanged ) {
1901 Modifications *mod, *modhead = NULL;
1902 Modifications *modtail = NULL;
1905 op->o_tag = LDAP_REQ_MODIFY;
1907 assert( *modlist != NULL );
1909 /* Delete all the old attrs */
1910 for ( i = 0; i < dni.attrs; i++ ) {
1911 mod = ch_malloc( sizeof( Modifications ) );
1912 mod->sml_op = LDAP_MOD_DELETE;
1914 mod->sml_desc = dni.ads[i];
1915 mod->sml_type = mod->sml_desc->ad_cname;
1916 mod->sml_values = NULL;
1917 mod->sml_nvalues = NULL;
1918 if ( !modhead ) modhead = mod;
1920 modtail->sml_next = mod;
1925 /* Append passed in list to ours */
1927 modtail->sml_next = *modlist;
1933 /* Find end of this list */
1934 for ( ; mod != NULL; mod = mod->sml_next ) {
1938 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1939 mod->sml_op = LDAP_MOD_REPLACE;
1941 mod->sml_desc = slap_schema.si_ad_entryUUID;
1942 mod->sml_type = mod->sml_desc->ad_cname;
1943 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1944 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1945 ber_dupbv( &uuid_bv, syncUUID );
1946 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1947 modtail->sml_next = mod;
1949 op->o_tag = LDAP_REQ_MODIFY;
1950 op->orm_modlist = *modlist;
1952 rc = be->be_modify( op, &rs_modify );
1953 Debug( LDAP_DEBUG_SYNC,
1954 "syncrepl_entry: %s (%d)\n",
1955 "be_modify", rc, 0 );
1956 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1957 Debug( LDAP_DEBUG_ANY,
1958 "syncrepl_entry : be_modify failed (%d)\n",
1959 rs_modify.sr_err, 0, 0 );
1963 case LDAP_SYNC_DELETE :
1964 if ( !BER_BVISNULL( &dni.dn )) {
1965 op->o_req_dn = dni.dn;
1966 op->o_req_ndn = dni.ndn;
1967 op->o_tag = LDAP_REQ_DELETE;
1968 rc = be->be_delete( op, &rs_delete );
1969 Debug( LDAP_DEBUG_SYNC,
1970 "syncrepl_entry: %s (%d)\n",
1971 "be_delete", rc, 0 );
1973 while ( rs_delete.sr_err == LDAP_SUCCESS
1974 && op->o_delete_glue_parent ) {
1975 op->o_delete_glue_parent = 0;
1976 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1977 slap_callback cb = { NULL };
1978 cb.sc_response = slap_null_cb;
1979 dnParent( &op->o_req_ndn, &pdn );
1981 op->o_req_ndn = pdn;
1982 op->o_callback = &cb;
1983 op->o_bd->be_delete( op, &rs_delete );
1992 Debug( LDAP_DEBUG_ANY,
1993 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1998 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1999 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2000 BER_BVZERO( &syncUUID_strrep );
2003 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2005 if ( !BER_BVISNULL( &dni.ndn ) ) {
2006 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2008 if ( !BER_BVISNULL( &dni.dn ) ) {
2009 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2012 entry_free( entry );
2013 BER_BVZERO( &op->o_csn );
2017 static struct berval gcbva[] = {
2023 #define NP_DELETE_ONE 2
2026 syncrepl_del_nonpresent(
2031 Backend* be = op->o_bd;
2032 slap_callback cb = { NULL };
2033 SlapReply rs_search = {REP_RESULT};
2034 SlapReply rs_delete = {REP_RESULT};
2035 SlapReply rs_modify = {REP_RESULT};
2036 struct nonpresent_entry *np_list, *np_prev;
2038 AttributeName an[2];
2040 struct berval pdn = BER_BVNULL;
2042 op->o_req_dn = si->si_base;
2043 op->o_req_ndn = si->si_base;
2045 cb.sc_response = nonpresent_callback;
2048 op->o_callback = &cb;
2049 op->o_tag = LDAP_REQ_SEARCH;
2050 op->ors_scope = si->si_scope;
2051 op->ors_deref = LDAP_DEREF_NEVER;
2052 op->o_time = slap_get_time();
2053 op->ors_tlimit = SLAP_NO_LIMIT;
2058 #ifdef LDAP_COMP_MATCH
2059 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2061 AttributeAssertion eq = { NULL, BER_BVNULL };
2065 op->ors_attrsonly = 1;
2066 op->ors_attrs = slap_anlist_no_attrs;
2067 op->ors_limit = NULL;
2068 op->ors_filter = &uf;
2071 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2073 uf.f_choice = LDAP_FILTER_EQUALITY;
2074 si->si_refreshDelete |= NP_DELETE_ONE;
2076 for (i=0; uuids[i].bv_val; i++) {
2078 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2080 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2081 uf.f_av_value = uuids[i];
2082 rc = be->be_search( op, &rs_search );
2083 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2085 si->si_refreshDelete ^= NP_DELETE_ONE;
2087 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2088 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2089 an[0].an_desc = slap_schema.si_ad_entryUUID;
2091 op->ors_slimit = SLAP_NO_LIMIT;
2092 op->ors_attrsonly = 0;
2093 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2094 op->ors_filterstr = si->si_filterstr;
2095 op->o_nocaching = 1;
2097 if ( limits_check( op, &rs_search ) == 0 ) {
2098 rc = be->be_search( op, &rs_search );
2100 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2103 op->o_nocaching = 0;
2105 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2107 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2109 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2110 while ( np_list != NULL ) {
2111 LDAP_LIST_REMOVE( np_list, npe_link );
2113 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2114 op->o_tag = LDAP_REQ_DELETE;
2115 op->o_callback = &cb;
2116 cb.sc_response = null_callback;
2118 op->o_req_dn = *np_prev->npe_name;
2119 op->o_req_ndn = *np_prev->npe_nname;
2120 rc = op->o_bd->be_delete( op, &rs_delete );
2121 Debug( LDAP_DEBUG_SYNC,
2122 "syncrepl_del_nonpresent: be_delete %s (%d)\n",
2123 op->o_req_dn.bv_val, rc, 0 );
2125 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2126 Modifications mod1, mod2;
2127 mod1.sml_op = LDAP_MOD_REPLACE;
2129 mod1.sml_desc = slap_schema.si_ad_objectClass;
2130 mod1.sml_type = mod1.sml_desc->ad_cname;
2131 mod1.sml_values = &gcbva[0];
2132 mod1.sml_nvalues = NULL;
2133 mod1.sml_next = &mod2;
2135 mod2.sml_op = LDAP_MOD_REPLACE;
2137 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2138 mod2.sml_type = mod2.sml_desc->ad_cname;
2139 mod2.sml_values = &gcbva[1];
2140 mod2.sml_nvalues = NULL;
2141 mod2.sml_next = NULL;
2143 op->o_tag = LDAP_REQ_MODIFY;
2144 op->orm_modlist = &mod1;
2146 rc = be->be_modify( op, &rs_modify );
2149 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2150 op->o_delete_glue_parent ) {
2151 op->o_delete_glue_parent = 0;
2152 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2153 slap_callback cb = { NULL };
2154 cb.sc_response = slap_null_cb;
2155 dnParent( &op->o_req_ndn, &pdn );
2157 op->o_req_ndn = pdn;
2158 op->o_callback = &cb;
2159 /* give it a root privil ? */
2160 op->o_bd->be_delete( op, &rs_delete );
2166 op->o_delete_glue_parent = 0;
2168 ber_bvfree( np_prev->npe_name );
2169 ber_bvfree( np_prev->npe_nname );
2173 slap_graduate_commit_csn( op );
2175 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2176 BER_BVZERO( &op->o_csn );
2187 Backend *be = op->o_bd;
2188 slap_callback cb = { NULL };
2193 struct berval dn = BER_BVNULL;
2194 struct berval ndn = BER_BVNULL;
2196 SlapReply rs_add = {REP_RESULT};
2197 struct berval ptr, nptr;
2200 op->o_tag = LDAP_REQ_ADD;
2201 op->o_callback = &cb;
2202 cb.sc_response = null_callback;
2203 cb.sc_private = NULL;
2208 /* count RDNs in suffix */
2209 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2210 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2212 ptr.bv_len -= comma - ptr.bv_val;
2222 /* Start with BE suffix */
2224 for ( i = 0; i < suffrdns; i++ ) {
2225 comma = ber_bvrchr( &ptr, ',' );
2226 if ( comma != NULL ) {
2227 ptr.bv_len = comma - ptr.bv_val;
2234 if ( !BER_BVISEMPTY( &ptr ) ) {
2235 dn.bv_len -= ptr.bv_len + 1;
2236 dn.bv_val += ptr.bv_len + 1;
2239 /* the normalizedDNs are always the same length, no counting
2243 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2244 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2245 ndn.bv_len = be->be_nsuffix[0].bv_len;
2247 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2253 while ( ndn.bv_val > e->e_nname.bv_val ) {
2254 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2255 ber_dupbv( &glue->e_name, &dn );
2256 ber_dupbv( &glue->e_nname, &ndn );
2258 a = ch_calloc( 1, sizeof( Attribute ));
2259 a->a_desc = slap_schema.si_ad_objectClass;
2261 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2262 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2263 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2264 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2266 a->a_nvals = a->a_vals;
2268 a->a_next = glue->e_attrs;
2271 a = ch_calloc( 1, sizeof( Attribute ));
2272 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2274 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2275 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2276 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2278 a->a_nvals = a->a_vals;
2280 a->a_next = glue->e_attrs;
2283 op->o_req_dn = glue->e_name;
2284 op->o_req_ndn = glue->e_nname;
2286 rc = be->be_add ( op, &rs_add );
2287 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2288 be_entry_release_w( op, glue );
2290 /* incl. ALREADY EXIST */
2292 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2298 /* Move to next child */
2299 comma = ber_bvrchr( &ptr, ',' );
2300 if ( comma == NULL ) {
2303 ptr.bv_len = comma - ptr.bv_val;
2305 dn.bv_val = ++comma;
2306 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2308 comma = ber_bvrchr( &nptr, ',' );
2309 assert( comma != NULL );
2310 nptr.bv_len = comma - nptr.bv_val;
2312 ndn.bv_val = ++comma;
2313 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2316 op->o_req_dn = e->e_name;
2317 op->o_req_ndn = e->e_nname;
2319 rc = be->be_add ( op, &rs_add );
2320 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2321 be_entry_release_w( op, e );
2330 syncrepl_updateCookie(
2334 struct sync_cookie *syncCookie )
2336 Backend *be = op->o_bd;
2337 Modifications mod = { { 0 } };
2338 struct berval vals[ 2 ];
2342 slap_callback cb = { NULL };
2343 SlapReply rs_modify = {REP_RESULT};
2345 mod.sml_op = LDAP_MOD_REPLACE;
2346 mod.sml_desc = slap_schema.si_ad_contextCSN;
2347 mod.sml_type = mod.sml_desc->ad_cname;
2348 mod.sml_values = vals;
2349 vals[0] = syncCookie->ctxcsn;
2350 BER_BVZERO( &vals[1] );
2352 slap_queue_csn( op, &syncCookie->ctxcsn );
2354 op->o_tag = LDAP_REQ_MODIFY;
2356 assert( si->si_rid < 1000 );
2358 cb.sc_response = null_callback;
2361 op->o_callback = &cb;
2362 op->o_req_dn = op->o_bd->be_suffix[0];
2363 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2365 /* update contextCSN */
2366 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2367 op->orm_modlist = &mod;
2368 rc = be->be_modify( op, &rs_modify );
2371 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2372 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2373 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2375 Debug( LDAP_DEBUG_ANY,
2376 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2379 slap_graduate_commit_csn( op );
2381 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2382 BER_BVZERO( &op->o_csn );
2392 dninfo *dni = op->o_callback->sc_private;
2394 if ( rs->sr_type == REP_SEARCH ) {
2395 if ( !BER_BVISNULL( &dni->dn ) ) {
2396 Debug( LDAP_DEBUG_ANY,
2397 "dn_callback : consistency error - "
2398 "entryUUID is not unique\n", 0, 0, 0 );
2400 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2401 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2402 /* If there is a new entry, see if it differs from the old.
2403 * We compare the non-normalized values so that cosmetic changes
2404 * in the provider are always propagated.
2406 if ( dni->new_entry ) {
2407 Attribute *old, *new;
2410 /* Did the DN change? Note that we don't explicitly try to
2411 * discover if the deleteOldRdn argument applies here. It
2412 * would save an unnecessary Modify if we detected it, but
2413 * that's a fair amount of trouble to compare the two attr
2414 * lists in detail. (Just test normalized DN; we ignore
2415 * insignificant changes here.)
2417 if ( !dn_match( &rs->sr_entry->e_nname,
2418 &dni->new_entry->e_nname ) )
2423 for ( i = 0, old = rs->sr_entry->e_attrs;
2425 i++, old = old->a_next )
2430 /* We assume that attributes are saved in the same order
2431 * in the remote and local databases. So if we walk through
2432 * the attributeDescriptions one by one they should match in
2433 * lock step. If not, we signal a change. Otherwise we test
2436 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2438 old = old->a_next, new = new->a_next )
2440 if ( old->a_desc != new->a_desc ) {
2441 dni->wasChanged = 1;
2444 for ( i = 0; ; i++ ) {
2446 nold = BER_BVISNULL( &old->a_vals[i] );
2447 nnew = BER_BVISNULL( &new->a_vals[i] );
2448 /* If both are empty, stop looking */
2449 if ( nold && nnew ) {
2452 /* If they are different, stop looking */
2453 if ( nold != nnew ) {
2454 dni->wasChanged = 1;
2457 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2458 dni->wasChanged = 1;
2462 if ( dni->wasChanged ) break;
2464 if ( dni->wasChanged ) {
2465 dni->ads = op->o_tmpalloc( dni->attrs *
2466 sizeof(AttributeDescription *), op->o_tmpmemctx );
2468 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2469 dni->ads[i] = old->a_desc;
2475 } else if ( rs->sr_type == REP_RESULT ) {
2476 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2477 Debug( LDAP_DEBUG_ANY,
2478 "dn_callback : consistency error - "
2479 "entryUUID is not unique\n", 0, 0, 0 );
2483 return LDAP_SUCCESS;
2487 nonpresent_callback(
2491 syncinfo_t *si = op->o_callback->sc_private;
2494 struct berval* present_uuid = NULL;
2495 struct nonpresent_entry *np_entry;
2497 if ( rs->sr_type == REP_RESULT ) {
2498 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2499 si->si_presentlist = NULL;
2501 } else if ( rs->sr_type == REP_SEARCH ) {
2502 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2503 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2506 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2509 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: UUID %s, dn %s, %sfound\n",
2510 a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val, present_uuid ? "" : "not " );
2512 if ( a == NULL ) return 0;
2515 if ( present_uuid == NULL ) {
2516 np_entry = (struct nonpresent_entry *)
2517 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2518 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2519 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2520 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2523 avl_delete( &si->si_presentlist,
2524 &a->a_nvals[0], syncuuid_cmp );
2525 ch_free( present_uuid->bv_val );
2526 ch_free( present_uuid );
2529 return LDAP_SUCCESS;
2537 if ( rs->sr_err != LDAP_SUCCESS &&
2538 rs->sr_err != LDAP_REFERRAL &&
2539 rs->sr_err != LDAP_ALREADY_EXISTS &&
2540 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2541 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2543 Debug( LDAP_DEBUG_ANY,
2544 "null_callback : error code 0x%x\n",
2547 return LDAP_SUCCESS;
2550 static struct berval *
2551 slap_uuidstr_from_normalized(
2552 struct berval* uuidstr,
2553 struct berval* normalized,
2557 unsigned char nibble;
2560 if ( normalized == NULL ) return NULL;
2561 if ( normalized->bv_len != 16 ) return NULL;
2566 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2567 if ( new == NULL ) {
2574 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2575 if ( new != uuidstr ) {
2576 slap_sl_free( new, ctx );
2581 for ( i = 0; i < 16; i++ ) {
2582 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2583 new->bv_val[(i<<1)+d] = '-';
2587 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2588 if ( nibble < 10 ) {
2589 new->bv_val[(i<<1)+d] = nibble + '0';
2591 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2594 nibble = (normalized->bv_val[i]) & 0xF;
2595 if ( nibble < 10 ) {
2596 new->bv_val[(i<<1)+d+1] = nibble + '0';
2598 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2602 new->bv_val[new->bv_len] = '\0';
2607 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2609 const struct berval *uuid1 = v_uuid1;
2610 const struct berval *uuid2 = v_uuid2;
2611 int rc = uuid1->bv_len - uuid2->bv_len;
2612 if ( rc ) return rc;
2613 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2617 avl_ber_bvfree( void *v_bv )
2619 struct berval *bv = (struct berval *)v_bv;
2621 if( v_bv == NULL ) return;
2622 if ( !BER_BVISNULL( bv ) ) {
2623 ch_free( bv->bv_val );
2625 ch_free( (char *) bv );
2629 syncinfo_free( syncinfo_t *sie )
2632 if ( sie->si_conn_setup ) {
2634 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2635 connection_client_stop( s );
2636 sie->si_conn_setup = 0;
2638 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2641 /* re-fetch it, in case it was already removed */
2642 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2644 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2645 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2646 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2649 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2651 bindconf_free( &sie->si_bindconf );
2653 if ( sie->si_filterstr.bv_val ) {
2654 ch_free( sie->si_filterstr.bv_val );
2656 if ( sie->si_base.bv_val ) {
2657 ch_free( sie->si_base.bv_val );
2659 if ( sie->si_attrs ) {
2661 while ( sie->si_attrs[i] != NULL ) {
2662 ch_free( sie->si_attrs[i] );
2665 ch_free( sie->si_attrs );
2667 if ( sie->si_exattrs ) {
2669 while ( sie->si_exattrs[i] != NULL ) {
2670 ch_free( sie->si_exattrs[i] );
2673 ch_free( sie->si_exattrs );
2675 if ( sie->si_anlist ) {
2677 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2678 ch_free( sie->si_anlist[i].an_name.bv_val );
2681 ch_free( sie->si_anlist );
2683 if ( sie->si_exanlist ) {
2685 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2686 ch_free( sie->si_exanlist[i].an_name.bv_val );
2689 ch_free( sie->si_exanlist );
2691 if ( sie->si_retryinterval ) {
2692 ch_free( sie->si_retryinterval );
2694 if ( sie->si_retrynum ) {
2695 ch_free( sie->si_retrynum );
2697 if ( sie->si_retrynum_init ) {
2698 ch_free( sie->si_retrynum_init );
2700 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2701 if ( sie->si_presentlist ) {
2702 avl_free( sie->si_presentlist, avl_ber_bvfree );
2704 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2705 struct nonpresent_entry* npe;
2706 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2707 LDAP_LIST_REMOVE( npe, npe_link );
2708 if ( npe->npe_name ) {
2709 if ( npe->npe_name->bv_val ) {
2710 ch_free( npe->npe_name->bv_val );
2712 ch_free( npe->npe_name );
2714 if ( npe->npe_nname ) {
2715 if ( npe->npe_nname->bv_val ) {
2716 ch_free( npe->npe_nname->bv_val );
2718 ch_free( npe->npe_nname );
2727 /* NOTE: used & documented in slapd.conf(5) */
2729 #define PROVIDERSTR "provider"
2730 #define SCHEMASTR "schemachecking"
2731 #define FILTERSTR "filter"
2732 #define SEARCHBASESTR "searchbase"
2733 #define SCOPESTR "scope"
2734 #define ATTRSONLYSTR "attrsonly"
2735 #define ATTRSSTR "attrs"
2736 #define TYPESTR "type"
2737 #define INTERVALSTR "interval"
2738 #define RETRYSTR "retry"
2739 #define SLIMITSTR "sizelimit"
2740 #define TLIMITSTR "timelimit"
2741 #define SYNCDATASTR "syncdata"
2743 /* FIXME: undocumented */
2744 #define LOGBASESTR "logbase"
2745 #define LOGFILTERSTR "logfilter"
2746 #define OLDAUTHCSTR "bindprincipal"
2747 #define EXATTRSSTR "exattrs"
2748 #define MANAGEDSAITSTR "manageDSAit"
2751 #define LASTMODSTR "lastmod"
2752 #define LMGENSTR "gen"
2753 #define LMNOSTR "no"
2754 #define LMREQSTR "req"
2755 #define SRVTABSTR "srvtab"
2756 #define SUFFIXSTR "suffix"
2759 #define GOT_ID 0x0001
2760 #define GOT_PROVIDER 0x0002
2763 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2769 { BER_BVC("base"), LDAP_SCOPE_BASE },
2770 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2771 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2772 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2773 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
2774 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2775 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2776 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2780 static slap_verbmasks datamodes[] = {
2781 { BER_BVC("default"), SYNCDATA_DEFAULT },
2782 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2783 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2788 parse_syncrepl_line(
2796 for ( i = 1; i < c->argc; i++ ) {
2797 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2798 STRLENOF( IDSTR "=" ) ) )
2801 /* '\0' string terminator accounts for '=' */
2802 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2803 if ( lutil_atoi( &tmp, val ) != 0 ) {
2804 snprintf( c->msg, sizeof( c->msg ),
2805 "Error: parse_syncrepl_line: "
2806 "unable to parse syncrepl id \"%s\"", val );
2807 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2810 if ( tmp >= 1000 || tmp < 0 ) {
2811 snprintf( c->msg, sizeof( c->msg ),
2812 "Error: parse_syncrepl_line: "
2813 "syncrepl id %d is out of range [0..999]", tmp );
2814 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2819 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2820 STRLENOF( PROVIDERSTR "=" ) ) )
2822 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2823 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2824 gots |= GOT_PROVIDER;
2825 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2826 STRLENOF( SCHEMASTR "=" ) ) )
2828 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2829 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2830 si->si_schemachecking = 1;
2831 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2832 si->si_schemachecking = 0;
2834 si->si_schemachecking = 1;
2836 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2837 STRLENOF( FILTERSTR "=" ) ) )
2839 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2840 if ( si->si_filterstr.bv_val )
2841 ch_free( si->si_filterstr.bv_val );
2842 ber_str2bv( val, 0, 1, &si->si_filterstr );
2843 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2844 STRLENOF( LOGFILTERSTR "=" ) ) )
2846 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2847 if ( si->si_logfilterstr.bv_val )
2848 ch_free( si->si_logfilterstr.bv_val );
2849 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2850 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2851 STRLENOF( SEARCHBASESTR "=" ) ) )
2856 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2857 if ( si->si_base.bv_val ) {
2858 ch_free( si->si_base.bv_val );
2860 ber_str2bv( val, 0, 0, &bv );
2861 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2862 if ( rc != LDAP_SUCCESS ) {
2863 snprintf( c->msg, sizeof( c->msg ),
2864 "Invalid base DN \"%s\": %d (%s)",
2865 val, rc, ldap_err2string( rc ) );
2866 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2869 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2870 STRLENOF( LOGBASESTR "=" ) ) )
2875 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2876 if ( si->si_logbase.bv_val ) {
2877 ch_free( si->si_logbase.bv_val );
2879 ber_str2bv( val, 0, 0, &bv );
2880 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2881 if ( rc != LDAP_SUCCESS ) {
2882 snprintf( c->msg, sizeof( c->msg ),
2883 "Invalid logbase DN \"%s\": %d (%s)",
2884 val, rc, ldap_err2string( rc ) );
2885 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2888 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2889 STRLENOF( SCOPESTR "=" ) ) )
2892 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2893 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2894 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2895 si->si_scope = scopes[j].val;
2899 if ( BER_BVISNULL(&scopes[j].key) ) {
2900 snprintf( c->msg, sizeof( c->msg ),
2901 "Error: parse_syncrepl_line: "
2902 "unknown scope \"%s\"", val);
2903 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2906 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2907 STRLENOF( ATTRSONLYSTR ) ) )
2909 si->si_attrsonly = 1;
2910 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2911 STRLENOF( ATTRSSTR "=" ) ) )
2913 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2914 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2916 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2917 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2918 if ( si->si_anlist == NULL ) {
2919 ch_free( attr_fname );
2922 si->si_anfile = attr_fname;
2924 char *str, *s, *next;
2925 char delimstr[] = " ,\t";
2926 str = ch_strdup( val );
2927 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2929 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2931 if ( strlen(s) == 1 && *s == '*' ) {
2932 si->si_allattrs = 1;
2933 *(val + ( s - str )) = delimstr[0];
2935 if ( strlen(s) == 1 && *s == '+' ) {
2936 si->si_allopattrs = 1;
2937 *(val + ( s - str )) = delimstr[0];
2941 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2942 if ( si->si_anlist == NULL ) {
2946 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2947 STRLENOF( EXATTRSSTR "=" ) ) )
2949 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2950 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2952 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2953 si->si_exanlist = file2anlist(
2954 si->si_exanlist, attr_fname, " ,\t" );
2955 if ( si->si_exanlist == NULL ) {
2956 ch_free( attr_fname );
2959 ch_free( attr_fname );
2961 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2962 if ( si->si_exanlist == NULL ) {
2966 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2967 STRLENOF( TYPESTR "=" ) ) )
2969 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2970 if ( !strncasecmp( val, "refreshOnly",
2971 STRLENOF("refreshOnly") ) )
2973 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2974 } else if ( !strncasecmp( val, "refreshAndPersist",
2975 STRLENOF("refreshAndPersist") ) )
2977 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2978 si->si_interval = 60;
2980 snprintf( c->msg, sizeof( c->msg ),
2981 "Error: parse_syncrepl_line: "
2982 "unknown sync type \"%s\"", val);
2983 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2986 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2987 STRLENOF( INTERVALSTR "=" ) ) )
2989 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2990 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2991 si->si_interval = 0;
2992 } else if ( strchr( val, ':' ) != NULL ) {
2993 char *next, *ptr = val;
2994 unsigned dd, hh, mm, ss;
2996 /* NOTE: the test for ptr[ 0 ] == '-'
2997 * should go before the call to strtoul() */
2998 dd = strtoul( ptr, &next, 10 );
2999 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3000 snprintf( c->msg, sizeof( c->msg ),
3001 "Error: parse_syncrepl_line: "
3002 "invalid interval \"%s\", unable to parse days", val );
3003 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3007 hh = strtoul( ptr, &next, 10 );
3008 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3009 snprintf( c->msg, sizeof( c->msg ),
3010 "Error: parse_syncrepl_line: "
3011 "invalid interval \"%s\", unable to parse hours", val );
3012 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3016 mm = strtoul( ptr, &next, 10 );
3017 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3018 snprintf( c->msg, sizeof( c->msg ),
3019 "Error: parse_syncrepl_line: "
3020 "invalid interval \"%s\", unable to parse minutes", val );
3021 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3025 ss = strtoul( ptr, &next, 10 );
3026 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3027 snprintf( c->msg, sizeof( c->msg ),
3028 "Error: parse_syncrepl_line: "
3029 "invalid interval \"%s\", unable to parse seconds", val );
3030 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3033 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3037 if ( lutil_parse_time( val, &t ) != 0 ) {
3038 snprintf( c->msg, sizeof( c->msg ),
3039 "Error: parse_syncrepl_line: "
3040 "invalid interval \"%s\"", val );
3041 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3044 si->si_interval = (time_t)t;
3046 if ( si->si_interval < 0 ) {
3047 snprintf( c->msg, sizeof( c->msg ),
3048 "Error: parse_syncrepl_line: "
3049 "invalid interval \"%ld\"",
3050 (long) si->si_interval);
3051 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3054 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3055 STRLENOF( RETRYSTR "=" ) ) )
3060 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3061 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3062 retry_list[0] = NULL;
3064 slap_str2clist( &retry_list, val, " ,\t" );
3066 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3069 snprintf( c->msg, sizeof( c->msg ),
3070 "Error: incomplete syncrepl retry list" );
3071 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3072 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3073 ch_free( retry_list[k] );
3075 ch_free( retry_list );
3078 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3079 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3080 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3081 for ( j = 0; j < n; j++ ) {
3083 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3084 snprintf( c->msg, sizeof( c->msg ),
3085 "Error: invalid retry interval \"%s\" (#%d)",
3086 retry_list[j*2], j );
3087 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3088 /* do some cleanup */
3091 si->si_retryinterval[j] = (time_t)t;
3092 if ( *retry_list[j*2+1] == '+' ) {
3093 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3094 si->si_retrynum[j] = RETRYNUM_FOREVER;
3098 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3099 || si->si_retrynum_init[j] <= 0 )
3101 snprintf( c->msg, sizeof( c->msg ),
3102 "Error: invalid initial retry number \"%s\" (#%d)",
3103 retry_list[j*2+1], j );
3104 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3105 /* do some cleanup */
3108 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3109 || si->si_retrynum[j] <= 0 )
3111 snprintf( c->msg, sizeof( c->msg ),
3112 "Error: invalid retry number \"%s\" (#%d)",
3113 retry_list[j*2+1], j );
3114 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3115 /* do some cleanup */
3120 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3121 si->si_retrynum[j] = RETRYNUM_TAIL;
3122 si->si_retryinterval[j] = 0;
3124 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3125 ch_free( retry_list[k] );
3127 ch_free( retry_list );
3128 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3129 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3131 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3132 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3133 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3135 snprintf( c->msg, sizeof( c->msg ),
3136 "invalid manageDSAit value \"%s\".\n",
3138 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3141 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3142 STRLENOF( SLIMITSTR "=") ) )
3144 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3145 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3148 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3149 snprintf( c->msg, sizeof( c->msg ),
3150 "invalid size limit value \"%s\".\n",
3152 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3155 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3156 STRLENOF( TLIMITSTR "=" ) ) )
3158 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3159 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3162 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3163 snprintf( c->msg, sizeof( c->msg ),
3164 "invalid time limit value \"%s\".\n",
3166 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3169 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3170 STRLENOF( SYNCDATASTR "=" ) ) )
3172 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3173 si->si_syncdata = verb_to_mask( val, datamodes );
3174 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3175 snprintf( c->msg, sizeof( c->msg ),
3176 "Error: parse_syncrepl_line: "
3177 "unknown keyword \"%s\"\n", c->argv[ i ] );
3178 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3183 if ( gots != GOT_ALL ) {
3184 snprintf( c->msg, sizeof( c->msg ),
3185 "Error: Malformed \"syncrepl\" line in slapd config file" );
3186 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3200 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3201 Debug( LDAP_DEBUG_ANY, "%s: database %s does not support operations "
3202 "required for syncrepl\n", c->log, c->be->be_type, 0 );
3205 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3208 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3212 si->si_bindconf.sb_tls = SB_TLS_OFF;
3213 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3214 si->si_schemachecking = 0;
3215 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3216 &si->si_filterstr );
3217 si->si_base.bv_val = NULL;
3218 si->si_scope = LDAP_SCOPE_SUBTREE;
3219 si->si_attrsonly = 0;
3220 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3221 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3222 si->si_attrs = NULL;
3223 si->si_allattrs = 0;
3224 si->si_allopattrs = 0;
3225 si->si_exattrs = NULL;
3226 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3227 si->si_interval = 86400;
3228 si->si_retryinterval = NULL;
3229 si->si_retrynum_init = NULL;
3230 si->si_retrynum = NULL;
3231 si->si_manageDSAit = 0;
3234 si->si_conn_setup = 0;
3237 si->si_check_tls = 1;
3240 si->si_presentlist = NULL;
3241 LDAP_LIST_INIT( &si->si_nonpresentlist );
3242 ldap_pvt_thread_mutex_init( &si->si_mutex );
3244 rc = parse_syncrepl_line( c, si );
3248 init_syncrepl( si );
3249 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3250 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3255 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3256 syncinfo_free( si );
3259 Debug( LDAP_DEBUG_CONFIG,
3260 "Config: ** successfully added syncrepl \"%s\"\n",
3261 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3262 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3263 if ( !si->si_schemachecking ) {
3264 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3266 c->be->be_syncinfo = si;
3272 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3274 struct berval bc, uri;
3275 char buf[BUFSIZ*2], *ptr;
3278 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3282 /* temporarily inhibit bindconf from printing URI */
3283 uri = si->si_bindconf.sb_uri;
3284 BER_BVZERO( &si->si_bindconf.sb_uri );
3285 bindconf_unparse( &si->si_bindconf, &bc );
3286 si->si_bindconf.sb_uri = uri;
3289 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3290 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3291 if ( ptr - buf >= sizeof( buf ) ) return;
3292 if ( !BER_BVISNULL( &bc )) {
3293 if ( WHATSLEFT <= bc.bv_len ) {
3297 ptr = lutil_strcopy( ptr, bc.bv_val );
3300 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3301 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3302 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3303 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3306 if ( !BER_BVISNULL( &si->si_base )) {
3307 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3308 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3309 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3312 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3313 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3314 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3315 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3318 if ( !BER_BVISNULL( &si->si_logbase )) {
3319 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3320 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3321 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3324 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3325 if ( si->si_scope == scopes[i].val ) {
3326 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3327 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3328 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3332 if ( si->si_attrsonly ) {
3333 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3334 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3336 if ( si->si_anfile ) {
3337 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3338 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3339 ptr = lutil_strcopy( ptr, si->si_anfile );
3341 } else if ( si->si_allattrs || si->si_allopattrs ||
3342 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3346 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3347 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3349 /* FIXME: add check for overflow */
3350 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3351 if ( si->si_allattrs ) {
3352 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3353 if ( old != ptr ) *ptr++ = ',';
3356 if ( si->si_allopattrs ) {
3357 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3358 if ( old != ptr ) *ptr++ = ',';
3363 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3364 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3365 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3366 /* FIXME: add check for overflow */
3367 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3369 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3370 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3371 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3373 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3374 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3375 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3376 "refreshAndPersist" : "refreshOnly" );
3378 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3381 dd = si->si_interval;
3388 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3389 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3390 if ( ptr - buf >= sizeof( buf ) ) return;
3391 } else if ( si->si_retryinterval ) {
3393 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3394 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3395 for (i=0; si->si_retryinterval[i]; i++) {
3396 if ( space ) *ptr++ = ' ';
3398 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3399 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3402 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3404 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3408 if ( si->si_slimit ) {
3409 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3410 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3411 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3414 if ( si->si_tlimit ) {
3415 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3416 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3417 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3420 if ( si->si_syncdata ) {
3421 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3422 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3423 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3424 ptr = lutil_strcopy( ptr, bc.bv_val );
3427 bc.bv_len = ptr - buf;
3429 ber_dupbv( bv, &bc );
3433 syncrepl_config( ConfigArgs *c )
3435 if (c->op == SLAP_CONFIG_EMIT) {
3436 if ( c->be->be_syncinfo ) {
3438 syncrepl_unparse( c->be->be_syncinfo, &bv );
3439 ber_bvarray_add( &c->rvalue_vals, &bv );
3443 } else if ( c->op == LDAP_MOD_DELETE ) {
3444 if ( c->be->be_syncinfo ) {
3445 syncinfo_free( c->be->be_syncinfo );
3446 c->be->be_syncinfo = NULL;
3448 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3451 if ( SLAP_SHADOW( c->be ) ) {
3452 Debug(LDAP_DEBUG_ANY, "%s: "
3453 "syncrepl: database already shadowed.\n",
3456 } else if ( add_syncrepl( c ) ) {
3459 return config_sync_shadow( c );