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-2008 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;
88 Avlnode *si_presentlist;
90 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
91 ldap_pvt_thread_mutex_t si_mutex;
94 static int syncuuid_cmp( const void *, const void * );
95 static void avl_ber_bvfree( void * );
96 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
97 static int syncrepl_message_to_op(
98 syncinfo_t *, Operation *, LDAPMessage * );
99 static int syncrepl_message_to_entry(
100 syncinfo_t *, Operation *, LDAPMessage *,
101 Modifications **, Entry **, int );
102 static int syncrepl_entry(
103 syncinfo_t *, Operation*, Entry*,
104 Modifications**,int, struct berval*,
105 struct sync_cookie *,
107 static void syncrepl_updateCookie(
108 syncinfo_t *, Operation *, struct berval *,
109 struct sync_cookie * );
110 static struct berval * slap_uuidstr_from_normalized(
111 struct berval *, struct berval *, void * );
113 /* callback functions */
114 static int dn_callback( struct slap_op *, struct slap_rep * );
115 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
116 static int null_callback( struct slap_op *, struct slap_rep * );
118 static AttributeDescription *sync_descs[4];
121 init_syncrepl(syncinfo_t *si)
124 char **attrs, **exattrs;
126 if ( !sync_descs[0] ) {
127 sync_descs[0] = slap_schema.si_ad_objectClass;
128 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
129 sync_descs[2] = slap_schema.si_ad_entryCSN;
130 sync_descs[3] = NULL;
133 if ( si->si_allattrs && si->si_allopattrs )
136 attrs = anlist2attrs( si->si_anlist );
139 if ( si->si_allattrs ) {
142 if ( !is_at_operational( at_find( attrs[i] ))) {
143 for ( j = i; attrs[j] != NULL; j++ ) {
146 attrs[j] = attrs[j+1];
152 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
153 attrs[i] = ch_strdup("*");
156 } else if ( si->si_allopattrs ) {
159 if ( is_at_operational( at_find( attrs[i] ))) {
160 for ( j = i; attrs[j] != NULL; j++ ) {
163 attrs[j] = attrs[j+1];
169 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
170 attrs[i] = ch_strdup("+");
174 for ( i = 0; sync_descs[i] != NULL; i++ ) {
177 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
178 for ( k = j; attrs[k] != NULL; k++ ) {
181 attrs[k] = attrs[k+1];
189 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
191 if ( si->si_allopattrs ) {
192 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
194 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
197 if ( attrs == NULL ) {
198 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
202 if ( si->si_allopattrs ) {
203 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
205 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
206 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
214 if ( si->si_allattrs == si->si_allopattrs ) {
215 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
216 attrs[i++] = ch_strdup( "*" );
217 attrs[i++] = ch_strdup( "+" );
218 } else if ( si->si_allattrs && !si->si_allopattrs ) {
219 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
220 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
221 attrs[i++] = ch_strdup( "*" );
222 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
223 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
225 } else if ( !si->si_allattrs && si->si_allopattrs ) {
226 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
227 attrs[i++] = ch_strdup( "+" );
228 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
233 si->si_attrs = attrs;
235 exattrs = anlist2attrs( si->si_exanlist );
238 for ( n = 0; exattrs[n] != NULL; n++ ) ;
240 for ( i = 0; sync_descs[i] != NULL; i++ ) {
242 while ( exattrs[j] != NULL ) {
243 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
244 ch_free( exattrs[j] );
245 for ( k = j; exattrs[k] != NULL; k++ ) {
246 exattrs[k] = exattrs[k+1];
254 for ( i = 0; exattrs[i] != NULL; i++ ) {
255 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
257 if ( ( oc = si->si_anlist[j].an_oc ) ) {
259 while ( oc->soc_required[k] ) {
260 if ( !strcmp( exattrs[i],
261 oc->soc_required[k]->sat_cname.bv_val )) {
262 ch_free( exattrs[i] );
263 for ( l = i; exattrs[l]; l++ ) {
264 exattrs[l] = exattrs[l+1];
274 for ( i = 0; exattrs[i] != NULL; i++ ) ;
277 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
280 si->si_exattrs = exattrs;
283 typedef struct logschema {
285 struct berval ls_req;
286 struct berval ls_mod;
287 struct berval ls_newRdn;
288 struct berval ls_delRdn;
289 struct berval ls_newSup;
292 static logschema changelog_sc = {
294 BER_BVC("changeType"),
297 BER_BVC("deleteOldRDN"),
298 BER_BVC("newSuperior")
301 static logschema accesslog_sc = {
305 BER_BVC("reqNewRDN"),
306 BER_BVC("reqDeleteOldRDN"),
307 BER_BVC("reqNewSuperior")
315 BerElementBuffer berbuf;
316 BerElement *ber = (BerElement *)&berbuf;
317 LDAPControl c[2], *ctrls[3];
318 struct timeval timeout;
322 char **attrs, *lattrs[8];
327 /* setup LDAP SYNC control */
328 ber_init2( ber, NULL, LBER_USE_DER );
329 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
331 /* If we're using a log but we have no state, then fallback to
332 * normal mode for a full refresh.
334 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
335 si->si_logstate = SYNCLOG_FALLBACK;
337 /* Use the log parameters if we're in log mode */
338 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
340 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
344 lattrs[0] = ls->ls_dn.bv_val;
345 lattrs[1] = ls->ls_req.bv_val;
346 lattrs[2] = ls->ls_mod.bv_val;
347 lattrs[3] = ls->ls_newRdn.bv_val;
348 lattrs[4] = ls->ls_delRdn.bv_val;
349 lattrs[5] = ls->ls_newSup.bv_val;
350 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
354 base = si->si_logbase.bv_val;
355 filter = si->si_logfilterstr.bv_val;
358 scope = LDAP_SCOPE_SUBTREE;
361 base = si->si_base.bv_val;
362 filter = si->si_filterstr.bv_val;
363 attrs = si->si_attrs;
364 attrsonly = si->si_attrsonly;
365 scope = si->si_scope;
367 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
368 si->si_type = LDAP_SYNC_REFRESH_ONLY;
370 si->si_type = si->si_ctype;
373 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
375 ber_printf( ber, "{eOb}",
376 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
378 ber_printf( ber, "{eb}",
379 abs(si->si_type), rhint );
382 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
387 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
388 c[0].ldctl_iscritical = si->si_type < 0;
391 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
392 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
393 c[1].ldctl_value = si->si_bindconf.sb_authzId;
394 c[1].ldctl_iscritical = 1;
401 timeout.tv_sec = si->si_tlimit;
404 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
405 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
406 si->si_slimit, &si->si_msgid );
417 int cmdline_cookie_found = 0;
419 struct sync_cookie *sc = NULL;
425 psub = &si->si_be->be_nsuffix[0];
427 /* Init connection to master */
428 rc = ldap_initialize( &si->si_ld, si->si_bindconf.sb_uri.bv_val );
429 if ( rc != LDAP_SUCCESS ) {
430 Debug( LDAP_DEBUG_ANY,
431 "do_syncrep1: rid %03d ldap_initialize failed (%s)\n",
432 si->si_rid, si->si_bindconf.sb_uri.bv_val, 0 );
436 op->o_protocol = LDAP_VERSION3;
437 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
441 if ( si->si_bindconf.sb_tls ) {
442 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
443 if( rc != LDAP_SUCCESS ) {
444 Debug( LDAP_DEBUG_ANY,
445 "%s: rid %03d ldap_start_tls failed (%d)\n",
446 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
448 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
452 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
453 #ifdef HAVE_CYRUS_SASL
456 if ( si->si_bindconf.sb_secprops != NULL ) {
457 rc = ldap_set_option( si->si_ld,
458 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
460 if( rc != LDAP_OPT_SUCCESS ) {
461 Debug( LDAP_DEBUG_ANY, "Error: rid %03d ldap_set_option "
462 "(%s,SECPROPS,\"%s\") failed!\n",
463 si->si_rid, si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops );
468 defaults = lutil_sasl_defaults( si->si_ld,
469 si->si_bindconf.sb_saslmech.bv_val,
470 si->si_bindconf.sb_realm.bv_val,
471 si->si_bindconf.sb_authcId.bv_val,
472 si->si_bindconf.sb_cred.bv_val,
473 si->si_bindconf.sb_authzId.bv_val );
475 rc = ldap_sasl_interactive_bind_s( si->si_ld,
476 si->si_bindconf.sb_binddn.bv_val,
477 si->si_bindconf.sb_saslmech.bv_val,
483 lutil_sasl_freedefs( defaults );
485 /* FIXME: different error behaviors according to
487 * 2) on err policy : exit, retry, backoff ...
489 if ( rc != LDAP_SUCCESS ) {
490 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
492 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
493 "ldap_sasl_interactive_bind_s failed (%d)\n",
496 /* FIXME (see above comment) */
497 /* if Kerberos credentials cache is not active, retry */
498 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
499 rc == LDAP_LOCAL_ERROR )
501 rc = LDAP_SERVER_DOWN;
506 #else /* HAVE_CYRUS_SASL */
507 /* Should never get here, we trapped this at config time */
509 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
514 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
515 rc = ldap_sasl_bind_s( si->si_ld,
516 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
517 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
518 if ( rc != LDAP_SUCCESS ) {
519 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
520 "ldap_sasl_bind_s failed (%d)\n", si->si_rid, rc, 0 );
525 /* Set SSF to strongest of TLS, SASL SSFs */
528 op->o_transport_ssf = 0;
530 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
531 == LDAP_SUCCESS && ssl != NULL )
533 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
535 #endif /* HAVE_TLS */
536 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
537 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
538 ? op->o_sasl_ssf : op->o_tls_ssf;
541 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
542 /* get contextCSN shadow replica from database */
543 BerVarray csn = NULL;
545 assert( si->si_rid < 1000 );
546 op->o_req_ndn = op->o_bd->be_nsuffix[0];
547 op->o_req_dn = op->o_req_ndn;
549 /* try to read stored contextCSN */
550 backend_attribute( op, NULL, &op->o_req_ndn,
551 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
553 ch_free( si->si_syncCookie.ctxcsn.bv_val );
554 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
555 ber_bvarray_free_x( csn, op->o_tmpmemctx );
558 si->si_syncCookie.rid = si->si_rid;
560 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
561 if ( si->si_rid == sc->rid ) {
562 cmdline_cookie_found = 1;
567 if ( cmdline_cookie_found ) {
568 /* cookie is supplied in the command line */
570 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
572 /* ctxcsn wasn't parsed yet, do it now */
573 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
574 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
575 /* if cmdline cookie does not have ctxcsn */
576 /* component, set it to an initial value */
577 slap_init_sync_cookie_ctxcsn( sc );
579 slap_sync_cookie_free( &si->si_syncCookie, 0 );
580 slap_dup_sync_cookie( &si->si_syncCookie, sc );
581 slap_sync_cookie_free( sc, 1 );
584 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
585 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
588 si->si_refreshDone = 0;
590 rc = ldap_sync_search( si, op->o_tmpmemctx );
592 if( rc != LDAP_SUCCESS ) {
593 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
594 "ldap_search_ext: %s (%d)\n",
595 si->si_rid, ldap_err2string( rc ), rc );
601 ldap_unbind_ext( si->si_ld, NULL, NULL );
614 LDAPControl **rctrls = NULL;
617 BerElementBuffer berbuf;
618 BerElement *ber = (BerElement *)&berbuf;
620 LDAPMessage *res = NULL;
621 LDAPMessage *msg = NULL;
624 struct berval *retdata = NULL;
629 struct berval syncUUID = BER_BVNULL;
630 struct sync_cookie syncCookie = { BER_BVNULL };
631 struct sync_cookie syncCookie_req = { BER_BVNULL };
632 struct berval cookie = BER_BVNULL;
638 Modifications *modlist = NULL;
643 struct timeval *tout_p = NULL;
644 struct timeval tout = { 0, 0 };
646 int refreshDeletes = 0;
647 BerVarray syncUUIDs = NULL;
650 if ( slapd_shutdown ) {
655 ber_init2( ber, NULL, LBER_USE_DER );
656 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
658 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 rid %03d\n", si->si_rid, 0, 0 );
660 psub = &si->si_be->be_nsuffix[0];
662 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
664 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
670 while (( rc = ldap_result( si->si_ld, si->si_msgid, LDAP_MSG_ONE,
671 tout_p, &res )) > 0 )
673 if ( slapd_shutdown ) {
677 for( msg = ldap_first_message( si->si_ld, res );
679 msg = ldap_next_message( si->si_ld, msg ) )
681 if ( slapd_shutdown ) {
685 switch( ldap_msgtype( msg ) ) {
686 case LDAP_RES_SEARCH_ENTRY:
687 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
688 /* we can't work without the control */
690 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d"
691 "got search entry without "
692 "control\n", si->si_rid, 0, 0 );
697 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
698 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
699 /* FIXME: what if syncUUID is NULL or empty?
700 * (happens with back-sql...) */
701 if ( BER_BVISEMPTY( &syncUUID ) ) {
702 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
703 "got empty syncUUID\n", si->si_rid, 0, 0 );
704 ldap_controls_free( rctrls );
708 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
709 ber_scanf( ber, /*"{"*/ "m}", &cookie );
710 if ( !BER_BVISNULL( &cookie ) ) {
711 ch_free( syncCookie.octet_str.bv_val );
712 ber_dupbv( &syncCookie.octet_str, &cookie );
714 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
716 slap_parse_sync_cookie( &syncCookie, NULL );
720 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
722 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
723 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
724 syncrepl_updateCookie( si, op, psub, &syncCookie );
725 } else switch ( rc ) {
726 case LDAP_ALREADY_EXISTS:
727 case LDAP_NO_SUCH_OBJECT:
728 case LDAP_NO_SUCH_ATTRIBUTE:
729 case LDAP_TYPE_OR_VALUE_EXISTS:
730 rc = LDAP_SYNC_REFRESH_REQUIRED;
731 si->si_logstate = SYNCLOG_FALLBACK;
732 ldap_abandon_ext( si->si_ld, si->si_msgid, NULL, NULL );
737 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
738 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
739 if (( rc = syncrepl_entry( si, op, entry, &modlist,
740 syncstate, &syncUUID, &syncCookie_req,
741 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
742 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
743 syncrepl_updateCookie( si, op, psub, &syncCookie );
746 ldap_controls_free( rctrls );
748 slap_mods_free( modlist, 1 );
754 case LDAP_RES_SEARCH_REFERENCE:
755 Debug( LDAP_DEBUG_ANY,
756 "do_syncrep2: rid %03d reference received error\n",
760 case LDAP_RES_SEARCH_RESULT:
761 Debug( LDAP_DEBUG_SYNC,
762 "do_syncrep2: rid %03d LDAP_RES_SEARCH_RESULT\n",
764 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
766 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
767 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
768 /* map old result code to registered code */
769 err = LDAP_SYNC_REFRESH_REQUIRED;
772 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
773 if ( si->si_logstate == SYNCLOG_LOGGING ) {
774 si->si_logstate = SYNCLOG_FALLBACK;
781 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
783 ber_scanf( ber, "{" /*"}"*/);
784 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
785 ber_scanf( ber, "m", &cookie );
786 if ( !BER_BVISNULL( &cookie ) ) {
787 ch_free( syncCookie.octet_str.bv_val );
788 ber_dupbv( &syncCookie.octet_str, &cookie);
790 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
792 slap_parse_sync_cookie( &syncCookie, NULL );
795 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
797 ber_scanf( ber, "b", &refreshDeletes );
799 ber_scanf( ber, /*"{"*/ "}" );
801 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
803 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
806 value_match( &match, slap_schema.si_ad_entryCSN,
807 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
808 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
809 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
813 ldap_controls_free( rctrls );
815 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
816 /* FIXME : different error behaviors according to
817 * 1) err code : LDAP_BUSY ...
818 * 2) on err policy : stop service, stop sync, retry
820 if ( refreshDeletes == 0 && match < 0 &&
821 err == LDAP_SUCCESS )
823 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
825 avl_free( si->si_presentlist, avl_ber_bvfree );
826 si->si_presentlist = NULL;
829 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
830 match < 0 && err == LDAP_SUCCESS )
832 syncrepl_updateCookie( si, op, psub, &syncCookie );
834 if ( err == LDAP_SUCCESS
835 && si->si_logstate == SYNCLOG_FALLBACK ) {
836 si->si_logstate = SYNCLOG_LOGGING;
837 rc = LDAP_SYNC_REFRESH_REQUIRED;
844 case LDAP_RES_INTERMEDIATE:
845 rc = ldap_parse_intermediate( si->si_ld, msg,
846 &retoid, &retdata, NULL, 0 );
847 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
848 ber_init2( ber, retdata, LBER_USE_DER );
850 switch ( si_tag = ber_peek_tag( ber, &len )) {
852 case LDAP_TAG_SYNC_NEW_COOKIE:
853 Debug( LDAP_DEBUG_SYNC,
854 "do_syncrep2: rid %03d %s - %s\n",
856 "LDAP_RES_INTERMEDIATE",
858 ber_scanf( ber, "tm", &tag, &cookie );
860 case LDAP_TAG_SYNC_REFRESH_DELETE:
861 case LDAP_TAG_SYNC_REFRESH_PRESENT:
862 Debug( LDAP_DEBUG_SYNC,
863 "do_syncrep2: rid %03d %s - %s\n",
865 "LDAP_RES_INTERMEDIATE",
866 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
867 "REFRESH_PRESENT" : "REFRESH_DELETE" );
868 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
869 si->si_refreshDelete = 1;
871 si->si_refreshPresent = 1;
873 ber_scanf( ber, "t{" /*"}"*/, &tag );
874 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
876 ber_scanf( ber, "m", &cookie );
877 if ( !BER_BVISNULL( &cookie ) ) {
878 ch_free( syncCookie.octet_str.bv_val );
879 ber_dupbv( &syncCookie.octet_str, &cookie );
881 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
883 slap_parse_sync_cookie( &syncCookie, NULL );
886 /* Defaults to TRUE */
887 if ( ber_peek_tag( ber, &len ) ==
888 LDAP_TAG_REFRESHDONE )
890 ber_scanf( ber, "b", &si->si_refreshDone );
893 si->si_refreshDone = 1;
895 ber_scanf( ber, /*"{"*/ "}" );
897 case LDAP_TAG_SYNC_ID_SET:
898 Debug( LDAP_DEBUG_SYNC,
899 "do_syncrep2: rid %03d %s - %s\n",
901 "LDAP_RES_INTERMEDIATE",
903 ber_scanf( ber, "t{" /*"}"*/, &tag );
904 if ( ber_peek_tag( ber, &len ) ==
905 LDAP_TAG_SYNC_COOKIE )
907 ber_scanf( ber, "m", &cookie );
908 if ( !BER_BVISNULL( &cookie ) ) {
909 ch_free( syncCookie.octet_str.bv_val );
910 ber_dupbv( &syncCookie.octet_str, &cookie );
912 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
914 slap_parse_sync_cookie( &syncCookie, NULL );
917 if ( ber_peek_tag( ber, &len ) ==
918 LDAP_TAG_REFRESHDELETES )
920 ber_scanf( ber, "b", &refreshDeletes );
922 ber_scanf( ber, "[W]", &syncUUIDs );
923 ber_scanf( ber, /*"{"*/ "}" );
924 if ( refreshDeletes ) {
925 syncrepl_del_nonpresent( op, si, syncUUIDs,
926 &syncCookie.ctxcsn );
927 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
929 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
930 struct berval *syncuuid_bv;
931 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
932 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
933 avl_insert( &si->si_presentlist,
934 (caddr_t) syncuuid_bv,
935 syncuuid_cmp, avl_dup_error );
937 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
939 slap_sync_cookie_free( &syncCookie, 0 );
942 Debug( LDAP_DEBUG_ANY,
943 "do_syncrep2: rid %03d unknown syncinfo tag (%ld)\n",
944 si->si_rid, (long) si_tag, 0 );
945 ldap_memfree( retoid );
946 ber_bvfree( retdata );
950 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
952 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
955 value_match( &match, slap_schema.si_ad_entryCSN,
956 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
957 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
958 &syncCookie_req.ctxcsn,
959 &syncCookie.ctxcsn, &text );
963 if ( si->si_refreshPresent == 1 ) {
964 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
966 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
968 syncrepl_updateCookie( si, op, psub, &syncCookie);
972 ldap_memfree( retoid );
973 ber_bvfree( retdata );
977 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
978 "unknown intermediate response (%d)\n",
980 ldap_memfree( retoid );
981 ber_bvfree( retdata );
987 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
988 "unknown message\n", si->si_rid, 0, 0 );
992 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
993 slap_sync_cookie_free( &syncCookie_req, 0 );
994 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
995 slap_sync_cookie_free( &syncCookie, 0 );
1005 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1006 errstr = ldap_err2string( rc );
1008 Debug( LDAP_DEBUG_ANY,
1009 "do_syncrep2: rid %03d %s\n", si->si_rid, errstr, 0 );
1013 slap_sync_cookie_free( &syncCookie, 0 );
1014 slap_sync_cookie_free( &syncCookie_req, 0 );
1016 if ( res ) ldap_msgfree( res );
1018 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1019 if ( si->si_conn_setup ) {
1021 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1022 connection_client_stop( s );
1023 si->si_conn_setup = 0;
1025 ldap_unbind_ext( si->si_ld, NULL, NULL );
1037 struct re_s* rtask = arg;
1038 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1039 Connection conn = {0};
1040 OperationBuffer opbuf;
1042 int rc = LDAP_SUCCESS;
1045 int i, defer = 1, fail = 0;
1048 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl rid %03d\n", si->si_rid, 0, 0 );
1053 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1055 switch( abs( si->si_type )) {
1056 case LDAP_SYNC_REFRESH_ONLY:
1057 case LDAP_SYNC_REFRESH_AND_PERSIST:
1060 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1064 if ( slapd_shutdown ) {
1066 if ( si->si_conn_setup ) {
1067 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1068 connection_client_stop( s );
1069 si->si_conn_setup = 0;
1071 ldap_unbind_ext( si->si_ld, NULL, NULL );
1074 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1078 op = (Operation *) &opbuf;
1079 connection_fake_init( &conn, op, ctx );
1081 /* use global malloc for now */
1082 op->o_tmpmemctx = NULL;
1083 op->o_tmpmfuncs = &ch_mfuncs;
1085 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1086 op->o_bd = be = si->si_be;
1087 op->o_dn = op->o_bd->be_rootdn;
1088 op->o_ndn = op->o_bd->be_rootndn;
1090 /* Establish session, do search */
1092 si->si_refreshDelete = 0;
1093 si->si_refreshPresent = 0;
1094 rc = do_syncrep1( op, si );
1098 /* Process results */
1099 if ( rc == LDAP_SUCCESS ) {
1100 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1102 rc = do_syncrep2( op, si );
1103 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1104 rc = ldap_sync_search( si, op->o_tmpmemctx );
1108 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1109 /* If we succeeded, enable the connection for further listening.
1110 * If we failed, tear down the connection and reschedule.
1112 if ( rc == LDAP_SUCCESS ) {
1113 if ( si->si_conn_setup ) {
1114 connection_client_enable( s );
1116 rc = connection_client_setup( s, do_syncrepl, arg );
1118 si->si_conn_setup = 1;
1120 } else if ( si->si_conn_setup ) {
1124 if ( rc == -2 ) rc = 0;
1128 /* At this point, we have 4 cases:
1129 * 1) for any hard failure, give up and remove this task
1130 * 2) for ServerDown, reschedule this task to run
1131 * 3) for Refresh and Success, reschedule to run
1132 * 4) for Persist and Success, reschedule to defer
1134 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1136 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1137 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1141 connection_client_stop( s );
1144 if ( rc == LDAP_SUCCESS ) {
1145 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1148 rtask->interval.tv_sec = si->si_interval;
1149 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1150 if ( si->si_retrynum ) {
1151 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1152 si->si_retrynum[i] = si->si_retrynum_init[i];
1154 si->si_retrynum[i] = RETRYNUM_TAIL;
1157 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1158 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1162 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1163 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1164 fail = RETRYNUM_TAIL;
1165 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1166 if ( si->si_retrynum[i] > 0 )
1167 si->si_retrynum[i]--;
1168 fail = si->si_retrynum[i];
1169 rtask->interval.tv_sec = si->si_retryinterval[i];
1170 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1171 slap_wake_listener();
1175 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1176 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1179 if ( fail == RETRYNUM_TAIL ) {
1180 Debug( LDAP_DEBUG_ANY,
1181 "do_syncrepl: rid %03d quitting\n",
1183 } else if ( fail > 0 ) {
1184 Debug( LDAP_DEBUG_ANY,
1185 "do_syncrepl: rid %03d retrying (%d retries left)\n",
1186 si->si_rid, fail, 0 );
1188 Debug( LDAP_DEBUG_ANY,
1189 "do_syncrepl: rid %03d retrying\n",
1197 static slap_verbmasks modops[] = {
1198 { BER_BVC("add"), LDAP_REQ_ADD },
1199 { BER_BVC("delete"), LDAP_REQ_DELETE },
1200 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1201 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1205 static Modifications *
1206 syncrepl_accesslog_mods(
1213 AttributeDescription *ad;
1214 struct berval bv, bv2;
1216 Modifications *mod = NULL, *modlist = NULL, **modtail;
1221 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1225 colon = ber_bvchr( &bv, ':' );
1227 continue; /* invalid */
1228 bv.bv_len = colon - bv.bv_val;
1229 if ( slap_bv2ad( &bv, &ad, &text )) {
1233 /* Ignore dynamically generated attrs */
1234 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1236 /* Ignore excluded attrs */
1237 if ( ldap_charray_inlist( si->si_exattrs,
1238 ad->ad_type->sat_cname.bv_val ))
1242 case '+': op = LDAP_MOD_ADD; break;
1243 case '-': op = LDAP_MOD_DELETE; break;
1244 case '=': op = LDAP_MOD_REPLACE; break;
1245 case '#': op = LDAP_MOD_INCREMENT; break;
1249 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1250 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1253 mod->sml_next = NULL;
1255 mod->sml_type = ad->ad_cname;
1256 mod->sml_values = NULL;
1257 mod->sml_nvalues = NULL;
1260 modtail = &mod->sml_next;
1262 if ( colon[2] == ' ' ) {
1263 bv.bv_val = colon + 3;
1264 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1265 ber_dupbv( &bv2, &bv );
1266 ber_bvarray_add( &mod->sml_values, &bv2 );
1272 static Modifications *
1273 syncrepl_changelog_mods(
1278 return NULL; /* FIXME */
1282 syncrepl_message_to_op(
1288 BerElement *ber = NULL;
1289 Modifications *modlist = NULL;
1291 SlapReply rs = { REP_RESULT };
1292 slap_callback cb = { NULL, null_callback, NULL, NULL };
1295 char txtbuf[SLAP_TEXT_BUFLEN];
1296 size_t textlen = sizeof txtbuf;
1298 struct berval bdn, dn = BER_BVNULL, ndn;
1299 struct berval bv, *bvals = NULL;
1300 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1301 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1302 psup = BER_BVNULL, nsup = BER_BVNULL;
1303 int rc, deleteOldRdn = 0, freeReqDn = 0;
1305 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1306 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1307 "Message type should be entry (%d)",
1308 si->si_rid, ldap_msgtype( msg ), 0 );
1312 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1317 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1319 if ( rc != LDAP_SUCCESS ) {
1320 Debug( LDAP_DEBUG_ANY,
1321 "syncrepl_message_to_op: rid %03d dn get failed (%d)",
1322 si->si_rid, rc, 0 );
1326 op->o_tag = LBER_DEFAULT;
1328 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1330 if ( bv.bv_val == NULL )
1333 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1335 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1336 ber_dupbv( &op->o_req_dn, &dn );
1337 ber_dupbv( &op->o_req_ndn, &ndn );
1338 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1339 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1341 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1342 int i = verb_to_mask( bvals[0].bv_val, modops );
1344 Debug( LDAP_DEBUG_ANY,
1345 "syncrepl_message_to_op: rid %03d unknown op %s",
1346 si->si_rid, bvals[0].bv_val, 0 );
1351 op->o_tag = modops[i].mask;
1352 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1353 /* Parse attribute into modlist */
1354 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1355 modlist = syncrepl_accesslog_mods( si, bvals );
1357 modlist = syncrepl_changelog_mods( si, bvals );
1358 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1360 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1361 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1363 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1365 } else if ( !ber_bvstrcasecmp( &bv,
1366 &slap_schema.si_ad_entryCSN->ad_cname )) {
1367 slap_queue_csn( op, bvals );
1372 /* If we didn't get a mod type or a target DN, bail out */
1373 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1378 op->o_callback = &cb;
1379 slap_op_time( &op->o_time, &op->o_tincr );
1381 switch( op->o_tag ) {
1383 case LDAP_REQ_MODIFY:
1384 /* If we didn't get required data, bail */
1385 if ( !modlist ) goto done;
1387 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1389 if ( rc != LDAP_SUCCESS ) {
1390 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1391 "mods check (%s)\n",
1392 si->si_rid, text, 0 );
1396 if ( op->o_tag == LDAP_REQ_ADD ) {
1397 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1398 op->ora_e->e_name = op->o_req_dn;
1399 op->ora_e->e_nname = op->o_req_ndn;
1401 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1402 if( rc != LDAP_SUCCESS ) {
1403 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1404 "mods2entry (%s)\n",
1405 si->si_rid, text, 0 );
1407 rc = op->o_bd->be_add( op, &rs );
1408 Debug( LDAP_DEBUG_SYNC,
1409 "syncrepl_message_to_op: rid %03d be_add %s (%d)\n",
1410 si->si_rid, op->o_req_dn.bv_val, rc );
1412 be_entry_release_w( op, op->ora_e );
1414 op->orm_modlist = modlist;
1415 rc = op->o_bd->be_modify( op, &rs );
1416 modlist = op->orm_modlist;
1417 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1418 "syncrepl_message_to_op: rid %03d be_modify %s (%d)\n",
1419 si->si_rid, op->o_req_dn.bv_val, rc );
1422 case LDAP_REQ_MODRDN:
1423 if ( BER_BVISNULL( &rdn )) goto done;
1425 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1427 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1429 if ( !BER_BVISNULL( &sup )) {
1430 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1432 op->orr_newSup = &psup;
1433 op->orr_nnewSup = ⊅
1435 op->orr_newSup = NULL;
1436 op->orr_nnewSup = NULL;
1438 op->orr_newrdn = prdn;
1439 op->orr_nnewrdn = nrdn;
1440 op->orr_deleteoldrdn = deleteOldRdn;
1441 rc = op->o_bd->be_modrdn( op, &rs );
1442 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1443 "syncrepl_message_to_op: rid %03d be_modrdn %s (%d)\n",
1444 si->si_rid, op->o_req_dn.bv_val, rc );
1446 case LDAP_REQ_DELETE:
1447 rc = op->o_bd->be_delete( op, &rs );
1448 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1449 "syncrepl_message_to_op: rid %03d be_delete %s (%d)\n",
1450 si->si_rid, op->o_req_dn.bv_val, rc );
1454 slap_graduate_commit_csn( op );
1455 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1456 BER_BVZERO( &op->o_csn );
1458 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1459 if ( !BER_BVISNULL( &rdn )) {
1460 if ( !BER_BVISNULL( &nsup ))
1461 ch_free( nsup.bv_val );
1462 if ( !BER_BVISNULL( &psup ))
1463 ch_free( psup.bv_val );
1464 if ( !BER_BVISNULL( &nrdn ))
1465 ch_free( nrdn.bv_val );
1466 if ( !BER_BVISNULL( &prdn ))
1467 ch_free( prdn.bv_val );
1470 ch_free( op->o_req_ndn.bv_val );
1471 ch_free( op->o_req_dn.bv_val );
1473 ber_free ( ber, 0 );
1478 syncrepl_message_to_entry(
1482 Modifications **modlist,
1488 BerElement *ber = NULL;
1491 Modifications **modtail = modlist;
1494 char txtbuf[SLAP_TEXT_BUFLEN];
1495 size_t textlen = sizeof txtbuf;
1497 struct berval bdn = BER_BVNULL, dn, ndn;
1502 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1503 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d "
1504 "Message type should be entry (%d)",
1505 si->si_rid, ldap_msgtype( msg ), 0 );
1509 op->o_tag = LDAP_REQ_ADD;
1511 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1512 if ( rc != LDAP_SUCCESS ) {
1513 Debug( LDAP_DEBUG_ANY,
1514 "syncrepl_message_to_entry: rid %03d dn get failed (%d)",
1515 si->si_rid, rc, 0 );
1519 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1520 ber_dupbv( &op->o_req_dn, &dn );
1521 ber_dupbv( &op->o_req_ndn, &ndn );
1522 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1523 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1525 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1526 /* NOTE: this could be done even before decoding the DN,
1527 * although encoding errors wouldn't be detected */
1532 if ( entry == NULL ) {
1537 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1538 e->e_name = op->o_req_dn;
1539 e->e_nname = op->o_req_ndn;
1541 while ( ber_remaining( ber ) ) {
1542 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1543 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1548 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1550 mod->sml_op = LDAP_MOD_REPLACE;
1552 mod->sml_next = NULL;
1553 mod->sml_desc = NULL;
1554 mod->sml_type = tmp.sml_type;
1555 mod->sml_values = tmp.sml_values;
1556 mod->sml_nvalues = NULL;
1559 modtail = &mod->sml_next;
1562 if ( *modlist == NULL ) {
1563 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d no attributes\n",
1569 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1571 if ( rc != LDAP_SUCCESS ) {
1572 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods check (%s)\n",
1573 si->si_rid, text, 0 );
1577 /* Strip out dynamically generated attrs */
1578 for ( modtail = modlist; *modtail ; ) {
1580 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1581 *modtail = mod->sml_next;
1582 slap_mod_free( &mod->sml_mod, 0 );
1585 modtail = &mod->sml_next;
1589 /* Strip out attrs in exattrs list */
1590 for ( modtail = modlist; *modtail ; ) {
1592 if ( ldap_charray_inlist( si->si_exattrs,
1593 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1594 *modtail = mod->sml_next;
1595 slap_mod_free( &mod->sml_mod, 0 );
1598 modtail = &mod->sml_next;
1602 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1603 if( rc != LDAP_SUCCESS ) {
1604 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods2entry (%s)\n",
1605 si->si_rid, text, 0 );
1610 if ( rc != LDAP_SUCCESS ) {
1621 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1623 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1624 * entry if a previous refresh was interrupted before sending us a new
1625 * context state. We try to compare the new entry to the existing entry
1626 * and ignore the new entry if they are the same.
1628 * Also, we may get an update where the entryDN has changed, due to
1629 * a ModDn on the provider. We detect this as well, so we can issue
1630 * the corresponding operation locally.
1632 * In the case of a modify, we get a list of all the attributes
1633 * in the original entry. Rather than deleting the entry and re-adding it,
1634 * we issue a Modify request that deletes all the attributes and adds all
1635 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1638 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1639 * a ModDN we will issue both operations on the local database.
1641 typedef struct dninfo {
1645 int renamed; /* Was an existing entry renamed? */
1646 int wasChanged; /* are the attributes changed? */
1647 int attrs; /* how many attribute types are in the ads list */
1648 AttributeDescription **ads;
1656 Modifications** modlist,
1658 struct berval* syncUUID,
1659 struct sync_cookie* syncCookie_req,
1660 struct berval* syncCSN )
1662 Backend *be = op->o_bd;
1663 slap_callback cb = { NULL, NULL, NULL, NULL };
1664 struct berval *syncuuid_bv = NULL;
1665 struct berval syncUUID_strrep = BER_BVNULL;
1666 struct berval uuid_bv = BER_BVNULL;
1668 SlapReply rs_search = {REP_RESULT};
1669 SlapReply rs_delete = {REP_RESULT};
1670 SlapReply rs_add = {REP_RESULT};
1671 SlapReply rs_modify = {REP_RESULT};
1673 #ifdef LDAP_COMP_MATCH
1674 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1676 AttributeAssertion ava = { NULL, BER_BVNULL };
1678 int rc = LDAP_SUCCESS;
1680 struct berval pdn = BER_BVNULL;
1684 switch( syncstate ) {
1685 case LDAP_SYNC_PRESENT:
1686 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1688 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1691 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1693 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1695 case LDAP_SYNC_DELETE:
1696 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1698 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1700 case LDAP_SYNC_MODIFY:
1701 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1703 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1706 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: rid %03d %s\n",
1708 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1711 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1712 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
1713 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1714 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1715 syncuuid_cmp, avl_dup_error );
1719 if ( syncstate == LDAP_SYNC_PRESENT ) {
1721 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1722 if ( entry == NULL ) {
1727 f.f_choice = LDAP_FILTER_EQUALITY;
1729 ava.aa_desc = slap_schema.si_ad_entryUUID;
1730 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1731 ava.aa_value = *syncUUID;
1732 op->ors_filter = &f;
1734 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1735 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1736 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1737 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1738 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1739 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1740 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1741 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1743 op->o_tag = LDAP_REQ_SEARCH;
1744 op->ors_scope = LDAP_SCOPE_SUBTREE;
1745 op->ors_deref = LDAP_DEREF_NEVER;
1747 /* get the entry for this UUID */
1748 op->o_req_dn = si->si_base;
1749 op->o_req_ndn = si->si_base;
1751 op->o_time = slap_get_time();
1752 op->ors_tlimit = SLAP_NO_LIMIT;
1755 op->ors_attrs = slap_anlist_all_attributes;
1756 op->ors_attrsonly = 0;
1758 /* set callback function */
1759 op->o_callback = &cb;
1760 cb.sc_response = dn_callback;
1761 cb.sc_private = &dni;
1762 dni.new_entry = entry;
1764 if ( limits_check( op, &rs_search ) == 0 ) {
1765 rc = be->be_search( op, &rs_search );
1766 Debug( LDAP_DEBUG_SYNC,
1767 "syncrepl_entry: rid %03d be_search (%d)\n",
1768 si->si_rid, rc, 0 );
1771 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1772 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1775 cb.sc_response = null_callback;
1778 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1779 Debug( LDAP_DEBUG_SYNC,
1780 "syncrepl_entry: rid %03d %s\n",
1781 si->si_rid, entry->e_name.bv_val, 0 );
1783 Debug( LDAP_DEBUG_SYNC,
1784 "syncrepl_entry: rid %03d %s\n",
1785 si->si_rid, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1788 if ( syncstate != LDAP_SYNC_DELETE ) {
1789 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1792 /* add if missing */
1793 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1794 &syncUUID_strrep, syncUUID );
1796 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1797 /* replace only if necessary */
1798 if ( a->a_nvals != a->a_vals ) {
1799 ber_memfree( a->a_nvals[0].bv_val );
1800 ber_dupbv( &a->a_nvals[0], syncUUID );
1802 ber_memfree( a->a_vals[0].bv_val );
1803 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1805 /* Don't save the contextCSN on the inooming context entry,
1806 * we'll write it when syncrepl_updateCookie eventually
1807 * gets called. (ITS#4622)
1809 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1810 &be->be_nsuffix[0] )) {
1812 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1814 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1823 slap_op_time( &op->o_time, &op->o_tincr );
1824 switch ( syncstate ) {
1826 case LDAP_SYNC_MODIFY:
1828 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1830 op->o_csn = a->a_vals[0];
1833 if ( BER_BVISNULL( &dni.dn )) {
1835 op->o_req_dn = entry->e_name;
1836 op->o_req_ndn = entry->e_nname;
1837 op->o_tag = LDAP_REQ_ADD;
1840 rc = be->be_add( op, &rs_add );
1841 Debug( LDAP_DEBUG_SYNC,
1842 "syncrepl_entry: rid %03d be_add (%d)\n",
1843 si->si_rid, rc, 0 );
1844 switch ( rs_add.sr_err ) {
1846 be_entry_release_w( op, entry );
1851 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1852 * only if the suffix entry is not present */
1853 case LDAP_NO_SUCH_OBJECT:
1854 rc = syncrepl_add_glue( op, entry );
1858 /* if an entry was added via syncrepl_add_glue(),
1859 * it likely has no entryUUID, so the previous
1860 * be_search() doesn't find it. In this case,
1861 * give syncrepl a chance to modify it. Also
1862 * allow for entries that were recreated with the
1863 * same DN but a different entryUUID.
1865 case LDAP_ALREADY_EXISTS:
1867 Operation op2 = *op;
1868 SlapReply rs2 = { 0 };
1869 slap_callback cb2 = { 0 };
1871 op2.o_tag = LDAP_REQ_SEARCH;
1872 op2.o_req_dn = entry->e_name;
1873 op2.o_req_ndn = entry->e_nname;
1874 op2.ors_scope = LDAP_SCOPE_BASE;
1875 op2.ors_deref = LDAP_DEREF_NEVER;
1876 op2.ors_attrs = slap_anlist_all_attributes;
1877 op2.ors_attrsonly = 0;
1878 op2.ors_limit = NULL;
1880 op2.ors_tlimit = SLAP_NO_LIMIT;
1882 f.f_choice = LDAP_FILTER_PRESENT;
1883 f.f_desc = slap_schema.si_ad_objectClass;
1884 op2.ors_filter = &f;
1885 op2.ors_filterstr = generic_filterstr;
1887 op2.o_callback = &cb2;
1888 cb2.sc_response = dn_callback;
1889 cb2.sc_private = &dni;
1891 rc = be->be_search( &op2, &rs2 );
1892 if ( rc ) goto done;
1895 slap_op_time( &op->o_time, &op->o_tincr );
1901 Debug( LDAP_DEBUG_ANY,
1902 "syncrepl_entry: rid %03d be_add failed (%d)\n",
1903 si->si_rid, rs_add.sr_err, 0 );
1909 op->o_req_dn = dni.dn;
1910 op->o_req_ndn = dni.ndn;
1911 if ( dni.renamed ) {
1912 struct berval noldp, newp, nnewp;
1914 op->o_tag = LDAP_REQ_MODRDN;
1915 dnRdn( &entry->e_name, &op->orr_newrdn );
1916 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1918 dnParent( &dni.ndn, &noldp );
1919 dnParent( &entry->e_nname, &nnewp );
1920 if ( !dn_match( &noldp, &nnewp )) {
1921 dnParent( &entry->e_name, &newp );
1922 op->orr_newSup = &newp;
1923 op->orr_nnewSup = &nnewp;
1925 op->orr_newSup = NULL;
1926 op->orr_nnewSup = NULL;
1928 op->orr_deleteoldrdn = 0;
1929 rc = be->be_modrdn( op, &rs_modify );
1930 Debug( LDAP_DEBUG_SYNC,
1931 "syncrepl_entry: rid %03d be_modrdn (%d)\n",
1932 si->si_rid, rc, 0 );
1933 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1934 op->o_req_dn = entry->e_name;
1935 op->o_req_ndn = entry->e_nname;
1939 if ( dni.wasChanged )
1940 slap_op_time( &op->o_time, &op->o_tincr );
1942 if ( dni.wasChanged ) {
1943 Modifications *mod, *modhead = NULL;
1944 Modifications *modtail = NULL;
1947 op->o_tag = LDAP_REQ_MODIFY;
1949 assert( *modlist != NULL );
1951 /* Delete all the old attrs */
1952 for ( i = 0; i < dni.attrs; i++ ) {
1953 mod = ch_malloc( sizeof( Modifications ) );
1954 mod->sml_op = LDAP_MOD_DELETE;
1956 mod->sml_desc = dni.ads[i];
1957 mod->sml_type = mod->sml_desc->ad_cname;
1958 mod->sml_values = NULL;
1959 mod->sml_nvalues = NULL;
1960 if ( !modhead ) modhead = mod;
1962 modtail->sml_next = mod;
1967 /* Append passed in list to ours */
1969 modtail->sml_next = *modlist;
1975 /* Find end of this list */
1976 for ( ; mod != NULL; mod = mod->sml_next ) {
1980 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1981 mod->sml_op = LDAP_MOD_REPLACE;
1983 mod->sml_desc = slap_schema.si_ad_entryUUID;
1984 mod->sml_type = mod->sml_desc->ad_cname;
1985 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1986 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1987 ber_dupbv( &uuid_bv, syncUUID );
1988 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1989 modtail->sml_next = mod;
1991 op->o_tag = LDAP_REQ_MODIFY;
1992 op->orm_modlist = *modlist;
1994 rc = be->be_modify( op, &rs_modify );
1995 Debug( LDAP_DEBUG_SYNC,
1996 "syncrepl_entry: rid %03d be_modify (%d)\n",
1997 si->si_rid, rc, 0 );
1998 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1999 Debug( LDAP_DEBUG_ANY,
2000 "syncrepl_entry: rid %03d be_modify failed (%d)\n",
2001 si->si_rid, rs_modify.sr_err, 0 );
2005 case LDAP_SYNC_DELETE :
2006 if ( !BER_BVISNULL( &dni.dn )) {
2007 op->o_req_dn = dni.dn;
2008 op->o_req_ndn = dni.ndn;
2009 op->o_tag = LDAP_REQ_DELETE;
2010 rc = be->be_delete( op, &rs_delete );
2011 Debug( LDAP_DEBUG_SYNC,
2012 "syncrepl_entry: rid %03d be_delete (%d)\n",
2013 si->si_rid, rc, 0 );
2015 while ( rs_delete.sr_err == LDAP_SUCCESS
2016 && op->o_delete_glue_parent ) {
2017 op->o_delete_glue_parent = 0;
2018 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2019 slap_callback cb = { NULL };
2020 cb.sc_response = slap_null_cb;
2021 dnParent( &op->o_req_ndn, &pdn );
2023 op->o_req_ndn = pdn;
2024 op->o_callback = &cb;
2025 op->o_bd->be_delete( op, &rs_delete );
2034 Debug( LDAP_DEBUG_ANY,
2035 "syncrepl_entry: rid %03d unknown syncstate\n", si->si_rid, 0, 0 );
2040 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2041 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2042 BER_BVZERO( &syncUUID_strrep );
2045 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2047 if ( !BER_BVISNULL( &dni.ndn ) ) {
2048 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2050 if ( !BER_BVISNULL( &dni.dn ) ) {
2051 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2054 entry_free( entry );
2055 BER_BVZERO( &op->o_csn );
2059 static struct berval gcbva[] = {
2065 #define NP_DELETE_ONE 2
2068 syncrepl_del_nonpresent(
2072 struct berval *cookiecsn )
2074 Backend* be = op->o_bd;
2075 slap_callback cb = { NULL };
2076 SlapReply rs_search = {REP_RESULT};
2077 SlapReply rs_delete = {REP_RESULT};
2078 SlapReply rs_modify = {REP_RESULT};
2079 struct nonpresent_entry *np_list, *np_prev;
2081 AttributeName an[2];
2083 struct berval pdn = BER_BVNULL;
2086 op->o_req_dn = si->si_base;
2087 op->o_req_ndn = si->si_base;
2089 cb.sc_response = nonpresent_callback;
2092 op->o_callback = &cb;
2093 op->o_tag = LDAP_REQ_SEARCH;
2094 op->ors_scope = si->si_scope;
2095 op->ors_deref = LDAP_DEREF_NEVER;
2096 op->o_time = slap_get_time();
2097 op->ors_tlimit = SLAP_NO_LIMIT;
2102 #ifdef LDAP_COMP_MATCH
2103 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2105 AttributeAssertion eq = { NULL, BER_BVNULL };
2109 op->ors_attrsonly = 1;
2110 op->ors_attrs = slap_anlist_no_attrs;
2111 op->ors_limit = NULL;
2112 op->ors_filter = &uf;
2115 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2117 uf.f_choice = LDAP_FILTER_EQUALITY;
2118 si->si_refreshDelete |= NP_DELETE_ONE;
2120 for (i=0; uuids[i].bv_val; i++) {
2122 uf.f_av_value = uuids[i];
2123 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2124 rc = be->be_search( op, &rs_search );
2125 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2127 si->si_refreshDelete ^= NP_DELETE_ONE;
2129 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2130 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2131 an[0].an_desc = slap_schema.si_ad_entryUUID;
2133 op->ors_slimit = SLAP_NO_LIMIT;
2134 op->ors_attrsonly = 0;
2135 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2136 op->ors_filterstr = si->si_filterstr;
2137 op->o_nocaching = 1;
2139 if ( limits_check( op, &rs_search ) == 0 ) {
2140 rc = be->be_search( op, &rs_search );
2142 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2145 op->o_nocaching = 0;
2147 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2149 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2152 csn = si->si_syncCookie.ctxcsn;
2154 slap_queue_csn( op, &csn );
2156 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2157 while ( np_list != NULL ) {
2158 LDAP_LIST_REMOVE( np_list, npe_link );
2160 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2161 op->o_tag = LDAP_REQ_DELETE;
2162 op->o_callback = &cb;
2163 cb.sc_response = null_callback;
2165 op->o_req_dn = *np_prev->npe_name;
2166 op->o_req_ndn = *np_prev->npe_nname;
2167 rc = op->o_bd->be_delete( op, &rs_delete );
2168 Debug( LDAP_DEBUG_SYNC,
2169 "syncrepl_del_nonpresent: rid %03d be_delete %s (%d)\n",
2170 si->si_rid, op->o_req_dn.bv_val, rc );
2172 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2173 Modifications mod1, mod2;
2174 mod1.sml_op = LDAP_MOD_REPLACE;
2176 mod1.sml_desc = slap_schema.si_ad_objectClass;
2177 mod1.sml_type = mod1.sml_desc->ad_cname;
2178 mod1.sml_values = &gcbva[0];
2179 mod1.sml_nvalues = NULL;
2180 mod1.sml_next = &mod2;
2182 mod2.sml_op = LDAP_MOD_REPLACE;
2184 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2185 mod2.sml_type = mod2.sml_desc->ad_cname;
2186 mod2.sml_values = &gcbva[1];
2187 mod2.sml_nvalues = NULL;
2188 mod2.sml_next = NULL;
2190 op->o_tag = LDAP_REQ_MODIFY;
2191 op->orm_modlist = &mod1;
2193 rc = be->be_modify( op, &rs_modify );
2194 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2197 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2198 op->o_delete_glue_parent ) {
2199 op->o_delete_glue_parent = 0;
2200 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2201 slap_callback cb = { NULL };
2202 cb.sc_response = slap_null_cb;
2203 dnParent( &op->o_req_ndn, &pdn );
2205 op->o_req_ndn = pdn;
2206 op->o_callback = &cb;
2207 /* give it a root privil ? */
2208 op->o_bd->be_delete( op, &rs_delete );
2214 op->o_delete_glue_parent = 0;
2216 ber_bvfree( np_prev->npe_name );
2217 ber_bvfree( np_prev->npe_nname );
2221 slap_graduate_commit_csn( op );
2222 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2223 BER_BVZERO( &op->o_csn );
2234 Backend *be = op->o_bd;
2235 slap_callback cb = { NULL };
2240 struct berval dn = BER_BVNULL;
2241 struct berval ndn = BER_BVNULL;
2243 SlapReply rs_add = {REP_RESULT};
2244 struct berval ptr, nptr;
2247 op->o_tag = LDAP_REQ_ADD;
2248 op->o_callback = &cb;
2249 cb.sc_response = null_callback;
2250 cb.sc_private = NULL;
2255 /* count RDNs in suffix */
2256 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2257 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2259 ptr.bv_len -= comma - ptr.bv_val;
2269 /* Start with BE suffix */
2271 for ( i = 0; i < suffrdns; i++ ) {
2272 comma = ber_bvrchr( &ptr, ',' );
2273 if ( comma != NULL ) {
2274 ptr.bv_len = comma - ptr.bv_val;
2281 if ( !BER_BVISEMPTY( &ptr ) ) {
2282 dn.bv_len -= ptr.bv_len + 1;
2283 dn.bv_val += ptr.bv_len + 1;
2286 /* the normalizedDNs are always the same length, no counting
2290 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2291 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2292 ndn.bv_len = be->be_nsuffix[0].bv_len;
2294 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2300 while ( ndn.bv_val > e->e_nname.bv_val ) {
2301 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2302 ber_dupbv( &glue->e_name, &dn );
2303 ber_dupbv( &glue->e_nname, &ndn );
2305 a = ch_calloc( 1, sizeof( Attribute ));
2306 a->a_desc = slap_schema.si_ad_objectClass;
2308 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2309 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2310 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2311 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2313 a->a_nvals = a->a_vals;
2315 a->a_next = glue->e_attrs;
2318 a = ch_calloc( 1, sizeof( Attribute ));
2319 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2321 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2322 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2323 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2325 a->a_nvals = a->a_vals;
2327 a->a_next = glue->e_attrs;
2330 op->o_req_dn = glue->e_name;
2331 op->o_req_ndn = glue->e_nname;
2333 rc = be->be_add ( op, &rs_add );
2334 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2335 be_entry_release_w( op, glue );
2337 /* incl. ALREADY EXIST */
2339 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2345 /* Move to next child */
2346 comma = ber_bvrchr( &ptr, ',' );
2347 if ( comma == NULL ) {
2350 ptr.bv_len = comma - ptr.bv_val;
2352 dn.bv_val = ++comma;
2353 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2355 comma = ber_bvrchr( &nptr, ',' );
2356 assert( comma != NULL );
2357 nptr.bv_len = comma - nptr.bv_val;
2359 ndn.bv_val = ++comma;
2360 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2363 op->o_req_dn = e->e_name;
2364 op->o_req_ndn = e->e_nname;
2366 rc = be->be_add ( op, &rs_add );
2367 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2368 be_entry_release_w( op, e );
2377 syncrepl_updateCookie(
2381 struct sync_cookie *syncCookie )
2383 Backend *be = op->o_bd;
2384 Modifications mod = { { 0 } };
2385 struct berval vals[ 2 ];
2389 slap_callback cb = { NULL };
2390 SlapReply rs_modify = {REP_RESULT};
2392 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2393 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2395 mod.sml_op = LDAP_MOD_REPLACE;
2396 mod.sml_desc = slap_schema.si_ad_contextCSN;
2397 mod.sml_type = mod.sml_desc->ad_cname;
2398 mod.sml_values = vals;
2399 vals[0] = si->si_syncCookie.ctxcsn;
2400 vals[1].bv_val = NULL;
2403 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2405 op->o_tag = LDAP_REQ_MODIFY;
2407 assert( si->si_rid < 1000 );
2409 cb.sc_response = null_callback;
2412 op->o_callback = &cb;
2413 op->o_req_dn = op->o_bd->be_suffix[0];
2414 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2416 /* update contextCSN */
2417 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2418 op->orm_modlist = &mod;
2419 dbflags = SLAP_DBFLAGS(op->o_bd);
2420 SLAP_DBFLAGS(op->o_bd) |= SLAP_DBFLAG_NOLASTMOD;
2421 rc = be->be_modify( op, &rs_modify );
2422 SLAP_DBFLAGS(op->o_bd) = dbflags;
2425 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2426 Debug( LDAP_DEBUG_ANY,
2427 "syncrepl_updateCookie: rid %03d be_modify failed (%d)\n",
2428 si->si_rid, rs_modify.sr_err, 0 );
2431 slap_graduate_commit_csn( op );
2432 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2433 BER_BVZERO( &op->o_csn );
2434 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2444 dninfo *dni = op->o_callback->sc_private;
2446 if ( rs->sr_type == REP_SEARCH ) {
2447 if ( !BER_BVISNULL( &dni->dn ) ) {
2448 Debug( LDAP_DEBUG_ANY,
2449 "dn_callback: consistency error - "
2450 "entryUUID is not unique\n", 0, 0, 0 );
2452 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2453 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2454 /* If there is a new entry, see if it differs from the old.
2455 * We compare the non-normalized values so that cosmetic changes
2456 * in the provider are always propagated.
2458 if ( dni->new_entry ) {
2459 Attribute *old, *new;
2462 /* Did the DN change? Note that we don't explicitly try to
2463 * discover if the deleteOldRdn argument applies here. It
2464 * would save an unnecessary Modify if we detected it, but
2465 * that's a fair amount of trouble to compare the two attr
2466 * lists in detail. (Just test normalized DN; we ignore
2467 * insignificant changes here.)
2469 if ( !dn_match( &rs->sr_entry->e_nname,
2470 &dni->new_entry->e_nname ) )
2475 for ( i = 0, old = rs->sr_entry->e_attrs;
2477 i++, old = old->a_next )
2482 /* We assume that attributes are saved in the same order
2483 * in the remote and local databases. So if we walk through
2484 * the attributeDescriptions one by one they should match in
2485 * lock step. If not, we signal a change. Otherwise we test
2488 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2490 old = old->a_next, new = new->a_next )
2492 if ( old->a_desc != new->a_desc ) {
2493 dni->wasChanged = 1;
2496 for ( i = 0; ; i++ ) {
2498 nold = BER_BVISNULL( &old->a_vals[i] );
2499 nnew = BER_BVISNULL( &new->a_vals[i] );
2500 /* If both are empty, stop looking */
2501 if ( nold && nnew ) {
2504 /* If they are different, stop looking */
2505 if ( nold != nnew ) {
2506 dni->wasChanged = 1;
2509 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2510 dni->wasChanged = 1;
2514 if ( dni->wasChanged ) break;
2516 if ( dni->wasChanged ) {
2517 dni->ads = op->o_tmpalloc( dni->attrs *
2518 sizeof(AttributeDescription *), op->o_tmpmemctx );
2520 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2521 dni->ads[i] = old->a_desc;
2527 } else if ( rs->sr_type == REP_RESULT ) {
2528 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2529 Debug( LDAP_DEBUG_ANY,
2530 "dn_callback: consistency error - "
2531 "entryUUID is not unique\n", 0, 0, 0 );
2535 return LDAP_SUCCESS;
2539 nonpresent_callback(
2543 syncinfo_t *si = op->o_callback->sc_private;
2546 struct berval* present_uuid = NULL;
2547 struct nonpresent_entry *np_entry;
2549 if ( rs->sr_type == REP_RESULT ) {
2550 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2551 si->si_presentlist = NULL;
2553 } else if ( rs->sr_type == REP_SEARCH ) {
2554 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2555 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2557 if ( a == NULL ) return 0;
2559 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2563 if ( present_uuid == NULL ) {
2564 np_entry = (struct nonpresent_entry *)
2565 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2566 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2567 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2568 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2571 avl_delete( &si->si_presentlist,
2572 &a->a_nvals[0], syncuuid_cmp );
2573 ch_free( present_uuid->bv_val );
2574 ch_free( present_uuid );
2577 return LDAP_SUCCESS;
2585 if ( rs->sr_err != LDAP_SUCCESS &&
2586 rs->sr_err != LDAP_REFERRAL &&
2587 rs->sr_err != LDAP_ALREADY_EXISTS &&
2588 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2589 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2591 Debug( LDAP_DEBUG_ANY,
2592 "null_callback: error code 0x%x\n",
2595 return LDAP_SUCCESS;
2598 static struct berval *
2599 slap_uuidstr_from_normalized(
2600 struct berval* uuidstr,
2601 struct berval* normalized,
2605 unsigned char nibble;
2608 if ( normalized == NULL ) return NULL;
2609 if ( normalized->bv_len != 16 ) return NULL;
2614 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2615 if ( new == NULL ) {
2622 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2623 if ( new != uuidstr ) {
2624 slap_sl_free( new, ctx );
2629 for ( i = 0; i < 16; i++ ) {
2630 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2631 new->bv_val[(i<<1)+d] = '-';
2635 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2636 if ( nibble < 10 ) {
2637 new->bv_val[(i<<1)+d] = nibble + '0';
2639 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2642 nibble = (normalized->bv_val[i]) & 0xF;
2643 if ( nibble < 10 ) {
2644 new->bv_val[(i<<1)+d+1] = nibble + '0';
2646 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2650 new->bv_val[new->bv_len] = '\0';
2655 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2657 const struct berval *uuid1 = v_uuid1;
2658 const struct berval *uuid2 = v_uuid2;
2659 int rc = uuid1->bv_len - uuid2->bv_len;
2660 if ( rc ) return rc;
2661 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2665 avl_ber_bvfree( void *v_bv )
2667 struct berval *bv = (struct berval *)v_bv;
2669 if( v_bv == NULL ) return;
2670 if ( !BER_BVISNULL( bv ) ) {
2671 ch_free( bv->bv_val );
2673 ch_free( (char *) bv );
2677 syncinfo_free( syncinfo_t *sie )
2680 if ( sie->si_conn_setup ) {
2682 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2683 connection_client_stop( s );
2684 sie->si_conn_setup = 0;
2686 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2689 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2691 bindconf_free( &sie->si_bindconf );
2693 if ( sie->si_filterstr.bv_val ) {
2694 ch_free( sie->si_filterstr.bv_val );
2696 if ( sie->si_base.bv_val ) {
2697 ch_free( sie->si_base.bv_val );
2699 if ( sie->si_attrs ) {
2701 while ( sie->si_attrs[i] != NULL ) {
2702 ch_free( sie->si_attrs[i] );
2705 ch_free( sie->si_attrs );
2707 if ( sie->si_exattrs ) {
2709 while ( sie->si_exattrs[i] != NULL ) {
2710 ch_free( sie->si_exattrs[i] );
2713 ch_free( sie->si_exattrs );
2715 if ( sie->si_anlist ) {
2717 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2718 ch_free( sie->si_anlist[i].an_name.bv_val );
2721 ch_free( sie->si_anlist );
2723 if ( sie->si_exanlist ) {
2725 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2726 ch_free( sie->si_exanlist[i].an_name.bv_val );
2729 ch_free( sie->si_exanlist );
2731 if ( sie->si_retryinterval ) {
2732 ch_free( sie->si_retryinterval );
2734 if ( sie->si_retrynum ) {
2735 ch_free( sie->si_retrynum );
2737 if ( sie->si_retrynum_init ) {
2738 ch_free( sie->si_retrynum_init );
2740 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2741 if ( sie->si_presentlist ) {
2742 avl_free( sie->si_presentlist, avl_ber_bvfree );
2744 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2745 struct nonpresent_entry* npe;
2746 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2747 LDAP_LIST_REMOVE( npe, npe_link );
2748 if ( npe->npe_name ) {
2749 if ( npe->npe_name->bv_val ) {
2750 ch_free( npe->npe_name->bv_val );
2752 ch_free( npe->npe_name );
2754 if ( npe->npe_nname ) {
2755 if ( npe->npe_nname->bv_val ) {
2756 ch_free( npe->npe_nname->bv_val );
2758 ch_free( npe->npe_nname );
2767 /* NOTE: used & documented in slapd.conf(5) */
2769 #define PROVIDERSTR "provider"
2770 #define SCHEMASTR "schemachecking"
2771 #define FILTERSTR "filter"
2772 #define SEARCHBASESTR "searchbase"
2773 #define SCOPESTR "scope"
2774 #define ATTRSONLYSTR "attrsonly"
2775 #define ATTRSSTR "attrs"
2776 #define TYPESTR "type"
2777 #define INTERVALSTR "interval"
2778 #define RETRYSTR "retry"
2779 #define SLIMITSTR "sizelimit"
2780 #define TLIMITSTR "timelimit"
2781 #define SYNCDATASTR "syncdata"
2783 /* FIXME: undocumented */
2784 #define LOGBASESTR "logbase"
2785 #define LOGFILTERSTR "logfilter"
2786 #define OLDAUTHCSTR "bindprincipal"
2787 #define EXATTRSSTR "exattrs"
2788 #define MANAGEDSAITSTR "manageDSAit"
2791 #define LASTMODSTR "lastmod"
2792 #define LMGENSTR "gen"
2793 #define LMNOSTR "no"
2794 #define LMREQSTR "req"
2795 #define SRVTABSTR "srvtab"
2796 #define SUFFIXSTR "suffix"
2799 #define GOT_ID 0x0001
2800 #define GOT_PROVIDER 0x0002
2801 #define GOT_BASE 0x0004
2804 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2810 { BER_BVC("base"), LDAP_SCOPE_BASE },
2811 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2812 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2813 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2814 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2815 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2816 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2820 static slap_verbmasks datamodes[] = {
2821 { BER_BVC("default"), SYNCDATA_DEFAULT },
2822 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2823 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2828 parse_syncrepl_line(
2836 for ( i = 1; i < c->argc; i++ ) {
2837 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2838 STRLENOF( IDSTR "=" ) ) )
2841 /* '\0' string terminator accounts for '=' */
2842 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2843 if ( lutil_atoi( &tmp, val ) != 0 ) {
2844 snprintf( c->msg, sizeof( c->msg ),
2845 "Error: parse_syncrepl_line: "
2846 "unable to parse syncrepl id \"%s\"", val );
2847 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2850 if ( tmp >= 1000 || tmp < 0 ) {
2851 snprintf( c->msg, sizeof( c->msg ),
2852 "Error: parse_syncrepl_line: "
2853 "syncrepl id %d is out of range [0..999]", tmp );
2854 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2859 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2860 STRLENOF( PROVIDERSTR "=" ) ) )
2862 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2863 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2864 gots |= GOT_PROVIDER;
2865 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2866 STRLENOF( SCHEMASTR "=" ) ) )
2868 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2869 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2870 si->si_schemachecking = 1;
2871 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2872 si->si_schemachecking = 0;
2874 si->si_schemachecking = 1;
2876 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2877 STRLENOF( FILTERSTR "=" ) ) )
2879 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2880 if ( si->si_filterstr.bv_val )
2881 ch_free( si->si_filterstr.bv_val );
2882 ber_str2bv( val, 0, 1, &si->si_filterstr );
2883 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2884 STRLENOF( LOGFILTERSTR "=" ) ) )
2886 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2887 if ( si->si_logfilterstr.bv_val )
2888 ch_free( si->si_logfilterstr.bv_val );
2889 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2890 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2891 STRLENOF( SEARCHBASESTR "=" ) ) )
2896 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2897 if ( si->si_base.bv_val ) {
2898 ch_free( si->si_base.bv_val );
2900 ber_str2bv( val, 0, 0, &bv );
2901 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2902 if ( rc != LDAP_SUCCESS ) {
2903 snprintf( c->msg, sizeof( c->msg ),
2904 "Invalid base DN \"%s\": %d (%s)",
2905 val, rc, ldap_err2string( rc ) );
2906 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2909 if ( select_backend( &si->si_base, 0, 0 ) != c->be ) {
2910 ber_memfree( si->si_base.bv_val );
2911 snprintf( c->msg, sizeof( c->msg ),
2912 "Base DN \"%s\" is not within the database naming context",
2914 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2918 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2919 STRLENOF( LOGBASESTR "=" ) ) )
2924 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2925 if ( si->si_logbase.bv_val ) {
2926 ch_free( si->si_logbase.bv_val );
2928 ber_str2bv( val, 0, 0, &bv );
2929 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2930 if ( rc != LDAP_SUCCESS ) {
2931 snprintf( c->msg, sizeof( c->msg ),
2932 "Invalid logbase DN \"%s\": %d (%s)",
2933 val, rc, ldap_err2string( rc ) );
2934 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2937 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2938 STRLENOF( SCOPESTR "=" ) ) )
2941 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2942 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2943 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2944 si->si_scope = scopes[j].val;
2948 if ( BER_BVISNULL(&scopes[j].key) ) {
2949 snprintf( c->msg, sizeof( c->msg ),
2950 "Error: parse_syncrepl_line: "
2951 "unknown scope \"%s\"", val);
2952 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2955 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2956 STRLENOF( ATTRSONLYSTR ) ) )
2958 si->si_attrsonly = 1;
2959 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2960 STRLENOF( ATTRSSTR "=" ) ) )
2962 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2963 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2965 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2966 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2967 if ( si->si_anlist == NULL ) {
2968 ch_free( attr_fname );
2971 si->si_anfile = attr_fname;
2973 char *str, *s, *next;
2974 char delimstr[] = " ,\t";
2975 str = ch_strdup( val );
2976 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2978 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2980 if ( strlen(s) == 1 && *s == '*' ) {
2981 si->si_allattrs = 1;
2982 *(val + ( s - str )) = delimstr[0];
2984 if ( strlen(s) == 1 && *s == '+' ) {
2985 si->si_allopattrs = 1;
2986 *(val + ( s - str )) = delimstr[0];
2990 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2991 if ( si->si_anlist == NULL ) {
2995 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2996 STRLENOF( EXATTRSSTR "=" ) ) )
2998 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2999 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
3001 attr_fname = ch_strdup( val + STRLENOF(":include:") );
3002 si->si_exanlist = file2anlist(
3003 si->si_exanlist, attr_fname, " ,\t" );
3004 if ( si->si_exanlist == NULL ) {
3005 ch_free( attr_fname );
3008 ch_free( attr_fname );
3010 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3011 if ( si->si_exanlist == NULL ) {
3015 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3016 STRLENOF( TYPESTR "=" ) ) )
3018 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3019 if ( !strncasecmp( val, "refreshOnly",
3020 STRLENOF("refreshOnly") ) )
3022 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3023 } else if ( !strncasecmp( val, "refreshAndPersist",
3024 STRLENOF("refreshAndPersist") ) )
3026 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3027 si->si_interval = 60;
3029 snprintf( c->msg, sizeof( c->msg ),
3030 "Error: parse_syncrepl_line: "
3031 "unknown sync type \"%s\"", val);
3032 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3035 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3036 STRLENOF( INTERVALSTR "=" ) ) )
3038 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3039 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3040 si->si_interval = 0;
3041 } else if ( strchr( val, ':' ) != NULL ) {
3042 char *next, *ptr = val;
3043 unsigned dd, hh, mm, ss;
3045 /* NOTE: the test for ptr[ 0 ] == '-'
3046 * should go before the call to strtoul() */
3047 dd = strtoul( ptr, &next, 10 );
3048 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3049 snprintf( c->msg, sizeof( c->msg ),
3050 "Error: parse_syncrepl_line: "
3051 "invalid interval \"%s\", unable to parse days", val );
3052 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3056 hh = strtoul( ptr, &next, 10 );
3057 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3058 snprintf( c->msg, sizeof( c->msg ),
3059 "Error: parse_syncrepl_line: "
3060 "invalid interval \"%s\", unable to parse hours", val );
3061 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3065 mm = strtoul( ptr, &next, 10 );
3066 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3067 snprintf( c->msg, sizeof( c->msg ),
3068 "Error: parse_syncrepl_line: "
3069 "invalid interval \"%s\", unable to parse minutes", val );
3070 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3074 ss = strtoul( ptr, &next, 10 );
3075 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3076 snprintf( c->msg, sizeof( c->msg ),
3077 "Error: parse_syncrepl_line: "
3078 "invalid interval \"%s\", unable to parse seconds", val );
3079 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3082 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3086 if ( lutil_parse_time( val, &t ) != 0 ) {
3087 snprintf( c->msg, sizeof( c->msg ),
3088 "Error: parse_syncrepl_line: "
3089 "invalid interval \"%s\"", val );
3090 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3093 si->si_interval = (time_t)t;
3095 if ( si->si_interval < 0 ) {
3096 snprintf( c->msg, sizeof( c->msg ),
3097 "Error: parse_syncrepl_line: "
3098 "invalid interval \"%ld\"",
3099 (long) si->si_interval);
3100 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3103 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3104 STRLENOF( RETRYSTR "=" ) ) )
3109 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3110 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3111 retry_list[0] = NULL;
3113 slap_str2clist( &retry_list, val, " ,\t" );
3115 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3118 snprintf( c->msg, sizeof( c->msg ),
3119 "Error: incomplete syncrepl retry list" );
3120 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3121 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3122 ch_free( retry_list[k] );
3124 ch_free( retry_list );
3127 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3128 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3129 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3130 for ( j = 0; j < n; j++ ) {
3132 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3133 snprintf( c->msg, sizeof( c->msg ),
3134 "Error: invalid retry interval \"%s\" (#%d)",
3135 retry_list[j*2], j );
3136 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3137 /* do some cleanup */
3140 si->si_retryinterval[j] = (time_t)t;
3141 if ( *retry_list[j*2+1] == '+' ) {
3142 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3143 si->si_retrynum[j] = RETRYNUM_FOREVER;
3147 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3148 || si->si_retrynum_init[j] <= 0 )
3150 snprintf( c->msg, sizeof( c->msg ),
3151 "Error: invalid initial retry number \"%s\" (#%d)",
3152 retry_list[j*2+1], j );
3153 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3154 /* do some cleanup */
3157 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3158 || si->si_retrynum[j] <= 0 )
3160 snprintf( c->msg, sizeof( c->msg ),
3161 "Error: invalid retry number \"%s\" (#%d)",
3162 retry_list[j*2+1], j );
3163 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3164 /* do some cleanup */
3169 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3170 si->si_retrynum[j] = RETRYNUM_TAIL;
3171 si->si_retryinterval[j] = 0;
3173 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3174 ch_free( retry_list[k] );
3176 ch_free( retry_list );
3177 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3178 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3180 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3181 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3182 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3184 snprintf( c->msg, sizeof( c->msg ),
3185 "invalid manageDSAit value \"%s\".\n",
3187 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3190 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3191 STRLENOF( SLIMITSTR "=") ) )
3193 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3194 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3197 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3198 snprintf( c->msg, sizeof( c->msg ),
3199 "invalid size limit value \"%s\".\n",
3201 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3204 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3205 STRLENOF( TLIMITSTR "=" ) ) )
3207 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3208 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3211 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3212 snprintf( c->msg, sizeof( c->msg ),
3213 "invalid time limit value \"%s\".\n",
3215 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3218 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3219 STRLENOF( SYNCDATASTR "=" ) ) )
3221 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3222 si->si_syncdata = verb_to_mask( val, datamodes );
3223 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3224 snprintf( c->msg, sizeof( c->msg ),
3225 "Error: parse_syncrepl_line: "
3226 "unable to parse \"%s\"\n", c->argv[ i ] );
3227 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3232 if ( gots != GOT_ALL ) {
3233 snprintf( c->msg, sizeof( c->msg ),
3234 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3235 gots & GOT_ID ? "" : " "IDSTR,
3236 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3237 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3238 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3252 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3253 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3254 "operations required for syncrepl", c->be->be_type );
3255 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3258 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3259 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3260 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3263 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3266 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3270 si->si_bindconf.sb_tls = SB_TLS_OFF;
3271 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3272 si->si_schemachecking = 0;
3273 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3274 &si->si_filterstr );
3275 si->si_base.bv_val = NULL;
3276 si->si_scope = LDAP_SCOPE_SUBTREE;
3277 si->si_attrsonly = 0;
3278 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3279 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3280 si->si_attrs = NULL;
3281 si->si_allattrs = 0;
3282 si->si_allopattrs = 0;
3283 si->si_exattrs = NULL;
3284 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3285 si->si_interval = 86400;
3286 si->si_retryinterval = NULL;
3287 si->si_retrynum_init = NULL;
3288 si->si_retrynum = NULL;
3289 si->si_manageDSAit = 0;
3292 si->si_conn_setup = 0;
3294 si->si_presentlist = NULL;
3295 LDAP_LIST_INIT( &si->si_nonpresentlist );
3296 ldap_pvt_thread_mutex_init( &si->si_mutex );
3298 rc = parse_syncrepl_line( c, si );
3302 init_syncrepl( si );
3303 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3304 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3309 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3310 syncinfo_free( si );
3313 Debug( LDAP_DEBUG_CONFIG,
3314 "Config: ** successfully added syncrepl \"%s\"\n",
3315 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3316 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3317 if ( !si->si_schemachecking ) {
3318 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3320 c->be->be_syncinfo = si;
3326 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3328 struct berval bc, uri;
3329 char buf[BUFSIZ*2], *ptr;
3332 /* FIXME: we're not checking for buf[] overflow! */
3334 /* temporarily inhibit bindconf from printing URI */
3335 uri = si->si_bindconf.sb_uri;
3336 BER_BVZERO( &si->si_bindconf.sb_uri );
3337 bindconf_unparse( &si->si_bindconf, &bc );
3338 si->si_bindconf.sb_uri = uri;
3341 ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03d " PROVIDERSTR "=%s",
3342 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3343 if ( !BER_BVISNULL( &bc )) {
3344 ptr = lutil_strcopy( ptr, bc.bv_val );
3347 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3348 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3349 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3352 if ( !BER_BVISNULL( &si->si_base )) {
3353 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3354 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3357 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3358 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3359 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3362 if ( !BER_BVISNULL( &si->si_logbase )) {
3363 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3364 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3367 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3368 if ( si->si_scope == scopes[i].val ) {
3369 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3370 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3374 if ( si->si_attrsonly ) {
3375 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3377 if ( si->si_anfile ) {
3378 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3379 ptr = lutil_strcopy( ptr, si->si_anfile );
3380 } else if ( si->si_allattrs || si->si_allopattrs ||
3381 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3383 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3385 ptr = anlist_unparse( si->si_anlist, ptr );
3386 if ( si->si_allattrs ) {
3387 if ( old != ptr ) *ptr++ = ',';
3390 if ( si->si_allopattrs ) {
3391 if ( old != ptr ) *ptr++ = ',';
3396 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3397 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3398 ptr = anlist_unparse( si->si_exanlist, ptr );
3400 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3401 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3403 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3404 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3405 "refreshAndPersist" : "refreshOnly" );
3407 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3410 dd = si->si_interval;
3417 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3418 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3419 } else if ( si->si_retryinterval ) {
3421 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3422 for (i=0; si->si_retryinterval[i]; i++) {
3423 if ( space ) *ptr++ = ' ';
3425 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3426 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3429 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3434 if ( si->si_slimit ) {
3435 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3436 ptr += sprintf( ptr, "%d", si->si_slimit );
3439 if ( si->si_tlimit ) {
3440 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3441 ptr += sprintf( ptr, "%d", si->si_tlimit );
3444 if ( si->si_syncdata ) {
3445 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3446 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3447 ptr = lutil_strcopy( ptr, bc.bv_val );
3450 bc.bv_len = ptr - buf;
3452 ber_dupbv( bv, &bc );
3456 syncrepl_config( ConfigArgs *c )
3458 if (c->op == SLAP_CONFIG_EMIT) {
3459 if ( c->be->be_syncinfo ) {
3461 syncrepl_unparse( c->be->be_syncinfo, &bv );
3462 ber_bvarray_add( &c->rvalue_vals, &bv );
3466 } else if ( c->op == LDAP_MOD_DELETE ) {
3469 if ( c->be->be_syncinfo ) {
3470 re = c->be->be_syncinfo->si_re;
3472 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3473 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3474 ldap_pvt_runqueue_remove( &slapd_rq, re );
3476 syncinfo_free( c->be->be_syncinfo );
3477 c->be->be_syncinfo = NULL;
3479 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3482 if ( SLAP_SHADOW( c->be ) ) {
3483 Debug(LDAP_DEBUG_ANY, "%s: "
3484 "syncrepl: database already shadowed.\n",
3487 } else if ( add_syncrepl( c ) ) {
3490 return config_sync_shadow( c );