1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2006 The OpenLDAP Foundation.
6 * Portions Copyright 2003 by IBM Corporation.
7 * Portions Copyright 2003 by Howard Chu, Symas Corporation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
23 #include <ac/string.h>
24 #include <ac/socket.h>
28 #include "lutil_ldap.h"
34 struct nonpresent_entry {
35 struct berval *npe_name;
36 struct berval *npe_nname;
37 LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
40 #define SYNCDATA_DEFAULT 0 /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG 1 /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG 2 /* entries are changelog format */
44 #define SYNCLOG_LOGGING 0 /* doing a log-based update */
45 #define SYNCLOG_FALLBACK 1 /* doing a full refresh */
47 #define RETRYNUM_FOREVER (-1) /* retry forever */
48 #define RETRYNUM_TAIL (-2) /* end of retrynum array */
49 #define RETRYNUM_VALID(n) ((n) >= RETRYNUM_FOREVER) /* valid retrynum */
50 #define RETRYNUM_FINITE(n) ((n) > RETRYNUM_FOREVER) /* not forever */
52 typedef struct syncinfo_s {
53 struct slap_backend_db *si_be;
56 slap_bindconf si_bindconf;
57 struct berval si_base;
58 struct berval si_logbase;
59 struct berval si_filterstr;
60 struct berval si_logfilterstr;
64 AttributeName *si_anlist;
65 AttributeName *si_exanlist;
70 int si_schemachecking;
71 int si_type; /* the active type */
72 int si_ctype; /* the configured type */
74 time_t *si_retryinterval;
75 int *si_retrynum_init;
77 struct sync_cookie si_syncCookie;
82 int si_refreshPresent;
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 int 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: ldap_initialize failed (%s)\n",
431 si->si_bindconf.sb_uri.bv_val, 0, 0 );
435 op->o_protocol = LDAP_VERSION3;
436 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION,
437 (const void *)&op->o_protocol );
440 if ( si->si_bindconf.sb_tls_do_init ) {
441 rc = bindconf_tls_set( &si->si_bindconf, si->si_ld );
442 } else if ( si->si_bindconf.sb_tls_ctx ) {
443 rc = ldap_set_option( si->si_ld, LDAP_OPT_X_TLS_CTX,
444 si->si_bindconf.sb_tls_ctx );
447 Debug( LDAP_DEBUG_ANY,
448 "do_syncrep1: TLS context initialization failed\n", 0, 0, 0 );
455 if ( si->si_bindconf.sb_tls ) {
456 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
457 if( rc != LDAP_SUCCESS ) {
458 Debug( LDAP_DEBUG_ANY,
459 "%s: ldap_start_tls failed (%d)\n",
460 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
462 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
466 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
467 #ifdef HAVE_CYRUS_SASL
470 if ( si->si_bindconf.sb_secprops != NULL ) {
471 rc = ldap_set_option( si->si_ld,
472 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
474 if( rc != LDAP_OPT_SUCCESS ) {
475 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
476 "(%s,SECPROPS,\"%s\") failed!\n",
477 si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops, 0 );
482 defaults = lutil_sasl_defaults( si->si_ld,
483 si->si_bindconf.sb_saslmech.bv_val,
484 si->si_bindconf.sb_realm.bv_val,
485 si->si_bindconf.sb_authcId.bv_val,
486 si->si_bindconf.sb_cred.bv_val,
487 si->si_bindconf.sb_authzId.bv_val );
489 rc = ldap_sasl_interactive_bind_s( si->si_ld,
490 si->si_bindconf.sb_binddn.bv_val,
491 si->si_bindconf.sb_saslmech.bv_val,
497 lutil_sasl_freedefs( defaults );
499 /* FIXME: different error behaviors according to
501 * 2) on err policy : exit, retry, backoff ...
503 if ( rc != LDAP_SUCCESS ) {
504 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
506 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
507 "ldap_sasl_interactive_bind_s failed (%d)\n",
510 /* FIXME (see above comment) */
511 /* if Kerberos credentials cache is not active, retry */
512 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
513 rc == LDAP_LOCAL_ERROR )
515 rc = LDAP_SERVER_DOWN;
520 #else /* HAVE_CYRUS_SASL */
521 /* Should never get here, we trapped this at config time */
523 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
528 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
529 rc = ldap_sasl_bind_s( si->si_ld,
530 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
531 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
532 if ( rc != LDAP_SUCCESS ) {
533 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
534 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
539 /* Set SSF to strongest of TLS, SASL SSFs */
542 op->o_transport_ssf = 0;
544 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
545 == LDAP_SUCCESS && ssl != NULL )
547 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
549 #endif /* HAVE_TLS */
550 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
551 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
552 ? op->o_sasl_ssf : op->o_tls_ssf;
555 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
556 /* get contextCSN shadow replica from database */
557 BerVarray csn = NULL;
559 assert( si->si_rid < 1000 );
560 op->o_req_ndn = op->o_bd->be_nsuffix[0];
561 op->o_req_dn = op->o_req_ndn;
563 /* try to read stored contextCSN */
564 backend_attribute( op, NULL, &op->o_req_ndn,
565 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
567 ch_free( si->si_syncCookie.ctxcsn.bv_val );
568 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
569 ber_bvarray_free_x( csn, op->o_tmpmemctx );
572 si->si_syncCookie.rid = si->si_rid;
574 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
575 if ( si->si_rid == sc->rid ) {
576 cmdline_cookie_found = 1;
581 if ( cmdline_cookie_found ) {
582 /* cookie is supplied in the command line */
584 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
586 /* ctxcsn wasn't parsed yet, do it now */
587 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
588 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
589 /* if cmdline cookie does not have ctxcsn */
590 /* component, set it to an initial value */
591 slap_init_sync_cookie_ctxcsn( sc );
593 slap_sync_cookie_free( &si->si_syncCookie, 0 );
594 slap_dup_sync_cookie( &si->si_syncCookie, sc );
595 slap_sync_cookie_free( sc, 1 );
598 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
599 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
602 rc = ldap_sync_search( si, op->o_tmpmemctx );
604 if( rc != LDAP_SUCCESS ) {
605 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
606 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
612 ldap_unbind_ext( si->si_ld, NULL, NULL );
625 LDAPControl **rctrls = NULL;
628 BerElementBuffer berbuf;
629 BerElement *ber = (BerElement *)&berbuf;
631 LDAPMessage *res = NULL;
632 LDAPMessage *msg = NULL;
635 struct berval *retdata = NULL;
640 struct berval syncUUID = BER_BVNULL;
641 struct sync_cookie syncCookie = { BER_BVNULL };
642 struct sync_cookie syncCookie_req = { BER_BVNULL };
643 struct berval cookie = BER_BVNULL;
649 Modifications *modlist = NULL;
654 struct timeval *tout_p = NULL;
655 struct timeval tout = { 0, 0 };
657 int refreshDeletes = 0;
659 BerVarray syncUUIDs = NULL;
662 if ( slapd_shutdown ) {
667 ber_init2( ber, NULL, LBER_USE_DER );
668 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
670 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
672 psub = &si->si_be->be_nsuffix[0];
674 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
676 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
682 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
683 tout_p, &res )) > 0 )
685 if ( slapd_shutdown ) {
689 for( msg = ldap_first_message( si->si_ld, res );
691 msg = ldap_next_message( si->si_ld, msg ) )
693 if ( slapd_shutdown ) {
697 switch( ldap_msgtype( msg ) ) {
698 case LDAP_RES_SEARCH_ENTRY:
699 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
700 /* we can't work without the control */
702 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
703 "got search entry without "
704 "control\n", 0, 0, 0 );
709 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
710 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
711 /* FIXME: what if syncUUID is NULL or empty?
712 * (happens with back-sql...) */
713 if ( BER_BVISEMPTY( &syncUUID ) ) {
714 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
715 "got empty syncUUID\n", 0, 0, 0 );
716 ldap_controls_free( rctrls );
720 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
721 ber_scanf( ber, /*"{"*/ "m}", &cookie );
722 if ( !BER_BVISNULL( &cookie ) ) {
723 ch_free( syncCookie.octet_str.bv_val );
724 ber_dupbv( &syncCookie.octet_str, &cookie );
726 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
728 slap_parse_sync_cookie( &syncCookie, NULL );
732 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
734 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
735 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
736 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
738 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
739 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
740 if (( rc = syncrepl_entry( si, op, entry, &modlist,
741 syncstate, &syncUUID, &syncCookie_req,
742 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
743 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
744 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
747 ldap_controls_free( rctrls );
749 slap_mods_free( modlist, 1 );
755 case LDAP_RES_SEARCH_REFERENCE:
756 Debug( LDAP_DEBUG_ANY,
757 "do_syncrep2: reference received error\n", 0, 0, 0 );
760 case LDAP_RES_SEARCH_RESULT:
761 Debug( LDAP_DEBUG_SYNC,
762 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
763 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
765 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
766 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
767 /* map old result code to registered code */
768 err = LDAP_SYNC_REFRESH_REQUIRED;
771 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
772 if ( si->si_logstate == SYNCLOG_LOGGING ) {
773 si->si_logstate = SYNCLOG_FALLBACK;
780 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
782 ber_scanf( ber, "{" /*"}"*/);
783 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
784 ber_scanf( ber, "m", &cookie );
785 if ( !BER_BVISNULL( &cookie ) ) {
786 ch_free( syncCookie.octet_str.bv_val );
787 ber_dupbv( &syncCookie.octet_str, &cookie);
789 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
791 slap_parse_sync_cookie( &syncCookie, NULL );
794 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
796 ber_scanf( ber, "b", &refreshDeletes );
798 ber_scanf( ber, /*"{"*/ "}" );
800 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
802 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
805 value_match( &match, slap_schema.si_ad_entryCSN,
806 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
807 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
808 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
812 ldap_controls_free( rctrls );
814 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
815 /* FIXME : different error behaviors according to
816 * 1) err code : LDAP_BUSY ...
817 * 2) on err policy : stop service, stop sync, retry
819 if ( refreshDeletes == 0 && match < 0 &&
820 err == LDAP_SUCCESS )
822 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
824 avl_free( si->si_presentlist, avl_ber_bvfree );
825 si->si_presentlist = NULL;
828 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
829 match < 0 && err == LDAP_SUCCESS )
831 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
833 if ( err == LDAP_SUCCESS
834 && si->si_logstate == SYNCLOG_FALLBACK ) {
835 si->si_logstate = SYNCLOG_LOGGING;
836 rc = LDAP_SYNC_REFRESH_REQUIRED;
843 case LDAP_RES_INTERMEDIATE:
844 rc = ldap_parse_intermediate( si->si_ld, msg,
845 &retoid, &retdata, NULL, 0 );
846 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
847 ber_init2( ber, retdata, LBER_USE_DER );
849 switch ( si_tag = ber_peek_tag( ber, &len )) {
851 case LDAP_TAG_SYNC_NEW_COOKIE:
852 Debug( LDAP_DEBUG_SYNC,
853 "do_syncrep2: %s - %s%s\n",
854 "LDAP_RES_INTERMEDIATE",
855 "NEW_COOKIE", "\n" );
856 ber_scanf( ber, "tm", &tag, &cookie );
858 case LDAP_TAG_SYNC_REFRESH_DELETE:
859 case LDAP_TAG_SYNC_REFRESH_PRESENT:
860 Debug( LDAP_DEBUG_SYNC,
861 "do_syncrep2: %s - %s%s\n",
862 "LDAP_RES_INTERMEDIATE",
863 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
864 "REFRESH_PRESENT" : "REFRESH_DELETE",
866 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
867 si->si_refreshDelete = 1;
869 si->si_refreshPresent = 1;
871 ber_scanf( ber, "t{" /*"}"*/, &tag );
872 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
874 ber_scanf( ber, "m", &cookie );
875 if ( !BER_BVISNULL( &cookie ) ) {
876 ch_free( syncCookie.octet_str.bv_val );
877 ber_dupbv( &syncCookie.octet_str, &cookie );
879 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
881 slap_parse_sync_cookie( &syncCookie, NULL );
884 if ( ber_peek_tag( ber, &len ) ==
885 LDAP_TAG_REFRESHDONE )
887 ber_scanf( ber, "b", &refreshDone );
889 ber_scanf( ber, /*"{"*/ "}" );
891 case LDAP_TAG_SYNC_ID_SET:
892 Debug( LDAP_DEBUG_SYNC,
893 "do_syncrep2: %s - %s%s\n",
894 "LDAP_RES_INTERMEDIATE",
897 ber_scanf( ber, "t{" /*"}"*/, &tag );
898 if ( ber_peek_tag( ber, &len ) ==
899 LDAP_TAG_SYNC_COOKIE )
901 ber_scanf( ber, "m", &cookie );
902 if ( !BER_BVISNULL( &cookie ) ) {
903 ch_free( syncCookie.octet_str.bv_val );
904 ber_dupbv( &syncCookie.octet_str, &cookie );
906 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
908 slap_parse_sync_cookie( &syncCookie, NULL );
911 if ( ber_peek_tag( ber, &len ) ==
912 LDAP_TAG_REFRESHDELETES )
914 ber_scanf( ber, "b", &refreshDeletes );
916 ber_scanf( ber, "[W]", &syncUUIDs );
917 ber_scanf( ber, /*"{"*/ "}" );
918 if ( refreshDeletes ) {
919 syncrepl_del_nonpresent( op, si, syncUUIDs,
920 &syncCookie.ctxcsn );
921 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
923 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
924 struct berval *syncuuid_bv;
925 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
926 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
927 avl_insert( &si->si_presentlist,
928 (caddr_t) syncuuid_bv,
929 syncuuid_cmp, avl_dup_error );
931 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
933 slap_sync_cookie_free( &syncCookie, 0 );
936 Debug( LDAP_DEBUG_ANY,
937 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
938 (long) si_tag, 0, 0 );
939 ldap_memfree( retoid );
940 ber_bvfree( retdata );
944 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
946 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
949 value_match( &match, slap_schema.si_ad_entryCSN,
950 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
951 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
952 &syncCookie_req.ctxcsn,
953 &syncCookie.ctxcsn, &text );
957 if ( si->si_refreshPresent == 1 ) {
958 syncrepl_del_nonpresent( op, si, NULL, &syncCookie.ctxcsn );
961 if ( !BER_BVISNULL( &syncCookie.ctxcsn ))
963 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
967 ldap_memfree( retoid );
968 ber_bvfree( retdata );
972 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
973 "unknown intermediate response (%d)\n",
975 ldap_memfree( retoid );
976 ber_bvfree( retdata );
982 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
983 "unknown message\n", 0, 0, 0 );
987 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
988 slap_sync_cookie_free( &syncCookie_req, 0 );
989 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
990 slap_sync_cookie_free( &syncCookie, 0 );
1000 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
1001 errstr = ldap_err2string( rc );
1003 Debug( LDAP_DEBUG_ANY,
1004 "do_syncrep2 : %s\n", errstr, 0, 0 );
1008 slap_sync_cookie_free( &syncCookie, 0 );
1009 slap_sync_cookie_free( &syncCookie_req, 0 );
1011 if ( res ) ldap_msgfree( res );
1013 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
1014 if ( si->si_conn_setup ) {
1016 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1017 connection_client_stop( s );
1018 si->si_conn_setup = 0;
1020 ldap_unbind_ext( si->si_ld, NULL, NULL );
1032 struct re_s* rtask = arg;
1033 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1034 Connection conn = {0};
1035 OperationBuffer opbuf;
1037 int rc = LDAP_SUCCESS;
1043 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1048 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1050 switch( abs( si->si_type )) {
1051 case LDAP_SYNC_REFRESH_ONLY:
1052 case LDAP_SYNC_REFRESH_AND_PERSIST:
1055 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1059 if ( slapd_shutdown ) {
1061 if ( si->si_conn_setup ) {
1062 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1063 connection_client_stop( s );
1064 si->si_conn_setup = 0;
1066 ldap_unbind_ext( si->si_ld, NULL, NULL );
1069 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1073 op = (Operation *) &opbuf;
1074 connection_fake_init( &conn, op, ctx );
1076 /* use global malloc for now */
1077 op->o_tmpmemctx = NULL;
1078 op->o_tmpmfuncs = &ch_mfuncs;
1080 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1081 op->o_bd = be = si->si_be;
1082 op->o_dn = op->o_bd->be_rootdn;
1083 op->o_ndn = op->o_bd->be_rootndn;
1085 /* Establish session, do search */
1087 si->si_refreshDelete = 0;
1088 si->si_refreshPresent = 0;
1089 rc = do_syncrep1( op, si );
1093 /* Process results */
1094 if ( rc == LDAP_SUCCESS ) {
1095 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1097 rc = do_syncrep2( op, si );
1098 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1099 rc = ldap_sync_search( si, op->o_tmpmemctx );
1103 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1104 /* If we succeeded, enable the connection for further listening.
1105 * If we failed, tear down the connection and reschedule.
1107 if ( rc == LDAP_SUCCESS ) {
1108 if ( si->si_conn_setup ) {
1109 connection_client_enable( s );
1111 rc = connection_client_setup( s, do_syncrepl, arg );
1113 si->si_conn_setup = 1;
1115 } else if ( si->si_conn_setup ) {
1119 if ( rc == -2 ) rc = 0;
1123 /* At this point, we have 4 cases:
1124 * 1) for any hard failure, give up and remove this task
1125 * 2) for ServerDown, reschedule this task to run
1126 * 3) for Refresh and Success, reschedule to run
1127 * 4) for Persist and Success, reschedule to defer
1129 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1131 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1132 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1136 connection_client_stop( s );
1139 if ( rc == LDAP_SUCCESS ) {
1140 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1143 rtask->interval.tv_sec = si->si_interval;
1144 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1145 if ( si->si_retrynum ) {
1146 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1147 si->si_retrynum[i] = si->si_retrynum_init[i];
1149 si->si_retrynum[i] = RETRYNUM_TAIL;
1152 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1153 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1157 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1158 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1159 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1160 if ( si->si_retrynum[i] > 0 )
1161 si->si_retrynum[i]--;
1162 rtask->interval.tv_sec = si->si_retryinterval[i];
1163 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1164 slap_wake_listener();
1168 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1169 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1174 static slap_verbmasks modops[] = {
1175 { BER_BVC("add"), LDAP_REQ_ADD },
1176 { BER_BVC("delete"), LDAP_REQ_DELETE },
1177 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1178 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1182 static Modifications *
1183 syncrepl_accesslog_mods(
1190 AttributeDescription *ad;
1191 struct berval bv, bv2;
1193 Modifications *mod = NULL, *modlist = NULL, **modtail;
1198 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1202 colon = ber_bvchr( &bv, ':' );
1204 continue; /* invalid */
1205 bv.bv_len = colon - bv.bv_val;
1206 if ( slap_bv2ad( &bv, &ad, &text )) {
1210 /* Ignore dynamically generated attrs */
1211 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1213 /* Ignore excluded attrs */
1214 if ( ldap_charray_inlist( si->si_exattrs,
1215 ad->ad_type->sat_cname.bv_val ))
1219 case '+': op = LDAP_MOD_ADD; break;
1220 case '-': op = LDAP_MOD_DELETE; break;
1221 case '=': op = LDAP_MOD_REPLACE; break;
1222 case '#': op = LDAP_MOD_INCREMENT; break;
1226 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1227 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1230 mod->sml_next = NULL;
1232 mod->sml_type = ad->ad_cname;
1233 mod->sml_values = NULL;
1234 mod->sml_nvalues = NULL;
1237 modtail = &mod->sml_next;
1239 if ( colon[2] == ' ' ) {
1240 bv.bv_val = colon + 3;
1241 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1242 ber_dupbv( &bv2, &bv );
1243 ber_bvarray_add( &mod->sml_values, &bv2 );
1249 static Modifications *
1250 syncrepl_changelog_mods(
1255 return NULL; /* FIXME */
1259 syncrepl_message_to_op(
1265 BerElement *ber = NULL;
1266 Modifications *modlist = NULL;
1268 SlapReply rs = { REP_RESULT };
1269 slap_callback cb = { NULL, null_callback, NULL, NULL };
1272 char txtbuf[SLAP_TEXT_BUFLEN];
1273 size_t textlen = sizeof txtbuf;
1275 struct berval bdn, dn = BER_BVNULL, ndn;
1276 struct berval bv, *bvals = NULL;
1277 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1278 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1279 psup = BER_BVNULL, nsup = BER_BVNULL;
1280 int rc, deleteOldRdn = 0;
1282 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1283 Debug( LDAP_DEBUG_ANY,
1284 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1288 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1293 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1295 if ( rc != LDAP_SUCCESS ) {
1296 Debug( LDAP_DEBUG_ANY,
1297 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1301 op->o_tag = LBER_DEFAULT;
1303 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1305 if ( bv.bv_val == NULL )
1308 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1310 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1311 ber_dupbv( &op->o_req_dn, &dn );
1312 ber_dupbv( &op->o_req_ndn, &ndn );
1313 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1314 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1315 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1316 int i = verb_to_mask( bvals[0].bv_val, modops );
1318 Debug( LDAP_DEBUG_ANY,
1319 "syncrepl_message_to_op : unknown op %s",
1320 bvals[0].bv_val, 0, 0 );
1325 op->o_tag = modops[i].mask;
1326 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1327 /* Parse attribute into modlist */
1328 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1329 modlist = syncrepl_accesslog_mods( si, bvals );
1331 modlist = syncrepl_changelog_mods( si, bvals );
1332 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1334 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1335 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1337 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1339 } else if ( !ber_bvstrcasecmp( &bv,
1340 &slap_schema.si_ad_entryCSN->ad_cname )) {
1341 slap_queue_csn( op, bvals );
1346 /* If we didn't get a mod type or a target DN, bail out */
1347 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1352 op->o_callback = &cb;
1353 slap_op_time( &op->o_time, &op->o_tincr );
1355 switch( op->o_tag ) {
1357 case LDAP_REQ_MODIFY:
1358 /* If we didn't get required data, bail */
1359 if ( !modlist ) goto done;
1361 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1363 if ( rc != LDAP_SUCCESS ) {
1364 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1369 if ( op->o_tag == LDAP_REQ_ADD ) {
1370 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1371 op->ora_e->e_name = op->o_req_dn;
1372 op->ora_e->e_nname = op->o_req_ndn;
1373 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1374 if( rc != LDAP_SUCCESS ) {
1375 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1378 rc = op->o_bd->be_add( op, &rs );
1379 Debug( LDAP_DEBUG_SYNC,
1380 "syncrepl_message_to_op: be_add %s (%d)\n",
1381 op->o_req_dn.bv_val, rc, 0 );
1383 be_entry_release_w( op, op->ora_e );
1385 op->orm_modlist = modlist;
1386 rc = op->o_bd->be_modify( op, &rs );
1387 Debug( LDAP_DEBUG_SYNC,
1388 "syncrepl_message_to_op: be_modify %s (%d)\n",
1389 op->o_req_dn.bv_val, rc, 0 );
1392 case LDAP_REQ_MODRDN:
1393 if ( BER_BVISNULL( &rdn )) goto done;
1395 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1397 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1399 if ( !BER_BVISNULL( &sup )) {
1400 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1402 op->orr_newSup = &psup;
1403 op->orr_nnewSup = ⊅
1405 op->orr_newrdn = prdn;
1406 op->orr_nnewrdn = nrdn;
1407 op->orr_deleteoldrdn = deleteOldRdn;
1408 op->orr_modlist = NULL;
1409 if ( slap_modrdn2mods( op, &rs ))
1411 rc = op->o_bd->be_modrdn( op, &rs );
1412 slap_mods_free( op->orr_modlist, 1 );
1413 Debug( LDAP_DEBUG_SYNC,
1414 "syncrepl_message_to_op: be_modrdn %s (%d)\n",
1415 op->o_req_dn.bv_val, rc, 0 );
1417 case LDAP_REQ_DELETE:
1418 rc = op->o_bd->be_delete( op, &rs );
1419 Debug( LDAP_DEBUG_SYNC,
1420 "syncrepl_message_to_op: be_delete %s (%d)\n",
1421 op->o_req_dn.bv_val, rc, 0 );
1425 slap_graduate_commit_csn( op );
1427 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1428 if ( !BER_BVISNULL( &rdn )) {
1429 if ( !BER_BVISNULL( &nsup ))
1430 ch_free( nsup.bv_val );
1431 if ( !BER_BVISNULL( &psup ))
1432 ch_free( psup.bv_val );
1433 if ( !BER_BVISNULL( &nrdn ))
1434 ch_free( nrdn.bv_val );
1435 if ( !BER_BVISNULL( &prdn ))
1436 ch_free( prdn.bv_val );
1438 ber_free ( ber, 0 );
1443 syncrepl_message_to_entry(
1447 Modifications **modlist,
1453 BerElement *ber = NULL;
1456 Modifications **modtail = modlist;
1459 char txtbuf[SLAP_TEXT_BUFLEN];
1460 size_t textlen = sizeof txtbuf;
1462 struct berval bdn = {0, NULL}, dn, ndn;
1467 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1468 Debug( LDAP_DEBUG_ANY,
1469 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1473 op->o_tag = LDAP_REQ_ADD;
1475 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1477 if ( rc != LDAP_SUCCESS ) {
1478 Debug( LDAP_DEBUG_ANY,
1479 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1483 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1484 ber_dupbv( &op->o_req_dn, &dn );
1485 ber_dupbv( &op->o_req_ndn, &ndn );
1486 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1487 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1489 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1492 return LDAP_SUCCESS;
1495 if ( entry == NULL ) {
1499 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1500 e->e_name = op->o_req_dn;
1501 e->e_nname = op->o_req_ndn;
1503 while ( ber_remaining( ber ) ) {
1504 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1505 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1510 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1512 mod->sml_op = LDAP_MOD_REPLACE;
1514 mod->sml_next = NULL;
1515 mod->sml_desc = NULL;
1516 mod->sml_type = tmp.sml_type;
1517 mod->sml_values = tmp.sml_values;
1518 mod->sml_nvalues = NULL;
1521 modtail = &mod->sml_next;
1524 if ( *modlist == NULL ) {
1525 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1531 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1533 if ( rc != LDAP_SUCCESS ) {
1534 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1539 /* Strip out dynamically generated attrs */
1540 for ( modtail = modlist; *modtail ; ) {
1542 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1543 *modtail = mod->sml_next;
1544 slap_mod_free( &mod->sml_mod, 0 );
1547 modtail = &mod->sml_next;
1551 /* Strip out attrs in exattrs list */
1552 for ( modtail = modlist; *modtail ; ) {
1554 if ( ldap_charray_inlist( si->si_exattrs,
1555 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1556 *modtail = mod->sml_next;
1557 slap_mod_free( &mod->sml_mod, 0 );
1560 modtail = &mod->sml_next;
1564 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1565 if( rc != LDAP_SUCCESS ) {
1566 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1571 ber_free ( ber, 0 );
1572 if ( rc != LDAP_SUCCESS ) {
1583 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1585 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1586 * entry if a previous refresh was interrupted before sending us a new
1587 * context state. We try to compare the new entry to the existing entry
1588 * and ignore the new entry if they are the same.
1590 * Also, we may get an update where the entryDN has changed, due to
1591 * a ModDn on the provider. We detect this as well, so we can issue
1592 * the corresponding operation locally.
1594 * In the case of a modify, we get a list of all the attributes
1595 * in the original entry. Rather than deleting the entry and re-adding it,
1596 * we issue a Modify request that deletes all the attributes and adds all
1597 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1600 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1601 * a ModDN we will issue both operations on the local database.
1603 typedef struct dninfo {
1607 int renamed; /* Was an existing entry renamed? */
1608 int wasChanged; /* are the attributes changed? */
1609 int attrs; /* how many attribute types are in the ads list */
1610 AttributeDescription **ads;
1618 Modifications** modlist,
1620 struct berval* syncUUID,
1621 struct sync_cookie* syncCookie_req,
1622 struct berval* syncCSN )
1624 Backend *be = op->o_bd;
1625 slap_callback cb = { NULL, NULL, NULL, NULL };
1626 struct berval *syncuuid_bv = NULL;
1627 struct berval syncUUID_strrep = BER_BVNULL;
1628 struct berval uuid_bv = BER_BVNULL;
1630 SlapReply rs_search = {REP_RESULT};
1631 SlapReply rs_delete = {REP_RESULT};
1632 SlapReply rs_add = {REP_RESULT};
1633 SlapReply rs_modify = {REP_RESULT};
1635 #ifdef LDAP_COMP_MATCH
1636 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1638 AttributeAssertion ava = { NULL, BER_BVNULL };
1640 int rc = LDAP_SUCCESS;
1642 struct berval pdn = BER_BVNULL;
1646 switch( syncstate ) {
1647 case LDAP_SYNC_PRESENT:
1648 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1650 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1653 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1655 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1657 case LDAP_SYNC_DELETE:
1658 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1660 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1662 case LDAP_SYNC_MODIFY:
1663 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1665 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1668 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1670 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1673 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1674 if ( !si->si_refreshPresent ) {
1675 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1676 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1677 syncuuid_cmp, avl_dup_error );
1681 if ( syncstate == LDAP_SYNC_PRESENT ) {
1683 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1684 if ( entry == NULL ) {
1689 f.f_choice = LDAP_FILTER_EQUALITY;
1691 ava.aa_desc = slap_schema.si_ad_entryUUID;
1692 ava.aa_value = *syncUUID;
1694 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1695 if ( syncuuid_bv ) {
1696 Debug( LDAP_DEBUG_SYNC, "syncrepl_entry: inserted UUID %s\n",
1697 syncUUID_strrep.bv_val, 0, 0 );
1699 op->ors_filter = &f;
1701 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1702 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1703 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1704 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1705 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1706 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1707 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1708 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1710 op->o_tag = LDAP_REQ_SEARCH;
1711 op->ors_scope = LDAP_SCOPE_SUBTREE;
1712 op->ors_deref = LDAP_DEREF_NEVER;
1714 /* get the entry for this UUID */
1715 op->o_req_dn = si->si_base;
1716 op->o_req_ndn = si->si_base;
1718 op->o_time = slap_get_time();
1719 op->ors_tlimit = SLAP_NO_LIMIT;
1722 op->ors_attrs = slap_anlist_all_attributes;
1723 op->ors_attrsonly = 0;
1725 /* set callback function */
1726 op->o_callback = &cb;
1727 cb.sc_response = dn_callback;
1728 cb.sc_private = &dni;
1729 dni.new_entry = entry;
1731 if ( limits_check( op, &rs_search ) == 0 ) {
1732 rc = be->be_search( op, &rs_search );
1733 Debug( LDAP_DEBUG_SYNC,
1734 "syncrepl_entry: %s (%d)\n",
1735 "be_search", rc, 0 );
1738 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1739 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1742 cb.sc_response = null_callback;
1745 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1746 Debug( LDAP_DEBUG_SYNC,
1747 "syncrepl_entry: %s\n",
1748 entry->e_name.bv_val, 0, 0 );
1750 Debug( LDAP_DEBUG_SYNC,
1751 "syncrepl_entry: %s\n",
1752 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1755 if ( syncstate != LDAP_SYNC_DELETE ) {
1756 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1759 /* add if missing */
1760 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1761 &syncUUID_strrep, syncUUID );
1763 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1764 /* replace only if necessary */
1765 if ( a->a_nvals != a->a_vals ) {
1766 ber_memfree( a->a_nvals[0].bv_val );
1767 ber_dupbv( &a->a_nvals[0], syncUUID );
1769 ber_memfree( a->a_vals[0].bv_val );
1770 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1774 slap_op_time( &op->o_time, &op->o_tincr );
1775 switch ( syncstate ) {
1777 case LDAP_SYNC_MODIFY:
1779 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1781 /* FIXME: op->o_csn is assumed to be
1782 * on the thread's slab; this needs
1783 * to be cleared ASAP.
1784 * What happens if already present?
1786 assert( BER_BVISNULL( &op->o_csn ) );
1787 op->o_csn = a->a_vals[0];
1791 if ( BER_BVISNULL( &dni.dn )) {
1793 op->o_req_dn = entry->e_name;
1794 op->o_req_ndn = entry->e_nname;
1795 op->o_tag = LDAP_REQ_ADD;
1798 rc = be->be_add( op, &rs_add );
1799 Debug( LDAP_DEBUG_SYNC,
1800 "syncrepl_entry: %s (%d)\n",
1802 switch ( rs_add.sr_err ) {
1804 be_entry_release_w( op, entry );
1809 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1810 * only if the suffix entry is not present */
1811 case LDAP_NO_SUCH_OBJECT:
1812 rc = syncrepl_add_glue( op, entry );
1816 /* if an entry was added via syncrepl_add_glue(),
1817 * it likely has no entryUUID, so the previous
1818 * be_search() doesn't find it. In this case,
1819 * give syncrepl a chance to modify it. Also
1820 * allow for entries that were recreated with the
1821 * same DN but a different entryUUID.
1823 case LDAP_ALREADY_EXISTS:
1825 Operation op2 = *op;
1826 SlapReply rs2 = { 0 };
1827 slap_callback cb2 = { 0 };
1829 op2.o_tag = LDAP_REQ_SEARCH;
1830 op2.o_req_dn = entry->e_name;
1831 op2.o_req_ndn = entry->e_nname;
1832 op2.ors_scope = LDAP_SCOPE_BASE;
1833 op2.ors_deref = LDAP_DEREF_NEVER;
1834 op2.ors_attrs = slap_anlist_all_attributes;
1835 op2.ors_attrsonly = 0;
1836 op2.ors_limit = NULL;
1838 op2.ors_tlimit = SLAP_NO_LIMIT;
1840 f.f_choice = LDAP_FILTER_PRESENT;
1841 f.f_desc = slap_schema.si_ad_objectClass;
1842 op2.ors_filter = &f;
1843 op2.ors_filterstr = generic_filterstr;
1845 op2.o_callback = &cb2;
1846 cb2.sc_response = dn_callback;
1847 cb2.sc_private = &dni;
1849 rc = be->be_search( &op2, &rs2 );
1850 if ( rc ) goto done;
1853 slap_op_time( &op->o_time, &op->o_tincr );
1859 Debug( LDAP_DEBUG_ANY,
1860 "syncrepl_entry : be_add failed (%d)\n",
1861 rs_add.sr_err, 0, 0 );
1867 op->o_req_dn = dni.dn;
1868 op->o_req_ndn = dni.ndn;
1869 if ( dni.renamed ) {
1870 struct berval noldp, newp, nnewp;
1872 op->o_tag = LDAP_REQ_MODRDN;
1873 dnRdn( &entry->e_name, &op->orr_newrdn );
1874 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1876 dnParent( &dni.ndn, &noldp );
1877 dnParent( &entry->e_nname, &nnewp );
1878 if ( !dn_match( &noldp, &nnewp )) {
1879 dnParent( &entry->e_name, &newp );
1880 op->orr_newSup = &newp;
1881 op->orr_nnewSup = &nnewp;
1883 op->orr_deleteoldrdn = 0;
1884 op->orr_modlist = NULL;
1885 if (( rc = slap_modrdn2mods( op, &rs_modify ))) {
1889 /* RDNs must be NUL-terminated for back-ldap */
1890 noldp = op->orr_newrdn;
1891 ber_dupbv_x( &op->orr_newrdn, &noldp, op->o_tmpmemctx );
1892 noldp = op->orr_nnewrdn;
1893 ber_dupbv_x( &op->orr_nnewrdn, &noldp, op->o_tmpmemctx );
1895 rc = be->be_modrdn( op, &rs_modify );
1896 op->o_tmpfree( op->orr_nnewrdn.bv_val, op->o_tmpmemctx );
1897 op->o_tmpfree( op->orr_newrdn.bv_val, op->o_tmpmemctx );
1899 slap_mods_free( op->orr_modlist, 1 );
1900 Debug( LDAP_DEBUG_SYNC,
1901 "syncrepl_entry: %s (%d)\n",
1902 "be_modrdn", rc, 0 );
1903 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1904 op->o_req_dn = entry->e_name;
1905 op->o_req_ndn = entry->e_nname;
1909 if ( dni.wasChanged )
1910 slap_op_time( &op->o_time, &op->o_tincr );
1912 if ( dni.wasChanged ) {
1913 Modifications *mod, *modhead = NULL;
1914 Modifications *modtail = NULL;
1917 op->o_tag = LDAP_REQ_MODIFY;
1919 assert( *modlist != NULL );
1921 /* Delete all the old attrs */
1922 for ( i = 0; i < dni.attrs; i++ ) {
1923 mod = ch_malloc( sizeof( Modifications ) );
1924 mod->sml_op = LDAP_MOD_DELETE;
1926 mod->sml_desc = dni.ads[i];
1927 mod->sml_type = mod->sml_desc->ad_cname;
1928 mod->sml_values = NULL;
1929 mod->sml_nvalues = NULL;
1930 if ( !modhead ) modhead = mod;
1932 modtail->sml_next = mod;
1937 /* Append passed in list to ours */
1939 modtail->sml_next = *modlist;
1945 /* Find end of this list */
1946 for ( ; mod != NULL; mod = mod->sml_next ) {
1950 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1951 mod->sml_op = LDAP_MOD_REPLACE;
1953 mod->sml_desc = slap_schema.si_ad_entryUUID;
1954 mod->sml_type = mod->sml_desc->ad_cname;
1955 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1956 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1957 ber_dupbv( &uuid_bv, syncUUID );
1958 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1959 modtail->sml_next = mod;
1961 op->o_tag = LDAP_REQ_MODIFY;
1962 op->orm_modlist = *modlist;
1964 rc = be->be_modify( op, &rs_modify );
1965 Debug( LDAP_DEBUG_SYNC,
1966 "syncrepl_entry: %s (%d)\n",
1967 "be_modify", rc, 0 );
1968 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1969 Debug( LDAP_DEBUG_ANY,
1970 "syncrepl_entry : be_modify failed (%d)\n",
1971 rs_modify.sr_err, 0, 0 );
1975 case LDAP_SYNC_DELETE :
1976 if ( !BER_BVISNULL( &dni.dn )) {
1977 op->o_req_dn = dni.dn;
1978 op->o_req_ndn = dni.ndn;
1979 op->o_tag = LDAP_REQ_DELETE;
1980 rc = be->be_delete( op, &rs_delete );
1981 Debug( LDAP_DEBUG_SYNC,
1982 "syncrepl_entry: %s (%d)\n",
1983 "be_delete", rc, 0 );
1985 while ( rs_delete.sr_err == LDAP_SUCCESS
1986 && op->o_delete_glue_parent ) {
1987 op->o_delete_glue_parent = 0;
1988 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1989 slap_callback cb = { NULL };
1990 cb.sc_response = slap_null_cb;
1991 dnParent( &op->o_req_ndn, &pdn );
1993 op->o_req_ndn = pdn;
1994 op->o_callback = &cb;
1995 op->o_bd->be_delete( op, &rs_delete );
2004 Debug( LDAP_DEBUG_ANY,
2005 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
2010 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
2011 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
2012 BER_BVZERO( &syncUUID_strrep );
2015 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
2017 if ( !BER_BVISNULL( &dni.ndn ) ) {
2018 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
2020 if ( !BER_BVISNULL( &dni.dn ) ) {
2021 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
2024 entry_free( entry );
2025 BER_BVZERO( &op->o_csn );
2029 static struct berval gcbva[] = {
2035 #define NP_DELETE_ONE 2
2038 syncrepl_del_nonpresent(
2042 struct berval *cookiecsn )
2044 Backend* be = op->o_bd;
2045 slap_callback cb = { NULL };
2046 SlapReply rs_search = {REP_RESULT};
2047 SlapReply rs_delete = {REP_RESULT};
2048 SlapReply rs_modify = {REP_RESULT};
2049 struct nonpresent_entry *np_list, *np_prev;
2051 AttributeName an[2];
2053 struct berval pdn = BER_BVNULL;
2056 op->o_req_dn = si->si_base;
2057 op->o_req_ndn = si->si_base;
2059 cb.sc_response = nonpresent_callback;
2062 op->o_callback = &cb;
2063 op->o_tag = LDAP_REQ_SEARCH;
2064 op->ors_scope = si->si_scope;
2065 op->ors_deref = LDAP_DEREF_NEVER;
2066 op->o_time = slap_get_time();
2067 op->ors_tlimit = SLAP_NO_LIMIT;
2072 #ifdef LDAP_COMP_MATCH
2073 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2075 AttributeAssertion eq = { NULL, BER_BVNULL };
2079 op->ors_attrsonly = 1;
2080 op->ors_attrs = slap_anlist_no_attrs;
2081 op->ors_limit = NULL;
2082 op->ors_filter = &uf;
2085 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2087 uf.f_choice = LDAP_FILTER_EQUALITY;
2088 si->si_refreshDelete |= NP_DELETE_ONE;
2090 for (i=0; uuids[i].bv_val; i++) {
2092 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2094 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2095 uf.f_av_value = uuids[i];
2096 rc = be->be_search( op, &rs_search );
2097 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2099 si->si_refreshDelete ^= NP_DELETE_ONE;
2101 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2102 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2103 an[0].an_desc = slap_schema.si_ad_entryUUID;
2105 op->ors_slimit = SLAP_NO_LIMIT;
2106 op->ors_attrsonly = 0;
2107 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2108 op->ors_filterstr = si->si_filterstr;
2109 op->o_nocaching = 1;
2111 if ( limits_check( op, &rs_search ) == 0 ) {
2112 rc = be->be_search( op, &rs_search );
2114 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2117 op->o_nocaching = 0;
2119 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2121 if ( cookiecsn && !BER_BVISNULL( cookiecsn ))
2124 csn = si->si_syncCookie.ctxcsn;
2126 slap_queue_csn( op, &csn );
2128 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2129 while ( np_list != NULL ) {
2130 LDAP_LIST_REMOVE( np_list, npe_link );
2132 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2133 op->o_tag = LDAP_REQ_DELETE;
2134 op->o_callback = &cb;
2135 cb.sc_response = null_callback;
2137 op->o_req_dn = *np_prev->npe_name;
2138 op->o_req_ndn = *np_prev->npe_nname;
2139 rc = op->o_bd->be_delete( op, &rs_delete );
2140 Debug( LDAP_DEBUG_SYNC,
2141 "syncrepl_del_nonpresent: be_delete %s (%d)\n",
2142 op->o_req_dn.bv_val, rc, 0 );
2144 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2145 Modifications mod1, mod2;
2146 mod1.sml_op = LDAP_MOD_REPLACE;
2148 mod1.sml_desc = slap_schema.si_ad_objectClass;
2149 mod1.sml_type = mod1.sml_desc->ad_cname;
2150 mod1.sml_values = &gcbva[0];
2151 mod1.sml_nvalues = NULL;
2152 mod1.sml_next = &mod2;
2154 mod2.sml_op = LDAP_MOD_REPLACE;
2156 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2157 mod2.sml_type = mod2.sml_desc->ad_cname;
2158 mod2.sml_values = &gcbva[1];
2159 mod2.sml_nvalues = NULL;
2160 mod2.sml_next = NULL;
2162 op->o_tag = LDAP_REQ_MODIFY;
2163 op->orm_modlist = &mod1;
2165 rc = be->be_modify( op, &rs_modify );
2168 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2169 op->o_delete_glue_parent ) {
2170 op->o_delete_glue_parent = 0;
2171 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2172 slap_callback cb = { NULL };
2173 cb.sc_response = slap_null_cb;
2174 dnParent( &op->o_req_ndn, &pdn );
2176 op->o_req_ndn = pdn;
2177 op->o_callback = &cb;
2178 /* give it a root privil ? */
2179 op->o_bd->be_delete( op, &rs_delete );
2185 op->o_delete_glue_parent = 0;
2187 ber_bvfree( np_prev->npe_name );
2188 ber_bvfree( np_prev->npe_nname );
2192 slap_graduate_commit_csn( op );
2194 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2195 BER_BVZERO( &op->o_csn );
2206 Backend *be = op->o_bd;
2207 slap_callback cb = { NULL };
2212 struct berval dn = BER_BVNULL;
2213 struct berval ndn = BER_BVNULL;
2215 SlapReply rs_add = {REP_RESULT};
2216 struct berval ptr, nptr;
2219 op->o_tag = LDAP_REQ_ADD;
2220 op->o_callback = &cb;
2221 cb.sc_response = null_callback;
2222 cb.sc_private = NULL;
2227 /* count RDNs in suffix */
2228 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2229 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2231 ptr.bv_len -= comma - ptr.bv_val;
2241 /* Start with BE suffix */
2243 for ( i = 0; i < suffrdns; i++ ) {
2244 comma = ber_bvrchr( &ptr, ',' );
2245 if ( comma != NULL ) {
2246 ptr.bv_len = comma - ptr.bv_val;
2253 if ( !BER_BVISEMPTY( &ptr ) ) {
2254 dn.bv_len -= ptr.bv_len + 1;
2255 dn.bv_val += ptr.bv_len + 1;
2258 /* the normalizedDNs are always the same length, no counting
2262 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2263 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2264 ndn.bv_len = be->be_nsuffix[0].bv_len;
2266 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2272 while ( ndn.bv_val > e->e_nname.bv_val ) {
2273 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2274 ber_dupbv( &glue->e_name, &dn );
2275 ber_dupbv( &glue->e_nname, &ndn );
2277 a = ch_calloc( 1, sizeof( Attribute ));
2278 a->a_desc = slap_schema.si_ad_objectClass;
2280 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2281 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2282 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2283 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2285 a->a_nvals = a->a_vals;
2287 a->a_next = glue->e_attrs;
2290 a = ch_calloc( 1, sizeof( Attribute ));
2291 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2293 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2294 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2295 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2297 a->a_nvals = a->a_vals;
2299 a->a_next = glue->e_attrs;
2302 op->o_req_dn = glue->e_name;
2303 op->o_req_ndn = glue->e_nname;
2305 rc = be->be_add ( op, &rs_add );
2306 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2307 be_entry_release_w( op, glue );
2309 /* incl. ALREADY EXIST */
2311 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2317 /* Move to next child */
2318 comma = ber_bvrchr( &ptr, ',' );
2319 if ( comma == NULL ) {
2322 ptr.bv_len = comma - ptr.bv_val;
2324 dn.bv_val = ++comma;
2325 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2327 comma = ber_bvrchr( &nptr, ',' );
2328 assert( comma != NULL );
2329 nptr.bv_len = comma - nptr.bv_val;
2331 ndn.bv_val = ++comma;
2332 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2335 op->o_req_dn = e->e_name;
2336 op->o_req_ndn = e->e_nname;
2338 rc = be->be_add ( op, &rs_add );
2339 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2340 be_entry_release_w( op, e );
2349 syncrepl_updateCookie(
2353 struct sync_cookie *syncCookie )
2355 Backend *be = op->o_bd;
2356 Modifications mod = { { 0 } };
2357 struct berval vals[ 2 ];
2361 slap_callback cb = { NULL };
2362 SlapReply rs_modify = {REP_RESULT};
2364 mod.sml_op = LDAP_MOD_REPLACE;
2365 mod.sml_desc = slap_schema.si_ad_contextCSN;
2366 mod.sml_type = mod.sml_desc->ad_cname;
2367 mod.sml_values = vals;
2368 vals[0] = syncCookie->ctxcsn;
2369 BER_BVZERO( &vals[1] );
2371 slap_queue_csn( op, &syncCookie->ctxcsn );
2373 op->o_tag = LDAP_REQ_MODIFY;
2375 assert( si->si_rid < 1000 );
2377 cb.sc_response = null_callback;
2380 op->o_callback = &cb;
2381 op->o_req_dn = op->o_bd->be_suffix[0];
2382 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2384 /* update contextCSN */
2385 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2386 op->orm_modlist = &mod;
2387 rc = be->be_modify( op, &rs_modify );
2390 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2391 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2392 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2394 Debug( LDAP_DEBUG_ANY,
2395 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2398 slap_graduate_commit_csn( op );
2400 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2401 BER_BVZERO( &op->o_csn );
2411 dninfo *dni = op->o_callback->sc_private;
2413 if ( rs->sr_type == REP_SEARCH ) {
2414 if ( !BER_BVISNULL( &dni->dn ) ) {
2415 Debug( LDAP_DEBUG_ANY,
2416 "dn_callback : consistency error - "
2417 "entryUUID is not unique\n", 0, 0, 0 );
2419 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2420 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2421 /* If there is a new entry, see if it differs from the old.
2422 * We compare the non-normalized values so that cosmetic changes
2423 * in the provider are always propagated.
2425 if ( dni->new_entry ) {
2426 Attribute *old, *new;
2429 /* Did the DN change? Note that we don't explicitly try to
2430 * discover if the deleteOldRdn argument applies here. It
2431 * would save an unnecessary Modify if we detected it, but
2432 * that's a fair amount of trouble to compare the two attr
2433 * lists in detail. (Just test normalized DN; we ignore
2434 * insignificant changes here.)
2436 if ( !dn_match( &rs->sr_entry->e_nname,
2437 &dni->new_entry->e_nname ) )
2442 for ( i = 0, old = rs->sr_entry->e_attrs;
2444 i++, old = old->a_next )
2449 /* We assume that attributes are saved in the same order
2450 * in the remote and local databases. So if we walk through
2451 * the attributeDescriptions one by one they should match in
2452 * lock step. If not, we signal a change. Otherwise we test
2455 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2457 old = old->a_next, new = new->a_next )
2459 if ( old->a_desc != new->a_desc ) {
2460 dni->wasChanged = 1;
2463 for ( i = 0; ; i++ ) {
2465 nold = BER_BVISNULL( &old->a_vals[i] );
2466 nnew = BER_BVISNULL( &new->a_vals[i] );
2467 /* If both are empty, stop looking */
2468 if ( nold && nnew ) {
2471 /* If they are different, stop looking */
2472 if ( nold != nnew ) {
2473 dni->wasChanged = 1;
2476 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2477 dni->wasChanged = 1;
2481 if ( dni->wasChanged ) break;
2483 if ( dni->wasChanged ) {
2484 dni->ads = op->o_tmpalloc( dni->attrs *
2485 sizeof(AttributeDescription *), op->o_tmpmemctx );
2487 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2488 dni->ads[i] = old->a_desc;
2494 } else if ( rs->sr_type == REP_RESULT ) {
2495 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2496 Debug( LDAP_DEBUG_ANY,
2497 "dn_callback : consistency error - "
2498 "entryUUID is not unique\n", 0, 0, 0 );
2502 return LDAP_SUCCESS;
2506 nonpresent_callback(
2510 syncinfo_t *si = op->o_callback->sc_private;
2513 struct berval* present_uuid = NULL;
2514 struct nonpresent_entry *np_entry;
2516 if ( rs->sr_type == REP_RESULT ) {
2517 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2518 si->si_presentlist = NULL;
2520 } else if ( rs->sr_type == REP_SEARCH ) {
2521 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2522 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2525 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2528 Debug( LDAP_DEBUG_SYNC, "nonpresent_callback: UUID %s, dn %s, %sfound\n",
2529 a ? a->a_vals[0].bv_val : "<missing>", rs->sr_entry->e_name.bv_val, present_uuid ? "" : "not " );
2531 if ( a == NULL ) return 0;
2534 if ( present_uuid == NULL ) {
2535 np_entry = (struct nonpresent_entry *)
2536 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2537 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2538 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2539 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2542 avl_delete( &si->si_presentlist,
2543 &a->a_nvals[0], syncuuid_cmp );
2544 ch_free( present_uuid->bv_val );
2545 ch_free( present_uuid );
2548 return LDAP_SUCCESS;
2556 if ( rs->sr_err != LDAP_SUCCESS &&
2557 rs->sr_err != LDAP_REFERRAL &&
2558 rs->sr_err != LDAP_ALREADY_EXISTS &&
2559 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2560 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2562 Debug( LDAP_DEBUG_ANY,
2563 "null_callback : error code 0x%x\n",
2566 return LDAP_SUCCESS;
2569 static struct berval *
2570 slap_uuidstr_from_normalized(
2571 struct berval* uuidstr,
2572 struct berval* normalized,
2576 unsigned char nibble;
2579 if ( normalized == NULL ) return NULL;
2580 if ( normalized->bv_len != 16 ) return NULL;
2585 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2586 if ( new == NULL ) {
2593 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2594 if ( new != uuidstr ) {
2595 slap_sl_free( new, ctx );
2600 for ( i = 0; i < 16; i++ ) {
2601 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2602 new->bv_val[(i<<1)+d] = '-';
2606 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2607 if ( nibble < 10 ) {
2608 new->bv_val[(i<<1)+d] = nibble + '0';
2610 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2613 nibble = (normalized->bv_val[i]) & 0xF;
2614 if ( nibble < 10 ) {
2615 new->bv_val[(i<<1)+d+1] = nibble + '0';
2617 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2621 new->bv_val[new->bv_len] = '\0';
2626 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2628 const struct berval *uuid1 = v_uuid1;
2629 const struct berval *uuid2 = v_uuid2;
2630 int rc = uuid1->bv_len - uuid2->bv_len;
2631 if ( rc ) return rc;
2632 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2636 avl_ber_bvfree( void *v_bv )
2638 struct berval *bv = (struct berval *)v_bv;
2640 if( v_bv == NULL ) return;
2641 if ( !BER_BVISNULL( bv ) ) {
2642 ch_free( bv->bv_val );
2644 ch_free( (char *) bv );
2648 syncinfo_free( syncinfo_t *sie )
2651 if ( sie->si_conn_setup ) {
2653 ldap_get_option( sie->si_ld, LDAP_OPT_DESC, &s );
2654 connection_client_stop( s );
2655 sie->si_conn_setup = 0;
2657 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2660 /* re-fetch it, in case it was already removed */
2661 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2663 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2664 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2665 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2668 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2670 bindconf_free( &sie->si_bindconf );
2672 if ( sie->si_filterstr.bv_val ) {
2673 ch_free( sie->si_filterstr.bv_val );
2675 if ( sie->si_base.bv_val ) {
2676 ch_free( sie->si_base.bv_val );
2678 if ( sie->si_attrs ) {
2680 while ( sie->si_attrs[i] != NULL ) {
2681 ch_free( sie->si_attrs[i] );
2684 ch_free( sie->si_attrs );
2686 if ( sie->si_exattrs ) {
2688 while ( sie->si_exattrs[i] != NULL ) {
2689 ch_free( sie->si_exattrs[i] );
2692 ch_free( sie->si_exattrs );
2694 if ( sie->si_anlist ) {
2696 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2697 ch_free( sie->si_anlist[i].an_name.bv_val );
2700 ch_free( sie->si_anlist );
2702 if ( sie->si_exanlist ) {
2704 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2705 ch_free( sie->si_exanlist[i].an_name.bv_val );
2708 ch_free( sie->si_exanlist );
2710 if ( sie->si_retryinterval ) {
2711 ch_free( sie->si_retryinterval );
2713 if ( sie->si_retrynum ) {
2714 ch_free( sie->si_retrynum );
2716 if ( sie->si_retrynum_init ) {
2717 ch_free( sie->si_retrynum_init );
2719 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2720 if ( sie->si_presentlist ) {
2721 avl_free( sie->si_presentlist, avl_ber_bvfree );
2723 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2724 struct nonpresent_entry* npe;
2725 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2726 LDAP_LIST_REMOVE( npe, npe_link );
2727 if ( npe->npe_name ) {
2728 if ( npe->npe_name->bv_val ) {
2729 ch_free( npe->npe_name->bv_val );
2731 ch_free( npe->npe_name );
2733 if ( npe->npe_nname ) {
2734 if ( npe->npe_nname->bv_val ) {
2735 ch_free( npe->npe_nname->bv_val );
2737 ch_free( npe->npe_nname );
2746 /* NOTE: used & documented in slapd.conf(5) */
2748 #define PROVIDERSTR "provider"
2749 #define SCHEMASTR "schemachecking"
2750 #define FILTERSTR "filter"
2751 #define SEARCHBASESTR "searchbase"
2752 #define SCOPESTR "scope"
2753 #define ATTRSONLYSTR "attrsonly"
2754 #define ATTRSSTR "attrs"
2755 #define TYPESTR "type"
2756 #define INTERVALSTR "interval"
2757 #define RETRYSTR "retry"
2758 #define SLIMITSTR "sizelimit"
2759 #define TLIMITSTR "timelimit"
2760 #define SYNCDATASTR "syncdata"
2762 /* FIXME: undocumented */
2763 #define LOGBASESTR "logbase"
2764 #define LOGFILTERSTR "logfilter"
2765 #define OLDAUTHCSTR "bindprincipal"
2766 #define EXATTRSSTR "exattrs"
2767 #define MANAGEDSAITSTR "manageDSAit"
2770 #define LASTMODSTR "lastmod"
2771 #define LMGENSTR "gen"
2772 #define LMNOSTR "no"
2773 #define LMREQSTR "req"
2774 #define SRVTABSTR "srvtab"
2775 #define SUFFIXSTR "suffix"
2778 #define GOT_ID 0x0001
2779 #define GOT_PROVIDER 0x0002
2780 #define GOT_BASE 0x0004
2783 #define GOT_ALL (GOT_ID|GOT_PROVIDER|GOT_BASE)
2789 { BER_BVC("base"), LDAP_SCOPE_BASE },
2790 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2791 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2792 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2793 { BER_BVC("subord"), LDAP_SCOPE_SUBORDINATE },
2794 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2795 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2796 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2800 static slap_verbmasks datamodes[] = {
2801 { BER_BVC("default"), SYNCDATA_DEFAULT },
2802 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2803 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2808 parse_syncrepl_line(
2816 for ( i = 1; i < c->argc; i++ ) {
2817 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2818 STRLENOF( IDSTR "=" ) ) )
2821 /* '\0' string terminator accounts for '=' */
2822 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2823 if ( lutil_atoi( &tmp, val ) != 0 ) {
2824 snprintf( c->msg, sizeof( c->msg ),
2825 "Error: parse_syncrepl_line: "
2826 "unable to parse syncrepl id \"%s\"", val );
2827 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2830 if ( tmp >= 1000 || tmp < 0 ) {
2831 snprintf( c->msg, sizeof( c->msg ),
2832 "Error: parse_syncrepl_line: "
2833 "syncrepl id %d is out of range [0..999]", tmp );
2834 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2839 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2840 STRLENOF( PROVIDERSTR "=" ) ) )
2842 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2843 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2844 gots |= GOT_PROVIDER;
2845 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2846 STRLENOF( SCHEMASTR "=" ) ) )
2848 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2849 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2850 si->si_schemachecking = 1;
2851 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2852 si->si_schemachecking = 0;
2854 si->si_schemachecking = 1;
2856 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2857 STRLENOF( FILTERSTR "=" ) ) )
2859 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2860 if ( si->si_filterstr.bv_val )
2861 ch_free( si->si_filterstr.bv_val );
2862 ber_str2bv( val, 0, 1, &si->si_filterstr );
2863 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2864 STRLENOF( LOGFILTERSTR "=" ) ) )
2866 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2867 if ( si->si_logfilterstr.bv_val )
2868 ch_free( si->si_logfilterstr.bv_val );
2869 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2870 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2871 STRLENOF( SEARCHBASESTR "=" ) ) )
2876 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2877 if ( si->si_base.bv_val ) {
2878 ch_free( si->si_base.bv_val );
2880 ber_str2bv( val, 0, 0, &bv );
2881 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2882 if ( rc != LDAP_SUCCESS ) {
2883 snprintf( c->msg, sizeof( c->msg ),
2884 "Invalid base DN \"%s\": %d (%s)",
2885 val, rc, ldap_err2string( rc ) );
2886 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2890 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2891 STRLENOF( LOGBASESTR "=" ) ) )
2896 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2897 if ( si->si_logbase.bv_val ) {
2898 ch_free( si->si_logbase.bv_val );
2900 ber_str2bv( val, 0, 0, &bv );
2901 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2902 if ( rc != LDAP_SUCCESS ) {
2903 snprintf( c->msg, sizeof( c->msg ),
2904 "Invalid logbase DN \"%s\": %d (%s)",
2905 val, rc, ldap_err2string( rc ) );
2906 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2909 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2910 STRLENOF( SCOPESTR "=" ) ) )
2913 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2914 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2915 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2916 si->si_scope = scopes[j].val;
2920 if ( BER_BVISNULL(&scopes[j].key) ) {
2921 snprintf( c->msg, sizeof( c->msg ),
2922 "Error: parse_syncrepl_line: "
2923 "unknown scope \"%s\"", val);
2924 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2927 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2928 STRLENOF( ATTRSONLYSTR ) ) )
2930 si->si_attrsonly = 1;
2931 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2932 STRLENOF( ATTRSSTR "=" ) ) )
2934 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2935 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2937 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2938 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2939 if ( si->si_anlist == NULL ) {
2940 ch_free( attr_fname );
2943 si->si_anfile = attr_fname;
2945 char *str, *s, *next;
2946 char delimstr[] = " ,\t";
2947 str = ch_strdup( val );
2948 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2950 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2952 if ( strlen(s) == 1 && *s == '*' ) {
2953 si->si_allattrs = 1;
2954 *(val + ( s - str )) = delimstr[0];
2956 if ( strlen(s) == 1 && *s == '+' ) {
2957 si->si_allopattrs = 1;
2958 *(val + ( s - str )) = delimstr[0];
2962 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2963 if ( si->si_anlist == NULL ) {
2967 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2968 STRLENOF( EXATTRSSTR "=" ) ) )
2970 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2971 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2973 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2974 si->si_exanlist = file2anlist(
2975 si->si_exanlist, attr_fname, " ,\t" );
2976 if ( si->si_exanlist == NULL ) {
2977 ch_free( attr_fname );
2980 ch_free( attr_fname );
2982 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2983 if ( si->si_exanlist == NULL ) {
2987 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2988 STRLENOF( TYPESTR "=" ) ) )
2990 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2991 if ( !strncasecmp( val, "refreshOnly",
2992 STRLENOF("refreshOnly") ) )
2994 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2995 } else if ( !strncasecmp( val, "refreshAndPersist",
2996 STRLENOF("refreshAndPersist") ) )
2998 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2999 si->si_interval = 60;
3001 snprintf( c->msg, sizeof( c->msg ),
3002 "Error: parse_syncrepl_line: "
3003 "unknown sync type \"%s\"", val);
3004 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3007 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
3008 STRLENOF( INTERVALSTR "=" ) ) )
3010 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
3011 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
3012 si->si_interval = 0;
3013 } else if ( strchr( val, ':' ) != NULL ) {
3014 char *next, *ptr = val;
3015 unsigned dd, hh, mm, ss;
3017 /* NOTE: the test for ptr[ 0 ] == '-'
3018 * should go before the call to strtoul() */
3019 dd = strtoul( ptr, &next, 10 );
3020 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' ) {
3021 snprintf( c->msg, sizeof( c->msg ),
3022 "Error: parse_syncrepl_line: "
3023 "invalid interval \"%s\", unable to parse days", val );
3024 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3028 hh = strtoul( ptr, &next, 10 );
3029 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || hh > 24 ) {
3030 snprintf( c->msg, sizeof( c->msg ),
3031 "Error: parse_syncrepl_line: "
3032 "invalid interval \"%s\", unable to parse hours", val );
3033 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3037 mm = strtoul( ptr, &next, 10 );
3038 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != ':' || mm > 60 ) {
3039 snprintf( c->msg, sizeof( c->msg ),
3040 "Error: parse_syncrepl_line: "
3041 "invalid interval \"%s\", unable to parse minutes", val );
3042 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3046 ss = strtoul( ptr, &next, 10 );
3047 if ( ptr[ 0 ] == '-' || next == ptr || next[0] != '\0' || ss > 60 ) {
3048 snprintf( c->msg, sizeof( c->msg ),
3049 "Error: parse_syncrepl_line: "
3050 "invalid interval \"%s\", unable to parse seconds", val );
3051 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3054 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
3058 if ( lutil_parse_time( val, &t ) != 0 ) {
3059 snprintf( c->msg, sizeof( c->msg ),
3060 "Error: parse_syncrepl_line: "
3061 "invalid interval \"%s\"", val );
3062 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3065 si->si_interval = (time_t)t;
3067 if ( si->si_interval < 0 ) {
3068 snprintf( c->msg, sizeof( c->msg ),
3069 "Error: parse_syncrepl_line: "
3070 "invalid interval \"%ld\"",
3071 (long) si->si_interval);
3072 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3075 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3076 STRLENOF( RETRYSTR "=" ) ) )
3081 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3082 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3083 retry_list[0] = NULL;
3085 slap_str2clist( &retry_list, val, " ,\t" );
3087 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3090 snprintf( c->msg, sizeof( c->msg ),
3091 "Error: incomplete syncrepl retry list" );
3092 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3093 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3094 ch_free( retry_list[k] );
3096 ch_free( retry_list );
3099 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3100 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3101 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3102 for ( j = 0; j < n; j++ ) {
3104 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3105 snprintf( c->msg, sizeof( c->msg ),
3106 "Error: invalid retry interval \"%s\" (#%d)",
3107 retry_list[j*2], j );
3108 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3109 /* do some cleanup */
3112 si->si_retryinterval[j] = (time_t)t;
3113 if ( *retry_list[j*2+1] == '+' ) {
3114 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3115 si->si_retrynum[j] = RETRYNUM_FOREVER;
3119 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3120 || si->si_retrynum_init[j] <= 0 )
3122 snprintf( c->msg, sizeof( c->msg ),
3123 "Error: invalid initial retry number \"%s\" (#%d)",
3124 retry_list[j*2+1], j );
3125 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3126 /* do some cleanup */
3129 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3130 || si->si_retrynum[j] <= 0 )
3132 snprintf( c->msg, sizeof( c->msg ),
3133 "Error: invalid retry number \"%s\" (#%d)",
3134 retry_list[j*2+1], j );
3135 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3136 /* do some cleanup */
3141 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3142 si->si_retrynum[j] = RETRYNUM_TAIL;
3143 si->si_retryinterval[j] = 0;
3145 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3146 ch_free( retry_list[k] );
3148 ch_free( retry_list );
3149 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3150 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3152 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3153 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3154 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3156 snprintf( c->msg, sizeof( c->msg ),
3157 "invalid manageDSAit value \"%s\".\n",
3159 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3162 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3163 STRLENOF( SLIMITSTR "=") ) )
3165 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3166 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3169 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3170 snprintf( c->msg, sizeof( c->msg ),
3171 "invalid size limit value \"%s\".\n",
3173 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3176 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3177 STRLENOF( TLIMITSTR "=" ) ) )
3179 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3180 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3183 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3184 snprintf( c->msg, sizeof( c->msg ),
3185 "invalid time limit value \"%s\".\n",
3187 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3190 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3191 STRLENOF( SYNCDATASTR "=" ) ) )
3193 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3194 si->si_syncdata = verb_to_mask( val, datamodes );
3195 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3196 snprintf( c->msg, sizeof( c->msg ),
3197 "Error: parse_syncrepl_line: "
3198 "unknown keyword \"%s\"\n", c->argv[ i ] );
3199 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3204 if ( gots != GOT_ALL ) {
3205 snprintf( c->msg, sizeof( c->msg ),
3206 "Error: Malformed \"syncrepl\" line in slapd config file, missing%s%s%s",
3207 gots & GOT_ID ? "" : " "IDSTR,
3208 gots & GOT_PROVIDER ? "" : " "PROVIDERSTR,
3209 gots & GOT_BASE ? "" : " "SEARCHBASESTR );
3210 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3224 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3225 snprintf( c->msg, sizeof(c->msg), "database %s does not support "
3226 "operations required for syncrepl", c->be->be_type );
3227 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3230 if ( BER_BVISEMPTY( &c->be->be_rootdn )) {
3231 strcpy( c->msg, "rootDN must be defined before syncrepl may be used" );
3232 Debug( LDAP_DEBUG_ANY, "%s: %s\n", c->log, c->msg, 0 );
3235 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3238 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3242 si->si_bindconf.sb_tls = SB_TLS_OFF;
3243 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3244 si->si_schemachecking = 0;
3245 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3246 &si->si_filterstr );
3247 si->si_base.bv_val = NULL;
3248 si->si_scope = LDAP_SCOPE_SUBTREE;
3249 si->si_attrsonly = 0;
3250 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3251 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3252 si->si_attrs = NULL;
3253 si->si_allattrs = 0;
3254 si->si_allopattrs = 0;
3255 si->si_exattrs = NULL;
3256 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3257 si->si_interval = 86400;
3258 si->si_retryinterval = NULL;
3259 si->si_retrynum_init = NULL;
3260 si->si_retrynum = NULL;
3261 si->si_manageDSAit = 0;
3264 si->si_conn_setup = 0;
3266 si->si_presentlist = NULL;
3267 LDAP_LIST_INIT( &si->si_nonpresentlist );
3268 ldap_pvt_thread_mutex_init( &si->si_mutex );
3270 rc = parse_syncrepl_line( c, si );
3274 init_syncrepl( si );
3275 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3276 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3281 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3282 syncinfo_free( si );
3285 Debug( LDAP_DEBUG_CONFIG,
3286 "Config: ** successfully added syncrepl \"%s\"\n",
3287 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3288 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3289 if ( !si->si_schemachecking ) {
3290 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3292 c->be->be_syncinfo = si;
3298 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3300 struct berval bc, uri;
3301 char buf[BUFSIZ*2], *ptr;
3304 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3308 /* temporarily inhibit bindconf from printing URI */
3309 uri = si->si_bindconf.sb_uri;
3310 BER_BVZERO( &si->si_bindconf.sb_uri );
3311 bindconf_unparse( &si->si_bindconf, &bc );
3312 si->si_bindconf.sb_uri = uri;
3315 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3316 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3317 if ( ptr - buf >= sizeof( buf ) ) return;
3318 if ( !BER_BVISNULL( &bc )) {
3319 if ( WHATSLEFT <= bc.bv_len ) {
3323 ptr = lutil_strcopy( ptr, bc.bv_val );
3326 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3327 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3328 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3329 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3332 if ( !BER_BVISNULL( &si->si_base )) {
3333 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3334 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3335 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3338 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3339 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3340 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3341 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3344 if ( !BER_BVISNULL( &si->si_logbase )) {
3345 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
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 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3353 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3354 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3358 if ( si->si_attrsonly ) {
3359 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3360 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3362 if ( si->si_anfile ) {
3363 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3364 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3365 ptr = lutil_strcopy( ptr, si->si_anfile );
3367 } else if ( si->si_allattrs || si->si_allopattrs ||
3368 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3372 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3373 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3375 /* FIXME: add check for overflow */
3376 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3377 if ( si->si_allattrs ) {
3378 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3379 if ( old != ptr ) *ptr++ = ',';
3382 if ( si->si_allopattrs ) {
3383 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3384 if ( old != ptr ) *ptr++ = ',';
3389 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3390 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3391 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3392 /* FIXME: add check for overflow */
3393 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3395 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3396 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3397 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3399 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3400 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3401 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3402 "refreshAndPersist" : "refreshOnly" );
3404 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3407 dd = si->si_interval;
3414 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3415 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3416 if ( ptr - buf >= sizeof( buf ) ) return;
3417 } else if ( si->si_retryinterval ) {
3419 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3420 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3421 for (i=0; si->si_retryinterval[i]; i++) {
3422 if ( space ) *ptr++ = ' ';
3424 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3425 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3428 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3430 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3434 if ( si->si_slimit ) {
3435 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3436 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3437 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3440 if ( si->si_tlimit ) {
3441 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3442 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3443 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3446 if ( si->si_syncdata ) {
3447 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3448 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3449 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3450 ptr = lutil_strcopy( ptr, bc.bv_val );
3453 bc.bv_len = ptr - buf;
3455 ber_dupbv( bv, &bc );
3459 syncrepl_config( ConfigArgs *c )
3461 if (c->op == SLAP_CONFIG_EMIT) {
3462 if ( c->be->be_syncinfo ) {
3464 syncrepl_unparse( c->be->be_syncinfo, &bv );
3465 ber_bvarray_add( &c->rvalue_vals, &bv );
3469 } else if ( c->op == LDAP_MOD_DELETE ) {
3470 if ( c->be->be_syncinfo ) {
3471 syncinfo_free( c->be->be_syncinfo );
3472 c->be->be_syncinfo = NULL;
3474 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3477 if ( SLAP_SHADOW( c->be ) ) {
3478 Debug(LDAP_DEBUG_ANY, "%s: "
3479 "syncrepl: database already shadowed.\n",
3482 } else if ( add_syncrepl( c ) ) {
3485 return config_sync_shadow( c );