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-2007 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 #define RETRYNUM_FOREVER (-1) /* retry forever */
48 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
49 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
50 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
52 typedef struct syncinfo_s {
53 struct slap_backend_db *si_be;
56 slap_bindconf si_bindconf;
57 struct berval si_base;
58 struct berval si_logbase;
59 struct berval si_filterstr;
60 struct berval si_logfilterstr;
64 AttributeName *si_anlist;
65 AttributeName *si_exanlist;
70 int si_schemachecking;
71 int si_type; /* the active type */
72 int si_ctype; /* the configured type */
74 time_t *si_retryinterval;
75 int *si_retrynum_init;
77 struct sync_cookie si_syncCookie;
82 int si_refreshPresent;
86 Avlnode *si_presentlist;
88 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
89 ldap_pvt_thread_mutex_t si_mutex;
92 static int syncuuid_cmp( const void *, const void * );
93 static void avl_ber_bvfree( void * );
94 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
95 static int syncrepl_message_to_op(
96 syncinfo_t *, Operation *, LDAPMessage * );
97 static int syncrepl_message_to_entry(
98 syncinfo_t *, Operation *, LDAPMessage *,
99 Modifications **, Entry **, int );
100 static int syncrepl_entry(
101 syncinfo_t *, Operation*, Entry*,
102 Modifications**,int, struct berval*,
103 struct sync_cookie *,
105 static void syncrepl_updateCookie(
106 syncinfo_t *, Operation *, struct berval *,
107 struct sync_cookie * );
108 static struct berval * slap_uuidstr_from_normalized(
109 struct berval *, struct berval *, void * );
111 /* callback functions */
112 static int dn_callback( struct slap_op *, struct slap_rep * );
113 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
114 static int null_callback( struct slap_op *, struct slap_rep * );
116 static AttributeDescription *sync_descs[4];
119 init_syncrepl(syncinfo_t *si)
122 char **attrs, **exattrs;
124 if ( !sync_descs[0] ) {
125 sync_descs[0] = slap_schema.si_ad_objectClass;
126 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
127 sync_descs[2] = slap_schema.si_ad_entryCSN;
128 sync_descs[3] = NULL;
131 if ( si->si_allattrs && si->si_allopattrs )
134 attrs = anlist2attrs( si->si_anlist );
137 if ( si->si_allattrs ) {
140 if ( !is_at_operational( at_find( attrs[i] ))) {
141 for ( j = i; attrs[j] != NULL; j++ ) {
144 attrs[j] = attrs[j+1];
150 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
151 attrs[i] = ch_strdup("*");
154 } else if ( si->si_allopattrs ) {
157 if ( is_at_operational( at_find( attrs[i] ))) {
158 for ( j = i; attrs[j] != NULL; j++ ) {
161 attrs[j] = attrs[j+1];
167 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
168 attrs[i] = ch_strdup("+");
172 for ( i = 0; sync_descs[i] != NULL; i++ ) {
175 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
176 for ( k = j; attrs[k] != NULL; k++ ) {
179 attrs[k] = attrs[k+1];
187 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
189 if ( si->si_allopattrs ) {
190 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
192 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
195 if ( attrs == NULL ) {
196 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
200 if ( si->si_allopattrs ) {
201 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
203 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
204 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
212 if ( si->si_allattrs == si->si_allopattrs ) {
213 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
214 attrs[i++] = ch_strdup( "*" );
215 attrs[i++] = ch_strdup( "+" );
216 } else if ( si->si_allattrs && !si->si_allopattrs ) {
217 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
218 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
219 attrs[i++] = ch_strdup( "*" );
220 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
221 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
223 } else if ( !si->si_allattrs && si->si_allopattrs ) {
224 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
225 attrs[i++] = ch_strdup( "+" );
226 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
231 si->si_attrs = attrs;
233 exattrs = anlist2attrs( si->si_exanlist );
236 for ( n = 0; exattrs[n] != NULL; n++ ) ;
238 for ( i = 0; sync_descs[i] != NULL; i++ ) {
240 while ( exattrs[j] != NULL ) {
241 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
242 ch_free( exattrs[j] );
243 for ( k = j; exattrs[k] != NULL; k++ ) {
244 exattrs[k] = exattrs[k+1];
252 for ( i = 0; exattrs[i] != NULL; i++ ) {
253 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
255 if ( ( oc = si->si_anlist[j].an_oc ) ) {
257 while ( oc->soc_required[k] ) {
258 if ( !strcmp( exattrs[i],
259 oc->soc_required[k]->sat_cname.bv_val )) {
260 ch_free( exattrs[i] );
261 for ( l = i; exattrs[l]; l++ ) {
262 exattrs[l] = exattrs[l+1];
272 for ( i = 0; exattrs[i] != NULL; i++ ) ;
275 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
278 si->si_exattrs = exattrs;
281 typedef struct logschema {
283 struct berval ls_req;
284 struct berval ls_mod;
285 struct berval ls_newRdn;
286 struct berval ls_delRdn;
287 struct berval ls_newSup;
290 static logschema changelog_sc = {
292 BER_BVC("changeType"),
295 BER_BVC("deleteOldRDN"),
296 BER_BVC("newSuperior")
299 static logschema accesslog_sc = {
303 BER_BVC("reqNewRDN"),
304 BER_BVC("reqDeleteOldRDN"),
305 BER_BVC("reqNewSuperior")
313 BerElementBuffer berbuf;
314 BerElement *ber = (BerElement *)&berbuf;
315 LDAPControl c[2], *ctrls[3];
316 struct timeval timeout;
321 char **attrs, *lattrs[8];
326 /* setup LDAP SYNC control */
327 ber_init2( ber, NULL, LBER_USE_DER );
328 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
330 /* If we're using a log but we have no state, then fallback to
331 * normal mode for a full refresh.
333 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
334 si->si_logstate = SYNCLOG_FALLBACK;
336 /* Use the log parameters if we're in log mode */
337 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
339 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
343 lattrs[0] = ls->ls_dn.bv_val;
344 lattrs[1] = ls->ls_req.bv_val;
345 lattrs[2] = ls->ls_mod.bv_val;
346 lattrs[3] = ls->ls_newRdn.bv_val;
347 lattrs[4] = ls->ls_delRdn.bv_val;
348 lattrs[5] = ls->ls_newSup.bv_val;
349 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
353 base = si->si_logbase.bv_val;
354 filter = si->si_logfilterstr.bv_val;
357 scope = LDAP_SCOPE_SUBTREE;
360 base = si->si_base.bv_val;
361 filter = si->si_filterstr.bv_val;
362 attrs = si->si_attrs;
363 attrsonly = si->si_attrsonly;
364 scope = si->si_scope;
366 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
367 si->si_type = LDAP_SYNC_REFRESH_ONLY;
369 si->si_type = si->si_ctype;
372 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
374 ber_printf( ber, "{eOb}",
375 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
377 ber_printf( ber, "{eb}",
378 abs(si->si_type), rhint );
381 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
386 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
387 c[0].ldctl_iscritical = si->si_type < 0;
390 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
391 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
392 c[1].ldctl_value = si->si_bindconf.sb_authzId;
393 c[1].ldctl_iscritical = 1;
400 timeout.tv_sec = si->si_tlimit;
403 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
404 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
405 si->si_slimit, &msgid );
416 int cmdline_cookie_found = 0;
418 struct sync_cookie *sc = NULL;
424 psub = &si->si_be->be_nsuffix[0];
426 /* Init connection to master */
427 rc = ldap_initialize( &si->si_ld, si->si_bindconf.sb_uri.bv_val );
428 if ( rc != LDAP_SUCCESS ) {
429 Debug( LDAP_DEBUG_ANY,
430 "do_syncrep1: rid %03d ldap_initialize failed (%s)\n",
431 si->si_rid, si->si_bindconf.sb_uri.bv_val, 0 );
435 op->o_protocol = LDAP_VERSION3;
436 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
440 if ( si->si_bindconf.sb_tls ) {
441 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
442 if( rc != LDAP_SUCCESS ) {
443 Debug( LDAP_DEBUG_ANY,
444 "%s: rid %03d ldap_start_tls failed (%d)\n",
445 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
447 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
451 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
452 #ifdef HAVE_CYRUS_SASL
455 if ( si->si_bindconf.sb_secprops != NULL ) {
456 rc = ldap_set_option( si->si_ld,
457 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
459 if( rc != LDAP_OPT_SUCCESS ) {
460 Debug( LDAP_DEBUG_ANY, "Error: rid %03d ldap_set_option "
461 "(%s,SECPROPS,\"%s\") failed!\n",
462 si->si_rid, si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops );
467 defaults = lutil_sasl_defaults( si->si_ld,
468 si->si_bindconf.sb_saslmech.bv_val,
469 si->si_bindconf.sb_realm.bv_val,
470 si->si_bindconf.sb_authcId.bv_val,
471 si->si_bindconf.sb_cred.bv_val,
472 si->si_bindconf.sb_authzId.bv_val );
474 rc = ldap_sasl_interactive_bind_s( si->si_ld,
475 si->si_bindconf.sb_binddn.bv_val,
476 si->si_bindconf.sb_saslmech.bv_val,
482 lutil_sasl_freedefs( defaults );
484 /* FIXME: different error behaviors according to
486 * 2) on err policy : exit, retry, backoff ...
488 if ( rc != LDAP_SUCCESS ) {
489 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
491 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
492 "ldap_sasl_interactive_bind_s failed (%d)\n",
495 /* FIXME (see above comment) */
496 /* if Kerberos credentials cache is not active, retry */
497 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
498 rc == LDAP_LOCAL_ERROR )
500 rc = LDAP_SERVER_DOWN;
505 #else /* HAVE_CYRUS_SASL */
506 /* Should never get here, we trapped this at config time */
508 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
513 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
514 rc = ldap_sasl_bind_s( si->si_ld,
515 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
516 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
517 if ( rc != LDAP_SUCCESS ) {
518 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
519 "ldap_sasl_bind_s failed (%d)\n", si->si_rid, rc, 0 );
524 /* Set SSF to strongest of TLS, SASL SSFs */
527 op->o_transport_ssf = 0;
529 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
530 == LDAP_SUCCESS && ssl != NULL )
532 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
534 #endif /* HAVE_TLS */
535 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
536 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
537 ? op->o_sasl_ssf : op->o_tls_ssf;
540 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
541 /* get contextCSN shadow replica from database */
542 BerVarray csn = NULL;
544 assert( si->si_rid < 1000 );
545 op->o_req_ndn = op->o_bd->be_nsuffix[0];
546 op->o_req_dn = op->o_req_ndn;
548 /* try to read stored contextCSN */
549 backend_attribute( op, NULL, &op->o_req_ndn,
550 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
552 ch_free( si->si_syncCookie.ctxcsn.bv_val );
553 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
554 ber_bvarray_free_x( csn, op->o_tmpmemctx );
557 si->si_syncCookie.rid = si->si_rid;
559 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
560 if ( si->si_rid == sc->rid ) {
561 cmdline_cookie_found = 1;
566 if ( cmdline_cookie_found ) {
567 /* cookie is supplied in the command line */
569 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
571 /* ctxcsn wasn't parsed yet, do it now */
572 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
573 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
574 /* if cmdline cookie does not have ctxcsn */
575 /* component, set it to an initial value */
576 slap_init_sync_cookie_ctxcsn( sc );
578 slap_sync_cookie_free( &si->si_syncCookie, 0 );
579 slap_dup_sync_cookie( &si->si_syncCookie, sc );
580 slap_sync_cookie_free( sc, 1 );
583 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
584 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
587 rc = ldap_sync_search( si, op->o_tmpmemctx );
589 if( rc != LDAP_SUCCESS ) {
590 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
591 "ldap_search_ext: %s (%d)\n",
592 si->si_rid, ldap_err2string( rc ), rc );
598 ldap_unbind_ext( si->si_ld, NULL, NULL );
611 LDAPControl **rctrls = NULL;
614 BerElementBuffer berbuf;
615 BerElement *ber = (BerElement *)&berbuf;
617 LDAPMessage *res = NULL;
618 LDAPMessage *msg = NULL;
621 struct berval *retdata = NULL;
626 struct berval syncUUID = BER_BVNULL;
627 struct sync_cookie syncCookie = { BER_BVNULL };
628 struct sync_cookie syncCookie_req = { BER_BVNULL };
629 struct berval cookie = BER_BVNULL;
637 Modifications *modlist = NULL;
642 struct timeval *tout_p = NULL;
643 struct timeval tout = { 0, 0 };
645 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, LDAP_RES_ANY, 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 );
719 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
722 if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
723 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
724 syncrepl_updateCookie( si, op, psub, &syncCookie );
726 } else if ( syncrepl_message_to_entry( si, op, msg,
727 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
728 rc_efree = syncrepl_entry( si, op, entry, &modlist,
729 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
730 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
732 syncrepl_updateCookie( si, op, psub, &syncCookie );
735 ldap_controls_free( rctrls );
737 slap_mods_free( modlist, 1 );
739 if ( rc_efree && entry ) {
745 case LDAP_RES_SEARCH_REFERENCE:
746 Debug( LDAP_DEBUG_ANY,
747 "do_syncrep2: rid %03d reference received error\n",
751 case LDAP_RES_SEARCH_RESULT:
752 Debug( LDAP_DEBUG_SYNC,
753 "do_syncrep2: rid %03d LDAP_RES_SEARCH_RESULT\n",
755 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
757 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
758 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
759 /* map old result code to registered code */
760 err = LDAP_SYNC_REFRESH_REQUIRED;
763 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
764 if ( si->si_logstate == SYNCLOG_LOGGING ) {
765 si->si_logstate = SYNCLOG_FALLBACK;
772 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
774 ber_scanf( ber, "{" /*"}"*/);
775 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
776 ber_scanf( ber, "m", &cookie );
777 if ( !BER_BVISNULL( &cookie ) ) {
778 ch_free( syncCookie.octet_str.bv_val );
779 ber_dupbv( &syncCookie.octet_str, &cookie);
781 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
783 slap_parse_sync_cookie( &syncCookie, NULL );
786 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
788 ber_scanf( ber, "b", &refreshDeletes );
790 ber_scanf( ber, /*"{"*/ "}" );
792 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
794 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
797 value_match( &match, slap_schema.si_ad_entryCSN,
798 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
799 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
800 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
804 ldap_controls_free( rctrls );
806 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
807 /* FIXME : different error behaviors according to
808 * 1) err code : LDAP_BUSY ...
809 * 2) on err policy : stop service, stop sync, retry
811 if ( refreshDeletes == 0 && match < 0 &&
812 err == LDAP_SUCCESS )
814 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
816 avl_free( si->si_presentlist, avl_ber_bvfree );
817 si->si_presentlist = NULL;
820 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
821 match < 0 && err == LDAP_SUCCESS )
823 syncrepl_updateCookie( si, op, psub, &syncCookie );
825 if ( err == LDAP_SUCCESS
826 && si->si_logstate == SYNCLOG_FALLBACK ) {
827 si->si_logstate = SYNCLOG_LOGGING;
828 rc = LDAP_SYNC_REFRESH_REQUIRED;
835 case LDAP_RES_INTERMEDIATE:
836 rc = ldap_parse_intermediate( si->si_ld, msg,
837 &retoid, &retdata, NULL, 0 );
838 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
839 ber_init2( ber, retdata, LBER_USE_DER );
841 switch ( si_tag = ber_peek_tag( ber, &len )) {
843 case LDAP_TAG_SYNC_NEW_COOKIE:
844 Debug( LDAP_DEBUG_SYNC,
845 "do_syncrep2: rid %03d %s - %s\n",
847 "LDAP_RES_INTERMEDIATE",
849 ber_scanf( ber, "tm", &tag, &cookie );
851 case LDAP_TAG_SYNC_REFRESH_DELETE:
852 case LDAP_TAG_SYNC_REFRESH_PRESENT:
853 Debug( LDAP_DEBUG_SYNC,
854 "do_syncrep2: rid %03d %s - %s\n",
856 "LDAP_RES_INTERMEDIATE",
857 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
858 "REFRESH_PRESENT" : "REFRESH_DELETE" );
859 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
860 si->si_refreshDelete = 1;
862 si->si_refreshPresent = 1;
864 ber_scanf( ber, "t{" /*"}"*/, &tag );
865 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
867 ber_scanf( ber, "m", &cookie );
868 if ( !BER_BVISNULL( &cookie ) ) {
869 ch_free( syncCookie.octet_str.bv_val );
870 ber_dupbv( &syncCookie.octet_str, &cookie );
872 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
874 slap_parse_sync_cookie( &syncCookie, NULL );
877 if ( ber_peek_tag( ber, &len ) ==
878 LDAP_TAG_REFRESHDONE )
880 ber_scanf( ber, "b", &refreshDone );
882 ber_scanf( ber, /*"{"*/ "}" );
884 case LDAP_TAG_SYNC_ID_SET:
885 Debug( LDAP_DEBUG_SYNC,
886 "do_syncrep2: rid %03d %s - %s\n",
888 "LDAP_RES_INTERMEDIATE",
890 ber_scanf( ber, "t{" /*"}"*/, &tag );
891 if ( ber_peek_tag( ber, &len ) ==
892 LDAP_TAG_SYNC_COOKIE )
894 ber_scanf( ber, "m", &cookie );
895 if ( !BER_BVISNULL( &cookie ) ) {
896 ch_free( syncCookie.octet_str.bv_val );
897 ber_dupbv( &syncCookie.octet_str, &cookie );
899 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
901 slap_parse_sync_cookie( &syncCookie, NULL );
904 if ( ber_peek_tag( ber, &len ) ==
905 LDAP_TAG_REFRESHDELETES )
907 ber_scanf( ber, "b", &refreshDeletes );
909 ber_scanf( ber, "[W]", &syncUUIDs );
910 ber_scanf( ber, /*"{"*/ "}" );
911 if ( refreshDeletes ) {
912 syncrepl_del_nonpresent( op, si, syncUUIDs,
913 &syncCookie.ctxcsn );
914 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
916 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
917 struct berval *syncuuid_bv;
918 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
919 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
920 avl_insert( &si->si_presentlist,
921 (caddr_t) syncuuid_bv,
922 syncuuid_cmp, avl_dup_error );
924 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
926 slap_sync_cookie_free( &syncCookie, 0 );
929 Debug( LDAP_DEBUG_ANY,
930 "do_syncrep2: rid %03d unknown syncinfo tag (%ld)\n",
931 si->si_rid, (long) si_tag, 0 );
932 ldap_memfree( retoid );
933 ber_bvfree( retdata );
937 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
939 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
942 value_match( &match, slap_schema.si_ad_entryCSN,
943 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
944 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
945 &syncCookie_req.ctxcsn,
946 &syncCookie.ctxcsn, &text );
950 if ( si->si_refreshPresent == 1 ) {
951 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
953 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
955 syncrepl_updateCookie( si, op, psub, &syncCookie);
959 ldap_memfree( retoid );
960 ber_bvfree( retdata );
964 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
965 "unknown intermediate response (%d)\n",
967 ldap_memfree( retoid );
968 ber_bvfree( retdata );
974 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
975 "unknown message\n", si->si_rid, 0, 0 );
979 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
980 slap_sync_cookie_free( &syncCookie_req, 0 );
981 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
982 slap_sync_cookie_free( &syncCookie, 0 );
992 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
993 errstr = ldap_err2string( rc );
995 Debug( LDAP_DEBUG_ANY,
996 "do_syncrep2: rid %03d %s\n", si->si_rid, errstr, 0 );
1000 slap_sync_cookie_free( &syncCookie, 0 );
1001 slap_sync_cookie_free( &syncCookie_req, 0 );
1003 if ( res ) ldap_msgfree( res );
1005 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1006 if ( si->si_conn_setup ) {
1008 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1009 connection_client_stop( s );
1010 si->si_conn_setup = 0;
1012 ldap_unbind_ext( si->si_ld, NULL, NULL );
1024 struct re_s* rtask = arg;
1025 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1026 Connection conn = {0};
1027 OperationBuffer opbuf;
1029 int rc = LDAP_SUCCESS;
1035 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl rid %03d\n", si->si_rid, 0, 0 );
1040 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1042 switch( abs( si->si_type )) {
1043 case LDAP_SYNC_REFRESH_ONLY:
1044 case LDAP_SYNC_REFRESH_AND_PERSIST:
1047 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1051 if ( slapd_shutdown ) {
1053 if ( si->si_conn_setup ) {
1054 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1055 connection_client_stop( s );
1056 si->si_conn_setup = 0;
1058 ldap_unbind_ext( si->si_ld, NULL, NULL );
1061 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1065 op = (Operation *) &opbuf;
1066 connection_fake_init( &conn, op, ctx );
1068 /* use global malloc for now */
1069 op->o_tmpmemctx = NULL;
1070 op->o_tmpmfuncs = &ch_mfuncs;
1072 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1073 op->o_bd = be = si->si_be;
1074 op->o_dn = op->o_bd->be_rootdn;
1075 op->o_ndn = op->o_bd->be_rootndn;
1077 /* Establish session, do search */
1079 si->si_refreshDelete = 0;
1080 si->si_refreshPresent = 0;
1081 rc = do_syncrep1( op, si );
1085 /* Process results */
1086 if ( rc == LDAP_SUCCESS ) {
1087 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1089 rc = do_syncrep2( op, si );
1090 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1091 rc = ldap_sync_search( si, op->o_tmpmemctx );
1095 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1096 /* If we succeeded, enable the connection for further listening.
1097 * If we failed, tear down the connection and reschedule.
1099 if ( rc == LDAP_SUCCESS ) {
1100 if ( si->si_conn_setup ) {
1101 connection_client_enable( s );
1103 rc = connection_client_setup( s, do_syncrepl, arg );
1105 si->si_conn_setup = 1;
1107 } else if ( si->si_conn_setup ) {
1111 if ( rc == -2 ) rc = 0;
1115 /* At this point, we have 4 cases:
1116 * 1) for any hard failure, give up and remove this task
1117 * 2) for ServerDown, reschedule this task to run
1118 * 3) for Refresh and Success, reschedule to run
1119 * 4) for Persist and Success, reschedule to defer
1121 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1123 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1124 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1128 connection_client_stop( s );
1131 if ( rc == LDAP_SUCCESS ) {
1132 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1135 rtask->interval.tv_sec = si->si_interval;
1136 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1137 if ( si->si_retrynum ) {
1138 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1139 si->si_retrynum[i] = si->si_retrynum_init[i];
1141 si->si_retrynum[i] = RETRYNUM_TAIL;
1144 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1145 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1149 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1150 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1151 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1152 if ( si->si_retrynum[i] > 0 )
1153 si->si_retrynum[i]--;
1154 rtask->interval.tv_sec = si->si_retryinterval[i];
1155 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1156 slap_wake_listener();
1160 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1161 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1166 static slap_verbmasks modops[] = {
1167 { BER_BVC("add"), LDAP_REQ_ADD },
1168 { BER_BVC("delete"), LDAP_REQ_DELETE },
1169 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1170 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1174 static Modifications *
1175 syncrepl_accesslog_mods(
1182 AttributeDescription *ad;
1183 struct berval bv, bv2;
1185 Modifications *mod = NULL, *modlist = NULL, **modtail;
1190 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1194 colon = ber_bvchr( &bv, ':' );
1196 continue; /* invalid */
1197 bv.bv_len = colon - bv.bv_val;
1198 if ( slap_bv2ad( &bv, &ad, &text )) {
1202 /* Ignore dynamically generated attrs */
1203 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1205 /* Ignore excluded attrs */
1206 if ( ldap_charray_inlist( si->si_exattrs,
1207 ad->ad_type->sat_cname.bv_val ))
1211 case '+': op = LDAP_MOD_ADD; break;
1212 case '-': op = LDAP_MOD_DELETE; break;
1213 case '=': op = LDAP_MOD_REPLACE; break;
1214 case '#': op = LDAP_MOD_INCREMENT; break;
1218 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1219 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1222 mod->sml_next = NULL;
1224 mod->sml_type = ad->ad_cname;
1225 mod->sml_values = NULL;
1226 mod->sml_nvalues = NULL;
1229 modtail = &mod->sml_next;
1231 if ( colon[2] == ' ' ) {
1232 bv.bv_val = colon + 3;
1233 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1234 ber_dupbv( &bv2, &bv );
1235 ber_bvarray_add( &mod->sml_values, &bv2 );
1241 static Modifications *
1242 syncrepl_changelog_mods(
1247 return NULL; /* FIXME */
1251 syncrepl_message_to_op(
1257 BerElement *ber = NULL;
1258 Modifications *modlist = NULL;
1260 SlapReply rs = { REP_RESULT };
1261 slap_callback cb = { NULL, null_callback, NULL, NULL };
1264 char txtbuf[SLAP_TEXT_BUFLEN];
1265 size_t textlen = sizeof txtbuf;
1267 struct berval bdn, dn = BER_BVNULL, ndn;
1268 struct berval bv, *bvals = NULL;
1269 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1270 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1271 psup = BER_BVNULL, nsup = BER_BVNULL;
1272 int rc, deleteOldRdn = 0, freeReqDn = 0;
1274 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1275 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1276 "Message type should be entry (%d)",
1277 si->si_rid, ldap_msgtype( msg ), 0 );
1281 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1286 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1288 if ( rc != LDAP_SUCCESS ) {
1289 Debug( LDAP_DEBUG_ANY,
1290 "syncrepl_message_to_op: rid %03d dn get failed (%d)",
1291 si->si_rid, rc, 0 );
1295 op->o_tag = LBER_DEFAULT;
1297 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1299 if ( bv.bv_val == NULL )
1302 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1304 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1305 ber_dupbv( &op->o_req_dn, &dn );
1306 ber_dupbv( &op->o_req_ndn, &ndn );
1307 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1308 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1310 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1311 int i = verb_to_mask( bvals[0].bv_val, modops );
1313 Debug( LDAP_DEBUG_ANY,
1314 "syncrepl_message_to_op: rid %03d unknown op %s",
1315 si->si_rid, bvals[0].bv_val, 0 );
1320 op->o_tag = modops[i].mask;
1321 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1322 /* Parse attribute into modlist */
1323 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1324 modlist = syncrepl_accesslog_mods( si, bvals );
1326 modlist = syncrepl_changelog_mods( si, bvals );
1327 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1329 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1330 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1332 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1334 } else if ( !ber_bvstrcasecmp( &bv,
1335 &slap_schema.si_ad_entryCSN->ad_cname )) {
1336 slap_queue_csn( op, bvals );
1341 /* If we didn't get a mod type or a target DN, bail out */
1342 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1347 op->o_callback = &cb;
1348 slap_op_time( &op->o_time, &op->o_tincr );
1350 switch( op->o_tag ) {
1352 case LDAP_REQ_MODIFY:
1353 /* If we didn't get required data, bail */
1354 if ( !modlist ) goto done;
1356 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1358 if ( rc != LDAP_SUCCESS ) {
1359 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1360 "mods check (%s)\n",
1361 si->si_rid, text, 0 );
1365 if ( op->o_tag == LDAP_REQ_ADD ) {
1366 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1367 op->ora_e->e_name = op->o_req_dn;
1368 op->ora_e->e_nname = op->o_req_ndn;
1370 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1371 if( rc != LDAP_SUCCESS ) {
1372 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1373 "mods2entry (%s)\n",
1374 si->si_rid, text, 0 );
1376 rc = op->o_bd->be_add( op, &rs );
1377 Debug( LDAP_DEBUG_SYNC,
1378 "syncrepl_message_to_op: rid %03d be_add %s (%d)\n",
1379 si->si_rid, op->o_req_dn.bv_val, rc );
1381 be_entry_release_w( op, op->ora_e );
1383 op->orm_modlist = modlist;
1384 rc = op->o_bd->be_modify( op, &rs );
1385 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1386 "syncrepl_message_to_op: rid %03d be_modify %s (%d)\n",
1387 si->si_rid, op->o_req_dn.bv_val, rc );
1390 case LDAP_REQ_MODRDN:
1391 if ( BER_BVISNULL( &rdn )) goto done;
1393 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1395 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1397 if ( !BER_BVISNULL( &sup )) {
1398 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1400 op->orr_newSup = &psup;
1401 op->orr_nnewSup = ⊅
1403 op->orr_newSup = NULL;
1404 op->orr_nnewSup = NULL;
1406 op->orr_newrdn = prdn;
1407 op->orr_nnewrdn = nrdn;
1408 op->orr_deleteoldrdn = deleteOldRdn;
1409 rc = op->o_bd->be_modrdn( op, &rs );
1410 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1411 "syncrepl_message_to_op: rid %03d be_modrdn %s (%d)\n",
1412 si->si_rid, op->o_req_dn.bv_val, rc );
1414 case LDAP_REQ_DELETE:
1415 rc = op->o_bd->be_delete( op, &rs );
1416 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1417 "syncrepl_message_to_op: rid %03d be_delete %s (%d)\n",
1418 si->si_rid, op->o_req_dn.bv_val, rc );
1422 slap_graduate_commit_csn( op );
1423 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1424 BER_BVZERO( &op->o_csn );
1426 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1427 if ( !BER_BVISNULL( &rdn )) {
1428 if ( !BER_BVISNULL( &nsup ))
1429 ch_free( nsup.bv_val );
1430 if ( !BER_BVISNULL( &psup ))
1431 ch_free( psup.bv_val );
1432 if ( !BER_BVISNULL( &nrdn ))
1433 ch_free( nrdn.bv_val );
1434 if ( !BER_BVISNULL( &prdn ))
1435 ch_free( prdn.bv_val );
1438 ch_free( op->o_req_ndn.bv_val );
1439 ch_free( op->o_req_dn.bv_val );
1441 ber_free ( ber, 0 );
1446 syncrepl_message_to_entry(
1450 Modifications **modlist,
1456 BerElement *ber = NULL;
1459 Modifications **modtail = modlist;
1462 char txtbuf[SLAP_TEXT_BUFLEN];
1463 size_t textlen = sizeof txtbuf;
1465 struct berval bdn = BER_BVNULL, dn, ndn;
1470 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1471 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d "
1472 "Message type should be entry (%d)",
1473 si->si_rid, ldap_msgtype( msg ), 0 );
1477 op->o_tag = LDAP_REQ_ADD;
1479 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1480 if ( rc != LDAP_SUCCESS ) {
1481 Debug( LDAP_DEBUG_ANY,
1482 "syncrepl_message_to_entry: rid %03d dn get failed (%d)",
1483 si->si_rid, rc, 0 );
1487 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1488 ber_dupbv( &op->o_req_dn, &dn );
1489 ber_dupbv( &op->o_req_ndn, &ndn );
1490 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1491 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1493 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1494 /* NOTE: this could be done even before decoding the DN,
1495 * although encoding errors wouldn't be detected */
1500 if ( entry == NULL ) {
1505 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1507 e->e_name = op->o_req_dn;
1508 e->e_nname = op->o_req_ndn;
1510 while ( ber_remaining( ber ) ) {
1511 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1512 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1517 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1519 mod->sml_op = LDAP_MOD_REPLACE;
1521 mod->sml_next = NULL;
1522 mod->sml_desc = NULL;
1523 mod->sml_type = tmp.sml_type;
1524 mod->sml_values = tmp.sml_values;
1525 mod->sml_nvalues = NULL;
1528 modtail = &mod->sml_next;
1531 if ( *modlist == NULL ) {
1532 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d no attributes\n",
1538 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1540 if ( rc != LDAP_SUCCESS ) {
1541 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods check (%s)\n",
1542 si->si_rid, text, 0 );
1546 /* Strip out dynamically generated attrs */
1547 for ( modtail = modlist; *modtail ; ) {
1549 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1550 *modtail = mod->sml_next;
1551 slap_mod_free( &mod->sml_mod, 0 );
1554 modtail = &mod->sml_next;
1558 /* Strip out attrs in exattrs list */
1559 for ( modtail = modlist; *modtail ; ) {
1561 if ( ldap_charray_inlist( si->si_exattrs,
1562 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1563 *modtail = mod->sml_next;
1564 slap_mod_free( &mod->sml_mod, 0 );
1567 modtail = &mod->sml_next;
1571 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1572 if( rc != LDAP_SUCCESS ) {
1573 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods2entry (%s)\n",
1574 si->si_rid, text, 0 );
1579 if ( rc != LDAP_SUCCESS ) {
1589 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1591 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1592 * entry if a previous refresh was interrupted before sending us a new
1593 * context state. We try to compare the new entry to the existing entry
1594 * and ignore the new entry if they are the same.
1596 * Also, we may get an update where the entryDN has changed, due to
1597 * a ModDn on the provider. We detect this as well, so we can issue
1598 * the corresponding operation locally.
1600 * In the case of a modify, we get a list of all the attributes
1601 * in the original entry. Rather than deleting the entry and re-adding it,
1602 * we issue a Modify request that deletes all the attributes and adds all
1603 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1606 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1607 * a ModDN we will issue both operations on the local database.
1609 typedef struct dninfo {
1613 int renamed; /* Was an existing entry renamed? */
1614 int wasChanged; /* are the attributes changed? */
1615 int attrs; /* how many attribute types are in the ads list */
1616 AttributeDescription **ads;
1624 Modifications** modlist,
1626 struct berval* syncUUID,
1627 struct sync_cookie* syncCookie_req,
1628 struct berval* syncCSN )
1630 Backend *be = op->o_bd;
1631 slap_callback cb = { NULL, NULL, NULL, NULL };
1632 struct berval *syncuuid_bv = NULL;
1633 struct berval syncUUID_strrep = BER_BVNULL;
1634 struct berval uuid_bv = BER_BVNULL;
1636 SlapReply rs_search = {REP_RESULT};
1637 SlapReply rs_delete = {REP_RESULT};
1638 SlapReply rs_add = {REP_RESULT};
1639 SlapReply rs_modify = {REP_RESULT};
1641 #ifdef LDAP_COMP_MATCH
1642 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1644 AttributeAssertion ava = { NULL, BER_BVNULL };
1646 int rc = LDAP_SUCCESS;
1647 int ret = LDAP_SUCCESS;
1649 struct berval pdn = BER_BVNULL;
1653 switch( syncstate ) {
1654 case LDAP_SYNC_PRESENT:
1655 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1657 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1660 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1662 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1664 case LDAP_SYNC_DELETE:
1665 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1667 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1669 case LDAP_SYNC_MODIFY:
1670 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1672 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1675 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: rid %03d %s\n",
1677 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1680 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1681 if ( !si->si_refreshPresent ) {
1682 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1683 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1684 syncuuid_cmp, avl_dup_error );
1688 if ( syncstate == LDAP_SYNC_PRESENT ) {
1690 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1691 if ( entry == NULL ) {
1696 f.f_choice = LDAP_FILTER_EQUALITY;
1698 ava.aa_desc = slap_schema.si_ad_entryUUID;
1699 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1700 ava.aa_value = *syncUUID;
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: rid %03d be_search (%d)\n",
1737 si->si_rid, 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: rid %03d %s\n",
1750 si->si_rid, entry->e_name.bv_val, 0 );
1752 Debug( LDAP_DEBUG_SYNC,
1753 "syncrepl_entry: rid %03d %s\n",
1754 si->si_rid, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 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 );
1774 /* Don't save the contextCSN on the inooming context entry,
1775 * we'll write it when syncrepl_updateCookie eventually
1776 * gets called. (ITS#4622)
1778 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1779 &be->be_nsuffix[0] )) {
1781 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1783 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1792 slap_op_time( &op->o_time, &op->o_tincr );
1793 switch ( syncstate ) {
1795 case LDAP_SYNC_MODIFY:
1797 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1799 op->o_csn = a->a_vals[0];
1802 if ( BER_BVISNULL( &dni.dn )) {
1804 op->o_req_dn = entry->e_name;
1805 op->o_req_ndn = entry->e_nname;
1806 op->o_tag = LDAP_REQ_ADD;
1809 rc = be->be_add( op, &rs_add );
1810 Debug( LDAP_DEBUG_SYNC,
1811 "syncrepl_entry: rid %03d be_add (%d)\n",
1812 si->si_rid, rc, 0 );
1813 switch ( rs_add.sr_err ) {
1815 be_entry_release_w( op, entry );
1820 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1821 * only if the suffix entry is not present */
1822 case LDAP_NO_SUCH_OBJECT:
1823 syncrepl_add_glue( op, entry );
1827 /* if an entry was added via syncrepl_add_glue(),
1828 * it likely has no entryUUID, so the previous
1829 * be_search() doesn't find it. In this case,
1830 * give syncrepl a chance to modify it. Also
1831 * allow for entries that were recreated with the
1832 * same DN but a different entryUUID.
1834 case LDAP_ALREADY_EXISTS:
1836 Operation op2 = *op;
1837 SlapReply rs2 = { 0 };
1838 slap_callback cb2 = { 0 };
1840 op2.o_tag = LDAP_REQ_SEARCH;
1841 op2.o_req_dn = entry->e_name;
1842 op2.o_req_ndn = entry->e_nname;
1843 op2.ors_scope = LDAP_SCOPE_BASE;
1844 op2.ors_deref = LDAP_DEREF_NEVER;
1845 op2.ors_attrs = slap_anlist_all_attributes;
1846 op2.ors_attrsonly = 0;
1847 op2.ors_limit = NULL;
1849 op2.ors_tlimit = SLAP_NO_LIMIT;
1851 f.f_choice = LDAP_FILTER_PRESENT;
1852 f.f_desc = slap_schema.si_ad_objectClass;
1853 op2.ors_filter = &f;
1854 op2.ors_filterstr = generic_filterstr;
1856 op2.o_callback = &cb2;
1857 cb2.sc_response = dn_callback;
1858 cb2.sc_private = &dni;
1860 be->be_search( &op2, &rs2 );
1863 slap_op_time( &op->o_time, &op->o_tincr );
1869 Debug( LDAP_DEBUG_ANY,
1870 "syncrepl_entry: rid %03d be_add failed (%d)\n",
1871 si->si_rid, rs_add.sr_err, 0 );
1878 op->o_req_dn = dni.dn;
1879 op->o_req_ndn = dni.ndn;
1880 if ( dni.renamed ) {
1881 struct berval noldp, newp, nnewp;
1883 op->o_tag = LDAP_REQ_MODRDN;
1884 dnRdn( &entry->e_name, &op->orr_newrdn );
1885 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1887 dnParent( &dni.ndn, &noldp );
1888 dnParent( &entry->e_nname, &nnewp );
1889 if ( !dn_match( &noldp, &nnewp )) {
1890 dnParent( &entry->e_name, &newp );
1891 op->orr_newSup = &newp;
1892 op->orr_nnewSup = &nnewp;
1894 op->orr_newSup = NULL;
1895 op->orr_nnewSup = NULL;
1897 op->orr_deleteoldrdn = 0;
1898 rc = be->be_modrdn( op, &rs_modify );
1899 Debug( LDAP_DEBUG_SYNC,
1900 "syncrepl_entry: rid %03d be_modrdn (%d)\n",
1901 si->si_rid, rc, 0 );
1902 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1903 op->o_req_dn = entry->e_name;
1904 op->o_req_ndn = entry->e_nname;
1909 if ( dni.wasChanged )
1910 slap_op_time( &op->o_time, &op->o_tincr );
1912 if ( dni.wasChanged ) {
1913 Modifications *mod, *modhead = NULL;
1914 Modifications *modtail = NULL;
1917 op->o_tag = LDAP_REQ_MODIFY;
1919 assert( *modlist != NULL );
1921 /* Delete all the old attrs */
1922 for ( i = 0; i < dni.attrs; i++ ) {
1923 mod = ch_malloc( sizeof( Modifications ) );
1924 mod->sml_op = LDAP_MOD_DELETE;
1926 mod->sml_desc = dni.ads[i];
1927 mod->sml_type = mod->sml_desc->ad_cname;
1928 mod->sml_values = NULL;
1929 mod->sml_nvalues = NULL;
1930 if ( !modhead ) modhead = mod;
1932 modtail->sml_next = mod;
1937 /* Append passed in list to ours */
1939 modtail->sml_next = *modlist;
1945 /* Find end of this list */
1946 for ( ; mod != NULL; mod = mod->sml_next ) {
1950 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1951 mod->sml_op = LDAP_MOD_REPLACE;
1953 mod->sml_desc = slap_schema.si_ad_entryUUID;
1954 mod->sml_type = mod->sml_desc->ad_cname;
1955 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1956 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1957 ber_dupbv( &uuid_bv, syncUUID );
1958 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1959 modtail->sml_next = mod;
1961 op->o_tag = LDAP_REQ_MODIFY;
1962 op->orm_modlist = *modlist;
1964 rc = be->be_modify( op, &rs_modify );
1965 Debug( LDAP_DEBUG_SYNC,
1966 "syncrepl_entry: rid %03d be_modify (%d)\n",
1967 si->si_rid, rc, 0 );
1968 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1969 Debug( LDAP_DEBUG_ANY,
1970 "syncrepl_entry: rid %03d be_modify failed (%d)\n",
1971 si->si_rid, rs_modify.sr_err, 0 );
1976 case LDAP_SYNC_DELETE :
1977 if ( !BER_BVISNULL( &dni.dn )) {
1978 op->o_req_dn = dni.dn;
1979 op->o_req_ndn = dni.ndn;
1980 op->o_tag = LDAP_REQ_DELETE;
1981 rc = be->be_delete( op, &rs_delete );
1982 Debug( LDAP_DEBUG_SYNC,
1983 "syncrepl_entry: rid %03d be_delete (%d)\n",
1984 si->si_rid, rc, 0 );
1986 while ( rs_delete.sr_err == LDAP_SUCCESS
1987 && op->o_delete_glue_parent ) {
1988 op->o_delete_glue_parent = 0;
1989 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1990 slap_callback cb = { NULL };
1991 cb.sc_response = slap_null_cb;
1992 dnParent( &op->o_req_ndn, &pdn );
1994 op->o_req_ndn = pdn;
1995 op->o_callback = &cb;
1996 op->o_bd->be_delete( op, &rs_delete );
2006 Debug( LDAP_DEBUG_ANY,
2007 "syncrepl_entry: rid %03d unknown syncstate\n", si->si_rid, 0, 0 );
2013 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2014 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2015 BER_BVZERO( &syncUUID_strrep );
2018 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2020 if ( !BER_BVISNULL( &dni.ndn ) ) {
2021 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2023 if ( !BER_BVISNULL( &dni.dn ) ) {
2024 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2026 BER_BVZERO( &op->o_csn );
2030 static struct berval gcbva[] = {
2036 #define NP_DELETE_ONE 2
2039 syncrepl_del_nonpresent(
2043 struct berval *cookiecsn )
2045 Backend* be = op->o_bd;
2046 slap_callback cb = { NULL };
2047 SlapReply rs_search = {REP_RESULT};
2048 SlapReply rs_delete = {REP_RESULT};
2049 SlapReply rs_modify = {REP_RESULT};
2050 struct nonpresent_entry *np_list, *np_prev;
2052 AttributeName an[2];
2054 struct berval pdn = BER_BVNULL;
2057 op->o_req_dn = si->si_base;
2058 op->o_req_ndn = si->si_base;
2060 cb.sc_response = nonpresent_callback;
2063 op->o_callback = &cb;
2064 op->o_tag = LDAP_REQ_SEARCH;
2065 op->ors_scope = si->si_scope;
2066 op->ors_deref = LDAP_DEREF_NEVER;
2067 op->o_time = slap_get_time();
2068 op->ors_tlimit = SLAP_NO_LIMIT;
2073 #ifdef LDAP_COMP_MATCH
2074 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2076 AttributeAssertion eq = { NULL, BER_BVNULL };
2080 op->ors_attrsonly = 1;
2081 op->ors_attrs = slap_anlist_no_attrs;
2082 op->ors_limit = NULL;
2083 op->ors_filter = &uf;
2086 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2088 uf.f_choice = LDAP_FILTER_EQUALITY;
2089 si->si_refreshDelete |= NP_DELETE_ONE;
2091 for (i=0; uuids[i].bv_val; i++) {
2093 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2095 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2096 op->o_tmpfree( uf.f_av_value.bv_val, op->o_tmpmemctx );
2097 uf.f_av_value = uuids[i];
2098 rc = be->be_search( op, &rs_search );
2099 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2101 si->si_refreshDelete ^= NP_DELETE_ONE;
2103 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2104 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2105 an[0].an_desc = slap_schema.si_ad_entryUUID;
2107 op->ors_slimit = SLAP_NO_LIMIT;
2108 op->ors_attrsonly = 0;
2109 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2110 op->ors_filterstr = si->si_filterstr;
2111 op->o_nocaching = 1;
2113 if ( limits_check( op, &rs_search ) == 0 ) {
2114 rc = be->be_search( op, &rs_search );
2116 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2119 op->o_nocaching = 0;
2121 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2123 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2126 csn = si->si_syncCookie.ctxcsn;
2128 slap_queue_csn( op, &csn );
2130 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2131 while ( np_list != NULL ) {
2132 LDAP_LIST_REMOVE( np_list, npe_link );
2134 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2135 op->o_tag = LDAP_REQ_DELETE;
2136 op->o_callback = &cb;
2137 cb.sc_response = null_callback;
2139 op->o_req_dn = *np_prev->npe_name;
2140 op->o_req_ndn = *np_prev->npe_nname;
2141 rc = op->o_bd->be_delete( op, &rs_delete );
2142 Debug( LDAP_DEBUG_SYNC,
2143 "syncrepl_del_nonpresent: rid %03d be_delete %s (%d)\n",
2144 si->si_rid, op->o_req_dn.bv_val, rc );
2146 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2147 Modifications mod1, mod2;
2148 mod1.sml_op = LDAP_MOD_REPLACE;
2150 mod1.sml_desc = slap_schema.si_ad_objectClass;
2151 mod1.sml_type = mod1.sml_desc->ad_cname;
2152 mod1.sml_values = &gcbva[0];
2153 mod1.sml_nvalues = NULL;
2154 mod1.sml_next = &mod2;
2156 mod2.sml_op = LDAP_MOD_REPLACE;
2158 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2159 mod2.sml_type = mod2.sml_desc->ad_cname;
2160 mod2.sml_values = &gcbva[1];
2161 mod2.sml_nvalues = NULL;
2162 mod2.sml_next = NULL;
2164 op->o_tag = LDAP_REQ_MODIFY;
2165 op->orm_modlist = &mod1;
2167 rc = be->be_modify( op, &rs_modify );
2168 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2171 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2172 op->o_delete_glue_parent ) {
2173 op->o_delete_glue_parent = 0;
2174 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2175 slap_callback cb = { NULL };
2176 cb.sc_response = slap_null_cb;
2177 dnParent( &op->o_req_ndn, &pdn );
2179 op->o_req_ndn = pdn;
2180 op->o_callback = &cb;
2181 /* give it a root privil ? */
2182 op->o_bd->be_delete( op, &rs_delete );
2188 op->o_delete_glue_parent = 0;
2190 ber_bvfree( np_prev->npe_name );
2191 ber_bvfree( np_prev->npe_nname );
2195 slap_graduate_commit_csn( op );
2196 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2197 BER_BVZERO( &op->o_csn );
2208 Backend *be = op->o_bd;
2209 slap_callback cb = { NULL };
2214 struct berval dn = BER_BVNULL;
2215 struct berval ndn = BER_BVNULL;
2217 SlapReply rs_add = {REP_RESULT};
2218 struct berval ptr, nptr;
2221 op->o_tag = LDAP_REQ_ADD;
2222 op->o_callback = &cb;
2223 cb.sc_response = null_callback;
2224 cb.sc_private = NULL;
2229 /* count RDNs in suffix */
2230 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2231 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2233 ptr.bv_len -= comma - ptr.bv_val;
2243 /* Start with BE suffix */
2245 for ( i = 0; i < suffrdns; i++ ) {
2246 comma = ber_bvrchr( &ptr, ',' );
2247 if ( comma != NULL ) {
2248 ptr.bv_len = comma - ptr.bv_val;
2255 if ( !BER_BVISEMPTY( &ptr ) ) {
2256 dn.bv_len -= ptr.bv_len + 1;
2257 dn.bv_val += ptr.bv_len + 1;
2260 /* the normalizedDNs are always the same length, no counting
2264 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2265 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2266 ndn.bv_len = be->be_nsuffix[0].bv_len;
2268 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2274 while ( ndn.bv_val > e->e_nname.bv_val ) {
2275 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2276 ber_dupbv( &glue->e_name, &dn );
2277 ber_dupbv( &glue->e_nname, &ndn );
2279 a = ch_calloc( 1, sizeof( Attribute ));
2280 a->a_desc = slap_schema.si_ad_objectClass;
2282 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2283 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2284 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2285 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2287 a->a_nvals = a->a_vals;
2289 a->a_next = glue->e_attrs;
2292 a = ch_calloc( 1, sizeof( Attribute ));
2293 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2295 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2296 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2297 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2299 a->a_nvals = a->a_vals;
2301 a->a_next = glue->e_attrs;
2304 op->o_req_dn = glue->e_name;
2305 op->o_req_ndn = glue->e_nname;
2307 rc = be->be_add ( op, &rs_add );
2308 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2309 be_entry_release_w( op, glue );
2311 /* incl. ALREADY EXIST */
2315 /* Move to next child */
2316 comma = ber_bvrchr( &ptr, ',' );
2317 if ( comma == NULL ) {
2320 ptr.bv_len = comma - ptr.bv_val;
2322 dn.bv_val = ++comma;
2323 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2325 comma = ber_bvrchr( &nptr, ',' );
2326 assert( comma != NULL );
2327 nptr.bv_len = comma - nptr.bv_val;
2329 ndn.bv_val = ++comma;
2330 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2333 op->o_req_dn = e->e_name;
2334 op->o_req_ndn = e->e_nname;
2336 rc = be->be_add ( op, &rs_add );
2337 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2338 be_entry_release_w( op, e );
2347 syncrepl_updateCookie(
2351 struct sync_cookie *syncCookie )
2353 Backend *be = op->o_bd;
2354 Modifications mod = { { 0 } };
2355 struct berval vals[ 2 ];
2359 slap_callback cb = { NULL };
2360 SlapReply rs_modify = {REP_RESULT};
2362 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2363 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2365 mod.sml_op = LDAP_MOD_REPLACE;
2366 mod.sml_desc = slap_schema.si_ad_contextCSN;
2367 mod.sml_type = mod.sml_desc->ad_cname;
2368 mod.sml_values = vals;
2369 vals[0] = si->si_syncCookie.ctxcsn;
2370 vals[1].bv_val = NULL;
2373 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2375 op->o_tag = LDAP_REQ_MODIFY;
2377 assert( si->si_rid < 1000 );
2379 cb.sc_response = null_callback;
2382 op->o_callback = &cb;
2383 op->o_req_dn = op->o_bd->be_suffix[0];
2384 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2386 /* update contextCSN */
2387 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2388 op->orm_modlist = &mod;
2389 rc = be->be_modify( op, &rs_modify );
2392 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2393 Debug( LDAP_DEBUG_ANY,
2394 "syncrepl_updateCookie: rid %03d be_modify failed (%d)\n",
2395 si->si_rid, rs_modify.sr_err, 0 );
2398 slap_graduate_commit_csn( op );
2399 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2400 BER_BVZERO( &op->o_csn );
2401 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2411 dninfo *dni = op->o_callback->sc_private;
2413 if ( rs->sr_type == REP_SEARCH ) {
2414 if ( !BER_BVISNULL( &dni->dn ) ) {
2415 Debug( LDAP_DEBUG_ANY,
2416 "dn_callback: consistency error - "
2417 "entryUUID is not unique\n", 0, 0, 0 );
2419 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2420 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2421 /* If there is a new entry, see if it differs from the old.
2422 * We compare the non-normalized values so that cosmetic changes
2423 * in the provider are always propagated.
2425 if ( dni->new_entry ) {
2426 Attribute *old, *new;
2429 /* Did the DN change? Note that we don't explicitly try to
2430 * discover if the deleteOldRdn argument applies here. It
2431 * would save an unnecessary Modify if we detected it, but
2432 * that's a fair amount of trouble to compare the two attr
2433 * lists in detail. (Just test normalized DN; we ignore
2434 * insignificant changes here.)
2436 if ( !dn_match( &rs->sr_entry->e_nname,
2437 &dni->new_entry->e_nname ) )
2442 for ( i = 0, old = rs->sr_entry->e_attrs;
2444 i++, old = old->a_next )
2449 /* We assume that attributes are saved in the same order
2450 * in the remote and local databases. So if we walk through
2451 * the attributeDescriptions one by one they should match in
2452 * lock step. If not, we signal a change. Otherwise we test
2455 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2457 old = old->a_next, new = new->a_next )
2459 if ( old->a_desc != new->a_desc ) {
2460 dni->wasChanged = 1;
2463 for ( i = 0; ; i++ ) {
2465 nold = BER_BVISNULL( &old->a_vals[i] );
2466 nnew = BER_BVISNULL( &new->a_vals[i] );
2467 /* If both are empty, stop looking */
2468 if ( nold && nnew ) {
2471 /* If they are different, stop looking */
2472 if ( nold != nnew ) {
2473 dni->wasChanged = 1;
2476 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2477 dni->wasChanged = 1;
2481 if ( dni->wasChanged ) break;
2483 if ( dni->wasChanged ) {
2484 dni->ads = op->o_tmpalloc( dni->attrs *
2485 sizeof(AttributeDescription *), op->o_tmpmemctx );
2487 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2488 dni->ads[i] = old->a_desc;
2494 } else if ( rs->sr_type == REP_RESULT ) {
2495 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2496 Debug( LDAP_DEBUG_ANY,
2497 "dn_callback: consistency error - "
2498 "entryUUID is not unique\n", 0, 0, 0 );
2502 return LDAP_SUCCESS;
2506 nonpresent_callback(
2510 syncinfo_t *si = op->o_callback->sc_private;
2513 struct berval* present_uuid = NULL;
2514 struct nonpresent_entry *np_entry;
2516 if ( rs->sr_type == REP_RESULT ) {
2517 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2518 si->si_presentlist = NULL;
2520 } else if ( rs->sr_type == REP_SEARCH ) {
2521 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2522 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2524 if ( a == NULL ) return 0;
2526 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2530 if ( present_uuid == NULL ) {
2531 np_entry = (struct nonpresent_entry *)
2532 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2533 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2534 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2535 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2538 avl_delete( &si->si_presentlist,
2539 &a->a_nvals[0], syncuuid_cmp );
2540 ch_free( present_uuid->bv_val );
2541 ch_free( present_uuid );
2544 return LDAP_SUCCESS;
2552 if ( rs->sr_err != LDAP_SUCCESS &&
2553 rs->sr_err != LDAP_REFERRAL &&
2554 rs->sr_err != LDAP_ALREADY_EXISTS &&
2555 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2556 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2558 Debug( LDAP_DEBUG_ANY,
2559 "null_callback: error code 0x%x\n",
2562 return LDAP_SUCCESS;
2565 static struct berval *
2566 slap_uuidstr_from_normalized(
2567 struct berval* uuidstr,
2568 struct berval* normalized,
2572 unsigned char nibble;
2575 if ( normalized == NULL ) return NULL;
2576 if ( normalized->bv_len != 16 ) return NULL;
2581 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2582 if ( new == NULL ) {
2589 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2590 if ( new != uuidstr ) {
2591 slap_sl_free( new, ctx );
2596 for ( i = 0; i < 16; i++ ) {
2597 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2598 new->bv_val[(i<<1)+d] = '-';
2602 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2603 if ( nibble < 10 ) {
2604 new->bv_val[(i<<1)+d] = nibble + '0';
2606 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2609 nibble = (normalized->bv_val[i]) & 0xF;
2610 if ( nibble < 10 ) {
2611 new->bv_val[(i<<1)+d+1] = nibble + '0';
2613 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2617 new->bv_val[new->bv_len] = '\0';
2622 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2624 const struct berval *uuid1 = v_uuid1;
2625 const struct berval *uuid2 = v_uuid2;
2626 int rc = uuid1->bv_len - uuid2->bv_len;
2627 if ( rc ) return rc;
2628 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2632 avl_ber_bvfree( void *v_bv )
2634 struct berval *bv = (struct berval *)v_bv;
2636 if( v_bv == NULL ) return;
2637 if ( !BER_BVISNULL( bv ) ) {
2638 ch_free( bv->bv_val );
2640 ch_free( (char *) bv );
2644 syncinfo_free( syncinfo_t *sie )
2647 if ( sie->si_conn_setup ) {
2649 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2650 connection_client_stop( s );
2651 sie->si_conn_setup = 0;
2653 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2656 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2658 bindconf_free( &sie->si_bindconf );
2660 if ( sie->si_filterstr.bv_val ) {
2661 ch_free( sie->si_filterstr.bv_val );
2663 if ( sie->si_base.bv_val ) {
2664 ch_free( sie->si_base.bv_val );
2666 if ( sie->si_attrs ) {
2668 while ( sie->si_attrs[i] != NULL ) {
2669 ch_free( sie->si_attrs[i] );
2672 ch_free( sie->si_attrs );
2674 if ( sie->si_exattrs ) {
2676 while ( sie->si_exattrs[i] != NULL ) {
2677 ch_free( sie->si_exattrs[i] );
2680 ch_free( sie->si_exattrs );
2682 if ( sie->si_anlist ) {
2684 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2685 ch_free( sie->si_anlist[i].an_name.bv_val );
2688 ch_free( sie->si_anlist );
2690 if ( sie->si_exanlist ) {
2692 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2693 ch_free( sie->si_exanlist[i].an_name.bv_val );
2696 ch_free( sie->si_exanlist );
2698 if ( sie->si_retryinterval ) {
2699 ch_free( sie->si_retryinterval );
2701 if ( sie->si_retrynum ) {
2702 ch_free( sie->si_retrynum );
2704 if ( sie->si_retrynum_init ) {
2705 ch_free( sie->si_retrynum_init );
2707 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2708 if ( sie->si_presentlist ) {
2709 avl_free( sie->si_presentlist, avl_ber_bvfree );
2711 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2712 struct nonpresent_entry* npe;
2713 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2714 LDAP_LIST_REMOVE( npe, npe_link );
2715 if ( npe->npe_name ) {
2716 if ( npe->npe_name->bv_val ) {
2717 ch_free( npe->npe_name->bv_val );
2719 ch_free( npe->npe_name );
2721 if ( npe->npe_nname ) {
2722 if ( npe->npe_nname->bv_val ) {
2723 ch_free( npe->npe_nname->bv_val );
2725 ch_free( npe->npe_nname );
2734 /* NOTE: used & documented in slapd.conf(5) */
2736 #define PROVIDERSTR "provider"
2737 #define SCHEMASTR "schemachecking"
2738 #define FILTERSTR "filter"
2739 #define SEARCHBASESTR "searchbase"
2740 #define SCOPESTR "scope"
2741 #define ATTRSONLYSTR "attrsonly"
2742 #define ATTRSSTR "attrs"
2743 #define TYPESTR "type"
2744 #define INTERVALSTR "interval"
2745 #define RETRYSTR "retry"
2746 #define SLIMITSTR "sizelimit"
2747 #define TLIMITSTR "timelimit"
2748 #define SYNCDATASTR "syncdata"
2750 /* FIXME: undocumented */
2751 #define LOGBASESTR "logbase"
2752 #define LOGFILTERSTR "logfilter"
2753 #define OLDAUTHCSTR "bindprincipal"
2754 #define EXATTRSSTR "exattrs"
2755 #define MANAGEDSAITSTR "manageDSAit"
2758 #define LASTMODSTR "lastmod"
2759 #define LMGENSTR "gen"
2760 #define LMNOSTR "no"
2761 #define LMREQSTR "req"
2762 #define SRVTABSTR "srvtab"
2763 #define SUFFIXSTR "suffix"
2766 #define GOT_ID 0x0001
2767 #define GOT_PROVIDER 0x0002
2768 #define GOT_BASE 0x0004
2771 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2777 { BER_BVC("base"), LDAP_SCOPE_BASE },
2778 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2779 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2780 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2781 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2782 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2783 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2787 static slap_verbmasks datamodes[] = {
2788 { BER_BVC("default"), SYNCDATA_DEFAULT },
2789 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2790 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2795 parse_syncrepl_line(
2803 for ( i = 1; i < c->argc; i++ ) {
2804 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2805 STRLENOF( IDSTR "=" ) ) )
2808 /* '\0' string terminator accounts for '=' */
2809 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2810 if ( lutil_atoi( &tmp, val ) != 0 ) {
2811 snprintf( c->msg, sizeof( c->msg ),
2812 "Error: parse_syncrepl_line: "
2813 "unable to parse syncrepl id \"%s\"", val );
2814 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2817 if ( tmp >= 1000 || tmp < 0 ) {
2818 snprintf( c->msg, sizeof( c->msg ),
2819 "Error: parse_syncrepl_line: "
2820 "syncrepl id %d is out of range [0..999]", tmp );
2821 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2826 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2827 STRLENOF( PROVIDERSTR "=" ) ) )
2829 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2830 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2831 gots |= GOT_PROVIDER;
2832 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2833 STRLENOF( SCHEMASTR "=" ) ) )
2835 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2836 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2837 si->si_schemachecking = 1;
2838 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2839 si->si_schemachecking = 0;
2841 si->si_schemachecking = 1;
2843 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2844 STRLENOF( FILTERSTR "=" ) ) )
2846 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2847 if ( si->si_filterstr.bv_val )
2848 ch_free( si->si_filterstr.bv_val );
2849 ber_str2bv( val, 0, 1, &si->si_filterstr );
2850 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2851 STRLENOF( LOGFILTERSTR "=" ) ) )
2853 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2854 if ( si->si_logfilterstr.bv_val )
2855 ch_free( si->si_logfilterstr.bv_val );
2856 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2857 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2858 STRLENOF( SEARCHBASESTR "=" ) ) )
2863 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2864 if ( si->si_base.bv_val ) {
2865 ch_free( si->si_base.bv_val );
2867 ber_str2bv( val, 0, 0, &bv );
2868 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2869 if ( rc != LDAP_SUCCESS ) {
2870 snprintf( c->msg, sizeof( c->msg ),
2871 "Invalid base DN \"%s\": %d (%s)",
2872 val, rc, ldap_err2string( rc ) );
2873 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2877 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2878 STRLENOF( LOGBASESTR "=" ) ) )
2883 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2884 if ( si->si_logbase.bv_val ) {
2885 ch_free( si->si_logbase.bv_val );
2887 ber_str2bv( val, 0, 0, &bv );
2888 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2889 if ( rc != LDAP_SUCCESS ) {
2890 snprintf( c->msg, sizeof( c->msg ),
2891 "Invalid logbase DN \"%s\": %d (%s)",
2892 val, rc, ldap_err2string( rc ) );
2893 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2896 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2897 STRLENOF( SCOPESTR "=" ) ) )
2900 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2901 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2902 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2903 si->si_scope = scopes[j].val;
2907 if ( BER_BVISNULL(&scopes[j].key) ) {
2908 snprintf( c->msg, sizeof( c->msg ),
2909 "Error: parse_syncrepl_line: "
2910 "unknown scope \"%s\"", val);
2911 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2914 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2915 STRLENOF( ATTRSONLYSTR ) ) )
2917 si->si_attrsonly = 1;
2918 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2919 STRLENOF( ATTRSSTR "=" ) ) )
2921 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2922 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2924 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2925 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2926 if ( si->si_anlist == NULL ) {
2927 ch_free( attr_fname );
2930 si->si_anfile = attr_fname;
2932 char *str, *s, *next;
2933 char delimstr[] = " ,\t";
2934 str = ch_strdup( val );
2935 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2937 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2939 if ( strlen(s) == 1 && *s == '*' ) {
2940 si->si_allattrs = 1;
2941 *(val + ( s - str )) = delimstr[0];
2943 if ( strlen(s) == 1 && *s == '+' ) {
2944 si->si_allopattrs = 1;
2945 *(val + ( s - str )) = delimstr[0];
2949 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2950 if ( si->si_anlist == NULL ) {
2954 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2955 STRLENOF( EXATTRSSTR "=" ) ) )
2957 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2958 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2960 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2961 si->si_exanlist = file2anlist(
2962 si->si_exanlist, attr_fname, " ,\t" );
2963 if ( si->si_exanlist == NULL ) {
2964 ch_free( attr_fname );
2967 ch_free( attr_fname );
2969 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2970 if ( si->si_exanlist == NULL ) {
2974 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2975 STRLENOF( TYPESTR "=" ) ) )
2977 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2978 if ( !strncasecmp( val, "refreshOnly",
2979 STRLENOF("refreshOnly") ) )
2981 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2982 } else if ( !strncasecmp( val, "refreshAndPersist",
2983 STRLENOF("refreshAndPersist") ) )
2985 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2986 si->si_interval = 60;
2988 snprintf( c->msg, sizeof( c->msg ),
2989 "Error: parse_syncrepl_line: "
2990 "unknown sync type \"%s\"", val);
2991 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2994 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2995 STRLENOF( INTERVALSTR "=" ) ) )
2997 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2998 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2999 si->si_interval = 0;
3000 } else if ( strchr( val, ':' ) != NULL ) {
3001 char *next, *ptr = val;
3002 unsigned dd, hh, mm, ss;
3004 /* NOTE: the test for ptr[ 0 ] == '-'
3005 * should go before the call to strtoul() */
3006 dd = strtoul( ptr, &next, 10 );
3007 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3008 snprintf( c->msg, sizeof( c->msg ),
3009 "Error: parse_syncrepl_line: "
3010 "invalid interval \"%s\", unable to parse days", val );
3011 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3015 hh = strtoul( ptr, &next, 10 );
3016 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3017 snprintf( c->msg, sizeof( c->msg ),
3018 "Error: parse_syncrepl_line: "
3019 "invalid interval \"%s\", unable to parse hours", val );
3020 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3024 mm = strtoul( ptr, &next, 10 );
3025 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3026 snprintf( c->msg, sizeof( c->msg ),
3027 "Error: parse_syncrepl_line: "
3028 "invalid interval \"%s\", unable to parse minutes", val );
3029 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3033 ss = strtoul( ptr, &next, 10 );
3034 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3035 snprintf( c->msg, sizeof( c->msg ),
3036 "Error: parse_syncrepl_line: "
3037 "invalid interval \"%s\", unable to parse seconds", val );
3038 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3041 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3045 if ( lutil_parse_time( val, &t ) != 0 ) {
3046 snprintf( c->msg, sizeof( c->msg ),
3047 "Error: parse_syncrepl_line: "
3048 "invalid interval \"%s\"", val );
3049 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3052 si->si_interval = (time_t)t;
3054 if ( si->si_interval < 0 ) {
3055 snprintf( c->msg, sizeof( c->msg ),
3056 "Error: parse_syncrepl_line: "
3057 "invalid interval \"%ld\"",
3058 (long) si->si_interval);
3059 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3062 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3063 STRLENOF( RETRYSTR "=" ) ) )
3068 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3069 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3070 retry_list[0] = NULL;
3072 slap_str2clist( &retry_list, val, " ,\t" );
3074 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3077 snprintf( c->msg, sizeof( c->msg ),
3078 "Error: incomplete syncrepl retry list" );
3079 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3080 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3081 ch_free( retry_list[k] );
3083 ch_free( retry_list );
3086 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3087 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3088 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3089 for ( j = 0; j < n; j++ ) {
3091 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3092 snprintf( c->msg, sizeof( c->msg ),
3093 "Error: invalid retry interval \"%s\" (#%d)",
3094 retry_list[j*2], j );
3095 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3096 /* do some cleanup */
3099 si->si_retryinterval[j] = (time_t)t;
3100 if ( *retry_list[j*2+1] == '+' ) {
3101 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3102 si->si_retrynum[j] = RETRYNUM_FOREVER;
3106 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3107 || si->si_retrynum_init[j] <= 0 )
3109 snprintf( c->msg, sizeof( c->msg ),
3110 "Error: invalid initial retry number \"%s\" (#%d)",
3111 retry_list[j*2+1], j );
3112 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3113 /* do some cleanup */
3116 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3117 || si->si_retrynum[j] <= 0 )
3119 snprintf( c->msg, sizeof( c->msg ),
3120 "Error: invalid retry number \"%s\" (#%d)",
3121 retry_list[j*2+1], j );
3122 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3123 /* do some cleanup */
3128 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3129 si->si_retrynum[j] = RETRYNUM_TAIL;
3130 si->si_retryinterval[j] = 0;
3132 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3133 ch_free( retry_list[k] );
3135 ch_free( retry_list );
3136 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3137 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3139 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3140 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3141 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3143 snprintf( c->msg, sizeof( c->msg ),
3144 "invalid manageDSAit value \"%s\".\n",
3146 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3149 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3150 STRLENOF( SLIMITSTR "=") ) )
3152 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3153 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3156 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3157 snprintf( c->msg, sizeof( c->msg ),
3158 "invalid size limit value \"%s\".\n",
3160 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3163 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3164 STRLENOF( TLIMITSTR "=" ) ) )
3166 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3167 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3170 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3171 snprintf( c->msg, sizeof( c->msg ),
3172 "invalid time limit value \"%s\".\n",
3174 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3177 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3178 STRLENOF( SYNCDATASTR "=" ) ) )
3180 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3181 si->si_syncdata = verb_to_mask( val, datamodes );
3182 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3183 snprintf( c->msg, sizeof( c->msg ),
3184 "Error: parse_syncrepl_line: "
3185 "unable to parse \"%s\"\n", c->argv[ i ] );
3186 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3191 if ( gots != GOT_ALL ) {
3192 snprintf( c->msg, sizeof( c->msg ),
3193 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3194 gots & GOT_ID ? "" : " "IDSTR,
3195 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3196 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3197 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3211 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3212 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3213 "operations required for syncrepl", c->be->be_type );
3214 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3217 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3218 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3219 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3222 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3225 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3229 si->si_bindconf.sb_tls = SB_TLS_OFF;
3230 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3231 si->si_schemachecking = 0;
3232 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3233 &si->si_filterstr );
3234 si->si_base.bv_val = NULL;
3235 si->si_scope = LDAP_SCOPE_SUBTREE;
3236 si->si_attrsonly = 0;
3237 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3238 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3239 si->si_attrs = NULL;
3240 si->si_allattrs = 0;
3241 si->si_allopattrs = 0;
3242 si->si_exattrs = NULL;
3243 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3244 si->si_interval = 86400;
3245 si->si_retryinterval = NULL;
3246 si->si_retrynum_init = NULL;
3247 si->si_retrynum = NULL;
3248 si->si_manageDSAit = 0;
3251 si->si_conn_setup = 0;
3253 si->si_presentlist = NULL;
3254 LDAP_LIST_INIT( &si->si_nonpresentlist );
3255 ldap_pvt_thread_mutex_init( &si->si_mutex );
3257 rc = parse_syncrepl_line( c, si );
3261 init_syncrepl( si );
3262 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3263 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3268 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3269 syncinfo_free( si );
3272 Debug( LDAP_DEBUG_CONFIG,
3273 "Config: ** successfully added syncrepl \"%s\"\n",
3274 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3275 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3276 if ( !si->si_schemachecking ) {
3277 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3279 c->be->be_syncinfo = si;
3285 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3287 struct berval bc, uri;
3288 char buf[BUFSIZ*2], *ptr;
3291 /* FIXME: we're not checking for buf[] overflow! */
3293 /* temporarily inhibit bindconf from printing URI */
3294 uri = si->si_bindconf.sb_uri;
3295 BER_BVZERO( &si->si_bindconf.sb_uri );
3296 bindconf_unparse( &si->si_bindconf, &bc );
3297 si->si_bindconf.sb_uri = uri;
3300 ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03ld " PROVIDERSTR "=%s",
3301 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3302 if ( !BER_BVISNULL( &bc )) {
3303 ptr = lutil_strcopy( ptr, bc.bv_val );
3306 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3307 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3308 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3311 if ( !BER_BVISNULL( &si->si_base )) {
3312 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3313 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3316 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3317 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3318 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3321 if ( !BER_BVISNULL( &si->si_logbase )) {
3322 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3323 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3326 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3327 if ( si->si_scope == scopes[i].val ) {
3328 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3329 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3333 if ( si->si_attrsonly ) {
3334 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3336 if ( si->si_anfile ) {
3337 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3338 ptr = lutil_strcopy( ptr, si->si_anfile );
3339 } else if ( si->si_allattrs || si->si_allopattrs ||
3340 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3342 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3344 ptr = anlist_unparse( si->si_anlist, ptr );
3345 if ( si->si_allattrs ) {
3346 if ( old != ptr ) *ptr++ = ',';
3349 if ( si->si_allopattrs ) {
3350 if ( old != ptr ) *ptr++ = ',';
3355 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3356 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3357 ptr = anlist_unparse( si->si_exanlist, ptr );
3359 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3360 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3362 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3363 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3364 "refreshAndPersist" : "refreshOnly" );
3366 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3369 dd = si->si_interval;
3376 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3377 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3378 } else if ( si->si_retryinterval ) {
3380 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3381 for (i=0; si->si_retryinterval[i]; i++) {
3382 if ( space ) *ptr++ = ' ';
3384 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3385 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3388 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3393 if ( si->si_slimit ) {
3394 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3395 ptr += sprintf( ptr, "%d", si->si_slimit );
3398 if ( si->si_tlimit ) {
3399 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3400 ptr += sprintf( ptr, "%d", si->si_tlimit );
3403 if ( si->si_syncdata ) {
3404 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3405 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3406 ptr = lutil_strcopy( ptr, bc.bv_val );
3409 bc.bv_len = ptr - buf;
3411 ber_dupbv( bv, &bc );
3415 syncrepl_config( ConfigArgs *c )
3417 if (c->op == SLAP_CONFIG_EMIT) {
3418 if ( c->be->be_syncinfo ) {
3420 syncrepl_unparse( c->be->be_syncinfo, &bv );
3421 ber_bvarray_add( &c->rvalue_vals, &bv );
3425 } else if ( c->op == LDAP_MOD_DELETE ) {
3428 if ( c->be->be_syncinfo ) {
3429 re = c->be->be_syncinfo->si_re;
3431 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3432 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3433 ldap_pvt_runqueue_remove( &slapd_rq, re );
3435 syncinfo_free( c->be->be_syncinfo );
3436 c->be->be_syncinfo = NULL;
3438 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3441 if ( SLAP_SHADOW( c->be ) ) {
3442 Debug(LDAP_DEBUG_ANY, "%s: "
3443 "syncrepl: database already shadowed.\n",
3446 } else if ( add_syncrepl( c ) ) {
3449 return config_sync_shadow( c );