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 );
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, &op->o_protocol );
440 if ( si->si_bindconf.sb_tls ) {
441 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
442 if( rc != LDAP_SUCCESS ) {
443 Debug( LDAP_DEBUG_ANY,
444 "%s: ldap_start_tls failed (%d)\n",
445 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
447 if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
451 if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
452 #ifdef HAVE_CYRUS_SASL
455 if ( si->si_bindconf.sb_secprops != NULL ) {
456 rc = ldap_set_option( si->si_ld,
457 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
459 if( rc != LDAP_OPT_SUCCESS ) {
460 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
461 "(%s,SECPROPS,\"%s\") failed!\n",
462 si->si_bindconf.sb_uri.bv_val, si->si_bindconf.sb_secprops, 0 );
467 defaults = lutil_sasl_defaults( si->si_ld,
468 si->si_bindconf.sb_saslmech.bv_val,
469 si->si_bindconf.sb_realm.bv_val,
470 si->si_bindconf.sb_authcId.bv_val,
471 si->si_bindconf.sb_cred.bv_val,
472 si->si_bindconf.sb_authzId.bv_val );
474 rc = ldap_sasl_interactive_bind_s( si->si_ld,
475 si->si_bindconf.sb_binddn.bv_val,
476 si->si_bindconf.sb_saslmech.bv_val,
482 lutil_sasl_freedefs( defaults );
484 /* FIXME: different error behaviors according to
486 * 2) on err policy : exit, retry, backoff ...
488 if ( rc != LDAP_SUCCESS ) {
489 static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
491 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
492 "ldap_sasl_interactive_bind_s failed (%d)\n",
495 /* FIXME (see above comment) */
496 /* if Kerberos credentials cache is not active, retry */
497 if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
498 rc == LDAP_LOCAL_ERROR )
500 rc = LDAP_SERVER_DOWN;
505 #else /* HAVE_CYRUS_SASL */
506 /* Should never get here, we trapped this at config time */
508 Debug( LDAP_DEBUG_SYNC, "not compiled with SASL support\n", 0, 0, 0 );
513 } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
514 rc = ldap_sasl_bind_s( si->si_ld,
515 si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
516 &si->si_bindconf.sb_cred, NULL, NULL, NULL );
517 if ( rc != LDAP_SUCCESS ) {
518 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
519 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
524 /* Set SSF to strongest of TLS, SASL SSFs */
527 op->o_transport_ssf = 0;
529 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
530 == LDAP_SUCCESS && ssl != NULL )
532 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
534 #endif /* HAVE_TLS */
535 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
536 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
537 ? op->o_sasl_ssf : op->o_tls_ssf;
540 if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
541 /* get contextCSN shadow replica from database */
542 BerVarray csn = NULL;
544 assert( si->si_rid < 1000 );
545 op->o_req_ndn = op->o_bd->be_nsuffix[0];
546 op->o_req_dn = op->o_req_ndn;
548 /* try to read stored contextCSN */
549 backend_attribute( op, NULL, &op->o_req_ndn,
550 slap_schema.si_ad_contextCSN, &csn, ACL_READ );
552 ch_free( si->si_syncCookie.ctxcsn.bv_val );
553 ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
554 ber_bvarray_free_x( csn, op->o_tmpmemctx );
557 si->si_syncCookie.rid = si->si_rid;
559 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
560 if ( si->si_rid == sc->rid ) {
561 cmdline_cookie_found = 1;
566 if ( cmdline_cookie_found ) {
567 /* cookie is supplied in the command line */
569 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
571 /* ctxcsn wasn't parsed yet, do it now */
572 slap_parse_sync_cookie( sc, op->o_tmpmemctx );
573 if ( BER_BVISNULL( &sc->ctxcsn ) ) {
574 /* if cmdline cookie does not have ctxcsn */
575 /* component, set it to an initial value */
576 slap_init_sync_cookie_ctxcsn( sc );
578 slap_sync_cookie_free( &si->si_syncCookie, 0 );
579 slap_dup_sync_cookie( &si->si_syncCookie, sc );
580 slap_sync_cookie_free( sc, 1 );
583 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
584 &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
587 rc = ldap_sync_search( si, op->o_tmpmemctx );
589 if( rc != LDAP_SUCCESS ) {
590 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
591 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
597 ldap_unbind_ext( si->si_ld, NULL, NULL );
610 LDAPControl **rctrls = NULL;
613 BerElementBuffer berbuf;
614 BerElement *ber = (BerElement *)&berbuf;
616 LDAPMessage *res = NULL;
617 LDAPMessage *msg = NULL;
620 struct berval *retdata = NULL;
625 struct berval syncUUID = BER_BVNULL;
626 struct sync_cookie syncCookie = { BER_BVNULL };
627 struct sync_cookie syncCookie_req = { BER_BVNULL };
628 struct berval cookie = BER_BVNULL;
634 Modifications *modlist = NULL;
639 struct timeval *tout_p = NULL;
640 struct timeval tout = { 0, 0 };
642 int refreshDeletes = 0;
644 BerVarray syncUUIDs = NULL;
647 if ( slapd_shutdown ) {
652 ber_init2( ber, NULL, LBER_USE_DER );
653 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
655 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
657 psub = &si->si_be->be_nsuffix[0];
659 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
661 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
667 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
668 tout_p, &res )) > 0 )
670 if ( slapd_shutdown ) {
674 for( msg = ldap_first_message( si->si_ld, res );
676 msg = ldap_next_message( si->si_ld, msg ) )
678 if ( slapd_shutdown ) {
682 switch( ldap_msgtype( msg ) ) {
683 case LDAP_RES_SEARCH_ENTRY:
684 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
685 /* we can't work without the control */
687 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
688 "got search entry without "
689 "control\n", 0, 0, 0 );
694 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
695 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
696 /* FIXME: what if syncUUID is NULL or empty?
697 * (happens with back-sql...) */
698 if ( BER_BVISEMPTY( &syncUUID ) ) {
699 Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
700 "got empty syncUUID\n", 0, 0, 0 );
701 ldap_controls_free( rctrls );
705 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
706 ber_scanf( ber, /*"{"*/ "m}", &cookie );
707 if ( !BER_BVISNULL( &cookie ) ) {
708 ch_free( syncCookie.octet_str.bv_val );
709 ber_dupbv( &syncCookie.octet_str, &cookie );
711 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
713 slap_parse_sync_cookie( &syncCookie, NULL );
717 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
719 if (( rc = syncrepl_message_to_op( si, op, msg )) == LDAP_SUCCESS &&
720 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
721 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
723 } else if (( rc = syncrepl_message_to_entry( si, op, msg,
724 &modlist, &entry, syncstate )) == LDAP_SUCCESS ) {
725 if (( rc = syncrepl_entry( si, op, entry, &modlist,
726 syncstate, &syncUUID, &syncCookie_req,
727 &syncCookie.ctxcsn )) == LDAP_SUCCESS &&
728 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
729 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
732 ldap_controls_free( rctrls );
734 slap_mods_free( modlist, 1 );
740 case LDAP_RES_SEARCH_REFERENCE:
741 Debug( LDAP_DEBUG_ANY,
742 "do_syncrep2: reference received error\n", 0, 0, 0 );
745 case LDAP_RES_SEARCH_RESULT:
746 Debug( LDAP_DEBUG_SYNC,
747 "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
748 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
750 #ifdef LDAP_X_SYNC_REFRESH_REQUIRED
751 if ( err == LDAP_X_SYNC_REFRESH_REQUIRED ) {
752 /* map old result code to registered code */
753 err = LDAP_SYNC_REFRESH_REQUIRED;
756 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
757 if ( si->si_logstate == SYNCLOG_LOGGING ) {
758 si->si_logstate = SYNCLOG_FALLBACK;
765 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
767 ber_scanf( ber, "{" /*"}"*/);
768 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
769 ber_scanf( ber, "m", &cookie );
770 if ( !BER_BVISNULL( &cookie ) ) {
771 ch_free( syncCookie.octet_str.bv_val );
772 ber_dupbv( &syncCookie.octet_str, &cookie);
774 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
776 slap_parse_sync_cookie( &syncCookie, NULL );
779 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
781 ber_scanf( ber, "b", &refreshDeletes );
783 ber_scanf( ber, /*"{"*/ "}" );
785 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
787 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
790 value_match( &match, slap_schema.si_ad_entryCSN,
791 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
792 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
793 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
796 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
797 match < 0 && err == LDAP_SUCCESS )
799 rc = syncrepl_updateCookie( si, op, psub, &syncCookie );
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 );
814 avl_free( si->si_presentlist, avl_ber_bvfree );
815 si->si_presentlist = NULL;
818 if ( err == LDAP_SUCCESS
819 && si->si_logstate == SYNCLOG_FALLBACK ) {
820 si->si_logstate = SYNCLOG_LOGGING;
821 rc = LDAP_SYNC_REFRESH_REQUIRED;
828 case LDAP_RES_INTERMEDIATE:
829 rc = ldap_parse_intermediate( si->si_ld, msg,
830 &retoid, &retdata, NULL, 0 );
831 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
832 ber_init2( ber, retdata, LBER_USE_DER );
834 switch ( si_tag = ber_peek_tag( ber, &len )) {
836 case LDAP_TAG_SYNC_NEW_COOKIE:
837 Debug( LDAP_DEBUG_SYNC,
838 "do_syncrep2: %s - %s%s\n",
839 "LDAP_RES_INTERMEDIATE",
840 "NEW_COOKIE", "\n" );
841 ber_scanf( ber, "tm", &tag, &cookie );
843 case LDAP_TAG_SYNC_REFRESH_DELETE:
844 case LDAP_TAG_SYNC_REFRESH_PRESENT:
845 Debug( LDAP_DEBUG_SYNC,
846 "do_syncrep2: %s - %s%s\n",
847 "LDAP_RES_INTERMEDIATE",
848 si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
849 "REFRESH_PRESENT" : "REFRESH_DELETE",
851 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
852 si->si_refreshDelete = 1;
854 si->si_refreshPresent = 1;
856 ber_scanf( ber, "t{" /*"}"*/, &tag );
857 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
859 ber_scanf( ber, "m", &cookie );
860 if ( !BER_BVISNULL( &cookie ) ) {
861 ch_free( syncCookie.octet_str.bv_val );
862 ber_dupbv( &syncCookie.octet_str, &cookie );
864 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
866 slap_parse_sync_cookie( &syncCookie, NULL );
869 if ( ber_peek_tag( ber, &len ) ==
870 LDAP_TAG_REFRESHDONE )
872 ber_scanf( ber, "b", &refreshDone );
874 ber_scanf( ber, /*"{"*/ "}" );
876 case LDAP_TAG_SYNC_ID_SET:
877 Debug( LDAP_DEBUG_SYNC,
878 "do_syncrep2: %s - %s%s\n",
879 "LDAP_RES_INTERMEDIATE",
882 ber_scanf( ber, "t{" /*"}"*/, &tag );
883 if ( ber_peek_tag( ber, &len ) ==
884 LDAP_TAG_SYNC_COOKIE )
886 ber_scanf( ber, "m", &cookie );
887 if ( !BER_BVISNULL( &cookie ) ) {
888 ch_free( syncCookie.octet_str.bv_val );
889 ber_dupbv( &syncCookie.octet_str, &cookie );
891 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
893 slap_parse_sync_cookie( &syncCookie, NULL );
896 if ( ber_peek_tag( ber, &len ) ==
897 LDAP_TAG_REFRESHDELETES )
899 ber_scanf( ber, "b", &refreshDeletes );
901 ber_scanf( ber, "[W]", &syncUUIDs );
902 ber_scanf( ber, /*"{"*/ "}" );
903 if ( refreshDeletes ) {
904 syncrepl_del_nonpresent( op, si, syncUUIDs );
905 ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
907 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
908 struct berval *syncuuid_bv;
909 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
910 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
911 avl_insert( &si->si_presentlist,
912 (caddr_t) syncuuid_bv,
913 syncuuid_cmp, avl_dup_error );
915 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
919 Debug( LDAP_DEBUG_ANY,
920 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
921 (long) si_tag, 0, 0 );
922 ldap_memfree( retoid );
923 ber_bvfree( retdata );
927 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
929 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
932 value_match( &match, slap_schema.si_ad_entryCSN,
933 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
934 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
935 &syncCookie_req.ctxcsn,
936 &syncCookie.ctxcsn, &text );
939 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
942 rc = syncrepl_updateCookie( si, op, psub, &syncCookie);
945 if ( si->si_refreshPresent == 1 ) {
947 syncrepl_del_nonpresent( op, si, NULL );
951 ldap_memfree( retoid );
952 ber_bvfree( retdata );
956 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
957 "unknown intermediate response (%d)\n",
959 ldap_memfree( retoid );
960 ber_bvfree( retdata );
966 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
967 "unknown message\n", 0, 0, 0 );
971 if ( !BER_BVISNULL( &syncCookie.octet_str )) {
972 slap_sync_cookie_free( &syncCookie_req, 0 );
973 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
974 slap_sync_cookie_free( &syncCookie, 0 );
984 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
985 errstr = ldap_err2string( rc );
987 Debug( LDAP_DEBUG_ANY,
988 "do_syncrep2 : %s\n", errstr, 0, 0 );
992 slap_sync_cookie_free( &syncCookie, 0 );
993 slap_sync_cookie_free( &syncCookie_req, 0 );
995 if ( res ) ldap_msgfree( res );
997 if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
998 if ( si->si_conn_setup ) {
1000 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1001 connection_client_stop( s );
1002 si->si_conn_setup = 0;
1004 ldap_unbind_ext( si->si_ld, NULL, NULL );
1016 struct re_s* rtask = arg;
1017 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
1018 Connection conn = {0};
1019 OperationBuffer opbuf;
1021 int rc = LDAP_SUCCESS;
1027 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
1032 ldap_pvt_thread_mutex_lock( &si->si_mutex );
1034 switch( abs( si->si_type )) {
1035 case LDAP_SYNC_REFRESH_ONLY:
1036 case LDAP_SYNC_REFRESH_AND_PERSIST:
1039 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1043 if ( slapd_shutdown ) {
1045 if ( si->si_conn_setup ) {
1046 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1047 connection_client_stop( s );
1048 si->si_conn_setup = 0;
1050 ldap_unbind_ext( si->si_ld, NULL, NULL );
1053 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1057 op = (Operation *) &opbuf;
1058 connection_fake_init( &conn, op, ctx );
1060 /* use global malloc for now */
1061 op->o_tmpmemctx = NULL;
1062 op->o_tmpmfuncs = &ch_mfuncs;
1064 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1065 op->o_bd = be = si->si_be;
1066 op->o_dn = op->o_bd->be_rootdn;
1067 op->o_ndn = op->o_bd->be_rootndn;
1069 /* Establish session, do search */
1071 si->si_refreshDelete = 0;
1072 si->si_refreshPresent = 0;
1073 rc = do_syncrep1( op, si );
1077 /* Process results */
1078 if ( rc == LDAP_SUCCESS ) {
1079 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1081 rc = do_syncrep2( op, si );
1082 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) {
1083 rc = ldap_sync_search( si, op->o_tmpmemctx );
1087 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1088 /* If we succeeded, enable the connection for further listening.
1089 * If we failed, tear down the connection and reschedule.
1091 if ( rc == LDAP_SUCCESS ) {
1092 if ( si->si_conn_setup ) {
1093 connection_client_enable( s );
1095 rc = connection_client_setup( s, do_syncrepl, arg );
1097 si->si_conn_setup = 1;
1099 } else if ( si->si_conn_setup ) {
1103 if ( rc == -2 ) rc = 0;
1107 /* At this point, we have 4 cases:
1108 * 1) for any hard failure, give up and remove this task
1109 * 2) for ServerDown, reschedule this task to run
1110 * 3) for Refresh and Success, reschedule to run
1111 * 4) for Persist and Success, reschedule to defer
1113 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1115 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1116 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1120 connection_client_stop( s );
1123 if ( rc == LDAP_SUCCESS ) {
1124 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1127 rtask->interval.tv_sec = si->si_interval;
1128 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1129 if ( si->si_retrynum ) {
1130 for ( i = 0; si->si_retrynum_init[i] != RETRYNUM_TAIL; i++ ) {
1131 si->si_retrynum[i] = si->si_retrynum_init[i];
1133 si->si_retrynum[i] = RETRYNUM_TAIL;
1136 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1137 if ( si->si_retrynum[i] == RETRYNUM_FOREVER || si->si_retrynum[i] == RETRYNUM_TAIL )
1141 if ( !si->si_retrynum || si->si_retrynum[i] == RETRYNUM_TAIL ) {
1142 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1143 } else if ( RETRYNUM_VALID( si->si_retrynum[i] ) ) {
1144 if ( si->si_retrynum[i] > 0 )
1145 si->si_retrynum[i]--;
1146 rtask->interval.tv_sec = si->si_retryinterval[i];
1147 ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1148 slap_wake_listener();
1152 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1153 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1158 static slap_verbmasks modops[] = {
1159 { BER_BVC("add"), LDAP_REQ_ADD },
1160 { BER_BVC("delete"), LDAP_REQ_DELETE },
1161 { BER_BVC("modify"), LDAP_REQ_MODIFY },
1162 { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1166 static Modifications *
1167 syncrepl_accesslog_mods(
1174 AttributeDescription *ad;
1175 struct berval bv, bv2;
1177 Modifications *mod = NULL, *modlist = NULL, **modtail;
1182 for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1186 colon = ber_bvchr( &bv, ':' );
1188 continue; /* invalid */
1189 bv.bv_len = colon - bv.bv_val;
1190 if ( slap_bv2ad( &bv, &ad, &text )) {
1194 /* Ignore dynamically generated attrs */
1195 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1197 /* Ignore excluded attrs */
1198 if ( ldap_charray_inlist( si->si_exattrs,
1199 ad->ad_type->sat_cname.bv_val ))
1203 case '+': op = LDAP_MOD_ADD; break;
1204 case '-': op = LDAP_MOD_DELETE; break;
1205 case '=': op = LDAP_MOD_REPLACE; break;
1206 case '#': op = LDAP_MOD_INCREMENT; break;
1210 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1211 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1214 mod->sml_next = NULL;
1216 mod->sml_type = ad->ad_cname;
1217 mod->sml_values = NULL;
1218 mod->sml_nvalues = NULL;
1221 modtail = &mod->sml_next;
1223 if ( colon[2] == ' ' ) {
1224 bv.bv_val = colon + 3;
1225 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1226 ber_dupbv( &bv2, &bv );
1227 ber_bvarray_add( &mod->sml_values, &bv2 );
1233 static Modifications *
1234 syncrepl_changelog_mods(
1239 return NULL; /* FIXME */
1243 syncrepl_message_to_op(
1249 BerElement *ber = NULL;
1250 Modifications *modlist = NULL;
1252 SlapReply rs = { REP_RESULT };
1253 slap_callback cb = { NULL, null_callback, NULL, NULL };
1256 char txtbuf[SLAP_TEXT_BUFLEN];
1257 size_t textlen = sizeof txtbuf;
1259 struct berval bdn, dn = BER_BVNULL, ndn;
1260 struct berval bv, *bvals = NULL;
1261 struct berval rdn = BER_BVNULL, sup = BER_BVNULL,
1262 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1263 psup = BER_BVNULL, nsup = BER_BVNULL;
1264 int rc, deleteOldRdn = 0;
1266 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1267 Debug( LDAP_DEBUG_ANY,
1268 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1272 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1277 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1279 if ( rc != LDAP_SUCCESS ) {
1280 Debug( LDAP_DEBUG_ANY,
1281 "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1285 op->o_tag = LBER_DEFAULT;
1287 while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1289 if ( bv.bv_val == NULL )
1292 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1294 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1295 ber_dupbv( &op->o_req_dn, &dn );
1296 ber_dupbv( &op->o_req_ndn, &ndn );
1297 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1298 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1299 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1300 int i = verb_to_mask( bvals[0].bv_val, modops );
1302 Debug( LDAP_DEBUG_ANY,
1303 "syncrepl_message_to_op : unknown op %s",
1304 bvals[0].bv_val, 0, 0 );
1309 op->o_tag = modops[i].mask;
1310 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1311 /* Parse attribute into modlist */
1312 if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1313 modlist = syncrepl_accesslog_mods( si, bvals );
1315 modlist = syncrepl_changelog_mods( si, bvals );
1316 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1318 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1319 if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1321 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1323 } else if ( !ber_bvstrcasecmp( &bv,
1324 &slap_schema.si_ad_entryCSN->ad_cname )) {
1325 slap_queue_csn( op, bvals );
1330 /* If we didn't get a mod type or a target DN, bail out */
1331 if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1336 op->o_callback = &cb;
1338 switch( op->o_tag ) {
1340 case LDAP_REQ_MODIFY:
1341 /* If we didn't get required data, bail */
1342 if ( !modlist ) goto done;
1344 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1346 if ( rc != LDAP_SUCCESS ) {
1347 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1352 if ( op->o_tag == LDAP_REQ_ADD ) {
1353 op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1354 op->ora_e->e_name = op->o_req_dn;
1355 op->ora_e->e_nname = op->o_req_ndn;
1356 rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1357 if( rc != LDAP_SUCCESS ) {
1358 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1361 rc = op->o_bd->be_add( op, &rs );
1362 Debug( LDAP_DEBUG_SYNC,
1363 "syncrepl_message_to_op: be_add %s (%d)\n",
1364 op->o_req_dn.bv_val, rc, 0 );
1366 be_entry_release_w( op, op->ora_e );
1368 op->orm_modlist = modlist;
1369 rc = op->o_bd->be_modify( op, &rs );
1370 Debug( LDAP_DEBUG_SYNC,
1371 "syncrepl_message_to_op: be_modify %s (%d)\n",
1372 op->o_req_dn.bv_val, rc, 0 );
1375 case LDAP_REQ_MODRDN:
1376 if ( BER_BVISNULL( &rdn )) goto done;
1378 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1380 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1382 if ( !BER_BVISNULL( &sup )) {
1383 if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1385 op->orr_newSup = &psup;
1386 op->orr_nnewSup = ⊅
1388 op->orr_newrdn = prdn;
1389 op->orr_nnewrdn = nrdn;
1390 op->orr_deleteoldrdn = deleteOldRdn;
1391 op->orr_modlist = NULL;
1392 if ( slap_modrdn2mods( op, &rs ))
1394 rc = op->o_bd->be_modrdn( op, &rs );
1395 slap_mods_free( op->orr_modlist, 1 );
1396 Debug( LDAP_DEBUG_SYNC,
1397 "syncrepl_message_to_op: be_modrdn %s (%d)\n",
1398 op->o_req_dn.bv_val, rc, 0 );
1400 case LDAP_REQ_DELETE:
1401 rc = op->o_bd->be_delete( op, &rs );
1402 Debug( LDAP_DEBUG_SYNC,
1403 "syncrepl_message_to_op: be_delete %s (%d)\n",
1404 op->o_req_dn.bv_val, rc, 0 );
1408 slap_graduate_commit_csn( op );
1410 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1411 if ( !BER_BVISNULL( &rdn )) {
1412 if ( !BER_BVISNULL( &nsup ))
1413 ch_free( nsup.bv_val );
1414 if ( !BER_BVISNULL( &psup ))
1415 ch_free( psup.bv_val );
1416 if ( !BER_BVISNULL( &nrdn ))
1417 ch_free( nrdn.bv_val );
1418 if ( !BER_BVISNULL( &prdn ))
1419 ch_free( prdn.bv_val );
1421 ber_free ( ber, 0 );
1426 syncrepl_message_to_entry(
1430 Modifications **modlist,
1436 BerElement *ber = NULL;
1439 Modifications **modtail = modlist;
1442 char txtbuf[SLAP_TEXT_BUFLEN];
1443 size_t textlen = sizeof txtbuf;
1445 struct berval bdn = {0, NULL}, dn, ndn;
1450 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1451 Debug( LDAP_DEBUG_ANY,
1452 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1456 op->o_tag = LDAP_REQ_ADD;
1458 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1460 if ( rc != LDAP_SUCCESS ) {
1461 Debug( LDAP_DEBUG_ANY,
1462 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1466 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1467 ber_dupbv( &op->o_req_dn, &dn );
1468 ber_dupbv( &op->o_req_ndn, &ndn );
1469 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1470 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1472 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1475 return LDAP_SUCCESS;
1478 if ( entry == NULL ) {
1482 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1483 e->e_name = op->o_req_dn;
1484 e->e_nname = op->o_req_ndn;
1486 while ( ber_remaining( ber ) ) {
1487 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1488 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1493 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1495 mod->sml_op = LDAP_MOD_REPLACE;
1497 mod->sml_next = NULL;
1498 mod->sml_desc = NULL;
1499 mod->sml_type = tmp.sml_type;
1500 mod->sml_values = tmp.sml_values;
1501 mod->sml_nvalues = NULL;
1504 modtail = &mod->sml_next;
1507 if ( *modlist == NULL ) {
1508 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1514 rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1516 if ( rc != LDAP_SUCCESS ) {
1517 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1522 /* Strip out dynamically generated attrs */
1523 for ( modtail = modlist; *modtail ; ) {
1525 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1526 *modtail = mod->sml_next;
1527 slap_mod_free( &mod->sml_mod, 0 );
1530 modtail = &mod->sml_next;
1534 /* Strip out attrs in exattrs list */
1535 for ( modtail = modlist; *modtail ; ) {
1537 if ( ldap_charray_inlist( si->si_exattrs,
1538 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1539 *modtail = mod->sml_next;
1540 slap_mod_free( &mod->sml_mod, 0 );
1543 modtail = &mod->sml_next;
1547 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1548 if( rc != LDAP_SUCCESS ) {
1549 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1554 ber_free ( ber, 0 );
1555 if ( rc != LDAP_SUCCESS ) {
1566 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1568 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1569 * entry if a previous refresh was interrupted before sending us a new
1570 * context state. We try to compare the new entry to the existing entry
1571 * and ignore the new entry if they are the same.
1573 * Also, we may get an update where the entryDN has changed, due to
1574 * a ModDn on the provider. We detect this as well, so we can issue
1575 * the corresponding operation locally.
1577 * In the case of a modify, we get a list of all the attributes
1578 * in the original entry. Rather than deleting the entry and re-adding it,
1579 * we issue a Modify request that deletes all the attributes and adds all
1580 * the new ones. This avoids the issue of trying to delete/add a non-leaf
1583 * We don't try to otherwise distinguish ModDN from Modify; in the case of
1584 * a ModDN we will issue both operations on the local database.
1586 typedef struct dninfo {
1590 int renamed; /* Was an existing entry renamed? */
1591 int wasChanged; /* are the attributes changed? */
1592 int attrs; /* how many attribute types are in the ads list */
1593 AttributeDescription **ads;
1601 Modifications** modlist,
1603 struct berval* syncUUID,
1604 struct sync_cookie* syncCookie_req,
1605 struct berval* syncCSN )
1607 Backend *be = op->o_bd;
1608 slap_callback cb = { NULL, NULL, NULL, NULL };
1609 struct berval *syncuuid_bv = NULL;
1610 struct berval syncUUID_strrep = BER_BVNULL;
1611 struct berval uuid_bv = BER_BVNULL;
1613 SlapReply rs_search = {REP_RESULT};
1614 SlapReply rs_delete = {REP_RESULT};
1615 SlapReply rs_add = {REP_RESULT};
1616 SlapReply rs_modify = {REP_RESULT};
1618 #ifdef LDAP_COMP_MATCH
1619 AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1621 AttributeAssertion ava = { NULL, BER_BVNULL };
1623 int rc = LDAP_SUCCESS;
1625 struct berval pdn = BER_BVNULL;
1629 switch( syncstate ) {
1630 case LDAP_SYNC_PRESENT:
1631 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1633 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1636 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1638 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1640 case LDAP_SYNC_DELETE:
1641 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1643 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1645 case LDAP_SYNC_MODIFY:
1646 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1648 "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1651 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1653 "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1656 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1657 if ( !si->si_refreshPresent ) {
1658 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1659 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1660 syncuuid_cmp, avl_dup_error );
1664 if ( syncstate == LDAP_SYNC_PRESENT ) {
1666 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1667 if ( entry == NULL ) {
1672 f.f_choice = LDAP_FILTER_EQUALITY;
1674 ava.aa_desc = slap_schema.si_ad_entryUUID;
1675 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1676 ava.aa_value = *syncUUID;
1677 op->ors_filter = &f;
1679 op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID_strrep.bv_len;
1680 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1681 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1682 AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1683 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1684 syncUUID_strrep.bv_val, syncUUID_strrep.bv_len );
1685 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1686 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1688 op->o_tag = LDAP_REQ_SEARCH;
1689 op->ors_scope = LDAP_SCOPE_SUBTREE;
1690 op->ors_deref = LDAP_DEREF_NEVER;
1692 /* get the entry for this UUID */
1693 op->o_req_dn = si->si_base;
1694 op->o_req_ndn = si->si_base;
1696 op->o_time = slap_get_time();
1697 op->ors_tlimit = SLAP_NO_LIMIT;
1700 op->ors_attrs = slap_anlist_all_attributes;
1701 op->ors_attrsonly = 0;
1703 /* set callback function */
1704 op->o_callback = &cb;
1705 cb.sc_response = dn_callback;
1706 cb.sc_private = &dni;
1707 dni.new_entry = entry;
1709 if ( limits_check( op, &rs_search ) == 0 ) {
1710 rc = be->be_search( op, &rs_search );
1711 Debug( LDAP_DEBUG_SYNC,
1712 "syncrepl_entry: %s (%d)\n",
1713 "be_search", rc, 0 );
1716 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1717 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1720 cb.sc_response = null_callback;
1723 if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1724 Debug( LDAP_DEBUG_SYNC,
1725 "syncrepl_entry: %s\n",
1726 entry->e_name.bv_val, 0, 0 );
1728 Debug( LDAP_DEBUG_SYNC,
1729 "syncrepl_entry: %s\n",
1730 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1733 if ( syncstate != LDAP_SYNC_DELETE ) {
1734 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1737 /* add if missing */
1738 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1739 &syncUUID_strrep, syncUUID );
1741 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1742 /* replace only if necessary */
1743 if ( a->a_nvals != a->a_vals ) {
1744 ber_memfree( a->a_nvals[0].bv_val );
1745 ber_dupbv( &a->a_nvals[0], syncUUID );
1747 ber_memfree( a->a_vals[0].bv_val );
1748 ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1752 switch ( syncstate ) {
1754 case LDAP_SYNC_MODIFY:
1756 Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1758 /* FIXME: op->o_csn is assumed to be
1759 * on the thread's slab; this needs
1760 * to be cleared ASAP.
1761 * What happens if already present?
1763 assert( BER_BVISNULL( &op->o_csn ) );
1764 op->o_csn = a->a_vals[0];
1768 if ( BER_BVISNULL( &dni.dn )) {
1770 op->o_req_dn = entry->e_name;
1771 op->o_req_ndn = entry->e_nname;
1772 op->o_tag = LDAP_REQ_ADD;
1775 rc = be->be_add( op, &rs_add );
1776 Debug( LDAP_DEBUG_SYNC,
1777 "syncrepl_entry: %s (%d)\n",
1779 switch ( rs_add.sr_err ) {
1781 be_entry_release_w( op, entry );
1786 /* we assume that LDAP_NO_SUCH_OBJECT is returned
1787 * only if the suffix entry is not present */
1788 case LDAP_NO_SUCH_OBJECT:
1789 rc = syncrepl_add_glue( op, entry );
1793 /* if an entry was added via syncrepl_add_glue(),
1794 * it likely has no entryUUID, so the previous
1795 * be_search() doesn't find it. In this case,
1796 * give syncrepl a chance to modify it. Also
1797 * allow for entries that were recreated with the
1798 * same DN but a different entryUUID.
1800 case LDAP_ALREADY_EXISTS:
1802 Operation op2 = *op;
1803 SlapReply rs2 = { 0 };
1804 slap_callback cb2 = { 0 };
1806 op2.o_tag = LDAP_REQ_SEARCH;
1807 op2.o_req_dn = entry->e_name;
1808 op2.o_req_ndn = entry->e_nname;
1809 op2.ors_scope = LDAP_SCOPE_BASE;
1810 op2.ors_deref = LDAP_DEREF_NEVER;
1811 op2.ors_attrs = slap_anlist_all_attributes;
1812 op2.ors_attrsonly = 0;
1813 op2.ors_limit = NULL;
1815 op2.ors_tlimit = SLAP_NO_LIMIT;
1817 f.f_choice = LDAP_FILTER_PRESENT;
1818 f.f_desc = slap_schema.si_ad_objectClass;
1819 op2.ors_filter = &f;
1820 op2.ors_filterstr = generic_filterstr;
1822 op2.o_callback = &cb2;
1823 cb2.sc_response = dn_callback;
1824 cb2.sc_private = &dni;
1826 rc = be->be_search( &op2, &rs2 );
1827 if ( rc ) goto done;
1835 Debug( LDAP_DEBUG_ANY,
1836 "syncrepl_entry : be_add failed (%d)\n",
1837 rs_add.sr_err, 0, 0 );
1843 op->o_req_dn = dni.dn;
1844 op->o_req_ndn = dni.ndn;
1845 if ( dni.renamed ) {
1846 struct berval noldp, newp, nnewp;
1848 op->o_tag = LDAP_REQ_MODRDN;
1849 dnRdn( &entry->e_name, &op->orr_newrdn );
1850 dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1852 dnParent( &dni.ndn, &noldp );
1853 dnParent( &entry->e_nname, &nnewp );
1854 if ( !dn_match( &noldp, &nnewp )) {
1855 dnParent( &entry->e_name, &newp );
1856 op->orr_newSup = &newp;
1857 op->orr_nnewSup = &nnewp;
1859 op->orr_deleteoldrdn = 0;
1860 op->orr_modlist = NULL;
1861 if (( rc = slap_modrdn2mods( op, &rs_modify ))) {
1864 rc = be->be_modrdn( op, &rs_modify );
1865 slap_mods_free( op->orr_modlist, 1 );
1866 Debug( LDAP_DEBUG_SYNC,
1867 "syncrepl_entry: %s (%d)\n",
1868 "be_modrdn", rc, 0 );
1869 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1870 op->o_req_dn = entry->e_name;
1871 op->o_req_ndn = entry->e_nname;
1876 if ( dni.wasChanged ) {
1877 Modifications *mod, *modhead = NULL;
1878 Modifications *modtail = NULL;
1881 op->o_tag = LDAP_REQ_MODIFY;
1883 assert( *modlist != NULL );
1885 /* Delete all the old attrs */
1886 for ( i = 0; i < dni.attrs; i++ ) {
1887 mod = ch_malloc( sizeof( Modifications ) );
1888 mod->sml_op = LDAP_MOD_DELETE;
1890 mod->sml_desc = dni.ads[i];
1891 mod->sml_type = mod->sml_desc->ad_cname;
1892 mod->sml_values = NULL;
1893 mod->sml_nvalues = NULL;
1894 if ( !modhead ) modhead = mod;
1896 modtail->sml_next = mod;
1901 /* Append passed in list to ours */
1903 modtail->sml_next = *modlist;
1909 /* Find end of this list */
1910 for ( ; mod != NULL; mod = mod->sml_next ) {
1914 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1915 mod->sml_op = LDAP_MOD_REPLACE;
1917 mod->sml_desc = slap_schema.si_ad_entryUUID;
1918 mod->sml_type = mod->sml_desc->ad_cname;
1919 ber_dupbv( &uuid_bv, &syncUUID_strrep );
1920 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1921 ber_dupbv( &uuid_bv, syncUUID );
1922 ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1923 modtail->sml_next = mod;
1925 op->o_tag = LDAP_REQ_MODIFY;
1926 op->orm_modlist = *modlist;
1928 rc = be->be_modify( op, &rs_modify );
1929 Debug( LDAP_DEBUG_SYNC,
1930 "syncrepl_entry: %s (%d)\n",
1931 "be_modify", rc, 0 );
1932 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1933 Debug( LDAP_DEBUG_ANY,
1934 "syncrepl_entry : be_modify failed (%d)\n",
1935 rs_modify.sr_err, 0, 0 );
1939 case LDAP_SYNC_DELETE :
1940 if ( !BER_BVISNULL( &dni.dn )) {
1941 op->o_req_dn = dni.dn;
1942 op->o_req_ndn = dni.ndn;
1943 op->o_tag = LDAP_REQ_DELETE;
1944 rc = be->be_delete( op, &rs_delete );
1945 Debug( LDAP_DEBUG_SYNC,
1946 "syncrepl_entry: %s (%d)\n",
1947 "be_delete", rc, 0 );
1949 while ( rs_delete.sr_err == LDAP_SUCCESS
1950 && op->o_delete_glue_parent ) {
1951 op->o_delete_glue_parent = 0;
1952 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1953 slap_callback cb = { NULL };
1954 cb.sc_response = slap_null_cb;
1955 dnParent( &op->o_req_ndn, &pdn );
1957 op->o_req_ndn = pdn;
1958 op->o_callback = &cb;
1959 op->o_bd->be_delete( op, &rs_delete );
1968 Debug( LDAP_DEBUG_ANY,
1969 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1974 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1975 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1976 BER_BVZERO( &syncUUID_strrep );
1979 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1981 if ( !BER_BVISNULL( &dni.ndn ) ) {
1982 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1984 if ( !BER_BVISNULL( &dni.dn ) ) {
1985 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1988 entry_free( entry );
1989 BER_BVZERO( &op->o_csn );
1993 static struct berval gcbva[] = {
1999 #define NP_DELETE_ONE 2
2002 syncrepl_del_nonpresent(
2007 Backend* be = op->o_bd;
2008 slap_callback cb = { NULL };
2009 SlapReply rs_search = {REP_RESULT};
2010 SlapReply rs_delete = {REP_RESULT};
2011 SlapReply rs_modify = {REP_RESULT};
2012 struct nonpresent_entry *np_list, *np_prev;
2014 AttributeName an[2];
2016 struct berval pdn = BER_BVNULL;
2018 op->o_req_dn = si->si_base;
2019 op->o_req_ndn = si->si_base;
2021 cb.sc_response = nonpresent_callback;
2024 op->o_callback = &cb;
2025 op->o_tag = LDAP_REQ_SEARCH;
2026 op->ors_scope = si->si_scope;
2027 op->ors_deref = LDAP_DEREF_NEVER;
2028 op->o_time = slap_get_time();
2029 op->ors_tlimit = SLAP_NO_LIMIT;
2034 #ifdef LDAP_COMP_MATCH
2035 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
2037 AttributeAssertion eq = { NULL, BER_BVNULL };
2041 op->ors_attrsonly = 1;
2042 op->ors_attrs = slap_anlist_no_attrs;
2043 op->ors_limit = NULL;
2044 op->ors_filter = &uf;
2047 uf.f_av_desc = slap_schema.si_ad_entryUUID;
2049 uf.f_choice = LDAP_FILTER_EQUALITY;
2050 si->si_refreshDelete |= NP_DELETE_ONE;
2052 for (i=0; uuids[i].bv_val; i++) {
2054 slap_uuidstr_from_normalized( &uf.f_av_value, &uuids[i],
2056 filter2bv_x( op, op->ors_filter, &op->ors_filterstr );
2057 uf.f_av_value = uuids[i];
2058 rc = be->be_search( op, &rs_search );
2059 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
2061 si->si_refreshDelete ^= NP_DELETE_ONE;
2063 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
2064 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
2065 an[0].an_desc = slap_schema.si_ad_entryUUID;
2067 op->ors_slimit = SLAP_NO_LIMIT;
2068 op->ors_attrsonly = 0;
2069 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2070 op->ors_filterstr = si->si_filterstr;
2071 op->o_nocaching = 1;
2073 if ( limits_check( op, &rs_search ) == 0 ) {
2074 rc = be->be_search( op, &rs_search );
2076 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2079 op->o_nocaching = 0;
2081 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2083 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2085 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2086 while ( np_list != NULL ) {
2087 LDAP_LIST_REMOVE( np_list, npe_link );
2089 np_list = LDAP_LIST_NEXT( np_list, npe_link );
2090 op->o_tag = LDAP_REQ_DELETE;
2091 op->o_callback = &cb;
2092 cb.sc_response = null_callback;
2094 op->o_req_dn = *np_prev->npe_name;
2095 op->o_req_ndn = *np_prev->npe_nname;
2096 rc = op->o_bd->be_delete( op, &rs_delete );
2098 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2099 Modifications mod1, mod2;
2100 mod1.sml_op = LDAP_MOD_REPLACE;
2102 mod1.sml_desc = slap_schema.si_ad_objectClass;
2103 mod1.sml_type = mod1.sml_desc->ad_cname;
2104 mod1.sml_values = &gcbva[0];
2105 mod1.sml_nvalues = NULL;
2106 mod1.sml_next = &mod2;
2108 mod2.sml_op = LDAP_MOD_REPLACE;
2110 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2111 mod2.sml_type = mod2.sml_desc->ad_cname;
2112 mod2.sml_values = &gcbva[1];
2113 mod2.sml_nvalues = NULL;
2114 mod2.sml_next = NULL;
2116 op->o_tag = LDAP_REQ_MODIFY;
2117 op->orm_modlist = &mod1;
2119 rc = be->be_modify( op, &rs_modify );
2122 while ( rs_delete.sr_err == LDAP_SUCCESS &&
2123 op->o_delete_glue_parent ) {
2124 op->o_delete_glue_parent = 0;
2125 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2126 slap_callback cb = { NULL };
2127 cb.sc_response = slap_null_cb;
2128 dnParent( &op->o_req_ndn, &pdn );
2130 op->o_req_ndn = pdn;
2131 op->o_callback = &cb;
2132 /* give it a root privil ? */
2133 op->o_bd->be_delete( op, &rs_delete );
2139 op->o_delete_glue_parent = 0;
2141 ber_bvfree( np_prev->npe_name );
2142 ber_bvfree( np_prev->npe_nname );
2146 slap_graduate_commit_csn( op );
2148 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2149 BER_BVZERO( &op->o_csn );
2160 Backend *be = op->o_bd;
2161 slap_callback cb = { NULL };
2166 struct berval dn = BER_BVNULL;
2167 struct berval ndn = BER_BVNULL;
2169 SlapReply rs_add = {REP_RESULT};
2170 struct berval ptr, nptr;
2173 op->o_tag = LDAP_REQ_ADD;
2174 op->o_callback = &cb;
2175 cb.sc_response = null_callback;
2176 cb.sc_private = NULL;
2181 /* count RDNs in suffix */
2182 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2183 for ( i = 0, ptr = be->be_nsuffix[0], comma = ptr.bv_val; comma != NULL; comma = ber_bvchr( &ptr, ',' ) ) {
2185 ptr.bv_len -= comma - ptr.bv_val;
2195 /* Start with BE suffix */
2197 for ( i = 0; i < suffrdns; i++ ) {
2198 comma = ber_bvrchr( &ptr, ',' );
2199 if ( comma != NULL ) {
2200 ptr.bv_len = comma - ptr.bv_val;
2207 if ( !BER_BVISEMPTY( &ptr ) ) {
2208 dn.bv_len -= ptr.bv_len + 1;
2209 dn.bv_val += ptr.bv_len + 1;
2212 /* the normalizedDNs are always the same length, no counting
2216 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2217 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2218 ndn.bv_len = be->be_nsuffix[0].bv_len;
2220 nptr.bv_len = ndn.bv_val - nptr.bv_val - 1;
2226 while ( ndn.bv_val > e->e_nname.bv_val ) {
2227 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2228 ber_dupbv( &glue->e_name, &dn );
2229 ber_dupbv( &glue->e_nname, &ndn );
2231 a = ch_calloc( 1, sizeof( Attribute ));
2232 a->a_desc = slap_schema.si_ad_objectClass;
2234 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2235 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2236 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2237 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2239 a->a_nvals = a->a_vals;
2241 a->a_next = glue->e_attrs;
2244 a = ch_calloc( 1, sizeof( Attribute ));
2245 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2247 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2248 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2249 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2251 a->a_nvals = a->a_vals;
2253 a->a_next = glue->e_attrs;
2256 op->o_req_dn = glue->e_name;
2257 op->o_req_ndn = glue->e_nname;
2259 rc = be->be_add ( op, &rs_add );
2260 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2261 be_entry_release_w( op, glue );
2263 /* incl. ALREADY EXIST */
2265 if ( rs_add.sr_err != LDAP_ALREADY_EXISTS ) {
2271 /* Move to next child */
2272 comma = ber_bvrchr( &ptr, ',' );
2273 if ( comma == NULL ) {
2276 ptr.bv_len = comma - ptr.bv_val;
2278 dn.bv_val = ++comma;
2279 dn.bv_len = e->e_name.bv_len - (dn.bv_val - e->e_name.bv_val);
2281 comma = ber_bvrchr( &nptr, ',' );
2282 assert( comma != NULL );
2283 nptr.bv_len = comma - nptr.bv_val;
2285 ndn.bv_val = ++comma;
2286 ndn.bv_len = e->e_nname.bv_len - (ndn.bv_val - e->e_nname.bv_val);
2289 op->o_req_dn = e->e_name;
2290 op->o_req_ndn = e->e_nname;
2292 rc = be->be_add ( op, &rs_add );
2293 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2294 be_entry_release_w( op, e );
2303 syncrepl_updateCookie(
2307 struct sync_cookie *syncCookie )
2309 Backend *be = op->o_bd;
2310 Modifications mod = { { 0 } };
2311 struct berval vals[ 2 ];
2315 slap_callback cb = { NULL };
2316 SlapReply rs_modify = {REP_RESULT};
2318 mod.sml_op = LDAP_MOD_REPLACE;
2319 mod.sml_desc = slap_schema.si_ad_contextCSN;
2320 mod.sml_type = mod.sml_desc->ad_cname;
2321 mod.sml_values = vals;
2322 vals[0] = syncCookie->ctxcsn;
2323 BER_BVZERO( &vals[1] );
2325 slap_queue_csn( op, &syncCookie->ctxcsn );
2327 op->o_tag = LDAP_REQ_MODIFY;
2329 assert( si->si_rid < 1000 );
2331 cb.sc_response = null_callback;
2334 op->o_callback = &cb;
2335 op->o_req_dn = op->o_bd->be_suffix[0];
2336 op->o_req_ndn = op->o_bd->be_nsuffix[0];
2338 /* update contextCSN */
2339 op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2340 op->orm_modlist = &mod;
2341 rc = be->be_modify( op, &rs_modify );
2344 if ( rs_modify.sr_err == LDAP_SUCCESS ) {
2345 slap_sync_cookie_free( &si->si_syncCookie, 0 );
2346 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2348 Debug( LDAP_DEBUG_ANY,
2349 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2352 slap_graduate_commit_csn( op );
2354 op->o_tmpfree( op->o_csn.bv_val, op->o_tmpmemctx );
2355 BER_BVZERO( &op->o_csn );
2365 dninfo *dni = op->o_callback->sc_private;
2367 if ( rs->sr_type == REP_SEARCH ) {
2368 if ( !BER_BVISNULL( &dni->dn ) ) {
2369 Debug( LDAP_DEBUG_ANY,
2370 "dn_callback : consistency error - "
2371 "entryUUID is not unique\n", 0, 0, 0 );
2373 ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2374 ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2375 /* If there is a new entry, see if it differs from the old.
2376 * We compare the non-normalized values so that cosmetic changes
2377 * in the provider are always propagated.
2379 if ( dni->new_entry ) {
2380 Attribute *old, *new;
2383 /* Did the DN change? Note that we don't explicitly try to
2384 * discover if the deleteOldRdn argument applies here. It
2385 * would save an unnecessary Modify if we detected it, but
2386 * that's a fair amount of trouble to compare the two attr
2387 * lists in detail. (Just test normalized DN; we ignore
2388 * insignificant changes here.)
2390 if ( !dn_match( &rs->sr_entry->e_nname,
2391 &dni->new_entry->e_nname ) )
2396 for ( i = 0, old = rs->sr_entry->e_attrs;
2398 i++, old = old->a_next )
2403 /* We assume that attributes are saved in the same order
2404 * in the remote and local databases. So if we walk through
2405 * the attributeDescriptions one by one they should match in
2406 * lock step. If not, we signal a change. Otherwise we test
2409 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2411 old = old->a_next, new = new->a_next )
2413 if ( old->a_desc != new->a_desc ) {
2414 dni->wasChanged = 1;
2417 for ( i = 0; ; i++ ) {
2419 nold = BER_BVISNULL( &old->a_vals[i] );
2420 nnew = BER_BVISNULL( &new->a_vals[i] );
2421 /* If both are empty, stop looking */
2422 if ( nold && nnew ) {
2425 /* If they are different, stop looking */
2426 if ( nold != nnew ) {
2427 dni->wasChanged = 1;
2430 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2431 dni->wasChanged = 1;
2435 if ( dni->wasChanged ) break;
2437 if ( dni->wasChanged ) {
2438 dni->ads = op->o_tmpalloc( dni->attrs *
2439 sizeof(AttributeDescription *), op->o_tmpmemctx );
2441 for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2442 dni->ads[i] = old->a_desc;
2448 } else if ( rs->sr_type == REP_RESULT ) {
2449 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2450 Debug( LDAP_DEBUG_ANY,
2451 "dn_callback : consistency error - "
2452 "entryUUID is not unique\n", 0, 0, 0 );
2456 return LDAP_SUCCESS;
2460 nonpresent_callback(
2464 syncinfo_t *si = op->o_callback->sc_private;
2467 struct berval* present_uuid = NULL;
2468 struct nonpresent_entry *np_entry;
2470 if ( rs->sr_type == REP_RESULT ) {
2471 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2472 si->si_presentlist = NULL;
2474 } else if ( rs->sr_type == REP_SEARCH ) {
2475 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2476 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2478 if ( a == NULL ) return 0;
2480 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2484 if ( present_uuid == NULL ) {
2485 np_entry = (struct nonpresent_entry *)
2486 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2487 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2488 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2489 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2492 avl_delete( &si->si_presentlist,
2493 &a->a_nvals[0], syncuuid_cmp );
2494 ch_free( present_uuid->bv_val );
2495 ch_free( present_uuid );
2498 return LDAP_SUCCESS;
2506 if ( rs->sr_err != LDAP_SUCCESS &&
2507 rs->sr_err != LDAP_REFERRAL &&
2508 rs->sr_err != LDAP_ALREADY_EXISTS &&
2509 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2510 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2512 Debug( LDAP_DEBUG_ANY,
2513 "null_callback : error code 0x%x\n",
2516 return LDAP_SUCCESS;
2519 static struct berval *
2520 slap_uuidstr_from_normalized(
2521 struct berval* uuidstr,
2522 struct berval* normalized,
2526 unsigned char nibble;
2529 if ( normalized == NULL ) return NULL;
2530 if ( normalized->bv_len != 16 ) return NULL;
2535 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2536 if ( new == NULL ) {
2543 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2544 if ( new != uuidstr ) {
2545 slap_sl_free( new, ctx );
2550 for ( i = 0; i < 16; i++ ) {
2551 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2552 new->bv_val[(i<<1)+d] = '-';
2556 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2557 if ( nibble < 10 ) {
2558 new->bv_val[(i<<1)+d] = nibble + '0';
2560 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2563 nibble = (normalized->bv_val[i]) & 0xF;
2564 if ( nibble < 10 ) {
2565 new->bv_val[(i<<1)+d+1] = nibble + '0';
2567 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2571 new->bv_val[new->bv_len] = '\0';
2576 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2578 const struct berval *uuid1 = v_uuid1;
2579 const struct berval *uuid2 = v_uuid2;
2580 int rc = uuid1->bv_len - uuid2->bv_len;
2581 if ( rc ) return rc;
2582 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2586 avl_ber_bvfree( void *v_bv )
2588 struct berval *bv = (struct berval *)v_bv;
2590 if( v_bv == NULL ) return;
2591 if ( !BER_BVISNULL( bv ) ) {
2592 ch_free( bv->bv_val );
2594 ch_free( (char *) bv );
2598 syncinfo_free( syncinfo_t *sie )
2600 /* re-fetch it, in case it was already removed */
2601 sie->si_re = ldap_pvt_runqueue_find( &slapd_rq, do_syncrepl, sie );
2603 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, sie->si_re ) )
2604 ldap_pvt_runqueue_stoptask( &slapd_rq, sie->si_re );
2605 ldap_pvt_runqueue_remove( &slapd_rq, sie->si_re );
2608 ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2610 bindconf_free( &sie->si_bindconf );
2612 if ( sie->si_filterstr.bv_val ) {
2613 ch_free( sie->si_filterstr.bv_val );
2615 if ( sie->si_base.bv_val ) {
2616 ch_free( sie->si_base.bv_val );
2618 if ( sie->si_attrs ) {
2620 while ( sie->si_attrs[i] != NULL ) {
2621 ch_free( sie->si_attrs[i] );
2624 ch_free( sie->si_attrs );
2626 if ( sie->si_exattrs ) {
2628 while ( sie->si_exattrs[i] != NULL ) {
2629 ch_free( sie->si_exattrs[i] );
2632 ch_free( sie->si_exattrs );
2634 if ( sie->si_anlist ) {
2636 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2637 ch_free( sie->si_anlist[i].an_name.bv_val );
2640 ch_free( sie->si_anlist );
2642 if ( sie->si_exanlist ) {
2644 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2645 ch_free( sie->si_exanlist[i].an_name.bv_val );
2648 ch_free( sie->si_exanlist );
2650 if ( sie->si_retryinterval ) {
2651 ch_free( sie->si_retryinterval );
2653 if ( sie->si_retrynum ) {
2654 ch_free( sie->si_retrynum );
2656 if ( sie->si_retrynum_init ) {
2657 ch_free( sie->si_retrynum_init );
2659 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2660 if ( sie->si_presentlist ) {
2661 avl_free( sie->si_presentlist, avl_ber_bvfree );
2664 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2666 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2667 struct nonpresent_entry* npe;
2668 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2669 LDAP_LIST_REMOVE( npe, npe_link );
2670 if ( npe->npe_name ) {
2671 if ( npe->npe_name->bv_val ) {
2672 ch_free( npe->npe_name->bv_val );
2674 ch_free( npe->npe_name );
2676 if ( npe->npe_nname ) {
2677 if ( npe->npe_nname->bv_val ) {
2678 ch_free( npe->npe_nname->bv_val );
2680 ch_free( npe->npe_nname );
2689 /* NOTE: used & documented in slapd.conf(5) */
2691 #define PROVIDERSTR "provider"
2692 #define SCHEMASTR "schemachecking"
2693 #define FILTERSTR "filter"
2694 #define SEARCHBASESTR "searchbase"
2695 #define SCOPESTR "scope"
2696 #define ATTRSONLYSTR "attrsonly"
2697 #define ATTRSSTR "attrs"
2698 #define TYPESTR "type"
2699 #define INTERVALSTR "interval"
2700 #define RETRYSTR "retry"
2701 #define SLIMITSTR "sizelimit"
2702 #define TLIMITSTR "timelimit"
2703 #define SYNCDATASTR "syncdata"
2705 /* FIXME: undocumented */
2706 #define LOGBASESTR "logbase"
2707 #define LOGFILTERSTR "logfilter"
2708 #define OLDAUTHCSTR "bindprincipal"
2709 #define EXATTRSSTR "exattrs"
2710 #define MANAGEDSAITSTR "manageDSAit"
2713 #define LASTMODSTR "lastmod"
2714 #define LMGENSTR "gen"
2715 #define LMNOSTR "no"
2716 #define LMREQSTR "req"
2717 #define SRVTABSTR "srvtab"
2718 #define SUFFIXSTR "suffix"
2721 #define GOT_ID 0x0001
2722 #define GOT_PROVIDER 0x0002
2725 #define GOT_ALL (GOT_ID|GOT_PROVIDER)
2731 { BER_BVC("base"), LDAP_SCOPE_BASE },
2732 { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2733 { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL }, /* OpenLDAP extension */
2734 { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2735 { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2736 { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2737 { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE }, /* OpenLDAP extension */
2741 static slap_verbmasks datamodes[] = {
2742 { BER_BVC("default"), SYNCDATA_DEFAULT },
2743 { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2744 { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2749 parse_syncrepl_line(
2757 for ( i = 1; i < c->argc; i++ ) {
2758 if ( !strncasecmp( c->argv[ i ], IDSTR "=",
2759 STRLENOF( IDSTR "=" ) ) )
2762 /* '\0' string terminator accounts for '=' */
2763 val = c->argv[ i ] + STRLENOF( IDSTR "=" );
2764 if ( lutil_atoi( &tmp, val ) != 0 ) {
2765 snprintf( c->msg, sizeof( c->msg ),
2766 "Error: parse_syncrepl_line: "
2767 "unable to parse syncrepl id \"%s\"", val );
2768 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2771 if ( tmp >= 1000 || tmp < 0 ) {
2772 snprintf( c->msg, sizeof( c->msg ),
2773 "Error: parse_syncrepl_line: "
2774 "syncrepl id %d is out of range [0..999]", tmp );
2775 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2780 } else if ( !strncasecmp( c->argv[ i ], PROVIDERSTR "=",
2781 STRLENOF( PROVIDERSTR "=" ) ) )
2783 val = c->argv[ i ] + STRLENOF( PROVIDERSTR "=" );
2784 ber_str2bv( val, 0, 1, &si->si_bindconf.sb_uri );
2785 gots |= GOT_PROVIDER;
2786 } else if ( !strncasecmp( c->argv[ i ], SCHEMASTR "=",
2787 STRLENOF( SCHEMASTR "=" ) ) )
2789 val = c->argv[ i ] + STRLENOF( SCHEMASTR "=" );
2790 if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2791 si->si_schemachecking = 1;
2792 } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2793 si->si_schemachecking = 0;
2795 si->si_schemachecking = 1;
2797 } else if ( !strncasecmp( c->argv[ i ], FILTERSTR "=",
2798 STRLENOF( FILTERSTR "=" ) ) )
2800 val = c->argv[ i ] + STRLENOF( FILTERSTR "=" );
2801 if ( si->si_filterstr.bv_val )
2802 ch_free( si->si_filterstr.bv_val );
2803 ber_str2bv( val, 0, 1, &si->si_filterstr );
2804 } else if ( !strncasecmp( c->argv[ i ], LOGFILTERSTR "=",
2805 STRLENOF( LOGFILTERSTR "=" ) ) )
2807 val = c->argv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2808 if ( si->si_logfilterstr.bv_val )
2809 ch_free( si->si_logfilterstr.bv_val );
2810 ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2811 } else if ( !strncasecmp( c->argv[ i ], SEARCHBASESTR "=",
2812 STRLENOF( SEARCHBASESTR "=" ) ) )
2817 val = c->argv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2818 if ( si->si_base.bv_val ) {
2819 ch_free( si->si_base.bv_val );
2821 ber_str2bv( val, 0, 0, &bv );
2822 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2823 if ( rc != LDAP_SUCCESS ) {
2824 snprintf( c->msg, sizeof( c->msg ),
2825 "Invalid base DN \"%s\": %d (%s)",
2826 val, rc, ldap_err2string( rc ) );
2827 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2830 } else if ( !strncasecmp( c->argv[ i ], LOGBASESTR "=",
2831 STRLENOF( LOGBASESTR "=" ) ) )
2836 val = c->argv[ i ] + STRLENOF( LOGBASESTR "=" );
2837 if ( si->si_logbase.bv_val ) {
2838 ch_free( si->si_logbase.bv_val );
2840 ber_str2bv( val, 0, 0, &bv );
2841 rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2842 if ( rc != LDAP_SUCCESS ) {
2843 snprintf( c->msg, sizeof( c->msg ),
2844 "Invalid logbase DN \"%s\": %d (%s)",
2845 val, rc, ldap_err2string( rc ) );
2846 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2849 } else if ( !strncasecmp( c->argv[ i ], SCOPESTR "=",
2850 STRLENOF( SCOPESTR "=" ) ) )
2853 val = c->argv[ i ] + STRLENOF( SCOPESTR "=" );
2854 for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2855 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2856 si->si_scope = scopes[j].val;
2860 if ( BER_BVISNULL(&scopes[j].key) ) {
2861 snprintf( c->msg, sizeof( c->msg ),
2862 "Error: parse_syncrepl_line: "
2863 "unknown scope \"%s\"", val);
2864 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2867 } else if ( !strncasecmp( c->argv[ i ], ATTRSONLYSTR,
2868 STRLENOF( ATTRSONLYSTR ) ) )
2870 si->si_attrsonly = 1;
2871 } else if ( !strncasecmp( c->argv[ i ], ATTRSSTR "=",
2872 STRLENOF( ATTRSSTR "=" ) ) )
2874 val = c->argv[ i ] + STRLENOF( ATTRSSTR "=" );
2875 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2877 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2878 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2879 if ( si->si_anlist == NULL ) {
2880 ch_free( attr_fname );
2883 si->si_anfile = attr_fname;
2885 char *str, *s, *next;
2886 char delimstr[] = " ,\t";
2887 str = ch_strdup( val );
2888 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2890 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2892 if ( strlen(s) == 1 && *s == '*' ) {
2893 si->si_allattrs = 1;
2894 *(val + ( s - str )) = delimstr[0];
2896 if ( strlen(s) == 1 && *s == '+' ) {
2897 si->si_allopattrs = 1;
2898 *(val + ( s - str )) = delimstr[0];
2902 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2903 if ( si->si_anlist == NULL ) {
2907 } else if ( !strncasecmp( c->argv[ i ], EXATTRSSTR "=",
2908 STRLENOF( EXATTRSSTR "=" ) ) )
2910 val = c->argv[ i ] + STRLENOF( EXATTRSSTR "=" );
2911 if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2913 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2914 si->si_exanlist = file2anlist(
2915 si->si_exanlist, attr_fname, " ,\t" );
2916 if ( si->si_exanlist == NULL ) {
2917 ch_free( attr_fname );
2920 ch_free( attr_fname );
2922 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2923 if ( si->si_exanlist == NULL ) {
2927 } else if ( !strncasecmp( c->argv[ i ], TYPESTR "=",
2928 STRLENOF( TYPESTR "=" ) ) )
2930 val = c->argv[ i ] + STRLENOF( TYPESTR "=" );
2931 if ( !strncasecmp( val, "refreshOnly",
2932 STRLENOF("refreshOnly") ) )
2934 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
2935 } else if ( !strncasecmp( val, "refreshAndPersist",
2936 STRLENOF("refreshAndPersist") ) )
2938 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_AND_PERSIST;
2939 si->si_interval = 60;
2941 snprintf( c->msg, sizeof( c->msg ),
2942 "Error: parse_syncrepl_line: "
2943 "unknown sync type \"%s\"", val);
2944 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2947 } else if ( !strncasecmp( c->argv[ i ], INTERVALSTR "=",
2948 STRLENOF( INTERVALSTR "=" ) ) )
2950 val = c->argv[ i ] + STRLENOF( INTERVALSTR "=" );
2951 if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2952 si->si_interval = 0;
2953 } else if ( strchr( val, ':' ) != NULL ) {
2954 char *next, *ptr = val;
2955 unsigned dd, hh, mm, ss;
2956 dd = strtoul( ptr, &next, 10 );
2957 if ( next == ptr || next[0] != ':' ) {
2958 snprintf( c->msg, sizeof( c->msg ),
2959 "Error: parse_syncrepl_line: "
2960 "invalid interval \"%s\", unable to parse days", val );
2961 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2965 hh = strtoul( ptr, &next, 10 );
2966 if ( next == ptr || next[0] != ':' || hh > 24 ) {
2967 snprintf( c->msg, sizeof( c->msg ),
2968 "Error: parse_syncrepl_line: "
2969 "invalid interval \"%s\", unable to parse hours", val );
2970 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2974 mm = strtoul( ptr, &next, 10 );
2975 if ( next == ptr || next[0] != ':' || mm > 60 ) {
2976 snprintf( c->msg, sizeof( c->msg ),
2977 "Error: parse_syncrepl_line: "
2978 "invalid interval \"%s\", unable to parse minutes", val );
2979 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2983 ss = strtoul( ptr, &next, 10 );
2984 if ( next == ptr || next[0] != '\0' || ss > 60 ) {
2985 snprintf( c->msg, sizeof( c->msg ),
2986 "Error: parse_syncrepl_line: "
2987 "invalid interval \"%s\", unable to parse seconds", val );
2988 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
2991 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2995 if ( lutil_parse_time( val, &t ) != 0 ) {
2996 snprintf( c->msg, sizeof( c->msg ),
2997 "Error: parse_syncrepl_line: "
2998 "invalid interval \"%s\"", val );
2999 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3002 si->si_interval = (time_t)t;
3004 if ( si->si_interval < 0 ) {
3005 snprintf( c->msg, sizeof( c->msg ),
3006 "Error: parse_syncrepl_line: "
3007 "invalid interval \"%ld\"",
3008 (long) si->si_interval);
3009 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3012 } else if ( !strncasecmp( c->argv[ i ], RETRYSTR "=",
3013 STRLENOF( RETRYSTR "=" ) ) )
3018 val = c->argv[ i ] + STRLENOF( RETRYSTR "=" );
3019 retry_list = (char **) ch_calloc( 1, sizeof( char * ));
3020 retry_list[0] = NULL;
3022 slap_str2clist( &retry_list, val, " ,\t" );
3024 for ( k = 0; retry_list && retry_list[k]; k++ ) ;
3027 snprintf( c->msg, sizeof( c->msg ),
3028 "Error: incomplete syncrepl retry list" );
3029 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3030 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3031 ch_free( retry_list[k] );
3033 ch_free( retry_list );
3036 si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
3037 si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
3038 si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
3039 for ( j = 0; j < n; j++ ) {
3041 if ( lutil_atoul( &t, retry_list[j*2] ) != 0 ) {
3042 snprintf( c->msg, sizeof( c->msg ),
3043 "Error: invalid retry interval \"%s\" (#%d)",
3044 retry_list[j*2], j );
3045 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3046 /* do some cleanup */
3049 si->si_retryinterval[j] = (time_t)t;
3050 if ( *retry_list[j*2+1] == '+' ) {
3051 si->si_retrynum_init[j] = RETRYNUM_FOREVER;
3052 si->si_retrynum[j] = RETRYNUM_FOREVER;
3056 if ( lutil_atoi( &si->si_retrynum_init[j], retry_list[j*2+1] ) != 0
3057 || si->si_retrynum_init[j] <= 0 )
3059 snprintf( c->msg, sizeof( c->msg ),
3060 "Error: invalid initial retry number \"%s\" (#%d)",
3061 retry_list[j*2+1], j );
3062 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3063 /* do some cleanup */
3066 if ( lutil_atoi( &si->si_retrynum[j], retry_list[j*2+1] ) != 0
3067 || si->si_retrynum[j] <= 0 )
3069 snprintf( c->msg, sizeof( c->msg ),
3070 "Error: invalid retry number \"%s\" (#%d)",
3071 retry_list[j*2+1], j );
3072 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3073 /* do some cleanup */
3078 si->si_retrynum_init[j] = RETRYNUM_TAIL;
3079 si->si_retrynum[j] = RETRYNUM_TAIL;
3080 si->si_retryinterval[j] = 0;
3082 for ( k = 0; retry_list && retry_list[k]; k++ ) {
3083 ch_free( retry_list[k] );
3085 ch_free( retry_list );
3086 } else if ( !strncasecmp( c->argv[ i ], MANAGEDSAITSTR "=",
3087 STRLENOF( MANAGEDSAITSTR "=" ) ) )
3089 val = c->argv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
3090 if ( lutil_atoi( &si->si_manageDSAit, val ) != 0
3091 || si->si_manageDSAit < 0 || si->si_manageDSAit > 1 )
3093 snprintf( c->msg, sizeof( c->msg ),
3094 "invalid manageDSAit value \"%s\".\n",
3096 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3099 } else if ( !strncasecmp( c->argv[ i ], SLIMITSTR "=",
3100 STRLENOF( SLIMITSTR "=") ) )
3102 val = c->argv[ i ] + STRLENOF( SLIMITSTR "=" );
3103 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3106 } else if ( lutil_atoi( &si->si_slimit, val ) != 0 || si->si_slimit < 0 ) {
3107 snprintf( c->msg, sizeof( c->msg ),
3108 "invalid size limit value \"%s\".\n",
3110 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3113 } else if ( !strncasecmp( c->argv[ i ], TLIMITSTR "=",
3114 STRLENOF( TLIMITSTR "=" ) ) )
3116 val = c->argv[ i ] + STRLENOF( TLIMITSTR "=" );
3117 if ( strcasecmp( val, "unlimited" ) == 0 ) {
3120 } else if ( lutil_atoi( &si->si_tlimit, val ) != 0 || si->si_tlimit < 0 ) {
3121 snprintf( c->msg, sizeof( c->msg ),
3122 "invalid time limit value \"%s\".\n",
3124 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3127 } else if ( !strncasecmp( c->argv[ i ], SYNCDATASTR "=",
3128 STRLENOF( SYNCDATASTR "=" ) ) )
3130 val = c->argv[ i ] + STRLENOF( SYNCDATASTR "=" );
3131 si->si_syncdata = verb_to_mask( val, datamodes );
3132 } else if ( bindconf_parse( c->argv[i], &si->si_bindconf ) ) {
3133 snprintf( c->msg, sizeof( c->msg ),
3134 "Error: parse_syncrepl_line: "
3135 "unknown keyword \"%s\"\n", c->argv[ i ] );
3136 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3141 if ( gots != GOT_ALL ) {
3142 snprintf( c->msg, sizeof( c->msg ),
3143 "Error: Malformed \"syncrepl\" line in slapd config file" );
3144 Debug( LDAP_DEBUG_ANY, "%s: %s.\n", c->log, c->msg, 0 );
3158 if ( !( c->be->be_search && c->be->be_add && c->be->be_modify && c->be->be_delete ) ) {
3159 Debug( LDAP_DEBUG_ANY, "%s: database %s does not support operations "
3160 "required for syncrepl\n", c->log, c->be->be_type, 0 );
3163 si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
3166 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
3170 si->si_bindconf.sb_tls = SB_TLS_OFF;
3171 si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
3172 si->si_schemachecking = 0;
3173 ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3174 &si->si_filterstr );
3175 si->si_base.bv_val = NULL;
3176 si->si_scope = LDAP_SCOPE_SUBTREE;
3177 si->si_attrsonly = 0;
3178 si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3179 si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3180 si->si_attrs = NULL;
3181 si->si_allattrs = 0;
3182 si->si_allopattrs = 0;
3183 si->si_exattrs = NULL;
3184 si->si_type = si->si_ctype = LDAP_SYNC_REFRESH_ONLY;
3185 si->si_interval = 86400;
3186 si->si_retryinterval = NULL;
3187 si->si_retrynum_init = NULL;
3188 si->si_retrynum = NULL;
3189 si->si_manageDSAit = 0;
3192 si->si_conn_setup = 0;
3194 si->si_presentlist = NULL;
3195 LDAP_LIST_INIT( &si->si_nonpresentlist );
3196 ldap_pvt_thread_mutex_init( &si->si_mutex );
3198 rc = parse_syncrepl_line( c, si );
3202 init_syncrepl( si );
3203 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3204 do_syncrepl, si, "do_syncrepl", c->be->be_suffix[0].bv_val );
3209 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3210 syncinfo_free( si );
3213 Debug( LDAP_DEBUG_CONFIG,
3214 "Config: ** successfully added syncrepl \"%s\"\n",
3215 BER_BVISNULL( &si->si_bindconf.sb_uri ) ?
3216 "(null)" : si->si_bindconf.sb_uri.bv_val, 0, 0 );
3217 if ( !si->si_schemachecking ) {
3218 SLAP_DBFLAGS(c->be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3220 c->be->be_syncinfo = si;
3226 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3228 struct berval bc, uri;
3229 char buf[BUFSIZ*2], *ptr;
3232 #define WHATSLEFT ( sizeof( buf ) - ( ptr - buf ) )
3236 /* temporarily inhibit bindconf from printing URI */
3237 uri = si->si_bindconf.sb_uri;
3238 BER_BVZERO( &si->si_bindconf.sb_uri );
3239 bindconf_unparse( &si->si_bindconf, &bc );
3240 si->si_bindconf.sb_uri = uri;
3243 ptr += snprintf( ptr, WHATSLEFT, IDSTR "=%03ld " PROVIDERSTR "=%s",
3244 si->si_rid, si->si_bindconf.sb_uri.bv_val );
3245 if ( ptr - buf >= sizeof( buf ) ) return;
3246 if ( !BER_BVISNULL( &bc )) {
3247 if ( WHATSLEFT <= bc.bv_len ) {
3251 ptr = lutil_strcopy( ptr, bc.bv_val );
3254 if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3255 if ( WHATSLEFT <= STRLENOF( " " FILTERSTR "=\"" "\"" ) + si->si_filterstr.bv_len ) return;
3256 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3257 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3260 if ( !BER_BVISNULL( &si->si_base )) {
3261 if ( WHATSLEFT <= STRLENOF( " " SEARCHBASESTR "=\"" "\"" ) + si->si_base.bv_len ) return;
3262 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3263 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3266 if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3267 if ( WHATSLEFT <= STRLENOF( " " LOGFILTERSTR "=\"" "\"" ) + si->si_logfilterstr.bv_len ) return;
3268 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3269 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3272 if ( !BER_BVISNULL( &si->si_logbase )) {
3273 if ( WHATSLEFT <= STRLENOF( " " LOGBASESTR "=\"" "\"" ) + si->si_logbase.bv_len ) return;
3274 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3275 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3278 for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3279 if ( si->si_scope == scopes[i].val ) {
3280 if ( WHATSLEFT <= STRLENOF( " " SCOPESTR "=" ) + scopes[i].key.bv_len ) return;
3281 ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3282 ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3286 if ( si->si_attrsonly ) {
3287 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3288 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR );
3290 if ( si->si_anfile ) {
3291 if ( WHATSLEFT <= STRLENOF( " " ATTRSSTR "=\":include:" "\"" ) + strlen( si->si_anfile ) ) return;
3292 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:\"" );
3293 ptr = lutil_strcopy( ptr, si->si_anfile );
3295 } else if ( si->si_allattrs || si->si_allopattrs ||
3296 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) ))
3300 if ( WHATSLEFT <= STRLENOF( " " ATTRSONLYSTR "=\"" "\"" ) ) return;
3301 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3303 /* FIXME: add check for overflow */
3304 ptr = anlist_unparse( si->si_anlist, ptr, WHATSLEFT );
3305 if ( si->si_allattrs ) {
3306 if ( WHATSLEFT <= STRLENOF( ",*\"" ) ) return;
3307 if ( old != ptr ) *ptr++ = ',';
3310 if ( si->si_allopattrs ) {
3311 if ( WHATSLEFT <= STRLENOF( ",+\"" ) ) return;
3312 if ( old != ptr ) *ptr++ = ',';
3317 if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3318 if ( WHATSLEFT <= STRLENOF( " " EXATTRSSTR "=" ) ) return;
3319 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3320 /* FIXME: add check for overflow */
3321 ptr = anlist_unparse( si->si_exanlist, ptr, WHATSLEFT );
3323 if ( WHATSLEFT <= STRLENOF( " " SCHEMASTR "=" ) + STRLENOF( "off" ) ) return;
3324 ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3325 ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3327 if ( WHATSLEFT <= STRLENOF( " " TYPESTR "=" ) + STRLENOF( "refreshAndPersist" ) ) return;
3328 ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3329 ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3330 "refreshAndPersist" : "refreshOnly" );
3332 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3335 dd = si->si_interval;
3342 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3343 ptr += snprintf( ptr, WHATSLEFT, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3344 if ( ptr - buf >= sizeof( buf ) ) return;
3345 } else if ( si->si_retryinterval ) {
3347 if ( WHATSLEFT <= STRLENOF( " " RETRYSTR "=\"" "\"" ) ) return;
3348 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3349 for (i=0; si->si_retryinterval[i]; i++) {
3350 if ( space ) *ptr++ = ' ';
3352 ptr += snprintf( ptr, WHATSLEFT, "%ld ", (long) si->si_retryinterval[i] );
3353 if ( si->si_retrynum_init[i] == RETRYNUM_FOREVER )
3356 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_retrynum_init[i] );
3358 if ( WHATSLEFT <= STRLENOF( "\"" ) ) return;
3362 if ( si->si_slimit ) {
3363 if ( WHATSLEFT <= STRLENOF( " " SLIMITSTR "=" ) ) return;
3364 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3365 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_slimit );
3368 if ( si->si_tlimit ) {
3369 if ( WHATSLEFT <= STRLENOF( " " TLIMITSTR "=" ) ) return;
3370 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3371 ptr += snprintf( ptr, WHATSLEFT, "%d", si->si_tlimit );
3374 if ( si->si_syncdata ) {
3375 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3376 if ( WHATSLEFT <= STRLENOF( " " SYNCDATASTR "=" ) + bc.bv_len ) return;
3377 ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3378 ptr = lutil_strcopy( ptr, bc.bv_val );
3381 bc.bv_len = ptr - buf;
3383 ber_dupbv( bv, &bc );
3387 syncrepl_config( ConfigArgs *c )
3389 if (c->op == SLAP_CONFIG_EMIT) {
3390 if ( c->be->be_syncinfo ) {
3392 syncrepl_unparse( c->be->be_syncinfo, &bv );
3393 ber_bvarray_add( &c->rvalue_vals, &bv );
3397 } else if ( c->op == LDAP_MOD_DELETE ) {
3400 if ( c->be->be_syncinfo ) {
3401 syncinfo_free( c->be->be_syncinfo );
3402 c->be->be_syncinfo = NULL;
3404 SLAP_DBFLAGS( c->be ) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3407 if ( SLAP_SHADOW( c->be ) ) {
3408 Debug(LDAP_DEBUG_ANY, "%s: "
3409 "syncrepl: database already shadowed.\n",
3412 } else if ( add_syncrepl( c ) ) {
3415 return config_sync_shadow( c );