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;
1032 int i, defer = 1, fail = 0;
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 fail = RETRYNUM_TAIL;
1152 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1153 if ( si->si_retrynum[i] > 0 )
1154 si->si_retrynum[i]--;
1155 fail = si->si_retrynum[i];
1156 rtask->interval.tv_sec = si->si_retryinterval[i];
1157 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1158 slap_wake_listener();
1162 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1163 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1166 if ( fail == RETRYNUM_TAIL ) {
1167 Debug( LDAP_DEBUG_ANY,
1168 "do_syncrepl: rid %03d quitting\n",
1170 } else if ( fail > 0 ) {
1171 Debug( LDAP_DEBUG_ANY,
1172 "do_syncrepl: rid %03d retrying (%d retries left)\n",
1173 si->si_rid, fail, 0 );
1175 Debug( LDAP_DEBUG_ANY,
1176 "do_syncrepl: rid %03d retrying\n",
1184 static slap_verbmasks modops[] = {
1185 { BER_BVC("add"), LDAP_REQ_ADD },
1186 { BER_BVC("delete"), LDAP_REQ_DELETE },
1187 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1188 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1192 static Modifications *
1193 syncrepl_accesslog_mods(
1200 AttributeDescription *ad;
1201 struct berval bv, bv2;
1203 Modifications *mod = NULL, *modlist = NULL, **modtail;
1208 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1212 colon = ber_bvchr( &bv, ':' );
1214 continue; /* invalid */
1215 bv.bv_len = colon - bv.bv_val;
1216 if ( slap_bv2ad( &bv, &ad, &text )) {
1220 /* Ignore dynamically generated attrs */
1221 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1223 /* Ignore excluded attrs */
1224 if ( ldap_charray_inlist( si->si_exattrs,
1225 ad->ad_type->sat_cname.bv_val ))
1229 case '+': op = LDAP_MOD_ADD; break;
1230 case '-': op = LDAP_MOD_DELETE; break;
1231 case '=': op = LDAP_MOD_REPLACE; break;
1232 case '#': op = LDAP_MOD_INCREMENT; break;
1236 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1237 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1240 mod->sml_next = NULL;
1242 mod->sml_type = ad->ad_cname;
1243 mod->sml_values = NULL;
1244 mod->sml_nvalues = NULL;
1247 modtail = &mod->sml_next;
1249 if ( colon[2] == ' ' ) {
1250 bv.bv_val = colon + 3;
1251 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1252 ber_dupbv( &bv2, &bv );
1253 ber_bvarray_add( &mod->sml_values, &bv2 );
1259 static Modifications *
1260 syncrepl_changelog_mods(
1265 return NULL; /* FIXME */
1269 syncrepl_message_to_op(
1275 BerElement *ber = NULL;
1276 Modifications *modlist = NULL;
1278 SlapReply rs = { REP_RESULT };
1279 slap_callback cb = { NULL, null_callback, NULL, NULL };
1282 char txtbuf[SLAP_TEXT_BUFLEN];
1283 size_t textlen = sizeof txtbuf;
1285 struct berval bdn, dn = BER_BVNULL, ndn;
1286 struct berval bv, *bvals = NULL;
1287 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1288 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1289 psup = BER_BVNULL, nsup = BER_BVNULL;
1290 int rc, deleteOldRdn = 0, freeReqDn = 0;
1292 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1293 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1294 "Message type should be entry (%d)",
1295 si->si_rid, ldap_msgtype( msg ), 0 );
1299 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1304 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1306 if ( rc != LDAP_SUCCESS ) {
1307 Debug( LDAP_DEBUG_ANY,
1308 "syncrepl_message_to_op: rid %03d dn get failed (%d)",
1309 si->si_rid, rc, 0 );
1313 op->o_tag = LBER_DEFAULT;
1315 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1317 if ( bv.bv_val == NULL )
1320 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1322 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1323 ber_dupbv( &op->o_req_dn, &dn );
1324 ber_dupbv( &op->o_req_ndn, &ndn );
1325 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1326 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1328 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1329 int i = verb_to_mask( bvals[0].bv_val, modops );
1331 Debug( LDAP_DEBUG_ANY,
1332 "syncrepl_message_to_op: rid %03d unknown op %s",
1333 si->si_rid, bvals[0].bv_val, 0 );
1338 op->o_tag = modops[i].mask;
1339 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1340 /* Parse attribute into modlist */
1341 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1342 modlist = syncrepl_accesslog_mods( si, bvals );
1344 modlist = syncrepl_changelog_mods( si, bvals );
1345 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1347 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1348 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1350 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1352 } else if ( !ber_bvstrcasecmp( &bv,
1353 &slap_schema.si_ad_entryCSN->ad_cname )) {
1354 slap_queue_csn( op, bvals );
1359 /* If we didn't get a mod type or a target DN, bail out */
1360 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1365 op->o_callback = &cb;
1366 slap_op_time( &op->o_time, &op->o_tincr );
1368 switch( op->o_tag ) {
1370 case LDAP_REQ_MODIFY:
1371 /* If we didn't get required data, bail */
1372 if ( !modlist ) goto done;
1374 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1376 if ( rc != LDAP_SUCCESS ) {
1377 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1378 "mods check (%s)\n",
1379 si->si_rid, text, 0 );
1383 if ( op->o_tag == LDAP_REQ_ADD ) {
1384 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1385 op->ora_e->e_name = op->o_req_dn;
1386 op->ora_e->e_nname = op->o_req_ndn;
1388 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1389 if( rc != LDAP_SUCCESS ) {
1390 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1391 "mods2entry (%s)\n",
1392 si->si_rid, text, 0 );
1394 rc = op->o_bd->be_add( op, &rs );
1395 Debug( LDAP_DEBUG_SYNC,
1396 "syncrepl_message_to_op: rid %03d be_add %s (%d)\n",
1397 si->si_rid, op->o_req_dn.bv_val, rc );
1399 be_entry_release_w( op, op->ora_e );
1401 op->orm_modlist = modlist;
1402 rc = op->o_bd->be_modify( op, &rs );
1403 modlist = op->orm_modlist;
1404 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1405 "syncrepl_message_to_op: rid %03d be_modify %s (%d)\n",
1406 si->si_rid, op->o_req_dn.bv_val, rc );
1409 case LDAP_REQ_MODRDN:
1410 if ( BER_BVISNULL( &rdn )) goto done;
1412 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1414 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1416 if ( !BER_BVISNULL( &sup )) {
1417 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1419 op->orr_newSup = &psup;
1420 op->orr_nnewSup = ⊅
1422 op->orr_newSup = NULL;
1423 op->orr_nnewSup = NULL;
1425 op->orr_newrdn = prdn;
1426 op->orr_nnewrdn = nrdn;
1427 op->orr_deleteoldrdn = deleteOldRdn;
1428 rc = op->o_bd->be_modrdn( op, &rs );
1429 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1430 "syncrepl_message_to_op: rid %03d be_modrdn %s (%d)\n",
1431 si->si_rid, op->o_req_dn.bv_val, rc );
1433 case LDAP_REQ_DELETE:
1434 rc = op->o_bd->be_delete( op, &rs );
1435 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1436 "syncrepl_message_to_op: rid %03d be_delete %s (%d)\n",
1437 si->si_rid, op->o_req_dn.bv_val, rc );
1441 slap_graduate_commit_csn( op );
1442 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1443 BER_BVZERO( &op->o_csn );
1445 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1446 if ( !BER_BVISNULL( &rdn )) {
1447 if ( !BER_BVISNULL( &nsup ))
1448 ch_free( nsup.bv_val );
1449 if ( !BER_BVISNULL( &psup ))
1450 ch_free( psup.bv_val );
1451 if ( !BER_BVISNULL( &nrdn ))
1452 ch_free( nrdn.bv_val );
1453 if ( !BER_BVISNULL( &prdn ))
1454 ch_free( prdn.bv_val );
1457 ch_free( op->o_req_ndn.bv_val );
1458 ch_free( op->o_req_dn.bv_val );
1460 ber_free ( ber, 0 );
1465 syncrepl_message_to_entry(
1469 Modifications **modlist,
1475 BerElement *ber = NULL;
1478 Modifications **modtail = modlist;
1481 char txtbuf[SLAP_TEXT_BUFLEN];
1482 size_t textlen = sizeof txtbuf;
1484 struct berval bdn = BER_BVNULL, dn, ndn;
1489 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1490 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d "
1491 "Message type should be entry (%d)",
1492 si->si_rid, ldap_msgtype( msg ), 0 );
1496 op->o_tag = LDAP_REQ_ADD;
1498 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1499 if ( rc != LDAP_SUCCESS ) {
1500 Debug( LDAP_DEBUG_ANY,
1501 "syncrepl_message_to_entry: rid %03d dn get failed (%d)",
1502 si->si_rid, rc, 0 );
1506 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1507 ber_dupbv( &op->o_req_dn, &dn );
1508 ber_dupbv( &op->o_req_ndn, &ndn );
1509 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1510 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1512 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1513 /* NOTE: this could be done even before decoding the DN,
1514 * although encoding errors wouldn't be detected */
1519 if ( entry == NULL ) {
1524 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1526 e->e_name = op->o_req_dn;
1527 e->e_nname = op->o_req_ndn;
1529 while ( ber_remaining( ber ) ) {
1530 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1531 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1536 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1538 mod->sml_op = LDAP_MOD_REPLACE;
1540 mod->sml_next = NULL;
1541 mod->sml_desc = NULL;
1542 mod->sml_type = tmp.sml_type;
1543 mod->sml_values = tmp.sml_values;
1544 mod->sml_nvalues = NULL;
1547 modtail = &mod->sml_next;
1550 if ( *modlist == NULL ) {
1551 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d no attributes\n",
1557 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1559 if ( rc != LDAP_SUCCESS ) {
1560 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods check (%s)\n",
1561 si->si_rid, text, 0 );
1565 /* Strip out dynamically generated attrs */
1566 for ( modtail = modlist; *modtail ; ) {
1568 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1569 *modtail = mod->sml_next;
1570 slap_mod_free( &mod->sml_mod, 0 );
1573 modtail = &mod->sml_next;
1577 /* Strip out attrs in exattrs list */
1578 for ( modtail = modlist; *modtail ; ) {
1580 if ( ldap_charray_inlist( si->si_exattrs,
1581 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1582 *modtail = mod->sml_next;
1583 slap_mod_free( &mod->sml_mod, 0 );
1586 modtail = &mod->sml_next;
1590 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1591 if( rc != LDAP_SUCCESS ) {
1592 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods2entry (%s)\n",
1593 si->si_rid, text, 0 );
1598 if ( rc != LDAP_SUCCESS ) {
1608 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1610 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1611 * entry if a previous refresh was interrupted before sending us a new
1612 * context state. We try to compare the new entry to the existing entry
1613 * and ignore the new entry if they are the same.
1615 * Also, we may get an update where the entryDN has changed, due to
1616 * a ModDn on the provider. We detect this as well, so we can issue
1617 * the corresponding operation locally.
1619 * In the case of a modify, we get a list of all the attributes
1620 * in the original entry. Rather than deleting the entry and re-adding it,
1621 * we issue a Modify request that deletes all the attributes and adds all
1622 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1625 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1626 * a ModDN we will issue both operations on the local database.
1628 typedef struct dninfo {
1632 int renamed; /* Was an existing entry renamed? */
1633 int wasChanged; /* are the attributes changed? */
1634 int attrs; /* how many attribute types are in the ads list */
1635 AttributeDescription **ads;
1643 Modifications** modlist,
1645 struct berval* syncUUID,
1646 struct sync_cookie* syncCookie_req,
1647 struct berval* syncCSN )
1649 Backend *be = op->o_bd;
1650 slap_callback cb = { NULL, NULL, NULL, NULL };
1651 struct berval *syncuuid_bv = NULL;
1652 struct berval syncUUID_strrep = BER_BVNULL;
1653 struct berval uuid_bv = BER_BVNULL;
1655 SlapReply rs_search = {REP_RESULT};
1656 SlapReply rs_delete = {REP_RESULT};
1657 SlapReply rs_add = {REP_RESULT};
1658 SlapReply rs_modify = {REP_RESULT};
1660 #ifdef LDAP_COMP_MATCH
1661 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1663 AttributeAssertion ava = { NULL, BER_BVNULL };
1665 int rc = LDAP_SUCCESS;
1666 int ret = LDAP_SUCCESS;
1668 struct berval pdn = BER_BVNULL;
1672 switch( syncstate ) {
1673 case LDAP_SYNC_PRESENT:
1674 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1676 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1679 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1681 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1683 case LDAP_SYNC_DELETE:
1684 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1686 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1688 case LDAP_SYNC_MODIFY:
1689 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1691 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1694 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: rid %03d %s\n",
1696 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1699 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1700 if ( !si->si_refreshPresent ) {
1701 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1702 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1703 syncuuid_cmp, avl_dup_error );
1707 if ( syncstate == LDAP_SYNC_PRESENT ) {
1709 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1710 if ( entry == NULL ) {
1715 f.f_choice = LDAP_FILTER_EQUALITY;
1717 ava.aa_desc = slap_schema.si_ad_entryUUID;
1718 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1719 ava.aa_value = *syncUUID;
1720 op->ors_filter = &f;
1722 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1723 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1724 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1725 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1726 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1727 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1728 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1729 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1731 op->o_tag = LDAP_REQ_SEARCH;
1732 op->ors_scope = LDAP_SCOPE_SUBTREE;
1733 op->ors_deref = LDAP_DEREF_NEVER;
1735 /* get the entry for this UUID */
1736 op->o_req_dn = si->si_base;
1737 op->o_req_ndn = si->si_base;
1739 op->o_time = slap_get_time();
1740 op->ors_tlimit = SLAP_NO_LIMIT;
1743 op->ors_attrs = slap_anlist_all_attributes;
1744 op->ors_attrsonly = 0;
1746 /* set callback function */
1747 op->o_callback = &cb;
1748 cb.sc_response = dn_callback;
1749 cb.sc_private = &dni;
1750 dni.new_entry = entry;
1752 if ( limits_check( op, &rs_search ) == 0 ) {
1753 rc = be->be_search( op, &rs_search );
1754 Debug( LDAP_DEBUG_SYNC,
1755 "syncrepl_entry: rid %03d be_search (%d)\n",
1756 si->si_rid, rc, 0 );
1759 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1760 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1763 cb.sc_response = null_callback;
1766 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1767 Debug( LDAP_DEBUG_SYNC,
1768 "syncrepl_entry: rid %03d %s\n",
1769 si->si_rid, entry->e_name.bv_val, 0 );
1771 Debug( LDAP_DEBUG_SYNC,
1772 "syncrepl_entry: rid %03d %s\n",
1773 si->si_rid, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1776 if ( syncstate != LDAP_SYNC_DELETE ) {
1777 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1780 /* add if missing */
1781 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1782 &syncUUID_strrep, syncUUID );
1784 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1785 /* replace only if necessary */
1786 if ( a->a_nvals != a->a_vals ) {
1787 ber_memfree( a->a_nvals[0].bv_val );
1788 ber_dupbv( &a->a_nvals[0], syncUUID );
1790 ber_memfree( a->a_vals[0].bv_val );
1791 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1793 /* Don't save the contextCSN on the inooming context entry,
1794 * we'll write it when syncrepl_updateCookie eventually
1795 * gets called. (ITS#4622)
1797 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1798 &be->be_nsuffix[0] )) {
1800 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1802 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1811 slap_op_time( &op->o_time, &op->o_tincr );
1812 switch ( syncstate ) {
1814 case LDAP_SYNC_MODIFY:
1816 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1818 op->o_csn = a->a_vals[0];
1821 if ( BER_BVISNULL( &dni.dn )) {
1823 op->o_req_dn = entry->e_name;
1824 op->o_req_ndn = entry->e_nname;
1825 op->o_tag = LDAP_REQ_ADD;
1828 rc = be->be_add( op, &rs_add );
1829 Debug( LDAP_DEBUG_SYNC,
1830 "syncrepl_entry: rid %03d be_add (%d)\n",
1831 si->si_rid, rc, 0 );
1832 switch ( rs_add.sr_err ) {
1834 be_entry_release_w( op, entry );
1839 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1840 * only if the suffix entry is not present */
1841 case LDAP_NO_SUCH_OBJECT:
1842 syncrepl_add_glue( op, entry );
1846 /* if an entry was added via syncrepl_add_glue(),
1847 * it likely has no entryUUID, so the previous
1848 * be_search() doesn't find it. In this case,
1849 * give syncrepl a chance to modify it. Also
1850 * allow for entries that were recreated with the
1851 * same DN but a different entryUUID.
1853 case LDAP_ALREADY_EXISTS:
1855 Operation op2 = *op;
1856 SlapReply rs2 = { 0 };
1857 slap_callback cb2 = { 0 };
1859 op2.o_tag = LDAP_REQ_SEARCH;
1860 op2.o_req_dn = entry->e_name;
1861 op2.o_req_ndn = entry->e_nname;
1862 op2.ors_scope = LDAP_SCOPE_BASE;
1863 op2.ors_deref = LDAP_DEREF_NEVER;
1864 op2.ors_attrs = slap_anlist_all_attributes;
1865 op2.ors_attrsonly = 0;
1866 op2.ors_limit = NULL;
1868 op2.ors_tlimit = SLAP_NO_LIMIT;
1870 f.f_choice = LDAP_FILTER_PRESENT;
1871 f.f_desc = slap_schema.si_ad_objectClass;
1872 op2.ors_filter = &f;
1873 op2.ors_filterstr = generic_filterstr;
1875 op2.o_callback = &cb2;
1876 cb2.sc_response = dn_callback;
1877 cb2.sc_private = &dni;
1879 be->be_search( &op2, &rs2 );
1882 slap_op_time( &op->o_time, &op->o_tincr );
1888 Debug( LDAP_DEBUG_ANY,
1889 "syncrepl_entry: rid %03d be_add failed (%d)\n",
1890 si->si_rid, rs_add.sr_err, 0 );
1897 op->o_req_dn = dni.dn;
1898 op->o_req_ndn = dni.ndn;
1899 if ( dni.renamed ) {
1900 struct berval noldp, newp, nnewp;
1902 op->o_tag = LDAP_REQ_MODRDN;
1903 dnRdn( &entry->e_name, &op->orr_newrdn );
1904 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1906 dnParent( &dni.ndn, &noldp );
1907 dnParent( &entry->e_nname, &nnewp );
1908 if ( !dn_match( &noldp, &nnewp )) {
1909 dnParent( &entry->e_name, &newp );
1910 op->orr_newSup = &newp;
1911 op->orr_nnewSup = &nnewp;
1913 op->orr_newSup = NULL;
1914 op->orr_nnewSup = NULL;
1916 op->orr_deleteoldrdn = 0;
1917 rc = be->be_modrdn( op, &rs_modify );
1918 Debug( LDAP_DEBUG_SYNC,
1919 "syncrepl_entry: rid %03d be_modrdn (%d)\n",
1920 si->si_rid, rc, 0 );
1921 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1922 op->o_req_dn = entry->e_name;
1923 op->o_req_ndn = entry->e_nname;
1928 if ( dni.wasChanged )
1929 slap_op_time( &op->o_time, &op->o_tincr );
1931 if ( dni.wasChanged ) {
1932 Modifications *mod, *modhead = NULL;
1933 Modifications *modtail = NULL;
1936 op->o_tag = LDAP_REQ_MODIFY;
1938 assert( *modlist != NULL );
1940 /* Delete all the old attrs */
1941 for ( i = 0; i < dni.attrs; i++ ) {
1942 mod = ch_malloc( sizeof( Modifications ) );
1943 mod->sml_op = LDAP_MOD_DELETE;
1945 mod->sml_desc = dni.ads[i];
1946 mod->sml_type = mod->sml_desc->ad_cname;
1947 mod->sml_values = NULL;
1948 mod->sml_nvalues = NULL;
1949 if ( !modhead ) modhead = mod;
1951 modtail->sml_next = mod;
1956 /* Append passed in list to ours */
1958 modtail->sml_next = *modlist;
1964 /* Find end of this list */
1965 for ( ; mod != NULL; mod = mod->sml_next ) {
1969 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1970 mod->sml_op = LDAP_MOD_REPLACE;
1972 mod->sml_desc = slap_schema.si_ad_entryUUID;
1973 mod->sml_type = mod->sml_desc->ad_cname;
1974 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1975 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1976 ber_dupbv( &uuid_bv, syncUUID );
1977 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1978 modtail->sml_next = mod;
1980 op->o_tag = LDAP_REQ_MODIFY;
1981 op->orm_modlist = *modlist;
1983 rc = be->be_modify( op, &rs_modify );
1984 Debug( LDAP_DEBUG_SYNC,
1985 "syncrepl_entry: rid %03d be_modify (%d)\n",
1986 si->si_rid, rc, 0 );
1987 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1988 Debug( LDAP_DEBUG_ANY,
1989 "syncrepl_entry: rid %03d be_modify failed (%d)\n",
1990 si->si_rid, rs_modify.sr_err, 0 );
1995 case LDAP_SYNC_DELETE :
1996 if ( !BER_BVISNULL( &dni.dn )) {
1997 op->o_req_dn = dni.dn;
1998 op->o_req_ndn = dni.ndn;
1999 op->o_tag = LDAP_REQ_DELETE;
2000 rc = be->be_delete( op, &rs_delete );
2001 Debug( LDAP_DEBUG_SYNC,
2002 "syncrepl_entry: rid %03d be_delete (%d)\n",
2003 si->si_rid, rc, 0 );
2005 while ( rs_delete.sr_err == LDAP_SUCCESS
2006 && op->o_delete_glue_parent ) {
2007 op->o_delete_glue_parent = 0;
2008 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2009 slap_callback cb = { NULL };
2010 cb.sc_response = slap_null_cb;
2011 dnParent( &op->o_req_ndn, &pdn );
2013 op->o_req_ndn = pdn;
2014 op->o_callback = &cb;
2015 op->o_bd->be_delete( op, &rs_delete );
2025 Debug( LDAP_DEBUG_ANY,
2026 "syncrepl_entry: rid %03d unknown syncstate\n", si->si_rid, 0, 0 );
2032 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2033 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2034 BER_BVZERO( &syncUUID_strrep );
2037 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2039 if ( !BER_BVISNULL( &dni.ndn ) ) {
2040 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2042 if ( !BER_BVISNULL( &dni.dn ) ) {
2043 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2045 BER_BVZERO( &op->o_csn );
2049 static struct berval gcbva[] = {
2055 #define NP_DELETE_ONE 2
2058 syncrepl_del_nonpresent(
2062 struct berval *cookiecsn )
2064 Backend* be = op->o_bd;
2065 slap_callback cb = { NULL };
2066 SlapReply rs_search = {REP_RESULT};
2067 SlapReply rs_delete = {REP_RESULT};
2068 SlapReply rs_modify = {REP_RESULT};
2069 struct nonpresent_entry *np_list, *np_prev;
2071 AttributeName an[2];
2073 struct berval pdn = BER_BVNULL;
2076 op->o_req_dn = si->si_base;
2077 op->o_req_ndn = si->si_base;
2079 cb.sc_response = nonpresent_callback;
2082 op->o_callback = &cb;
2083 op->o_tag = LDAP_REQ_SEARCH;
2084 op->ors_scope = si->si_scope;
2085 op->ors_deref = LDAP_DEREF_NEVER;
2086 op->o_time = slap_get_time();
2087 op->ors_tlimit = SLAP_NO_LIMIT;
2092 #ifdef LDAP_COMP_MATCH
2093 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2095 AttributeAssertion eq = { NULL, BER_BVNULL };
2099 op->ors_attrsonly = 1;
2100 op->ors_attrs = slap_anlist_no_attrs;
2101 op->ors_limit = NULL;
2102 op->ors_filter = &uf;
2105 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2107 uf.f_choice = LDAP_FILTER_EQUALITY;
2108 si->si_refreshDelete |= NP_DELETE_ONE;
2110 for (i=0; uuids[i].bv_val; i++) {
2112 uf.f_av_value = uuids[i];
2113 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2114 rc = be->be_search( op, &rs_search );
2115 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2117 si->si_refreshDelete ^= NP_DELETE_ONE;
2119 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2120 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2121 an[0].an_desc = slap_schema.si_ad_entryUUID;
2123 op->ors_slimit = SLAP_NO_LIMIT;
2124 op->ors_attrsonly = 0;
2125 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2126 op->ors_filterstr = si->si_filterstr;
2127 op->o_nocaching = 1;
2129 if ( limits_check( op, &rs_search ) == 0 ) {
2130 rc = be->be_search( op, &rs_search );
2132 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2135 op->o_nocaching = 0;
2137 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2139 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2142 csn = si->si_syncCookie.ctxcsn;
2144 slap_queue_csn( op, &csn );
2146 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2147 while ( np_list != NULL ) {
2148 LDAP_LIST_REMOVE( np_list, npe_link );
2150 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2151 op->o_tag = LDAP_REQ_DELETE;
2152 op->o_callback = &cb;
2153 cb.sc_response = null_callback;
2155 op->o_req_dn = *np_prev->npe_name;
2156 op->o_req_ndn = *np_prev->npe_nname;
2157 rc = op->o_bd->be_delete( op, &rs_delete );
2158 Debug( LDAP_DEBUG_SYNC,
2159 "syncrepl_del_nonpresent: rid %03d be_delete %s (%d)\n",
2160 si->si_rid, op->o_req_dn.bv_val, rc );
2162 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2163 Modifications mod1, mod2;
2164 mod1.sml_op = LDAP_MOD_REPLACE;
2166 mod1.sml_desc = slap_schema.si_ad_objectClass;
2167 mod1.sml_type = mod1.sml_desc->ad_cname;
2168 mod1.sml_values = &gcbva[0];
2169 mod1.sml_nvalues = NULL;
2170 mod1.sml_next = &mod2;
2172 mod2.sml_op = LDAP_MOD_REPLACE;
2174 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2175 mod2.sml_type = mod2.sml_desc->ad_cname;
2176 mod2.sml_values = &gcbva[1];
2177 mod2.sml_nvalues = NULL;
2178 mod2.sml_next = NULL;
2180 op->o_tag = LDAP_REQ_MODIFY;
2181 op->orm_modlist = &mod1;
2183 rc = be->be_modify( op, &rs_modify );
2184 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2187 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2188 op->o_delete_glue_parent ) {
2189 op->o_delete_glue_parent = 0;
2190 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2191 slap_callback cb = { NULL };
2192 cb.sc_response = slap_null_cb;
2193 dnParent( &op->o_req_ndn, &pdn );
2195 op->o_req_ndn = pdn;
2196 op->o_callback = &cb;
2197 /* give it a root privil ? */
2198 op->o_bd->be_delete( op, &rs_delete );
2204 op->o_delete_glue_parent = 0;
2206 ber_bvfree( np_prev->npe_name );
2207 ber_bvfree( np_prev->npe_nname );
2211 slap_graduate_commit_csn( op );
2212 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2213 BER_BVZERO( &op->o_csn );
2224 Backend *be = op->o_bd;
2225 slap_callback cb = { NULL };
2230 struct berval dn = BER_BVNULL;
2231 struct berval ndn = BER_BVNULL;
2233 SlapReply rs_add = {REP_RESULT};
2234 struct berval ptr, nptr;
2237 op->o_tag = LDAP_REQ_ADD;
2238 op->o_callback = &cb;
2239 cb.sc_response = null_callback;
2240 cb.sc_private = NULL;
2245 /* count RDNs in suffix */
2246 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2247 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2249 ptr.bv_len -= comma - ptr.bv_val;
2259 /* Start with BE suffix */
2261 for ( i = 0; i < suffrdns; i++ ) {
2262 comma = ber_bvrchr( &ptr, ',' );
2263 if ( comma != NULL ) {
2264 ptr.bv_len = comma - ptr.bv_val;
2271 if ( !BER_BVISEMPTY( &ptr ) ) {
2272 dn.bv_len -= ptr.bv_len + 1;
2273 dn.bv_val += ptr.bv_len + 1;
2276 /* the normalizedDNs are always the same length, no counting
2280 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2281 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2282 ndn.bv_len = be->be_nsuffix[0].bv_len;
2284 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2290 while ( ndn.bv_val > e->e_nname.bv_val ) {
2291 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2292 ber_dupbv( &glue->e_name, &dn );
2293 ber_dupbv( &glue->e_nname, &ndn );
2295 a = ch_calloc( 1, sizeof( Attribute ));
2296 a->a_desc = slap_schema.si_ad_objectClass;
2298 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2299 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2300 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2301 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2303 a->a_nvals = a->a_vals;
2305 a->a_next = glue->e_attrs;
2308 a = ch_calloc( 1, sizeof( Attribute ));
2309 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2311 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2312 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2313 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2315 a->a_nvals = a->a_vals;
2317 a->a_next = glue->e_attrs;
2320 op->o_req_dn = glue->e_name;
2321 op->o_req_ndn = glue->e_nname;
2323 rc = be->be_add ( op, &rs_add );
2324 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2325 be_entry_release_w( op, glue );
2327 /* incl. ALREADY EXIST */
2331 /* Move to next child */
2332 comma = ber_bvrchr( &ptr, ',' );
2333 if ( comma == NULL ) {
2336 ptr.bv_len = comma - ptr.bv_val;
2338 dn.bv_val = ++comma;
2339 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2341 comma = ber_bvrchr( &nptr, ',' );
2342 assert( comma != NULL );
2343 nptr.bv_len = comma - nptr.bv_val;
2345 ndn.bv_val = ++comma;
2346 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2349 op->o_req_dn = e->e_name;
2350 op->o_req_ndn = e->e_nname;
2352 rc = be->be_add ( op, &rs_add );
2353 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2354 be_entry_release_w( op, e );
2363 syncrepl_updateCookie(
2367 struct sync_cookie *syncCookie )
2369 Backend *be = op->o_bd;
2370 Modifications mod = { { 0 } };
2371 struct berval vals[ 2 ];
2375 slap_callback cb = { NULL };
2376 SlapReply rs_modify = {REP_RESULT};
2378 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2379 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2381 mod.sml_op = LDAP_MOD_REPLACE;
2382 mod.sml_desc = slap_schema.si_ad_contextCSN;
2383 mod.sml_type = mod.sml_desc->ad_cname;
2384 mod.sml_values = vals;
2385 vals[0] = si->si_syncCookie.ctxcsn;
2386 vals[1].bv_val = NULL;
2389 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2391 op->o_tag = LDAP_REQ_MODIFY;
2393 assert( si->si_rid < 1000 );
2395 cb.sc_response = null_callback;
2398 op->o_callback = &cb;
2399 op->o_req_dn = op->o_bd->be_suffix[0];
2400 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2402 /* update contextCSN */
2403 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2404 op->orm_modlist = &mod;
2405 rc = be->be_modify( op, &rs_modify );
2408 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2409 Debug( LDAP_DEBUG_ANY,
2410 "syncrepl_updateCookie: rid %03d be_modify failed (%d)\n",
2411 si->si_rid, rs_modify.sr_err, 0 );
2414 slap_graduate_commit_csn( op );
2415 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2416 BER_BVZERO( &op->o_csn );
2417 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2427 dninfo *dni = op->o_callback->sc_private;
2429 if ( rs->sr_type == REP_SEARCH ) {
2430 if ( !BER_BVISNULL( &dni->dn ) ) {
2431 Debug( LDAP_DEBUG_ANY,
2432 "dn_callback: consistency error - "
2433 "entryUUID is not unique\n", 0, 0, 0 );
2435 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2436 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2437 /* If there is a new entry, see if it differs from the old.
2438 * We compare the non-normalized values so that cosmetic changes
2439 * in the provider are always propagated.
2441 if ( dni->new_entry ) {
2442 Attribute *old, *new;
2445 /* Did the DN change? Note that we don't explicitly try to
2446 * discover if the deleteOldRdn argument applies here. It
2447 * would save an unnecessary Modify if we detected it, but
2448 * that's a fair amount of trouble to compare the two attr
2449 * lists in detail. (Just test normalized DN; we ignore
2450 * insignificant changes here.)
2452 if ( !dn_match( &rs->sr_entry->e_nname,
2453 &dni->new_entry->e_nname ) )
2458 for ( i = 0, old = rs->sr_entry->e_attrs;
2460 i++, old = old->a_next )
2465 /* We assume that attributes are saved in the same order
2466 * in the remote and local databases. So if we walk through
2467 * the attributeDescriptions one by one they should match in
2468 * lock step. If not, we signal a change. Otherwise we test
2471 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2473 old = old->a_next, new = new->a_next )
2475 if ( old->a_desc != new->a_desc ) {
2476 dni->wasChanged = 1;
2479 for ( i = 0; ; i++ ) {
2481 nold = BER_BVISNULL( &old->a_vals[i] );
2482 nnew = BER_BVISNULL( &new->a_vals[i] );
2483 /* If both are empty, stop looking */
2484 if ( nold && nnew ) {
2487 /* If they are different, stop looking */
2488 if ( nold != nnew ) {
2489 dni->wasChanged = 1;
2492 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2493 dni->wasChanged = 1;
2497 if ( dni->wasChanged ) break;
2499 if ( dni->wasChanged ) {
2500 dni->ads = op->o_tmpalloc( dni->attrs *
2501 sizeof(AttributeDescription *), op->o_tmpmemctx );
2503 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2504 dni->ads[i] = old->a_desc;
2510 } else if ( rs->sr_type == REP_RESULT ) {
2511 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2512 Debug( LDAP_DEBUG_ANY,
2513 "dn_callback: consistency error - "
2514 "entryUUID is not unique\n", 0, 0, 0 );
2518 return LDAP_SUCCESS;
2522 nonpresent_callback(
2526 syncinfo_t *si = op->o_callback->sc_private;
2529 struct berval* present_uuid = NULL;
2530 struct nonpresent_entry *np_entry;
2532 if ( rs->sr_type == REP_RESULT ) {
2533 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2534 si->si_presentlist = NULL;
2536 } else if ( rs->sr_type == REP_SEARCH ) {
2537 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2538 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2540 if ( a == NULL ) return 0;
2542 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2546 if ( present_uuid == NULL ) {
2547 np_entry = (struct nonpresent_entry *)
2548 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2549 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2550 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2551 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2554 avl_delete( &si->si_presentlist,
2555 &a->a_nvals[0], syncuuid_cmp );
2556 ch_free( present_uuid->bv_val );
2557 ch_free( present_uuid );
2560 return LDAP_SUCCESS;
2568 if ( rs->sr_err != LDAP_SUCCESS &&
2569 rs->sr_err != LDAP_REFERRAL &&
2570 rs->sr_err != LDAP_ALREADY_EXISTS &&
2571 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2572 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2574 Debug( LDAP_DEBUG_ANY,
2575 "null_callback: error code 0x%x\n",
2578 return LDAP_SUCCESS;
2581 static struct berval *
2582 slap_uuidstr_from_normalized(
2583 struct berval* uuidstr,
2584 struct berval* normalized,
2588 unsigned char nibble;
2591 if ( normalized == NULL ) return NULL;
2592 if ( normalized->bv_len != 16 ) return NULL;
2597 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2598 if ( new == NULL ) {
2605 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2606 if ( new != uuidstr ) {
2607 slap_sl_free( new, ctx );
2612 for ( i = 0; i < 16; i++ ) {
2613 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2614 new->bv_val[(i<<1)+d] = '-';
2618 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2619 if ( nibble < 10 ) {
2620 new->bv_val[(i<<1)+d] = nibble + '0';
2622 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2625 nibble = (normalized->bv_val[i]) & 0xF;
2626 if ( nibble < 10 ) {
2627 new->bv_val[(i<<1)+d+1] = nibble + '0';
2629 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2633 new->bv_val[new->bv_len] = '\0';
2638 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2640 const struct berval *uuid1 = v_uuid1;
2641 const struct berval *uuid2 = v_uuid2;
2642 int rc = uuid1->bv_len - uuid2->bv_len;
2643 if ( rc ) return rc;
2644 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2648 avl_ber_bvfree( void *v_bv )
2650 struct berval *bv = (struct berval *)v_bv;
2652 if( v_bv == NULL ) return;
2653 if ( !BER_BVISNULL( bv ) ) {
2654 ch_free( bv->bv_val );
2656 ch_free( (char *) bv );
2660 syncinfo_free( syncinfo_t *sie )
2663 if ( sie->si_conn_setup ) {
2665 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2666 connection_client_stop( s );
2667 sie->si_conn_setup = 0;
2669 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2672 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2674 bindconf_free( &sie->si_bindconf );
2676 if ( sie->si_filterstr.bv_val ) {
2677 ch_free( sie->si_filterstr.bv_val );
2679 if ( sie->si_base.bv_val ) {
2680 ch_free( sie->si_base.bv_val );
2682 if ( sie->si_attrs ) {
2684 while ( sie->si_attrs[i] != NULL ) {
2685 ch_free( sie->si_attrs[i] );
2688 ch_free( sie->si_attrs );
2690 if ( sie->si_exattrs ) {
2692 while ( sie->si_exattrs[i] != NULL ) {
2693 ch_free( sie->si_exattrs[i] );
2696 ch_free( sie->si_exattrs );
2698 if ( sie->si_anlist ) {
2700 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2701 ch_free( sie->si_anlist[i].an_name.bv_val );
2704 ch_free( sie->si_anlist );
2706 if ( sie->si_exanlist ) {
2708 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2709 ch_free( sie->si_exanlist[i].an_name.bv_val );
2712 ch_free( sie->si_exanlist );
2714 if ( sie->si_retryinterval ) {
2715 ch_free( sie->si_retryinterval );
2717 if ( sie->si_retrynum ) {
2718 ch_free( sie->si_retrynum );
2720 if ( sie->si_retrynum_init ) {
2721 ch_free( sie->si_retrynum_init );
2723 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2724 if ( sie->si_presentlist ) {
2725 avl_free( sie->si_presentlist, avl_ber_bvfree );
2727 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2728 struct nonpresent_entry* npe;
2729 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2730 LDAP_LIST_REMOVE( npe, npe_link );
2731 if ( npe->npe_name ) {
2732 if ( npe->npe_name->bv_val ) {
2733 ch_free( npe->npe_name->bv_val );
2735 ch_free( npe->npe_name );
2737 if ( npe->npe_nname ) {
2738 if ( npe->npe_nname->bv_val ) {
2739 ch_free( npe->npe_nname->bv_val );
2741 ch_free( npe->npe_nname );
2750 /* NOTE: used & documented in slapd.conf(5) */
2752 #define PROVIDERSTR "provider"
2753 #define SCHEMASTR "schemachecking"
2754 #define FILTERSTR "filter"
2755 #define SEARCHBASESTR "searchbase"
2756 #define SCOPESTR "scope"
2757 #define ATTRSONLYSTR "attrsonly"
2758 #define ATTRSSTR "attrs"
2759 #define TYPESTR "type"
2760 #define INTERVALSTR "interval"
2761 #define RETRYSTR "retry"
2762 #define SLIMITSTR "sizelimit"
2763 #define TLIMITSTR "timelimit"
2764 #define SYNCDATASTR "syncdata"
2766 /* FIXME: undocumented */
2767 #define LOGBASESTR "logbase"
2768 #define LOGFILTERSTR "logfilter"
2769 #define OLDAUTHCSTR "bindprincipal"
2770 #define EXATTRSSTR "exattrs"
2771 #define MANAGEDSAITSTR "manageDSAit"
2774 #define LASTMODSTR "lastmod"
2775 #define LMGENSTR "gen"
2776 #define LMNOSTR "no"
2777 #define LMREQSTR "req"
2778 #define SRVTABSTR "srvtab"
2779 #define SUFFIXSTR "suffix"
2782 #define GOT_ID 0x0001
2783 #define GOT_PROVIDER 0x0002
2784 #define GOT_BASE 0x0004
2787 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2793 { BER_BVC("base"), LDAP_SCOPE_BASE },
2794 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2795 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2796 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2797 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2798 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2799 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2803 static slap_verbmasks datamodes[] = {
2804 { BER_BVC("default"), SYNCDATA_DEFAULT },
2805 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2806 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2811 parse_syncrepl_line(
2819 for ( i = 1; i < c->argc; i++ ) {
2820 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2821 STRLENOF( IDSTR "=" ) ) )
2824 /* '\0' string terminator accounts for '=' */
2825 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2826 if ( lutil_atoi( &tmp, val ) != 0 ) {
2827 snprintf( c->msg, sizeof( c->msg ),
2828 "Error: parse_syncrepl_line: "
2829 "unable to parse syncrepl id \"%s\"", val );
2830 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2833 if ( tmp >= 1000 || tmp < 0 ) {
2834 snprintf( c->msg, sizeof( c->msg ),
2835 "Error: parse_syncrepl_line: "
2836 "syncrepl id %d is out of range [0..999]", tmp );
2837 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2842 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2843 STRLENOF( PROVIDERSTR "=" ) ) )
2845 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2846 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2847 gots |= GOT_PROVIDER;
2848 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2849 STRLENOF( SCHEMASTR "=" ) ) )
2851 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2852 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2853 si->si_schemachecking = 1;
2854 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2855 si->si_schemachecking = 0;
2857 si->si_schemachecking = 1;
2859 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2860 STRLENOF( FILTERSTR "=" ) ) )
2862 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2863 if ( si->si_filterstr.bv_val )
2864 ch_free( si->si_filterstr.bv_val );
2865 ber_str2bv( val, 0, 1, &si->si_filterstr );
2866 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2867 STRLENOF( LOGFILTERSTR "=" ) ) )
2869 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2870 if ( si->si_logfilterstr.bv_val )
2871 ch_free( si->si_logfilterstr.bv_val );
2872 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2873 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2874 STRLENOF( SEARCHBASESTR "=" ) ) )
2879 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2880 if ( si->si_base.bv_val ) {
2881 ch_free( si->si_base.bv_val );
2883 ber_str2bv( val, 0, 0, &bv );
2884 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2885 if ( rc != LDAP_SUCCESS ) {
2886 snprintf( c->msg, sizeof( c->msg ),
2887 "Invalid base DN \"%s\": %d (%s)",
2888 val, rc, ldap_err2string( rc ) );
2889 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2892 if ( select_backend( &si->si_base, 0, 0 ) != c->be ) {
2893 ber_memfree( si->si_base.bv_val );
2894 snprintf( c->msg, sizeof( c->msg ),
2895 "Base DN \"%s\" is not within the database naming context",
2897 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2901 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2902 STRLENOF( LOGBASESTR "=" ) ) )
2907 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2908 if ( si->si_logbase.bv_val ) {
2909 ch_free( si->si_logbase.bv_val );
2911 ber_str2bv( val, 0, 0, &bv );
2912 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2913 if ( rc != LDAP_SUCCESS ) {
2914 snprintf( c->msg, sizeof( c->msg ),
2915 "Invalid logbase DN \"%s\": %d (%s)",
2916 val, rc, ldap_err2string( rc ) );
2917 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2920 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2921 STRLENOF( SCOPESTR "=" ) ) )
2924 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2925 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2926 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2927 si->si_scope = scopes[j].val;
2931 if ( BER_BVISNULL(&scopes[j].key) ) {
2932 snprintf( c->msg, sizeof( c->msg ),
2933 "Error: parse_syncrepl_line: "
2934 "unknown scope \"%s\"", val);
2935 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2938 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2939 STRLENOF( ATTRSONLYSTR ) ) )
2941 si->si_attrsonly = 1;
2942 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2943 STRLENOF( ATTRSSTR "=" ) ) )
2945 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2946 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2948 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2949 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2950 if ( si->si_anlist == NULL ) {
2951 ch_free( attr_fname );
2954 si->si_anfile = attr_fname;
2956 char *str, *s, *next;
2957 char delimstr[] = " ,\t";
2958 str = ch_strdup( val );
2959 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2961 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2963 if ( strlen(s) == 1 && *s == '*' ) {
2964 si->si_allattrs = 1;
2965 *(val + ( s - str )) = delimstr[0];
2967 if ( strlen(s) == 1 && *s == '+' ) {
2968 si->si_allopattrs = 1;
2969 *(val + ( s - str )) = delimstr[0];
2973 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2974 if ( si->si_anlist == NULL ) {
2978 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2979 STRLENOF( EXATTRSSTR "=" ) ) )
2981 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2982 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2984 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2985 si->si_exanlist = file2anlist(
2986 si->si_exanlist, attr_fname, " ,\t" );
2987 if ( si->si_exanlist == NULL ) {
2988 ch_free( attr_fname );
2991 ch_free( attr_fname );
2993 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2994 if ( si->si_exanlist == NULL ) {
2998 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2999 STRLENOF( TYPESTR "=" ) ) )
3001 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3002 if ( !strncasecmp( val, "refreshOnly",
3003 STRLENOF("refreshOnly") ) )
3005 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3006 } else if ( !strncasecmp( val, "refreshAndPersist",
3007 STRLENOF("refreshAndPersist") ) )
3009 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3010 si->si_interval = 60;
3012 snprintf( c->msg, sizeof( c->msg ),
3013 "Error: parse_syncrepl_line: "
3014 "unknown sync type \"%s\"", val);
3015 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3018 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3019 STRLENOF( INTERVALSTR "=" ) ) )
3021 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3022 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3023 si->si_interval = 0;
3024 } else if ( strchr( val, ':' ) != NULL ) {
3025 char *next, *ptr = val;
3026 unsigned dd, hh, mm, ss;
3028 /* NOTE: the test for ptr[ 0 ] == '-'
3029 * should go before the call to strtoul() */
3030 dd = strtoul( ptr, &next, 10 );
3031 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3032 snprintf( c->msg, sizeof( c->msg ),
3033 "Error: parse_syncrepl_line: "
3034 "invalid interval \"%s\", unable to parse days", val );
3035 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3039 hh = strtoul( ptr, &next, 10 );
3040 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3041 snprintf( c->msg, sizeof( c->msg ),
3042 "Error: parse_syncrepl_line: "
3043 "invalid interval \"%s\", unable to parse hours", val );
3044 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3048 mm = strtoul( ptr, &next, 10 );
3049 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3050 snprintf( c->msg, sizeof( c->msg ),
3051 "Error: parse_syncrepl_line: "
3052 "invalid interval \"%s\", unable to parse minutes", val );
3053 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3057 ss = strtoul( ptr, &next, 10 );
3058 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3059 snprintf( c->msg, sizeof( c->msg ),
3060 "Error: parse_syncrepl_line: "
3061 "invalid interval \"%s\", unable to parse seconds", val );
3062 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3065 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3069 if ( lutil_parse_time( val, &t ) != 0 ) {
3070 snprintf( c->msg, sizeof( c->msg ),
3071 "Error: parse_syncrepl_line: "
3072 "invalid interval \"%s\"", val );
3073 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3076 si->si_interval = (time_t)t;
3078 if ( si->si_interval < 0 ) {
3079 snprintf( c->msg, sizeof( c->msg ),
3080 "Error: parse_syncrepl_line: "
3081 "invalid interval \"%ld\"",
3082 (long) si->si_interval);
3083 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3086 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3087 STRLENOF( RETRYSTR "=" ) ) )
3092 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3093 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3094 retry_list[0] = NULL;
3096 slap_str2clist( &retry_list, val, " ,\t" );
3098 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3101 snprintf( c->msg, sizeof( c->msg ),
3102 "Error: incomplete syncrepl retry list" );
3103 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3104 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3105 ch_free( retry_list[k] );
3107 ch_free( retry_list );
3110 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3111 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3112 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3113 for ( j = 0; j < n; j++ ) {
3115 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3116 snprintf( c->msg, sizeof( c->msg ),
3117 "Error: invalid retry interval \"%s\" (#%d)",
3118 retry_list[j*2], j );
3119 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3120 /* do some cleanup */
3123 si->si_retryinterval[j] = (time_t)t;
3124 if ( *retry_list[j*2+1] == '+' ) {
3125 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3126 si->si_retrynum[j] = RETRYNUM_FOREVER;
3130 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3131 || si->si_retrynum_init[j] <= 0 )
3133 snprintf( c->msg, sizeof( c->msg ),
3134 "Error: invalid initial retry number \"%s\" (#%d)",
3135 retry_list[j*2+1], j );
3136 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3137 /* do some cleanup */
3140 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3141 || si->si_retrynum[j] <= 0 )
3143 snprintf( c->msg, sizeof( c->msg ),
3144 "Error: invalid retry number \"%s\" (#%d)",
3145 retry_list[j*2+1], j );
3146 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3147 /* do some cleanup */
3152 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3153 si->si_retrynum[j] = RETRYNUM_TAIL;
3154 si->si_retryinterval[j] = 0;
3156 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3157 ch_free( retry_list[k] );
3159 ch_free( retry_list );
3160 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3161 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3163 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3164 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3165 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3167 snprintf( c->msg, sizeof( c->msg ),
3168 "invalid manageDSAit value \"%s\".\n",
3170 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3173 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3174 STRLENOF( SLIMITSTR "=") ) )
3176 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3177 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3180 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3181 snprintf( c->msg, sizeof( c->msg ),
3182 "invalid size limit value \"%s\".\n",
3184 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3187 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3188 STRLENOF( TLIMITSTR "=" ) ) )
3190 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3191 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3194 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3195 snprintf( c->msg, sizeof( c->msg ),
3196 "invalid time limit value \"%s\".\n",
3198 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3201 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3202 STRLENOF( SYNCDATASTR "=" ) ) )
3204 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3205 si->si_syncdata = verb_to_mask( val, datamodes );
3206 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3207 snprintf( c->msg, sizeof( c->msg ),
3208 "Error: parse_syncrepl_line: "
3209 "unable to parse \"%s\"\n", c->argv[ i ] );
3210 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3215 if ( gots != GOT_ALL ) {
3216 snprintf( c->msg, sizeof( c->msg ),
3217 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3218 gots & GOT_ID ? "" : " "IDSTR,
3219 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3220 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3221 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3235 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3236 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3237 "operations required for syncrepl", c->be->be_type );
3238 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3241 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3242 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3243 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3246 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3249 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3253 si->si_bindconf.sb_tls = SB_TLS_OFF;
3254 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3255 si->si_schemachecking = 0;
3256 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3257 &si->si_filterstr );
3258 si->si_base.bv_val = NULL;
3259 si->si_scope = LDAP_SCOPE_SUBTREE;
3260 si->si_attrsonly = 0;
3261 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3262 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3263 si->si_attrs = NULL;
3264 si->si_allattrs = 0;
3265 si->si_allopattrs = 0;
3266 si->si_exattrs = NULL;
3267 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3268 si->si_interval = 86400;
3269 si->si_retryinterval = NULL;
3270 si->si_retrynum_init = NULL;
3271 si->si_retrynum = NULL;
3272 si->si_manageDSAit = 0;
3275 si->si_conn_setup = 0;
3277 si->si_presentlist = NULL;
3278 LDAP_LIST_INIT( &si->si_nonpresentlist );
3279 ldap_pvt_thread_mutex_init( &si->si_mutex );
3281 rc = parse_syncrepl_line( c, si );
3285 init_syncrepl( si );
3286 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3287 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3292 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3293 syncinfo_free( si );
3296 Debug( LDAP_DEBUG_CONFIG,
3297 "Config: ** successfully added syncrepl \"%s\"\n",
3298 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3299 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3300 if ( !si->si_schemachecking ) {
3301 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3303 c->be->be_syncinfo = si;
3309 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3311 struct berval bc, uri;
3312 char buf[BUFSIZ*2], *ptr;
3315 /* FIXME: we're not checking for buf[] overflow! */
3317 /* temporarily inhibit bindconf from printing URI */
3318 uri = si->si_bindconf.sb_uri;
3319 BER_BVZERO( &si->si_bindconf.sb_uri );
3320 bindconf_unparse( &si->si_bindconf, &bc );
3321 si->si_bindconf.sb_uri = uri;
3324 ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03d " PROVIDERSTR "=%s",
3325 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3326 if ( !BER_BVISNULL( &bc )) {
3327 ptr = lutil_strcopy( ptr, bc.bv_val );
3330 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3331 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3332 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3335 if ( !BER_BVISNULL( &si->si_base )) {
3336 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3337 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3340 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3341 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3342 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3345 if ( !BER_BVISNULL( &si->si_logbase )) {
3346 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3347 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3350 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3351 if ( si->si_scope == scopes[i].val ) {
3352 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3353 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3357 if ( si->si_attrsonly ) {
3358 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3360 if ( si->si_anfile ) {
3361 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3362 ptr = lutil_strcopy( ptr, si->si_anfile );
3363 } else if ( si->si_allattrs || si->si_allopattrs ||
3364 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3366 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3368 ptr = anlist_unparse( si->si_anlist, ptr );
3369 if ( si->si_allattrs ) {
3370 if ( old != ptr ) *ptr++ = ',';
3373 if ( si->si_allopattrs ) {
3374 if ( old != ptr ) *ptr++ = ',';
3379 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3380 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3381 ptr = anlist_unparse( si->si_exanlist, ptr );
3383 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3384 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3386 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3387 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3388 "refreshAndPersist" : "refreshOnly" );
3390 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3393 dd = si->si_interval;
3400 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3401 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3402 } else if ( si->si_retryinterval ) {
3404 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3405 for (i=0; si->si_retryinterval[i]; i++) {
3406 if ( space ) *ptr++ = ' ';
3408 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3409 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3412 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3417 if ( si->si_slimit ) {
3418 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3419 ptr += sprintf( ptr, "%d", si->si_slimit );
3422 if ( si->si_tlimit ) {
3423 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3424 ptr += sprintf( ptr, "%d", si->si_tlimit );
3427 if ( si->si_syncdata ) {
3428 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3429 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3430 ptr = lutil_strcopy( ptr, bc.bv_val );
3433 bc.bv_len = ptr - buf;
3435 ber_dupbv( bv, &bc );
3439 syncrepl_config( ConfigArgs *c )
3441 if (c->op == SLAP_CONFIG_EMIT) {
3442 if ( c->be->be_syncinfo ) {
3444 syncrepl_unparse( c->be->be_syncinfo, &bv );
3445 ber_bvarray_add( &c->rvalue_vals, &bv );
3449 } else if ( c->op == LDAP_MOD_DELETE ) {
3452 if ( c->be->be_syncinfo ) {
3453 re = c->be->be_syncinfo->si_re;
3455 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3456 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3457 ldap_pvt_runqueue_remove( &slapd_rq, re );
3459 syncinfo_free( c->be->be_syncinfo );
3460 c->be->be_syncinfo = NULL;
3462 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3465 if ( SLAP_SHADOW( c->be ) ) {
3466 Debug(LDAP_DEBUG_ANY, "%s: "
3467 "syncrepl: database already shadowed.\n",
3470 } else if ( add_syncrepl( c ) ) {
3473 return config_sync_shadow( c );