1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2008 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 #define RETRYNUM_FOREVER (-1) /* retry forever */
48 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
49 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
50 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
52 typedef struct syncinfo_s {
53 struct slap_backend_db *si_be;
56 slap_bindconf si_bindconf;
57 struct berval si_base;
58 struct berval si_logbase;
59 struct berval si_filterstr;
60 struct berval si_logfilterstr;
64 AttributeName *si_anlist;
65 AttributeName *si_exanlist;
70 int si_schemachecking;
71 int si_type; /* the active type */
72 int si_ctype; /* the configured type */
74 time_t *si_retryinterval;
75 int *si_retrynum_init;
77 struct sync_cookie si_syncCookie;
82 int si_refreshPresent;
87 Avlnode *si_presentlist;
89 LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
90 ldap_pvt_thread_mutex_t si_mutex;
93 static int syncuuid_cmp( const void *, const void * );
94 static void avl_ber_bvfree( void * );
95 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray, struct berval * );
96 static int syncrepl_message_to_op(
97 syncinfo_t *, Operation *, LDAPMessage * );
98 static int syncrepl_message_to_entry(
99 syncinfo_t *, Operation *, LDAPMessage *,
100 Modifications **, Entry **, int );
101 static int syncrepl_entry(
102 syncinfo_t *, Operation*, Entry*,
103 Modifications**,int, struct berval*,
104 struct sync_cookie *,
106 static void syncrepl_updateCookie(
107 syncinfo_t *, Operation *, struct berval *,
108 struct sync_cookie * );
109 static struct berval * slap_uuidstr_from_normalized(
110 struct berval *, struct berval *, void * );
112 /* callback functions */
113 static int dn_callback( struct slap_op *, struct slap_rep * );
114 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
115 static int null_callback( struct slap_op *, struct slap_rep * );
117 static AttributeDescription *sync_descs[4];
120 init_syncrepl(syncinfo_t *si)
123 char **attrs, **exattrs;
125 if ( !sync_descs[0] ) {
126 sync_descs[0] = slap_schema.si_ad_objectClass;
127 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
128 sync_descs[2] = slap_schema.si_ad_entryCSN;
129 sync_descs[3] = NULL;
132 if ( si->si_allattrs && si->si_allopattrs )
135 attrs = anlist2attrs( si->si_anlist );
138 if ( si->si_allattrs ) {
141 if ( !is_at_operational( at_find( attrs[i] ))) {
142 for ( j = i; attrs[j] != NULL; j++ ) {
145 attrs[j] = attrs[j+1];
151 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
152 attrs[i] = ch_strdup("*");
155 } else if ( si->si_allopattrs ) {
158 if ( is_at_operational( at_find( attrs[i] ))) {
159 for ( j = i; attrs[j] != NULL; j++ ) {
162 attrs[j] = attrs[j+1];
168 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
169 attrs[i] = ch_strdup("+");
173 for ( i = 0; sync_descs[i] != NULL; i++ ) {
176 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
177 for ( k = j; attrs[k] != NULL; k++ ) {
180 attrs[k] = attrs[k+1];
188 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
190 if ( si->si_allopattrs ) {
191 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
193 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
196 if ( attrs == NULL ) {
197 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0, 0, 0 );
201 if ( si->si_allopattrs ) {
202 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
204 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
205 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
213 if ( si->si_allattrs == si->si_allopattrs ) {
214 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
215 attrs[i++] = ch_strdup( "*" );
216 attrs[i++] = ch_strdup( "+" );
217 } else if ( si->si_allattrs && !si->si_allopattrs ) {
218 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
219 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
220 attrs[i++] = ch_strdup( "*" );
221 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
222 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
224 } else if ( !si->si_allattrs && si->si_allopattrs ) {
225 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
226 attrs[i++] = ch_strdup( "+" );
227 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
232 si->si_attrs = attrs;
234 exattrs = anlist2attrs( si->si_exanlist );
237 for ( n = 0; exattrs[n] != NULL; n++ ) ;
239 for ( i = 0; sync_descs[i] != NULL; i++ ) {
241 while ( exattrs[j] != NULL ) {
242 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
243 ch_free( exattrs[j] );
244 for ( k = j; exattrs[k] != NULL; k++ ) {
245 exattrs[k] = exattrs[k+1];
253 for ( i = 0; exattrs[i] != NULL; i++ ) {
254 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
256 if ( ( oc = si->si_anlist[j].an_oc ) ) {
258 while ( oc->soc_required[k] ) {
259 if ( !strcmp( exattrs[i],
260 oc->soc_required[k]->sat_cname.bv_val )) {
261 ch_free( exattrs[i] );
262 for ( l = i; exattrs[l]; l++ ) {
263 exattrs[l] = exattrs[l+1];
273 for ( i = 0; exattrs[i] != NULL; i++ ) ;
276 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
279 si->si_exattrs = exattrs;
282 typedef struct logschema {
284 struct berval ls_req;
285 struct berval ls_mod;
286 struct berval ls_newRdn;
287 struct berval ls_delRdn;
288 struct berval ls_newSup;
291 static logschema changelog_sc = {
293 BER_BVC("changeType"),
296 BER_BVC("deleteOldRDN"),
297 BER_BVC("newSuperior")
300 static logschema accesslog_sc = {
304 BER_BVC("reqNewRDN"),
305 BER_BVC("reqDeleteOldRDN"),
306 BER_BVC("reqNewSuperior")
314 BerElementBuffer berbuf;
315 BerElement *ber = (BerElement *)&berbuf;
316 LDAPControl c[2], *ctrls[3];
317 struct timeval timeout;
322 char **attrs, *lattrs[8];
327 /* setup LDAP SYNC control */
328 ber_init2( ber, NULL, LBER_USE_DER );
329 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
331 /* If we're using a log but we have no state, then fallback to
332 * normal mode for a full refresh.
334 if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
335 si->si_logstate = SYNCLOG_FALLBACK;
337 /* Use the log parameters if we're in log mode */
338 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
340 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
344 lattrs[0] = ls->ls_dn.bv_val;
345 lattrs[1] = ls->ls_req.bv_val;
346 lattrs[2] = ls->ls_mod.bv_val;
347 lattrs[3] = ls->ls_newRdn.bv_val;
348 lattrs[4] = ls->ls_delRdn.bv_val;
349 lattrs[5] = ls->ls_newSup.bv_val;
350 lattrs[6] = slap_schema.si_ad_entryCSN->ad_cname.bv_val;
354 base = si->si_logbase.bv_val;
355 filter = si->si_logfilterstr.bv_val;
358 scope = LDAP_SCOPE_SUBTREE;
361 base = si->si_base.bv_val;
362 filter = si->si_filterstr.bv_val;
363 attrs = si->si_attrs;
364 attrsonly = si->si_attrsonly;
365 scope = si->si_scope;
367 if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
368 si->si_type = LDAP_SYNC_REFRESH_ONLY;
370 si->si_type = si->si_ctype;
373 if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
375 ber_printf( ber, "{eOb}",
376 abs(si->si_type), &si->si_syncCookie.octet_str, rhint );
378 ber_printf( ber, "{eb}",
379 abs(si->si_type), rhint );
382 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
387 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
388 c[0].ldctl_iscritical = si->si_type < 0;
391 if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
392 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
393 c[1].ldctl_value = si->si_bindconf.sb_authzId;
394 c[1].ldctl_iscritical = 1;
401 timeout.tv_sec = si->si_tlimit;
404 rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
405 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
406 si->si_slimit, &msgid );
417 int cmdline_cookie_found = 0;
419 struct sync_cookie *sc = NULL;
425 psub = &si->si_be->be_nsuffix[0];
427 /* Init connection to master */
428 rc = ldap_initialize( &si->si_ld, si->si_bindconf.sb_uri.bv_val );
429 if ( rc != LDAP_SUCCESS ) {
430 Debug( LDAP_DEBUG_ANY,
431 "do_syncrep1: rid %03d ldap_initialize failed (%s)\n",
432 si->si_rid, si->si_bindconf.sb_uri.bv_val, 0 );
436 op->o_protocol = LDAP_VERSION3;
437 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
441 if ( si->si_bindconf.sb_tls ) {
442 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
443 if( rc != LDAP_SUCCESS ) {
444 Debug( LDAP_DEBUG_ANY,
445 "%s: rid %03d ldap_start_tls failed (%d)\n",
446 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
448 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
452 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
453 #ifdef HAVE_CYRUS_SASL
456 if ( si->si_bindconf.sb_secprops != NULL ) {
457 rc = ldap_set_option( si->si_ld,
458 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
460 if( rc != LDAP_OPT_SUCCESS ) {
461 Debug( LDAP_DEBUG_ANY, "Error: rid %03d ldap_set_option "
462 "(%s,SECPROPS,\"%s\") failed!\n",
463 si->si_rid, si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops );
468 defaults = lutil_sasl_defaults( si->si_ld,
469 si->si_bindconf.sb_saslmech.bv_val,
470 si->si_bindconf.sb_realm.bv_val,
471 si->si_bindconf.sb_authcId.bv_val,
472 si->si_bindconf.sb_cred.bv_val,
473 si->si_bindconf.sb_authzId.bv_val );
475 rc = ldap_sasl_interactive_bind_s( si->si_ld,
476 si->si_bindconf.sb_binddn.bv_val,
477 si->si_bindconf.sb_saslmech.bv_val,
483 lutil_sasl_freedefs( defaults );
485 /* FIXME: different error behaviors according to
487 * 2) on err policy : exit, retry, backoff ...
489 if ( rc != LDAP_SUCCESS ) {
490 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
492 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
493 "ldap_sasl_interactive_bind_s failed (%d)\n",
496 /* FIXME (see above comment) */
497 /* if Kerberos credentials cache is not active, retry */
498 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
499 rc == LDAP_LOCAL_ERROR )
501 rc = LDAP_SERVER_DOWN;
506 #else /* HAVE_CYRUS_SASL */
507 /* Should never get here, we trapped this at config time */
509 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
514 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
515 rc = ldap_sasl_bind_s( si->si_ld,
516 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
517 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
518 if ( rc != LDAP_SUCCESS ) {
519 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
520 "ldap_sasl_bind_s failed (%d)\n", si->si_rid, rc, 0 );
525 /* Set SSF to strongest of TLS, SASL SSFs */
528 op->o_transport_ssf = 0;
530 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
531 == LDAP_SUCCESS && ssl != NULL )
533 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
535 #endif /* HAVE_TLS */
536 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
537 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
538 ? op->o_sasl_ssf : op->o_tls_ssf;
541 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
542 /* get contextCSN shadow replica from database */
543 BerVarray csn = NULL;
545 assert( si->si_rid < 1000 );
546 op->o_req_ndn = op->o_bd->be_nsuffix[0];
547 op->o_req_dn = op->o_req_ndn;
549 /* try to read stored contextCSN */
550 backend_attribute( op, NULL, &op->o_req_ndn,
551 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
553 ch_free( si->si_syncCookie.ctxcsn.bv_val );
554 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
555 ber_bvarray_free_x( csn, op->o_tmpmemctx );
558 si->si_syncCookie.rid = si->si_rid;
560 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
561 if ( si->si_rid == sc->rid ) {
562 cmdline_cookie_found = 1;
567 if ( cmdline_cookie_found ) {
568 /* cookie is supplied in the command line */
570 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
572 /* ctxcsn wasn't parsed yet, do it now */
573 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
574 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
575 /* if cmdline cookie does not have ctxcsn */
576 /* component, set it to an initial value */
577 slap_init_sync_cookie_ctxcsn( sc );
579 slap_sync_cookie_free( &si->si_syncCookie, 0 );
580 slap_dup_sync_cookie( &si->si_syncCookie, sc );
581 slap_sync_cookie_free( sc, 1 );
584 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
585 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
588 si->si_refreshDone = 0;
590 rc = ldap_sync_search( si, op->o_tmpmemctx );
592 if( rc != LDAP_SUCCESS ) {
593 Debug( LDAP_DEBUG_ANY, "do_syncrep1: rid %03d "
594 "ldap_search_ext: %s (%d)\n",
595 si->si_rid, ldap_err2string( rc ), rc );
601 ldap_unbind_ext( si->si_ld, NULL, NULL );
614 LDAPControl **rctrls = NULL;
617 BerElementBuffer berbuf;
618 BerElement *ber = (BerElement *)&berbuf;
620 LDAPMessage *res = NULL;
621 LDAPMessage *msg = NULL;
624 struct berval *retdata = NULL;
629 struct berval syncUUID = BER_BVNULL;
630 struct sync_cookie syncCookie = { BER_BVNULL };
631 struct sync_cookie syncCookie_req = { BER_BVNULL };
632 struct berval cookie = BER_BVNULL;
638 Modifications *modlist = NULL;
643 struct timeval *tout_p = NULL;
644 struct timeval tout = { 0, 0 };
646 int refreshDeletes = 0;
647 BerVarray syncUUIDs = NULL;
650 if ( slapd_shutdown ) {
655 ber_init2( ber, NULL, LBER_USE_DER );
656 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
658 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2 rid %03d\n", si->si_rid, 0, 0 );
660 psub = &si->si_be->be_nsuffix[0];
662 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
664 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
670 while (( rc = ldap_result( si->si_ld, 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 );
720 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
722 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
723 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
724 syncrepl_updateCookie( si, op, psub, &syncCookie );
726 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
727 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
728 if (( rc = syncrepl_entry( si, op, entry, &modlist,
729 syncstate, &syncUUID, &syncCookie_req,
730 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
731 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
732 syncrepl_updateCookie( si, op, psub, &syncCookie );
735 ldap_controls_free( rctrls );
737 slap_mods_free( modlist, 1 );
743 case LDAP_RES_SEARCH_REFERENCE:
744 Debug( LDAP_DEBUG_ANY,
745 "do_syncrep2: rid %03d reference received error\n",
749 case LDAP_RES_SEARCH_RESULT:
750 Debug( LDAP_DEBUG_SYNC,
751 "do_syncrep2: rid %03d LDAP_RES_SEARCH_RESULT\n",
753 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
755 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
756 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
757 /* map old result code to registered code */
758 err = LDAP_SYNC_REFRESH_REQUIRED;
761 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
762 if ( si->si_logstate == SYNCLOG_LOGGING ) {
763 si->si_logstate = SYNCLOG_FALLBACK;
770 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
772 ber_scanf( ber, "{" /*"}"*/);
773 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
774 ber_scanf( ber, "m", &cookie );
775 if ( !BER_BVISNULL( &cookie ) ) {
776 ch_free( syncCookie.octet_str.bv_val );
777 ber_dupbv( &syncCookie.octet_str, &cookie);
779 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
781 slap_parse_sync_cookie( &syncCookie, NULL );
784 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
786 ber_scanf( ber, "b", &refreshDeletes );
788 ber_scanf( ber, /*"{"*/ "}" );
790 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
792 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
795 value_match( &match, slap_schema.si_ad_entryCSN,
796 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
797 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
798 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
802 ldap_controls_free( rctrls );
804 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
805 /* FIXME : different error behaviors according to
806 * 1) err code : LDAP_BUSY ...
807 * 2) on err policy : stop service, stop sync, retry
809 if ( refreshDeletes == 0 && match < 0 &&
810 err == LDAP_SUCCESS )
812 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
814 avl_free( si->si_presentlist, avl_ber_bvfree );
815 si->si_presentlist = NULL;
818 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
819 match < 0 && err == LDAP_SUCCESS )
821 syncrepl_updateCookie( si, op, psub, &syncCookie );
823 if ( err == LDAP_SUCCESS
824 && si->si_logstate == SYNCLOG_FALLBACK ) {
825 si->si_logstate = SYNCLOG_LOGGING;
826 rc = LDAP_SYNC_REFRESH_REQUIRED;
833 case LDAP_RES_INTERMEDIATE:
834 rc = ldap_parse_intermediate( si->si_ld, msg,
835 &retoid, &retdata, NULL, 0 );
836 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
837 ber_init2( ber, retdata, LBER_USE_DER );
839 switch ( si_tag = ber_peek_tag( ber, &len )) {
841 case LDAP_TAG_SYNC_NEW_COOKIE:
842 Debug( LDAP_DEBUG_SYNC,
843 "do_syncrep2: rid %03d %s - %s\n",
845 "LDAP_RES_INTERMEDIATE",
847 ber_scanf( ber, "tm", &tag, &cookie );
849 case LDAP_TAG_SYNC_REFRESH_DELETE:
850 case LDAP_TAG_SYNC_REFRESH_PRESENT:
851 Debug( LDAP_DEBUG_SYNC,
852 "do_syncrep2: rid %03d %s - %s\n",
854 "LDAP_RES_INTERMEDIATE",
855 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
856 "REFRESH_PRESENT" : "REFRESH_DELETE" );
857 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
858 si->si_refreshDelete = 1;
860 si->si_refreshPresent = 1;
862 ber_scanf( ber, "t{" /*"}"*/, &tag );
863 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
865 ber_scanf( ber, "m", &cookie );
866 if ( !BER_BVISNULL( &cookie ) ) {
867 ch_free( syncCookie.octet_str.bv_val );
868 ber_dupbv( &syncCookie.octet_str, &cookie );
870 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
872 slap_parse_sync_cookie( &syncCookie, NULL );
875 /* Defaults to TRUE */
876 if ( ber_peek_tag( ber, &len ) ==
877 LDAP_TAG_REFRESHDONE )
879 ber_scanf( ber, "b", &si->si_refreshDone );
882 si->si_refreshDone = 1;
884 ber_scanf( ber, /*"{"*/ "}" );
886 case LDAP_TAG_SYNC_ID_SET:
887 Debug( LDAP_DEBUG_SYNC,
888 "do_syncrep2: rid %03d %s - %s\n",
890 "LDAP_RES_INTERMEDIATE",
892 ber_scanf( ber, "t{" /*"}"*/, &tag );
893 if ( ber_peek_tag( ber, &len ) ==
894 LDAP_TAG_SYNC_COOKIE )
896 ber_scanf( ber, "m", &cookie );
897 if ( !BER_BVISNULL( &cookie ) ) {
898 ch_free( syncCookie.octet_str.bv_val );
899 ber_dupbv( &syncCookie.octet_str, &cookie );
901 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
903 slap_parse_sync_cookie( &syncCookie, NULL );
906 if ( ber_peek_tag( ber, &len ) ==
907 LDAP_TAG_REFRESHDELETES )
909 ber_scanf( ber, "b", &refreshDeletes );
911 ber_scanf( ber, "[W]", &syncUUIDs );
912 ber_scanf( ber, /*"{"*/ "}" );
913 if ( refreshDeletes ) {
914 syncrepl_del_nonpresent( op, si, syncUUIDs,
915 &syncCookie.ctxcsn );
916 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
918 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
919 struct berval *syncuuid_bv;
920 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
921 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
922 avl_insert( &si->si_presentlist,
923 (caddr_t) syncuuid_bv,
924 syncuuid_cmp, avl_dup_error );
926 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
928 slap_sync_cookie_free( &syncCookie, 0 );
931 Debug( LDAP_DEBUG_ANY,
932 "do_syncrep2: rid %03d unknown syncinfo tag (%ld)\n",
933 si->si_rid, (long) si_tag, 0 );
934 ldap_memfree( retoid );
935 ber_bvfree( retdata );
939 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
941 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
944 value_match( &match, slap_schema.si_ad_entryCSN,
945 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
946 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
947 &syncCookie_req.ctxcsn,
948 &syncCookie.ctxcsn, &text );
952 if ( si->si_refreshPresent == 1 ) {
953 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
955 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
957 syncrepl_updateCookie( si, op, psub, &syncCookie);
961 ldap_memfree( retoid );
962 ber_bvfree( retdata );
966 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
967 "unknown intermediate response (%d)\n",
969 ldap_memfree( retoid );
970 ber_bvfree( retdata );
976 Debug( LDAP_DEBUG_ANY, "do_syncrep2: rid %03d "
977 "unknown message\n", si->si_rid, 0, 0 );
981 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
982 slap_sync_cookie_free( &syncCookie_req, 0 );
983 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
984 slap_sync_cookie_free( &syncCookie, 0 );
994 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
995 errstr = ldap_err2string( rc );
997 Debug( LDAP_DEBUG_ANY,
998 "do_syncrep2: rid %03d %s\n", si->si_rid, errstr, 0 );
1002 slap_sync_cookie_free( &syncCookie, 0 );
1003 slap_sync_cookie_free( &syncCookie_req, 0 );
1005 if ( res ) ldap_msgfree( res );
1007 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1008 if ( si->si_conn_setup ) {
1010 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1011 connection_client_stop( s );
1012 si->si_conn_setup = 0;
1014 ldap_unbind_ext( si->si_ld, NULL, NULL );
1026 struct re_s* rtask = arg;
1027 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1028 Connection conn = {0};
1029 OperationBuffer opbuf;
1031 int rc = LDAP_SUCCESS;
1034 int i, defer = 1, fail = 0;
1037 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl rid %03d\n", si->si_rid, 0, 0 );
1042 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1044 switch( abs( si->si_type )) {
1045 case LDAP_SYNC_REFRESH_ONLY:
1046 case LDAP_SYNC_REFRESH_AND_PERSIST:
1049 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1053 if ( slapd_shutdown ) {
1055 if ( si->si_conn_setup ) {
1056 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1057 connection_client_stop( s );
1058 si->si_conn_setup = 0;
1060 ldap_unbind_ext( si->si_ld, NULL, NULL );
1063 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1067 op = (Operation *) &opbuf;
1068 connection_fake_init( &conn, op, ctx );
1070 /* use global malloc for now */
1071 op->o_tmpmemctx = NULL;
1072 op->o_tmpmfuncs = &ch_mfuncs;
1074 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1075 op->o_bd = be = si->si_be;
1076 op->o_dn = op->o_bd->be_rootdn;
1077 op->o_ndn = op->o_bd->be_rootndn;
1079 /* Establish session, do search */
1081 si->si_refreshDelete = 0;
1082 si->si_refreshPresent = 0;
1083 rc = do_syncrep1( op, si );
1087 /* Process results */
1088 if ( rc == LDAP_SUCCESS ) {
1089 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1091 rc = do_syncrep2( op, si );
1092 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1093 rc = ldap_sync_search( si, op->o_tmpmemctx );
1097 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1098 /* If we succeeded, enable the connection for further listening.
1099 * If we failed, tear down the connection and reschedule.
1101 if ( rc == LDAP_SUCCESS ) {
1102 if ( si->si_conn_setup ) {
1103 connection_client_enable( s );
1105 rc = connection_client_setup( s, do_syncrepl, arg );
1107 si->si_conn_setup = 1;
1109 } else if ( si->si_conn_setup ) {
1113 if ( rc == -2 ) rc = 0;
1117 /* At this point, we have 4 cases:
1118 * 1) for any hard failure, give up and remove this task
1119 * 2) for ServerDown, reschedule this task to run
1120 * 3) for Refresh and Success, reschedule to run
1121 * 4) for Persist and Success, reschedule to defer
1123 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1125 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1126 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1130 connection_client_stop( s );
1133 if ( rc == LDAP_SUCCESS ) {
1134 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1137 rtask->interval.tv_sec = si->si_interval;
1138 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1139 if ( si->si_retrynum ) {
1140 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1141 si->si_retrynum[i] = si->si_retrynum_init[i];
1143 si->si_retrynum[i] = RETRYNUM_TAIL;
1146 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1147 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1151 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1152 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1153 fail = RETRYNUM_TAIL;
1154 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1155 if ( si->si_retrynum[i] > 0 )
1156 si->si_retrynum[i]--;
1157 fail = si->si_retrynum[i];
1158 rtask->interval.tv_sec = si->si_retryinterval[i];
1159 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1160 slap_wake_listener();
1164 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1165 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1168 if ( fail == RETRYNUM_TAIL ) {
1169 Debug( LDAP_DEBUG_ANY,
1170 "do_syncrepl: rid %03d quitting\n",
1172 } else if ( fail > 0 ) {
1173 Debug( LDAP_DEBUG_ANY,
1174 "do_syncrepl: rid %03d retrying (%d retries left)\n",
1175 si->si_rid, fail, 0 );
1177 Debug( LDAP_DEBUG_ANY,
1178 "do_syncrepl: rid %03d retrying\n",
1186 static slap_verbmasks modops[] = {
1187 { BER_BVC("add"), LDAP_REQ_ADD },
1188 { BER_BVC("delete"), LDAP_REQ_DELETE },
1189 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1190 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1194 static Modifications *
1195 syncrepl_accesslog_mods(
1202 AttributeDescription *ad;
1203 struct berval bv, bv2;
1205 Modifications *mod = NULL, *modlist = NULL, **modtail;
1210 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1214 colon = ber_bvchr( &bv, ':' );
1216 continue; /* invalid */
1217 bv.bv_len = colon - bv.bv_val;
1218 if ( slap_bv2ad( &bv, &ad, &text )) {
1222 /* Ignore dynamically generated attrs */
1223 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1225 /* Ignore excluded attrs */
1226 if ( ldap_charray_inlist( si->si_exattrs,
1227 ad->ad_type->sat_cname.bv_val ))
1231 case '+': op = LDAP_MOD_ADD; break;
1232 case '-': op = LDAP_MOD_DELETE; break;
1233 case '=': op = LDAP_MOD_REPLACE; break;
1234 case '#': op = LDAP_MOD_INCREMENT; break;
1238 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1239 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1242 mod->sml_next = NULL;
1244 mod->sml_type = ad->ad_cname;
1245 mod->sml_values = NULL;
1246 mod->sml_nvalues = NULL;
1249 modtail = &mod->sml_next;
1251 if ( colon[2] == ' ' ) {
1252 bv.bv_val = colon + 3;
1253 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1254 ber_dupbv( &bv2, &bv );
1255 ber_bvarray_add( &mod->sml_values, &bv2 );
1261 static Modifications *
1262 syncrepl_changelog_mods(
1267 return NULL; /* FIXME */
1271 syncrepl_message_to_op(
1277 BerElement *ber = NULL;
1278 Modifications *modlist = NULL;
1280 SlapReply rs = { REP_RESULT };
1281 slap_callback cb = { NULL, null_callback, NULL, NULL };
1284 char txtbuf[SLAP_TEXT_BUFLEN];
1285 size_t textlen = sizeof txtbuf;
1287 struct berval bdn, dn = BER_BVNULL, ndn;
1288 struct berval bv, *bvals = NULL;
1289 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1290 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1291 psup = BER_BVNULL, nsup = BER_BVNULL;
1292 int rc, deleteOldRdn = 0, freeReqDn = 0;
1294 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1295 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1296 "Message type should be entry (%d)",
1297 si->si_rid, ldap_msgtype( msg ), 0 );
1301 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1306 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1308 if ( rc != LDAP_SUCCESS ) {
1309 Debug( LDAP_DEBUG_ANY,
1310 "syncrepl_message_to_op: rid %03d dn get failed (%d)",
1311 si->si_rid, rc, 0 );
1315 op->o_tag = LBER_DEFAULT;
1317 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1319 if ( bv.bv_val == NULL )
1322 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1324 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1325 ber_dupbv( &op->o_req_dn, &dn );
1326 ber_dupbv( &op->o_req_ndn, &ndn );
1327 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1328 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1330 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1331 int i = verb_to_mask( bvals[0].bv_val, modops );
1333 Debug( LDAP_DEBUG_ANY,
1334 "syncrepl_message_to_op: rid %03d unknown op %s",
1335 si->si_rid, bvals[0].bv_val, 0 );
1340 op->o_tag = modops[i].mask;
1341 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1342 /* Parse attribute into modlist */
1343 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1344 modlist = syncrepl_accesslog_mods( si, bvals );
1346 modlist = syncrepl_changelog_mods( si, bvals );
1347 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1349 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1350 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1352 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1354 } else if ( !ber_bvstrcasecmp( &bv,
1355 &slap_schema.si_ad_entryCSN->ad_cname )) {
1356 slap_queue_csn( op, bvals );
1361 /* If we didn't get a mod type or a target DN, bail out */
1362 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1367 op->o_callback = &cb;
1368 slap_op_time( &op->o_time, &op->o_tincr );
1370 switch( op->o_tag ) {
1372 case LDAP_REQ_MODIFY:
1373 /* If we didn't get required data, bail */
1374 if ( !modlist ) goto done;
1376 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1378 if ( rc != LDAP_SUCCESS ) {
1379 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1380 "mods check (%s)\n",
1381 si->si_rid, text, 0 );
1385 if ( op->o_tag == LDAP_REQ_ADD ) {
1386 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1387 op->ora_e->e_name = op->o_req_dn;
1388 op->ora_e->e_nname = op->o_req_ndn;
1390 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1391 if( rc != LDAP_SUCCESS ) {
1392 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: rid %03d "
1393 "mods2entry (%s)\n",
1394 si->si_rid, text, 0 );
1396 rc = op->o_bd->be_add( op, &rs );
1397 Debug( LDAP_DEBUG_SYNC,
1398 "syncrepl_message_to_op: rid %03d be_add %s (%d)\n",
1399 si->si_rid, op->o_req_dn.bv_val, rc );
1401 be_entry_release_w( op, op->ora_e );
1403 op->orm_modlist = modlist;
1404 rc = op->o_bd->be_modify( op, &rs );
1405 modlist = op->orm_modlist;
1406 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1407 "syncrepl_message_to_op: rid %03d be_modify %s (%d)\n",
1408 si->si_rid, op->o_req_dn.bv_val, rc );
1411 case LDAP_REQ_MODRDN:
1412 if ( BER_BVISNULL( &rdn )) goto done;
1414 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1416 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1418 if ( !BER_BVISNULL( &sup )) {
1419 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1421 op->orr_newSup = &psup;
1422 op->orr_nnewSup = ⊅
1424 op->orr_newSup = NULL;
1425 op->orr_nnewSup = NULL;
1427 op->orr_newrdn = prdn;
1428 op->orr_nnewrdn = nrdn;
1429 op->orr_deleteoldrdn = deleteOldRdn;
1430 rc = op->o_bd->be_modrdn( op, &rs );
1431 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1432 "syncrepl_message_to_op: rid %03d be_modrdn %s (%d)\n",
1433 si->si_rid, op->o_req_dn.bv_val, rc );
1435 case LDAP_REQ_DELETE:
1436 rc = op->o_bd->be_delete( op, &rs );
1437 Debug( rc ? LDAP_DEBUG_ANY : LDAP_DEBUG_SYNC,
1438 "syncrepl_message_to_op: rid %03d be_delete %s (%d)\n",
1439 si->si_rid, op->o_req_dn.bv_val, rc );
1443 slap_graduate_commit_csn( op );
1444 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
1445 BER_BVZERO( &op->o_csn );
1447 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1448 if ( !BER_BVISNULL( &rdn )) {
1449 if ( !BER_BVISNULL( &nsup ))
1450 ch_free( nsup.bv_val );
1451 if ( !BER_BVISNULL( &psup ))
1452 ch_free( psup.bv_val );
1453 if ( !BER_BVISNULL( &nrdn ))
1454 ch_free( nrdn.bv_val );
1455 if ( !BER_BVISNULL( &prdn ))
1456 ch_free( prdn.bv_val );
1459 ch_free( op->o_req_ndn.bv_val );
1460 ch_free( op->o_req_dn.bv_val );
1462 ber_free ( ber, 0 );
1467 syncrepl_message_to_entry(
1471 Modifications **modlist,
1477 BerElement *ber = NULL;
1480 Modifications **modtail = modlist;
1483 char txtbuf[SLAP_TEXT_BUFLEN];
1484 size_t textlen = sizeof txtbuf;
1486 struct berval bdn = BER_BVNULL, dn, ndn;
1491 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1492 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d "
1493 "Message type should be entry (%d)",
1494 si->si_rid, ldap_msgtype( msg ), 0 );
1498 op->o_tag = LDAP_REQ_ADD;
1500 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1501 if ( rc != LDAP_SUCCESS ) {
1502 Debug( LDAP_DEBUG_ANY,
1503 "syncrepl_message_to_entry: rid %03d dn get failed (%d)",
1504 si->si_rid, rc, 0 );
1508 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1509 ber_dupbv( &op->o_req_dn, &dn );
1510 ber_dupbv( &op->o_req_ndn, &ndn );
1511 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1512 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1514 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1515 /* NOTE: this could be done even before decoding the DN,
1516 * although encoding errors wouldn't be detected */
1521 if ( entry == NULL ) {
1526 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1527 e->e_name = op->o_req_dn;
1528 e->e_nname = op->o_req_ndn;
1530 while ( ber_remaining( ber ) ) {
1531 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1532 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1537 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1539 mod->sml_op = LDAP_MOD_REPLACE;
1541 mod->sml_next = NULL;
1542 mod->sml_desc = NULL;
1543 mod->sml_type = tmp.sml_type;
1544 mod->sml_values = tmp.sml_values;
1545 mod->sml_nvalues = NULL;
1548 modtail = &mod->sml_next;
1551 if ( *modlist == NULL ) {
1552 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d no attributes\n",
1558 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1560 if ( rc != LDAP_SUCCESS ) {
1561 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods check (%s)\n",
1562 si->si_rid, text, 0 );
1566 /* Strip out dynamically generated attrs */
1567 for ( modtail = modlist; *modtail ; ) {
1569 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1570 *modtail = mod->sml_next;
1571 slap_mod_free( &mod->sml_mod, 0 );
1574 modtail = &mod->sml_next;
1578 /* Strip out attrs in exattrs list */
1579 for ( modtail = modlist; *modtail ; ) {
1581 if ( ldap_charray_inlist( si->si_exattrs,
1582 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1583 *modtail = mod->sml_next;
1584 slap_mod_free( &mod->sml_mod, 0 );
1587 modtail = &mod->sml_next;
1591 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1592 if( rc != LDAP_SUCCESS ) {
1593 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: rid %03d mods2entry (%s)\n",
1594 si->si_rid, text, 0 );
1599 if ( rc != LDAP_SUCCESS ) {
1610 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1612 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1613 * entry if a previous refresh was interrupted before sending us a new
1614 * context state. We try to compare the new entry to the existing entry
1615 * and ignore the new entry if they are the same.
1617 * Also, we may get an update where the entryDN has changed, due to
1618 * a ModDn on the provider. We detect this as well, so we can issue
1619 * the corresponding operation locally.
1621 * In the case of a modify, we get a list of all the attributes
1622 * in the original entry. Rather than deleting the entry and re-adding it,
1623 * we issue a Modify request that deletes all the attributes and adds all
1624 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1627 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1628 * a ModDN we will issue both operations on the local database.
1630 typedef struct dninfo {
1634 int renamed; /* Was an existing entry renamed? */
1635 int wasChanged; /* are the attributes changed? */
1636 int attrs; /* how many attribute types are in the ads list */
1637 AttributeDescription **ads;
1645 Modifications** modlist,
1647 struct berval* syncUUID,
1648 struct sync_cookie* syncCookie_req,
1649 struct berval* syncCSN )
1651 Backend *be = op->o_bd;
1652 slap_callback cb = { NULL, NULL, NULL, NULL };
1653 struct berval *syncuuid_bv = NULL;
1654 struct berval syncUUID_strrep = BER_BVNULL;
1655 struct berval uuid_bv = BER_BVNULL;
1657 SlapReply rs_search = {REP_RESULT};
1658 SlapReply rs_delete = {REP_RESULT};
1659 SlapReply rs_add = {REP_RESULT};
1660 SlapReply rs_modify = {REP_RESULT};
1662 #ifdef LDAP_COMP_MATCH
1663 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1665 AttributeAssertion ava = { NULL, BER_BVNULL };
1667 int rc = LDAP_SUCCESS;
1669 struct berval pdn = BER_BVNULL;
1673 switch( syncstate ) {
1674 case LDAP_SYNC_PRESENT:
1675 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1677 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1680 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1682 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1684 case LDAP_SYNC_DELETE:
1685 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1687 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1689 case LDAP_SYNC_MODIFY:
1690 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: rid %03d %s\n",
1692 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1695 Debug( LDAP_DEBUG_ANY, "syncrepl_entry: rid %03d %s\n",
1697 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1700 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1701 if ( !si->si_refreshPresent && !si->si_refreshDone ) {
1702 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1703 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1704 syncuuid_cmp, avl_dup_error );
1708 if ( syncstate == LDAP_SYNC_PRESENT ) {
1710 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1711 if ( entry == NULL ) {
1716 f.f_choice = LDAP_FILTER_EQUALITY;
1718 ava.aa_desc = slap_schema.si_ad_entryUUID;
1719 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1720 ava.aa_value = *syncUUID;
1721 op->ors_filter = &f;
1723 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1724 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1725 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1726 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1727 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1728 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1729 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1730 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1732 op->o_tag = LDAP_REQ_SEARCH;
1733 op->ors_scope = LDAP_SCOPE_SUBTREE;
1734 op->ors_deref = LDAP_DEREF_NEVER;
1736 /* get the entry for this UUID */
1737 op->o_req_dn = si->si_base;
1738 op->o_req_ndn = si->si_base;
1740 op->o_time = slap_get_time();
1741 op->ors_tlimit = SLAP_NO_LIMIT;
1744 op->ors_attrs = slap_anlist_all_attributes;
1745 op->ors_attrsonly = 0;
1747 /* set callback function */
1748 op->o_callback = &cb;
1749 cb.sc_response = dn_callback;
1750 cb.sc_private = &dni;
1751 dni.new_entry = entry;
1753 if ( limits_check( op, &rs_search ) == 0 ) {
1754 rc = be->be_search( op, &rs_search );
1755 Debug( LDAP_DEBUG_SYNC,
1756 "syncrepl_entry: rid %03d be_search (%d)\n",
1757 si->si_rid, rc, 0 );
1760 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1761 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1764 cb.sc_response = null_callback;
1767 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1768 Debug( LDAP_DEBUG_SYNC,
1769 "syncrepl_entry: rid %03d %s\n",
1770 si->si_rid, entry->e_name.bv_val, 0 );
1772 Debug( LDAP_DEBUG_SYNC,
1773 "syncrepl_entry: rid %03d %s\n",
1774 si->si_rid, dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0 );
1777 if ( syncstate != LDAP_SYNC_DELETE ) {
1778 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1781 /* add if missing */
1782 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1783 &syncUUID_strrep, syncUUID );
1785 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1786 /* replace only if necessary */
1787 if ( a->a_nvals != a->a_vals ) {
1788 ber_memfree( a->a_nvals[0].bv_val );
1789 ber_dupbv( &a->a_nvals[0], syncUUID );
1791 ber_memfree( a->a_vals[0].bv_val );
1792 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1794 /* Don't save the contextCSN on the inooming context entry,
1795 * we'll write it when syncrepl_updateCookie eventually
1796 * gets called. (ITS#4622)
1798 if ( syncstate == LDAP_SYNC_ADD && dn_match( &entry->e_nname,
1799 &be->be_nsuffix[0] )) {
1801 for ( ap = &entry->e_attrs; *ap; ap=&(*ap)->a_next ) {
1803 if ( a->a_desc == slap_schema.si_ad_contextCSN ) {
1812 slap_op_time( &op->o_time, &op->o_tincr );
1813 switch ( syncstate ) {
1815 case LDAP_SYNC_MODIFY:
1817 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1819 op->o_csn = a->a_vals[0];
1822 if ( BER_BVISNULL( &dni.dn )) {
1824 op->o_req_dn = entry->e_name;
1825 op->o_req_ndn = entry->e_nname;
1826 op->o_tag = LDAP_REQ_ADD;
1829 rc = be->be_add( op, &rs_add );
1830 Debug( LDAP_DEBUG_SYNC,
1831 "syncrepl_entry: rid %03d be_add (%d)\n",
1832 si->si_rid, rc, 0 );
1833 switch ( rs_add.sr_err ) {
1835 be_entry_release_w( op, entry );
1840 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1841 * only if the suffix entry is not present */
1842 case LDAP_NO_SUCH_OBJECT:
1843 rc = syncrepl_add_glue( op, entry );
1847 /* if an entry was added via syncrepl_add_glue(),
1848 * it likely has no entryUUID, so the previous
1849 * be_search() doesn't find it. In this case,
1850 * give syncrepl a chance to modify it. Also
1851 * allow for entries that were recreated with the
1852 * same DN but a different entryUUID.
1854 case LDAP_ALREADY_EXISTS:
1856 Operation op2 = *op;
1857 SlapReply rs2 = { 0 };
1858 slap_callback cb2 = { 0 };
1860 op2.o_tag = LDAP_REQ_SEARCH;
1861 op2.o_req_dn = entry->e_name;
1862 op2.o_req_ndn = entry->e_nname;
1863 op2.ors_scope = LDAP_SCOPE_BASE;
1864 op2.ors_deref = LDAP_DEREF_NEVER;
1865 op2.ors_attrs = slap_anlist_all_attributes;
1866 op2.ors_attrsonly = 0;
1867 op2.ors_limit = NULL;
1869 op2.ors_tlimit = SLAP_NO_LIMIT;
1871 f.f_choice = LDAP_FILTER_PRESENT;
1872 f.f_desc = slap_schema.si_ad_objectClass;
1873 op2.ors_filter = &f;
1874 op2.ors_filterstr = generic_filterstr;
1876 op2.o_callback = &cb2;
1877 cb2.sc_response = dn_callback;
1878 cb2.sc_private = &dni;
1880 rc = be->be_search( &op2, &rs2 );
1881 if ( rc ) goto done;
1884 slap_op_time( &op->o_time, &op->o_tincr );
1890 Debug( LDAP_DEBUG_ANY,
1891 "syncrepl_entry: rid %03d be_add failed (%d)\n",
1892 si->si_rid, rs_add.sr_err, 0 );
1898 op->o_req_dn = dni.dn;
1899 op->o_req_ndn = dni.ndn;
1900 if ( dni.renamed ) {
1901 struct berval noldp, newp, nnewp;
1903 op->o_tag = LDAP_REQ_MODRDN;
1904 dnRdn( &entry->e_name, &op->orr_newrdn );
1905 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1907 dnParent( &dni.ndn, &noldp );
1908 dnParent( &entry->e_nname, &nnewp );
1909 if ( !dn_match( &noldp, &nnewp )) {
1910 dnParent( &entry->e_name, &newp );
1911 op->orr_newSup = &newp;
1912 op->orr_nnewSup = &nnewp;
1914 op->orr_newSup = NULL;
1915 op->orr_nnewSup = NULL;
1917 op->orr_deleteoldrdn = 0;
1918 rc = be->be_modrdn( op, &rs_modify );
1919 Debug( LDAP_DEBUG_SYNC,
1920 "syncrepl_entry: rid %03d be_modrdn (%d)\n",
1921 si->si_rid, rc, 0 );
1922 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1923 op->o_req_dn = entry->e_name;
1924 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 );
1994 case LDAP_SYNC_DELETE :
1995 if ( !BER_BVISNULL( &dni.dn )) {
1996 op->o_req_dn = dni.dn;
1997 op->o_req_ndn = dni.ndn;
1998 op->o_tag = LDAP_REQ_DELETE;
1999 rc = be->be_delete( op, &rs_delete );
2000 Debug( LDAP_DEBUG_SYNC,
2001 "syncrepl_entry: rid %03d be_delete (%d)\n",
2002 si->si_rid, rc, 0 );
2004 while ( rs_delete.sr_err == LDAP_SUCCESS
2005 && op->o_delete_glue_parent ) {
2006 op->o_delete_glue_parent = 0;
2007 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2008 slap_callback cb = { NULL };
2009 cb.sc_response = slap_null_cb;
2010 dnParent( &op->o_req_ndn, &pdn );
2012 op->o_req_ndn = pdn;
2013 op->o_callback = &cb;
2014 op->o_bd->be_delete( op, &rs_delete );
2023 Debug( LDAP_DEBUG_ANY,
2024 "syncrepl_entry: rid %03d unknown syncstate\n", si->si_rid, 0, 0 );
2029 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2030 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2031 BER_BVZERO( &syncUUID_strrep );
2034 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2036 if ( !BER_BVISNULL( &dni.ndn ) ) {
2037 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2039 if ( !BER_BVISNULL( &dni.dn ) ) {
2040 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2043 entry_free( entry );
2044 BER_BVZERO( &op->o_csn );
2048 static struct berval gcbva[] = {
2054 #define NP_DELETE_ONE 2
2057 syncrepl_del_nonpresent(
2061 struct berval *cookiecsn )
2063 Backend* be = op->o_bd;
2064 slap_callback cb = { NULL };
2065 SlapReply rs_search = {REP_RESULT};
2066 SlapReply rs_delete = {REP_RESULT};
2067 SlapReply rs_modify = {REP_RESULT};
2068 struct nonpresent_entry *np_list, *np_prev;
2070 AttributeName an[2];
2072 struct berval pdn = BER_BVNULL;
2075 op->o_req_dn = si->si_base;
2076 op->o_req_ndn = si->si_base;
2078 cb.sc_response = nonpresent_callback;
2081 op->o_callback = &cb;
2082 op->o_tag = LDAP_REQ_SEARCH;
2083 op->ors_scope = si->si_scope;
2084 op->ors_deref = LDAP_DEREF_NEVER;
2085 op->o_time = slap_get_time();
2086 op->ors_tlimit = SLAP_NO_LIMIT;
2091 #ifdef LDAP_COMP_MATCH
2092 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2094 AttributeAssertion eq = { NULL, BER_BVNULL };
2098 op->ors_attrsonly = 1;
2099 op->ors_attrs = slap_anlist_no_attrs;
2100 op->ors_limit = NULL;
2101 op->ors_filter = &uf;
2104 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2106 uf.f_choice = LDAP_FILTER_EQUALITY;
2107 si->si_refreshDelete |= NP_DELETE_ONE;
2109 for (i=0; uuids[i].bv_val; i++) {
2111 uf.f_av_value = uuids[i];
2112 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2113 rc = be->be_search( op, &rs_search );
2114 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2116 si->si_refreshDelete ^= NP_DELETE_ONE;
2118 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2119 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2120 an[0].an_desc = slap_schema.si_ad_entryUUID;
2122 op->ors_slimit = SLAP_NO_LIMIT;
2123 op->ors_attrsonly = 0;
2124 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2125 op->ors_filterstr = si->si_filterstr;
2126 op->o_nocaching = 1;
2128 if ( limits_check( op, &rs_search ) == 0 ) {
2129 rc = be->be_search( op, &rs_search );
2131 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2134 op->o_nocaching = 0;
2136 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2138 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2141 csn = si->si_syncCookie.ctxcsn;
2143 slap_queue_csn( op, &csn );
2145 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2146 while ( np_list != NULL ) {
2147 LDAP_LIST_REMOVE( np_list, npe_link );
2149 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2150 op->o_tag = LDAP_REQ_DELETE;
2151 op->o_callback = &cb;
2152 cb.sc_response = null_callback;
2154 op->o_req_dn = *np_prev->npe_name;
2155 op->o_req_ndn = *np_prev->npe_nname;
2156 rc = op->o_bd->be_delete( op, &rs_delete );
2157 Debug( LDAP_DEBUG_SYNC,
2158 "syncrepl_del_nonpresent: rid %03d be_delete %s (%d)\n",
2159 si->si_rid, op->o_req_dn.bv_val, rc );
2161 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2162 Modifications mod1, mod2;
2163 mod1.sml_op = LDAP_MOD_REPLACE;
2165 mod1.sml_desc = slap_schema.si_ad_objectClass;
2166 mod1.sml_type = mod1.sml_desc->ad_cname;
2167 mod1.sml_values = &gcbva[0];
2168 mod1.sml_nvalues = NULL;
2169 mod1.sml_next = &mod2;
2171 mod2.sml_op = LDAP_MOD_REPLACE;
2173 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2174 mod2.sml_type = mod2.sml_desc->ad_cname;
2175 mod2.sml_values = &gcbva[1];
2176 mod2.sml_nvalues = NULL;
2177 mod2.sml_next = NULL;
2179 op->o_tag = LDAP_REQ_MODIFY;
2180 op->orm_modlist = &mod1;
2182 rc = be->be_modify( op, &rs_modify );
2183 if ( mod2.sml_next ) slap_mods_free( mod2.sml_next, 1 );
2186 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2187 op->o_delete_glue_parent ) {
2188 op->o_delete_glue_parent = 0;
2189 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2190 slap_callback cb = { NULL };
2191 cb.sc_response = slap_null_cb;
2192 dnParent( &op->o_req_ndn, &pdn );
2194 op->o_req_ndn = pdn;
2195 op->o_callback = &cb;
2196 /* give it a root privil ? */
2197 op->o_bd->be_delete( op, &rs_delete );
2203 op->o_delete_glue_parent = 0;
2205 ber_bvfree( np_prev->npe_name );
2206 ber_bvfree( np_prev->npe_nname );
2210 slap_graduate_commit_csn( op );
2211 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2212 BER_BVZERO( &op->o_csn );
2223 Backend *be = op->o_bd;
2224 slap_callback cb = { NULL };
2229 struct berval dn = BER_BVNULL;
2230 struct berval ndn = BER_BVNULL;
2232 SlapReply rs_add = {REP_RESULT};
2233 struct berval ptr, nptr;
2236 op->o_tag = LDAP_REQ_ADD;
2237 op->o_callback = &cb;
2238 cb.sc_response = null_callback;
2239 cb.sc_private = NULL;
2244 /* count RDNs in suffix */
2245 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2246 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2248 ptr.bv_len -= comma - ptr.bv_val;
2258 /* Start with BE suffix */
2260 for ( i = 0; i < suffrdns; i++ ) {
2261 comma = ber_bvrchr( &ptr, ',' );
2262 if ( comma != NULL ) {
2263 ptr.bv_len = comma - ptr.bv_val;
2270 if ( !BER_BVISEMPTY( &ptr ) ) {
2271 dn.bv_len -= ptr.bv_len + 1;
2272 dn.bv_val += ptr.bv_len + 1;
2275 /* the normalizedDNs are always the same length, no counting
2279 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2280 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2281 ndn.bv_len = be->be_nsuffix[0].bv_len;
2283 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2289 while ( ndn.bv_val > e->e_nname.bv_val ) {
2290 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2291 ber_dupbv( &glue->e_name, &dn );
2292 ber_dupbv( &glue->e_nname, &ndn );
2294 a = ch_calloc( 1, sizeof( Attribute ));
2295 a->a_desc = slap_schema.si_ad_objectClass;
2297 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2298 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2299 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2300 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2302 a->a_nvals = a->a_vals;
2304 a->a_next = glue->e_attrs;
2307 a = ch_calloc( 1, sizeof( Attribute ));
2308 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2310 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2311 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2312 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2314 a->a_nvals = a->a_vals;
2316 a->a_next = glue->e_attrs;
2319 op->o_req_dn = glue->e_name;
2320 op->o_req_ndn = glue->e_nname;
2322 rc = be->be_add ( op, &rs_add );
2323 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2324 be_entry_release_w( op, glue );
2326 /* incl. ALREADY EXIST */
2328 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2334 /* Move to next child */
2335 comma = ber_bvrchr( &ptr, ',' );
2336 if ( comma == NULL ) {
2339 ptr.bv_len = comma - ptr.bv_val;
2341 dn.bv_val = ++comma;
2342 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2344 comma = ber_bvrchr( &nptr, ',' );
2345 assert( comma != NULL );
2346 nptr.bv_len = comma - nptr.bv_val;
2348 ndn.bv_val = ++comma;
2349 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2352 op->o_req_dn = e->e_name;
2353 op->o_req_ndn = e->e_nname;
2355 rc = be->be_add ( op, &rs_add );
2356 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2357 be_entry_release_w( op, e );
2366 syncrepl_updateCookie(
2370 struct sync_cookie *syncCookie )
2372 Backend *be = op->o_bd;
2373 Modifications mod = { { 0 } };
2374 struct berval vals[ 2 ];
2378 slap_callback cb = { NULL };
2379 SlapReply rs_modify = {REP_RESULT};
2381 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2382 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2384 mod.sml_op = LDAP_MOD_REPLACE;
2385 mod.sml_desc = slap_schema.si_ad_contextCSN;
2386 mod.sml_type = mod.sml_desc->ad_cname;
2387 mod.sml_values = vals;
2388 vals[0] = si->si_syncCookie.ctxcsn;
2389 vals[1].bv_val = NULL;
2392 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2394 op->o_tag = LDAP_REQ_MODIFY;
2396 assert( si->si_rid < 1000 );
2398 cb.sc_response = null_callback;
2401 op->o_callback = &cb;
2402 op->o_req_dn = op->o_bd->be_suffix[0];
2403 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2405 /* update contextCSN */
2406 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2407 op->orm_modlist = &mod;
2408 dbflags = SLAP_DBFLAGS(op->o_bd);
2409 SLAP_DBFLAGS(op->o_bd) |= SLAP_DBFLAG_NOLASTMOD;
2410 rc = be->be_modify( op, &rs_modify );
2411 SLAP_DBFLAGS(op->o_bd) = dbflags;
2414 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2415 Debug( LDAP_DEBUG_ANY,
2416 "syncrepl_updateCookie: rid %03d be_modify failed (%d)\n",
2417 si->si_rid, rs_modify.sr_err, 0 );
2420 slap_graduate_commit_csn( op );
2421 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2422 BER_BVZERO( &op->o_csn );
2423 if ( mod.sml_next ) slap_mods_free( mod.sml_next, 1 );
2433 dninfo *dni = op->o_callback->sc_private;
2435 if ( rs->sr_type == REP_SEARCH ) {
2436 if ( !BER_BVISNULL( &dni->dn ) ) {
2437 Debug( LDAP_DEBUG_ANY,
2438 "dn_callback: consistency error - "
2439 "entryUUID is not unique\n", 0, 0, 0 );
2441 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2442 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2443 /* If there is a new entry, see if it differs from the old.
2444 * We compare the non-normalized values so that cosmetic changes
2445 * in the provider are always propagated.
2447 if ( dni->new_entry ) {
2448 Attribute *old, *new;
2451 /* Did the DN change? Note that we don't explicitly try to
2452 * discover if the deleteOldRdn argument applies here. It
2453 * would save an unnecessary Modify if we detected it, but
2454 * that's a fair amount of trouble to compare the two attr
2455 * lists in detail. (Just test normalized DN; we ignore
2456 * insignificant changes here.)
2458 if ( !dn_match( &rs->sr_entry->e_nname,
2459 &dni->new_entry->e_nname ) )
2464 for ( i = 0, old = rs->sr_entry->e_attrs;
2466 i++, old = old->a_next )
2471 /* We assume that attributes are saved in the same order
2472 * in the remote and local databases. So if we walk through
2473 * the attributeDescriptions one by one they should match in
2474 * lock step. If not, we signal a change. Otherwise we test
2477 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2479 old = old->a_next, new = new->a_next )
2481 if ( old->a_desc != new->a_desc ) {
2482 dni->wasChanged = 1;
2485 for ( i = 0; ; i++ ) {
2487 nold = BER_BVISNULL( &old->a_vals[i] );
2488 nnew = BER_BVISNULL( &new->a_vals[i] );
2489 /* If both are empty, stop looking */
2490 if ( nold && nnew ) {
2493 /* If they are different, stop looking */
2494 if ( nold != nnew ) {
2495 dni->wasChanged = 1;
2498 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2499 dni->wasChanged = 1;
2503 if ( dni->wasChanged ) break;
2505 if ( dni->wasChanged ) {
2506 dni->ads = op->o_tmpalloc( dni->attrs *
2507 sizeof(AttributeDescription *), op->o_tmpmemctx );
2509 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2510 dni->ads[i] = old->a_desc;
2516 } else if ( rs->sr_type == REP_RESULT ) {
2517 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2518 Debug( LDAP_DEBUG_ANY,
2519 "dn_callback: consistency error - "
2520 "entryUUID is not unique\n", 0, 0, 0 );
2524 return LDAP_SUCCESS;
2528 nonpresent_callback(
2532 syncinfo_t *si = op->o_callback->sc_private;
2535 struct berval* present_uuid = NULL;
2536 struct nonpresent_entry *np_entry;
2538 if ( rs->sr_type == REP_RESULT ) {
2539 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2540 si->si_presentlist = NULL;
2542 } else if ( rs->sr_type == REP_SEARCH ) {
2543 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2544 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2546 if ( a == NULL ) return 0;
2548 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2552 if ( present_uuid == NULL ) {
2553 np_entry = (struct nonpresent_entry *)
2554 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2555 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2556 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2557 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2560 avl_delete( &si->si_presentlist,
2561 &a->a_nvals[0], syncuuid_cmp );
2562 ch_free( present_uuid->bv_val );
2563 ch_free( present_uuid );
2566 return LDAP_SUCCESS;
2574 if ( rs->sr_err != LDAP_SUCCESS &&
2575 rs->sr_err != LDAP_REFERRAL &&
2576 rs->sr_err != LDAP_ALREADY_EXISTS &&
2577 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2578 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2580 Debug( LDAP_DEBUG_ANY,
2581 "null_callback: error code 0x%x\n",
2584 return LDAP_SUCCESS;
2587 static struct berval *
2588 slap_uuidstr_from_normalized(
2589 struct berval* uuidstr,
2590 struct berval* normalized,
2594 unsigned char nibble;
2597 if ( normalized == NULL ) return NULL;
2598 if ( normalized->bv_len != 16 ) return NULL;
2603 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2604 if ( new == NULL ) {
2611 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2612 if ( new != uuidstr ) {
2613 slap_sl_free( new, ctx );
2618 for ( i = 0; i < 16; i++ ) {
2619 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2620 new->bv_val[(i<<1)+d] = '-';
2624 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2625 if ( nibble < 10 ) {
2626 new->bv_val[(i<<1)+d] = nibble + '0';
2628 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2631 nibble = (normalized->bv_val[i]) & 0xF;
2632 if ( nibble < 10 ) {
2633 new->bv_val[(i<<1)+d+1] = nibble + '0';
2635 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2639 new->bv_val[new->bv_len] = '\0';
2644 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2646 const struct berval *uuid1 = v_uuid1;
2647 const struct berval *uuid2 = v_uuid2;
2648 int rc = uuid1->bv_len - uuid2->bv_len;
2649 if ( rc ) return rc;
2650 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2654 avl_ber_bvfree( void *v_bv )
2656 struct berval *bv = (struct berval *)v_bv;
2658 if( v_bv == NULL ) return;
2659 if ( !BER_BVISNULL( bv ) ) {
2660 ch_free( bv->bv_val );
2662 ch_free( (char *) bv );
2666 syncinfo_free( syncinfo_t *sie )
2669 if ( sie->si_conn_setup ) {
2671 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2672 connection_client_stop( s );
2673 sie->si_conn_setup = 0;
2675 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2678 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2680 bindconf_free( &sie->si_bindconf );
2682 if ( sie->si_filterstr.bv_val ) {
2683 ch_free( sie->si_filterstr.bv_val );
2685 if ( sie->si_base.bv_val ) {
2686 ch_free( sie->si_base.bv_val );
2688 if ( sie->si_attrs ) {
2690 while ( sie->si_attrs[i] != NULL ) {
2691 ch_free( sie->si_attrs[i] );
2694 ch_free( sie->si_attrs );
2696 if ( sie->si_exattrs ) {
2698 while ( sie->si_exattrs[i] != NULL ) {
2699 ch_free( sie->si_exattrs[i] );
2702 ch_free( sie->si_exattrs );
2704 if ( sie->si_anlist ) {
2706 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2707 ch_free( sie->si_anlist[i].an_name.bv_val );
2710 ch_free( sie->si_anlist );
2712 if ( sie->si_exanlist ) {
2714 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2715 ch_free( sie->si_exanlist[i].an_name.bv_val );
2718 ch_free( sie->si_exanlist );
2720 if ( sie->si_retryinterval ) {
2721 ch_free( sie->si_retryinterval );
2723 if ( sie->si_retrynum ) {
2724 ch_free( sie->si_retrynum );
2726 if ( sie->si_retrynum_init ) {
2727 ch_free( sie->si_retrynum_init );
2729 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2730 if ( sie->si_presentlist ) {
2731 avl_free( sie->si_presentlist, avl_ber_bvfree );
2733 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2734 struct nonpresent_entry* npe;
2735 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2736 LDAP_LIST_REMOVE( npe, npe_link );
2737 if ( npe->npe_name ) {
2738 if ( npe->npe_name->bv_val ) {
2739 ch_free( npe->npe_name->bv_val );
2741 ch_free( npe->npe_name );
2743 if ( npe->npe_nname ) {
2744 if ( npe->npe_nname->bv_val ) {
2745 ch_free( npe->npe_nname->bv_val );
2747 ch_free( npe->npe_nname );
2756 /* NOTE: used & documented in slapd.conf(5) */
2758 #define PROVIDERSTR "provider"
2759 #define SCHEMASTR "schemachecking"
2760 #define FILTERSTR "filter"
2761 #define SEARCHBASESTR "searchbase"
2762 #define SCOPESTR "scope"
2763 #define ATTRSONLYSTR "attrsonly"
2764 #define ATTRSSTR "attrs"
2765 #define TYPESTR "type"
2766 #define INTERVALSTR "interval"
2767 #define RETRYSTR "retry"
2768 #define SLIMITSTR "sizelimit"
2769 #define TLIMITSTR "timelimit"
2770 #define SYNCDATASTR "syncdata"
2772 /* FIXME: undocumented */
2773 #define LOGBASESTR "logbase"
2774 #define LOGFILTERSTR "logfilter"
2775 #define OLDAUTHCSTR "bindprincipal"
2776 #define EXATTRSSTR "exattrs"
2777 #define MANAGEDSAITSTR "manageDSAit"
2780 #define LASTMODSTR "lastmod"
2781 #define LMGENSTR "gen"
2782 #define LMNOSTR "no"
2783 #define LMREQSTR "req"
2784 #define SRVTABSTR "srvtab"
2785 #define SUFFIXSTR "suffix"
2788 #define GOT_ID 0x0001
2789 #define GOT_PROVIDER 0x0002
2790 #define GOT_BASE 0x0004
2793 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2799 { BER_BVC("base"), LDAP_SCOPE_BASE },
2800 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2801 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2802 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2803 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2804 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2805 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2809 static slap_verbmasks datamodes[] = {
2810 { BER_BVC("default"), SYNCDATA_DEFAULT },
2811 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2812 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2817 parse_syncrepl_line(
2825 for ( i = 1; i < c->argc; i++ ) {
2826 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2827 STRLENOF( IDSTR "=" ) ) )
2830 /* '\0' string terminator accounts for '=' */
2831 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2832 if ( lutil_atoi( &tmp, val ) != 0 ) {
2833 snprintf( c->msg, sizeof( c->msg ),
2834 "Error: parse_syncrepl_line: "
2835 "unable to parse syncrepl id \"%s\"", val );
2836 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2839 if ( tmp >= 1000 || tmp < 0 ) {
2840 snprintf( c->msg, sizeof( c->msg ),
2841 "Error: parse_syncrepl_line: "
2842 "syncrepl id %d is out of range [0..999]", tmp );
2843 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2848 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2849 STRLENOF( PROVIDERSTR "=" ) ) )
2851 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2852 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2853 gots |= GOT_PROVIDER;
2854 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2855 STRLENOF( SCHEMASTR "=" ) ) )
2857 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2858 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2859 si->si_schemachecking = 1;
2860 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2861 si->si_schemachecking = 0;
2863 si->si_schemachecking = 1;
2865 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2866 STRLENOF( FILTERSTR "=" ) ) )
2868 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2869 if ( si->si_filterstr.bv_val )
2870 ch_free( si->si_filterstr.bv_val );
2871 ber_str2bv( val, 0, 1, &si->si_filterstr );
2872 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2873 STRLENOF( LOGFILTERSTR "=" ) ) )
2875 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2876 if ( si->si_logfilterstr.bv_val )
2877 ch_free( si->si_logfilterstr.bv_val );
2878 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2879 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2880 STRLENOF( SEARCHBASESTR "=" ) ) )
2885 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2886 if ( si->si_base.bv_val ) {
2887 ch_free( si->si_base.bv_val );
2889 ber_str2bv( val, 0, 0, &bv );
2890 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2891 if ( rc != LDAP_SUCCESS ) {
2892 snprintf( c->msg, sizeof( c->msg ),
2893 "Invalid base DN \"%s\": %d (%s)",
2894 val, rc, ldap_err2string( rc ) );
2895 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2898 if ( select_backend( &si->si_base, 0, 0 ) != c->be ) {
2899 ber_memfree( si->si_base.bv_val );
2900 snprintf( c->msg, sizeof( c->msg ),
2901 "Base DN \"%s\" is not within the database naming context",
2903 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2907 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2908 STRLENOF( LOGBASESTR "=" ) ) )
2913 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2914 if ( si->si_logbase.bv_val ) {
2915 ch_free( si->si_logbase.bv_val );
2917 ber_str2bv( val, 0, 0, &bv );
2918 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2919 if ( rc != LDAP_SUCCESS ) {
2920 snprintf( c->msg, sizeof( c->msg ),
2921 "Invalid logbase DN \"%s\": %d (%s)",
2922 val, rc, ldap_err2string( rc ) );
2923 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2926 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2927 STRLENOF( SCOPESTR "=" ) ) )
2930 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2931 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2932 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2933 si->si_scope = scopes[j].val;
2937 if ( BER_BVISNULL(&scopes[j].key) ) {
2938 snprintf( c->msg, sizeof( c->msg ),
2939 "Error: parse_syncrepl_line: "
2940 "unknown scope \"%s\"", val);
2941 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2944 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2945 STRLENOF( ATTRSONLYSTR ) ) )
2947 si->si_attrsonly = 1;
2948 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2949 STRLENOF( ATTRSSTR "=" ) ) )
2951 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2952 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2954 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2955 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2956 if ( si->si_anlist == NULL ) {
2957 ch_free( attr_fname );
2960 si->si_anfile = attr_fname;
2962 char *str, *s, *next;
2963 char delimstr[] = " ,\t";
2964 str = ch_strdup( val );
2965 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2967 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2969 if ( strlen(s) == 1 && *s == '*' ) {
2970 si->si_allattrs = 1;
2971 *(val + ( s - str )) = delimstr[0];
2973 if ( strlen(s) == 1 && *s == '+' ) {
2974 si->si_allopattrs = 1;
2975 *(val + ( s - str )) = delimstr[0];
2979 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2980 if ( si->si_anlist == NULL ) {
2984 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2985 STRLENOF( EXATTRSSTR "=" ) ) )
2987 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2988 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2990 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2991 si->si_exanlist = file2anlist(
2992 si->si_exanlist, attr_fname, " ,\t" );
2993 if ( si->si_exanlist == NULL ) {
2994 ch_free( attr_fname );
2997 ch_free( attr_fname );
2999 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
3000 if ( si->si_exanlist == NULL ) {
3004 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
3005 STRLENOF( TYPESTR "=" ) ) )
3007 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
3008 if ( !strncasecmp( val, "refreshOnly",
3009 STRLENOF("refreshOnly") ) )
3011 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3012 } else if ( !strncasecmp( val, "refreshAndPersist",
3013 STRLENOF("refreshAndPersist") ) )
3015 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
3016 si->si_interval = 60;
3018 snprintf( c->msg, sizeof( c->msg ),
3019 "Error: parse_syncrepl_line: "
3020 "unknown sync type \"%s\"", val);
3021 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3024 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3025 STRLENOF( INTERVALSTR "=" ) ) )
3027 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3028 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3029 si->si_interval = 0;
3030 } else if ( strchr( val, ':' ) != NULL ) {
3031 char *next, *ptr = val;
3032 unsigned dd, hh, mm, ss;
3034 /* NOTE: the test for ptr[ 0 ] == '-'
3035 * should go before the call to strtoul() */
3036 dd = strtoul( ptr, &next, 10 );
3037 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3038 snprintf( c->msg, sizeof( c->msg ),
3039 "Error: parse_syncrepl_line: "
3040 "invalid interval \"%s\", unable to parse days", val );
3041 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3045 hh = strtoul( ptr, &next, 10 );
3046 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3047 snprintf( c->msg, sizeof( c->msg ),
3048 "Error: parse_syncrepl_line: "
3049 "invalid interval \"%s\", unable to parse hours", val );
3050 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3054 mm = strtoul( ptr, &next, 10 );
3055 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3056 snprintf( c->msg, sizeof( c->msg ),
3057 "Error: parse_syncrepl_line: "
3058 "invalid interval \"%s\", unable to parse minutes", val );
3059 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3063 ss = strtoul( ptr, &next, 10 );
3064 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3065 snprintf( c->msg, sizeof( c->msg ),
3066 "Error: parse_syncrepl_line: "
3067 "invalid interval \"%s\", unable to parse seconds", val );
3068 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3071 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3075 if ( lutil_parse_time( val, &t ) != 0 ) {
3076 snprintf( c->msg, sizeof( c->msg ),
3077 "Error: parse_syncrepl_line: "
3078 "invalid interval \"%s\"", val );
3079 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3082 si->si_interval = (time_t)t;
3084 if ( si->si_interval < 0 ) {
3085 snprintf( c->msg, sizeof( c->msg ),
3086 "Error: parse_syncrepl_line: "
3087 "invalid interval \"%ld\"",
3088 (long) si->si_interval);
3089 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3092 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3093 STRLENOF( RETRYSTR "=" ) ) )
3098 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3099 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3100 retry_list[0] = NULL;
3102 slap_str2clist( &retry_list, val, " ,\t" );
3104 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3107 snprintf( c->msg, sizeof( c->msg ),
3108 "Error: incomplete syncrepl retry list" );
3109 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3110 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3111 ch_free( retry_list[k] );
3113 ch_free( retry_list );
3116 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3117 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3118 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3119 for ( j = 0; j < n; j++ ) {
3121 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3122 snprintf( c->msg, sizeof( c->msg ),
3123 "Error: invalid retry interval \"%s\" (#%d)",
3124 retry_list[j*2], j );
3125 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3126 /* do some cleanup */
3129 si->si_retryinterval[j] = (time_t)t;
3130 if ( *retry_list[j*2+1] == '+' ) {
3131 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3132 si->si_retrynum[j] = RETRYNUM_FOREVER;
3136 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3137 || si->si_retrynum_init[j] <= 0 )
3139 snprintf( c->msg, sizeof( c->msg ),
3140 "Error: invalid initial retry number \"%s\" (#%d)",
3141 retry_list[j*2+1], j );
3142 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3143 /* do some cleanup */
3146 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3147 || si->si_retrynum[j] <= 0 )
3149 snprintf( c->msg, sizeof( c->msg ),
3150 "Error: invalid retry number \"%s\" (#%d)",
3151 retry_list[j*2+1], j );
3152 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3153 /* do some cleanup */
3158 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3159 si->si_retrynum[j] = RETRYNUM_TAIL;
3160 si->si_retryinterval[j] = 0;
3162 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3163 ch_free( retry_list[k] );
3165 ch_free( retry_list );
3166 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3167 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3169 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3170 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3171 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3173 snprintf( c->msg, sizeof( c->msg ),
3174 "invalid manageDSAit value \"%s\".\n",
3176 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3179 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3180 STRLENOF( SLIMITSTR "=") ) )
3182 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3183 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3186 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3187 snprintf( c->msg, sizeof( c->msg ),
3188 "invalid size limit value \"%s\".\n",
3190 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3193 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3194 STRLENOF( TLIMITSTR "=" ) ) )
3196 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3197 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3200 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3201 snprintf( c->msg, sizeof( c->msg ),
3202 "invalid time limit value \"%s\".\n",
3204 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3207 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3208 STRLENOF( SYNCDATASTR "=" ) ) )
3210 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3211 si->si_syncdata = verb_to_mask( val, datamodes );
3212 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3213 snprintf( c->msg, sizeof( c->msg ),
3214 "Error: parse_syncrepl_line: "
3215 "unable to parse \"%s\"\n", c->argv[ i ] );
3216 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3221 if ( gots != GOT_ALL ) {
3222 snprintf( c->msg, sizeof( c->msg ),
3223 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3224 gots & GOT_ID ? "" : " "IDSTR,
3225 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3226 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3227 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3241 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3242 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3243 "operations required for syncrepl", c->be->be_type );
3244 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3247 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3248 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3249 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3252 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3255 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3259 si->si_bindconf.sb_tls = SB_TLS_OFF;
3260 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3261 si->si_schemachecking = 0;
3262 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3263 &si->si_filterstr );
3264 si->si_base.bv_val = NULL;
3265 si->si_scope = LDAP_SCOPE_SUBTREE;
3266 si->si_attrsonly = 0;
3267 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3268 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3269 si->si_attrs = NULL;
3270 si->si_allattrs = 0;
3271 si->si_allopattrs = 0;
3272 si->si_exattrs = NULL;
3273 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3274 si->si_interval = 86400;
3275 si->si_retryinterval = NULL;
3276 si->si_retrynum_init = NULL;
3277 si->si_retrynum = NULL;
3278 si->si_manageDSAit = 0;
3281 si->si_conn_setup = 0;
3283 si->si_presentlist = NULL;
3284 LDAP_LIST_INIT( &si->si_nonpresentlist );
3285 ldap_pvt_thread_mutex_init( &si->si_mutex );
3287 rc = parse_syncrepl_line( c, si );
3291 init_syncrepl( si );
3292 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3293 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3298 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3299 syncinfo_free( si );
3302 Debug( LDAP_DEBUG_CONFIG,
3303 "Config: ** successfully added syncrepl \"%s\"\n",
3304 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3305 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3306 if ( !si->si_schemachecking ) {
3307 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3309 c->be->be_syncinfo = si;
3315 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3317 struct berval bc, uri;
3318 char buf[BUFSIZ*2], *ptr;
3321 /* FIXME: we're not checking for buf[] overflow! */
3323 /* temporarily inhibit bindconf from printing URI */
3324 uri = si->si_bindconf.sb_uri;
3325 BER_BVZERO( &si->si_bindconf.sb_uri );
3326 bindconf_unparse( &si->si_bindconf, &bc );
3327 si->si_bindconf.sb_uri = uri;
3330 ptr += snprintf( ptr, sizeof( buf ), IDSTR "=%03d " PROVIDERSTR "=%s",
3331 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3332 if ( !BER_BVISNULL( &bc )) {
3333 ptr = lutil_strcopy( ptr, bc.bv_val );
3336 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3337 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3338 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3341 if ( !BER_BVISNULL( &si->si_base )) {
3342 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3343 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3346 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3347 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3348 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3351 if ( !BER_BVISNULL( &si->si_logbase )) {
3352 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3353 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3356 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3357 if ( si->si_scope == scopes[i].val ) {
3358 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3359 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3363 if ( si->si_attrsonly ) {
3364 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3366 if ( si->si_anfile ) {
3367 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3368 ptr = lutil_strcopy( ptr, si->si_anfile );
3369 } else if ( si->si_allattrs || si->si_allopattrs ||
3370 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3372 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3374 ptr = anlist_unparse( si->si_anlist, ptr );
3375 if ( si->si_allattrs ) {
3376 if ( old != ptr ) *ptr++ = ',';
3379 if ( si->si_allopattrs ) {
3380 if ( old != ptr ) *ptr++ = ',';
3385 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3386 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3387 ptr = anlist_unparse( si->si_exanlist, ptr );
3389 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3390 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3392 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3393 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3394 "refreshAndPersist" : "refreshOnly" );
3396 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3399 dd = si->si_interval;
3406 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3407 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3408 } else if ( si->si_retryinterval ) {
3410 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3411 for (i=0; si->si_retryinterval[i]; i++) {
3412 if ( space ) *ptr++ = ' ';
3414 ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3415 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3418 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3423 if ( si->si_slimit ) {
3424 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3425 ptr += sprintf( ptr, "%d", si->si_slimit );
3428 if ( si->si_tlimit ) {
3429 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3430 ptr += sprintf( ptr, "%d", si->si_tlimit );
3433 if ( si->si_syncdata ) {
3434 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3435 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3436 ptr = lutil_strcopy( ptr, bc.bv_val );
3439 bc.bv_len = ptr - buf;
3441 ber_dupbv( bv, &bc );
3445 syncrepl_config( ConfigArgs *c )
3447 if (c->op == SLAP_CONFIG_EMIT) {
3448 if ( c->be->be_syncinfo ) {
3450 syncrepl_unparse( c->be->be_syncinfo, &bv );
3451 ber_bvarray_add( &c->rvalue_vals, &bv );
3455 } else if ( c->op == LDAP_MOD_DELETE ) {
3458 if ( c->be->be_syncinfo ) {
3459 re = c->be->be_syncinfo->si_re;
3461 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ) )
3462 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3463 ldap_pvt_runqueue_remove( &slapd_rq, re );
3465 syncinfo_free( c->be->be_syncinfo );
3466 c->be->be_syncinfo = NULL;
3468 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3471 if ( SLAP_SHADOW( c->be ) ) {
3472 Debug(LDAP_DEBUG_ANY, "%s: "
3473 "syncrepl: database already shadowed.\n",
3476 } else if ( add_syncrepl( c ) ) {
3479 return config_sync_shadow( c );