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-2004 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"
32 #define SYNCREPL_STR "syncreplxxx"
35 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
36 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
38 static int syncuuid_cmp( const void *, const void * );
39 static void avl_ber_bvfree( void * );
40 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
42 /* callback functions */
43 static int dn_callback( struct slap_op *, struct slap_rep * );
44 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
45 static int null_callback( struct slap_op *, struct slap_rep * );
47 static AttributeDescription *sync_descs[4];
50 init_syncrepl(syncinfo_t *si)
53 char **attrs, **exattrs;
56 if ( !sync_descs[0] ) {
57 sync_descs[0] = slap_schema.si_ad_objectClass;
58 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
59 sync_descs[2] = slap_schema.si_ad_entryCSN;
63 if ( si->si_allattrs && si->si_allopattrs )
66 attrs = anlist2attrs( si->si_anlist );
69 if ( si->si_allattrs ) {
72 if ( !is_at_operational( at_find( attrs[i] ))) {
73 for ( j = i; attrs[j] != NULL; j++ ) {
76 attrs[j] = attrs[j+1];
82 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
83 attrs[i] = ch_strdup("*");
86 } else if ( si->si_allopattrs ) {
89 if ( is_at_operational( at_find( attrs[i] ))) {
90 for ( j = i; attrs[j] != NULL; j++ ) {
93 attrs[j] = attrs[j+1];
99 attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
100 attrs[i] = ch_strdup("+");
104 for ( i = 0; sync_descs[i] != NULL; i++ ) {
107 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
108 for ( k = j; attrs[k] != NULL; k++ ) {
111 attrs[k] = attrs[k+1];
119 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
121 if ( si->si_allopattrs ) {
122 attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
124 attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
127 if ( attrs == NULL ) {
128 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
132 if ( si->si_allopattrs ) {
133 attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
135 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
136 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
144 if ( si->si_allattrs == si->si_allopattrs ) {
145 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
146 attrs[i++] = ch_strdup( "*" );
147 attrs[i++] = ch_strdup( "+" );
148 } else if ( si->si_allattrs && !si->si_allopattrs ) {
149 for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
150 attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
151 attrs[i++] = ch_strdup( "*" );
152 for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
153 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
155 } else if ( !si->si_allattrs && si->si_allopattrs ) {
156 attrs = (char**) ch_malloc( 3 * sizeof(char*) );
157 attrs[i++] = ch_strdup( "+" );
158 attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
163 si->si_attrs = attrs;
165 exattrs = anlist2attrs( si->si_exanlist );
168 for ( n = 0; exattrs[n] != NULL; n++ ) ;
170 for ( i = 0; sync_descs[i] != NULL; i++ ) {
172 while ( exattrs[j] != NULL ) {
173 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
174 for ( k = j; exattrs[k] != NULL; k++ ) {
176 ch_free( exattrs[k] );
177 exattrs[k] = exattrs[k+1];
185 for ( i = 0; exattrs[i] != NULL; i++ ) {
186 for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
187 if ( oc = si->si_anlist[j].an_oc ) {
189 while ( oc->soc_required[k] ) {
190 if ( !strcmp( exattrs[i],
191 oc->soc_required[k]->sat_cname.bv_val )) {
192 for ( l = i; exattrs[l]; l++ ) {
194 ch_free( exattrs[i] );
195 exattrs[l] = exattrs[l+1];
205 for ( i = 0; exattrs[i] != NULL; i++ ) ;
208 exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
211 si->si_exattrs = exattrs;
219 BerElementBuffer berbuf;
220 BerElement *ber = (BerElement *)&berbuf;
221 LDAPControl c[2], *ctrls[3];
222 struct timeval timeout;
226 /* setup LDAP SYNC control */
227 ber_init2( ber, NULL, LBER_USE_DER );
228 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
230 if ( si->si_syncCookie.octet_str &&
231 !BER_BVISNULL( &si->si_syncCookie.octet_str[0] ) )
233 ber_printf( ber, "{eO}",
235 &si->si_syncCookie.octet_str[0] );
237 ber_printf( ber, "{e}",
241 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
246 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
247 c[0].ldctl_iscritical = si->si_type < 0;
250 if ( si->si_authzId ) {
251 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
252 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
253 c[1].ldctl_iscritical = 1;
260 timeout.tv_sec = si->si_tlimit;
263 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
264 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
265 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
266 si->si_slimit, &msgid );
277 int cmdline_cookie_found = 0;
279 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
280 struct berval syncrepl_cn_bv;
281 struct sync_cookie *sc = NULL;
282 struct sync_cookie syncCookie = { NULL, -1, NULL };
288 psub = &si->si_be->be_nsuffix[0];
290 /* Init connection to master */
291 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
292 if ( rc != LDAP_SUCCESS ) {
293 Debug( LDAP_DEBUG_ANY,
294 "do_syncrep1: ldap_initialize failed (%s)\n",
295 si->si_provideruri, 0, 0 );
299 op->o_protocol = LDAP_VERSION3;
300 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
305 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
306 if( rc != LDAP_SUCCESS ) {
307 Debug( LDAP_DEBUG_ANY,
308 "%s: ldap_start_tls failed (%d)\n",
309 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
311 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
315 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
316 #ifdef HAVE_CYRUS_SASL
319 if ( si->si_secprops != NULL ) {
320 rc = ldap_set_option( si->si_ld,
321 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
323 if( rc != LDAP_OPT_SUCCESS ) {
324 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
325 "(%s,SECPROPS,\"%s\") failed!\n",
326 si->si_provideruri, si->si_secprops, 0 );
331 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
332 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
334 rc = ldap_sasl_interactive_bind_s( si->si_ld,
342 lutil_sasl_freedefs( defaults );
344 /* FIXME: different error behaviors according to
346 * 2) on err policy : exit, retry, backoff ...
348 if ( rc != LDAP_SUCCESS ) {
349 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
350 "ldap_sasl_interactive_bind_s failed (%d)\n",
353 /* FIXME (see above comment) */
354 /* if Kerberos credentials cache is not active, retry */
355 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
356 rc == LDAP_LOCAL_ERROR )
358 rc = LDAP_SERVER_DOWN;
363 #else /* HAVE_CYRUS_SASL */
364 /* Should never get here, we trapped this at config time */
366 fprintf( stderr, "not compiled with SASL support\n" );
372 rc = ldap_bind_s( si->si_ld,
373 si->si_binddn, si->si_passwd, si->si_bindmethod );
374 if ( rc != LDAP_SUCCESS ) {
375 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
376 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
381 /* Set SSF to strongest of TLS, SASL SSFs */
384 op->o_transport_ssf = 0;
386 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
387 == LDAP_SUCCESS && ssl != NULL )
389 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
391 #endif /* HAVE_TLS */
392 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
393 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
394 ? op->o_sasl_ssf : op->o_tls_ssf;
396 /* get syncrepl cookie of shadow replica from subentry */
397 assert( si->si_rid < 1000 );
398 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
399 syncrepl_cn_bv.bv_len = snprintf( syncrepl_cbuf, sizeof(syncrepl_cbuf),
400 CN_STR "syncrepl%ld", si->si_rid );
401 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
402 op->o_req_dn = op->o_req_ndn;
404 LDAP_STAILQ_FOREACH( sc, &SLAPD_GLOBAL(sync_cookie), sc_next ) {
405 if ( si->si_rid == sc->rid ) {
406 cmdline_cookie_found = 1;
411 if ( cmdline_cookie_found ) {
412 /* cookie is supplied in the command line */
413 BerVarray cookie = NULL;
414 struct berval cookie_bv;
416 LDAP_STAILQ_REMOVE( &SLAPD_GLOBAL(sync_cookie), sc, sync_cookie, sc_next );
417 slap_sync_cookie_free( &si->si_syncCookie, 0 );
419 /* read stored cookie if it exists */
420 backend_attribute( op, NULL, &op->o_req_ndn,
421 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
424 /* no stored cookie */
425 if ( sc->ctxcsn == NULL ||
426 BER_BVISNULL( sc->ctxcsn ) ) {
427 /* if cmdline cookie does not have ctxcsn */
428 /* component, set it to an initial value */
429 slap_init_sync_cookie_ctxcsn( sc );
431 slap_dup_sync_cookie( &si->si_syncCookie, sc );
432 slap_sync_cookie_free( sc, 1 );
437 struct berval newcookie = BER_BVNULL;
438 ber_dupbv( &cookie_bv, &cookie[0] );
439 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
440 slap_parse_sync_cookie( &si->si_syncCookie );
441 ber_bvarray_free( si->si_syncCookie.octet_str );
442 si->si_syncCookie.octet_str = NULL;
443 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
444 if ( sc->sid != -1 ) {
445 /* command line cookie wins */
446 si->si_syncCookie.sid = sc->sid;
448 if ( sc->ctxcsn != NULL ) {
449 /* command line cookie wins */
450 if ( si->si_syncCookie.ctxcsn ) {
451 ber_bvarray_free( si->si_syncCookie.ctxcsn );
452 si->si_syncCookie.ctxcsn = NULL;
454 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
455 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
457 if ( sc->rid != -1 ) {
458 /* command line cookie wins */
459 si->si_syncCookie.rid = sc->rid;
461 slap_sync_cookie_free( sc, 1 );
463 slap_compose_sync_cookie( NULL, &newcookie,
464 &si->si_syncCookie.ctxcsn[0],
465 si->si_syncCookie.sid, si->si_syncCookie.rid );
466 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
470 /* no command line cookie is specified */
471 if ( si->si_syncCookie.octet_str == NULL ) {
472 BerVarray cookie = NULL;
473 struct berval cookie_bv;
474 /* try to read stored cookie */
475 backend_attribute( op, NULL, &op->o_req_ndn,
476 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
478 ber_dupbv( &cookie_bv, &cookie[0] );
479 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
480 slap_parse_sync_cookie( &si->si_syncCookie );
481 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
486 rc = ldap_sync_search( si, op->o_tmpmemctx );
488 if( rc != LDAP_SUCCESS ) {
489 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
490 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
496 ldap_unbind( si->si_ld );
501 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
511 LDAPControl **rctrls = NULL;
514 BerElementBuffer berbuf;
515 BerElement *ber = (BerElement *)&berbuf;
517 LDAPMessage *res = NULL;
518 LDAPMessage *msg = NULL;
521 struct berval *retdata = NULL;
526 struct berval syncUUID = BER_BVNULL;
527 struct sync_cookie syncCookie = { NULL, -1, NULL };
528 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
529 struct berval cookie = BER_BVNULL;
537 Modifications *modlist = NULL;
542 struct timeval *tout_p = NULL;
543 struct timeval tout = { 0, 0 };
545 int refreshDeletes = 0;
547 BerVarray syncUUIDs = NULL;
550 if ( SLAPD_GLOBAL(shutdown) ) {
555 ber_init2( ber, NULL, LBER_USE_DER );
556 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
558 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
560 psub = &si->si_be->be_nsuffix[0];
562 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
564 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
570 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
571 tout_p, &res )) > 0 )
573 if ( SLAPD_GLOBAL(shutdown) ) {
577 for( msg = ldap_first_message( si->si_ld, res );
579 msg = ldap_next_message( si->si_ld, msg ) )
581 switch( ldap_msgtype( msg ) ) {
582 case LDAP_RES_SEARCH_ENTRY:
583 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
584 /* we can't work without the control */
586 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
587 "got search entry without "
588 "control\n", 0, 0, 0 );
593 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
594 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
595 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
596 ber_scanf( ber, /*"{"*/ "m}", &cookie );
597 if ( !BER_BVISNULL( &cookie ) ) {
598 struct berval tmp_bv;
599 ber_dupbv( &tmp_bv, &cookie );
600 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
602 if ( syncCookie.octet_str &&
603 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
605 slap_parse_sync_cookie( &syncCookie );
608 if ( syncrepl_message_to_entry( si, op, msg,
609 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
610 rc_efree = syncrepl_entry( si, op, entry, modlist,
611 syncstate, &syncUUID, &syncCookie_req );
612 if ( syncCookie.octet_str &&
613 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
615 syncrepl_updateCookie( si, op, psub, &syncCookie );
618 ldap_controls_free( rctrls );
620 slap_mods_free( modlist );
622 if ( rc_efree && entry ) {
628 case LDAP_RES_SEARCH_REFERENCE:
629 Debug( LDAP_DEBUG_ANY,
630 "do_syncrep2 : reference received\n", 0, 0, 0 );
633 case LDAP_RES_SEARCH_RESULT:
634 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
638 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
640 ber_scanf( ber, "{" /*"}"*/);
641 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
642 ber_scanf( ber, "m", &cookie );
643 if ( !BER_BVISNULL( &cookie ) ) {
644 struct berval tmp_bv;
645 ber_dupbv( &tmp_bv, &cookie );
646 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
648 if ( syncCookie.octet_str &&
649 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
651 slap_parse_sync_cookie( &syncCookie );
654 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
656 ber_scanf( ber, "b", &refreshDeletes );
658 ber_scanf( ber, /*"{"*/ "}" );
660 if ( syncCookie_req.ctxcsn == NULL ) {
662 } else if ( syncCookie.ctxcsn == NULL ) {
665 value_match( &match, slap_schema.si_ad_entryCSN,
666 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
667 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
668 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
671 if ( syncCookie.octet_str && !BER_BVISNULL( syncCookie.octet_str ) &&
672 match < 0 && err == LDAP_SUCCESS )
674 syncrepl_updateCookie( si, op, psub, &syncCookie );
677 ldap_controls_free( rctrls );
679 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
680 /* FIXME : different error behaviors according to
681 * 1) err code : LDAP_BUSY ...
682 * 2) on err policy : stop service, stop sync, retry
684 if ( refreshDeletes == 0 && match < 0 &&
685 err == LDAP_SUCCESS )
687 syncrepl_del_nonpresent( op, si );
689 avl_free( si->si_presentlist, avl_ber_bvfree );
690 si->si_presentlist = NULL;
697 case LDAP_RES_INTERMEDIATE:
698 rc = ldap_parse_intermediate( si->si_ld, msg,
699 &retoid, &retdata, NULL, 0 );
700 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
701 int si_refreshDelete = 0;
702 int si_refreshPresent = 0;
703 ber_init2( ber, retdata, LBER_USE_DER );
705 switch ( si_tag = ber_peek_tag( ber, &len )) {
707 case LDAP_TAG_SYNC_NEW_COOKIE:
708 ber_scanf( ber, "tm", &tag, &cookie );
710 case LDAP_TAG_SYNC_REFRESH_DELETE:
711 si_refreshDelete = 1;
712 case LDAP_TAG_SYNC_REFRESH_PRESENT:
713 si_refreshPresent = 1;
714 ber_scanf( ber, "t{" /*"}"*/, &tag );
715 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
717 ber_scanf( ber, "m", &cookie );
718 if ( !BER_BVISNULL( &cookie ) ) {
719 struct berval tmp_bv;
720 ber_dupbv( &tmp_bv, &cookie );
721 ber_bvarray_add( &syncCookie.octet_str,
724 if ( syncCookie.octet_str &&
725 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
727 slap_parse_sync_cookie( &syncCookie );
730 if ( ber_peek_tag( ber, &len ) ==
731 LDAP_TAG_REFRESHDONE )
733 ber_scanf( ber, "b", &refreshDone );
735 ber_scanf( ber, /*"{"*/ "}" );
737 case LDAP_TAG_SYNC_ID_SET:
738 ber_scanf( ber, "t{" /*"}"*/, &tag );
739 if ( ber_peek_tag( ber, &len ) ==
740 LDAP_TAG_SYNC_COOKIE )
742 ber_scanf( ber, "m", &cookie );
743 if ( !BER_BVISNULL( &cookie ) ) {
744 struct berval tmp_bv;
745 ber_dupbv( &tmp_bv, &cookie );
746 ber_bvarray_add( &syncCookie.octet_str,
749 if ( syncCookie.octet_str &&
750 !BER_BVISNULL( &syncCookie.octet_str[0] ) )
752 slap_parse_sync_cookie( &syncCookie );
755 if ( ber_peek_tag( ber, &len ) ==
756 LDAP_TAG_REFRESHDELETES )
758 ber_scanf( ber, "b", &refreshDeletes );
760 ber_scanf( ber, "[W]", &syncUUIDs );
761 ber_scanf( ber, /*"{"*/ "}" );
762 for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
763 struct berval *syncuuid_bv;
764 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
765 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
766 avl_insert( &si->si_presentlist,
767 (caddr_t) syncuuid_bv,
768 syncuuid_cmp, avl_dup_error );
770 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
773 Debug( LDAP_DEBUG_ANY,
774 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
775 (long) si_tag, 0, 0 );
776 ldap_memfree( retoid );
777 ber_bvfree( retdata );
781 if ( syncCookie_req.ctxcsn == NULL ) {
783 } else if ( syncCookie.ctxcsn == NULL ) {
786 value_match( &match, slap_schema.si_ad_entryCSN,
787 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
788 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
789 &syncCookie_req.ctxcsn[0],
790 &syncCookie.ctxcsn[0], &text );
793 if ( syncCookie.ctxcsn && !BER_BVISNULL( &syncCookie.ctxcsn[0] ) &&
796 syncrepl_updateCookie( si, op, psub, &syncCookie);
799 if ( si_refreshPresent == 1 ) {
801 syncrepl_del_nonpresent( op, si );
805 ldap_memfree( retoid );
806 ber_bvfree( retdata );
810 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
811 "unknown intermediate response (%d)\n",
813 ldap_memfree( retoid );
814 ber_bvfree( retdata );
820 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
821 "unknown message\n", 0, 0, 0 );
825 if ( syncCookie.octet_str ) {
826 slap_sync_cookie_free( &syncCookie_req, 0 );
827 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
828 slap_sync_cookie_free( &syncCookie, 0 );
838 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
839 errstr = ldap_err2string( rc );
841 Debug( LDAP_DEBUG_ANY,
842 "do_syncrep2 : %s\n", errstr, 0, 0 );
846 slap_sync_cookie_free( &syncCookie, 0 );
847 slap_sync_cookie_free( &syncCookie_req, 0 );
849 if ( res ) ldap_msgfree( res );
851 if ( rc && si->si_ld ) {
852 ldap_unbind( si->si_ld );
864 struct re_s* rtask = arg;
865 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
866 Connection conn = {0};
868 int rc = LDAP_SUCCESS;
875 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
880 switch( abs( si->si_type )) {
881 case LDAP_SYNC_REFRESH_ONLY:
882 case LDAP_SYNC_REFRESH_AND_PERSIST:
888 if ( SLAPD_GLOBAL(shutdown) && si->si_ld ) {
889 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
890 connection_client_stop( s );
891 ldap_unbind( si->si_ld );
896 connection_fake_init( &conn, &op, ctx );
898 /* use global malloc for now */
899 op.o_tmpmemctx = NULL;
900 op.o_tmpmfuncs = &ch_mfuncs;
902 op.o_dn = si->si_updatedn;
903 op.o_ndn = si->si_updatedn;
904 op.o_managedsait = 1;
905 op.o_bd = be = si->si_be;
907 op.o_sync_state.ctxcsn = NULL;
908 op.o_sync_state.sid = -1;
909 op.o_sync_state.octet_str = NULL;
910 op.o_sync_slog_size = -1;
911 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
912 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
914 /* Establish session, do search */
917 rc = do_syncrep1( &op, si );
920 /* Process results */
921 if ( rc == LDAP_SUCCESS ) {
922 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
924 rc = do_syncrep2( &op, si );
926 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
927 /* If we succeeded, enable the connection for further listening.
928 * If we failed, tear down the connection and reschedule.
930 if ( rc == LDAP_SUCCESS ) {
932 rc = connection_client_setup( s, do_syncrepl, arg );
934 connection_client_enable( s );
936 } else if ( !first ) {
940 if ( rc == -2 ) rc = 0;
944 /* At this point, we have 4 cases:
945 * 1) for any hard failure, give up and remove this task
946 * 2) for ServerDown, reschedule this task to run
947 * 3) for Refresh and Success, reschedule to run
948 * 4) for Persist and Success, reschedule to defer
950 ldap_pvt_thread_mutex_lock( &SLAPD_GLOBAL(runqueue).rq_mutex );
952 if ( ldap_pvt_runqueue_isrunning( &SLAPD_GLOBAL(runqueue), rtask )) {
953 ldap_pvt_runqueue_stoptask( &SLAPD_GLOBAL(runqueue), rtask );
957 connection_client_stop( s );
960 if ( rc == LDAP_SUCCESS ) {
961 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
964 rtask->interval.tv_sec = si->si_interval;
965 ldap_pvt_runqueue_resched( &SLAPD_GLOBAL(runqueue), rtask, defer );
966 if ( si->si_retrynum ) {
967 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
968 si->si_retrynum[i] = si->si_retrynum_init[i];
970 si->si_retrynum[i] = -2;
973 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
974 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
978 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
979 ldap_pvt_runqueue_remove( &SLAPD_GLOBAL(runqueue), rtask );
980 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
982 } else if ( si->si_retrynum[i] >= -1 ) {
983 if ( si->si_retrynum[i] > 0 )
984 si->si_retrynum[i]--;
985 rtask->interval.tv_sec = si->si_retryinterval[i];
986 ldap_pvt_runqueue_resched( &SLAPD_GLOBAL(runqueue), rtask, 0 );
987 slap_wake_listener();
991 ldap_pvt_thread_mutex_unlock( &SLAPD_GLOBAL(runqueue).rq_mutex );
997 syncrepl_message_to_entry(
1001 Modifications **modlist,
1007 BerElement *ber = NULL;
1010 Modifications **modtail = modlist;
1013 char txtbuf[SLAP_TEXT_BUFLEN];
1014 size_t textlen = sizeof txtbuf;
1016 struct berval bdn = {0, NULL}, dn, ndn;
1021 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1022 Debug( LDAP_DEBUG_ANY,
1023 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1027 op->o_tag = LDAP_REQ_ADD;
1029 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1031 if ( rc != LDAP_SUCCESS ) {
1032 Debug( LDAP_DEBUG_ANY,
1033 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1037 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1038 ber_dupbv( &op->o_req_dn, &dn );
1039 ber_dupbv( &op->o_req_ndn, &ndn );
1040 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1041 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1043 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1046 return LDAP_SUCCESS;
1049 if ( entry == NULL ) {
1053 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1055 e->e_name = op->o_req_dn;
1056 e->e_nname = op->o_req_ndn;
1058 while ( ber_remaining( ber ) ) {
1059 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1060 LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1065 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1067 mod->sml_op = LDAP_MOD_REPLACE;
1068 mod->sml_next = NULL;
1069 mod->sml_desc = NULL;
1070 mod->sml_type = tmp.sml_type;
1071 mod->sml_values = tmp.sml_values;
1072 mod->sml_nvalues = NULL;
1075 modtail = &mod->sml_next;
1078 if ( *modlist == NULL ) {
1079 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1085 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1087 if ( rc != LDAP_SUCCESS ) {
1088 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1093 /* Strip out dynamically generated attrs */
1094 for ( modtail = modlist; *modtail ; ) {
1096 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1097 *modtail = mod->sml_next;
1098 slap_mod_free( &mod->sml_mod, 0 );
1101 modtail = &mod->sml_next;
1105 /* Strip out attrs in exattrs list */
1106 for ( modtail = modlist; *modtail ; ) {
1108 if ( ldap_charray_inlist( si->si_exattrs,
1109 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1110 *modtail = mod->sml_next;
1111 slap_mod_free( &mod->sml_mod, 0 );
1114 modtail = &mod->sml_next;
1118 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1119 if( rc != LDAP_SUCCESS ) {
1120 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1125 ber_free ( ber, 0 );
1126 if ( rc != LDAP_SUCCESS ) {
1141 Modifications* modlist,
1143 struct berval* syncUUID,
1144 struct sync_cookie* syncCookie_req )
1146 Backend *be = op->o_bd;
1147 slap_callback cb = { NULL };
1148 struct berval *syncuuid_bv = NULL;
1149 struct berval syncUUID_strrep = BER_BVNULL;
1150 struct berval uuid_bv = BER_BVNULL;
1152 SlapReply rs_search = {REP_RESULT};
1153 SlapReply rs_delete = {REP_RESULT};
1154 SlapReply rs_add = {REP_RESULT};
1155 SlapReply rs_modify = {REP_RESULT};
1157 AttributeAssertion ava = {0};
1158 int rc = LDAP_SUCCESS;
1159 int ret = LDAP_SUCCESS;
1162 struct berval pdn = BER_BVNULL;
1163 struct berval org_req_dn = BER_BVNULL;
1164 struct berval org_req_ndn = BER_BVNULL;
1165 struct berval org_dn = BER_BVNULL;
1166 struct berval org_ndn = BER_BVNULL;
1167 int org_managedsait;
1169 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1170 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1171 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1172 syncuuid_cmp, avl_dup_error );
1175 if ( syncstate == LDAP_SYNC_PRESENT ) {
1177 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1178 if ( entry == NULL ) {
1183 f.f_choice = LDAP_FILTER_EQUALITY;
1185 ava.aa_desc = slap_schema.si_ad_entryUUID;
1186 (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1187 ava.aa_value = *syncUUID;
1188 op->ors_filter = &f;
1190 op->ors_filterstr.bv_len = STRLENOF( "entryUUID=" ) + syncUUID->bv_len;
1191 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1192 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1193 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", STRLENOF( "entryUUID=" ) );
1194 AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "entryUUID=" )],
1195 syncUUID->bv_val, syncUUID->bv_len );
1196 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1198 op->o_tag = LDAP_REQ_SEARCH;
1199 op->ors_scope = LDAP_SCOPE_SUBTREE;
1201 /* get syncrepl cookie of shadow replica from subentry */
1202 op->o_req_dn = si->si_base;
1203 op->o_req_ndn = si->si_base;
1205 op->o_time = slap_get_time();
1206 op->ors_tlimit = SLAP_NO_LIMIT;
1209 op->ors_attrs = slap_anlist_no_attrs;
1210 op->ors_attrsonly = 1;
1212 /* set callback function */
1213 op->o_callback = &cb;
1214 cb.sc_response = dn_callback;
1217 BER_BVZERO( &si->si_syncUUID_ndn );
1219 if ( limits_check( op, &rs_search ) == 0 ) {
1220 rc = be->be_search( op, &rs_search );
1223 if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1224 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1227 cb.sc_response = null_callback;
1230 if ( rs_search.sr_err == LDAP_SUCCESS && !BER_BVISNULL( &si->si_syncUUID_ndn ) )
1234 if ( syncstate != LDAP_SYNC_DELETE ) {
1235 op->o_no_psearch = 1;
1238 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1239 if ( !BER_BVISNULL( &op->o_sync_csn ) ) {
1240 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1245 op->o_req_dn = si->si_syncUUID_ndn;
1246 op->o_req_ndn = si->si_syncUUID_ndn;
1247 op->o_tag = LDAP_REQ_DELETE;
1248 rc = be->be_delete( op, &rs_delete );
1250 org_req_dn = op->o_req_dn;
1251 org_req_ndn = op->o_req_ndn;
1253 org_ndn = op->o_ndn;
1254 org_managedsait = get_manageDSAit( op );
1255 op->o_dn = op->o_bd->be_rootdn;
1256 op->o_ndn = op->o_bd->be_rootndn;
1257 op->o_managedsait = 1;
1259 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1260 op->o_delete_glue_parent = 0;
1261 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1262 slap_callback cb = { NULL };
1263 cb.sc_response = slap_null_cb;
1264 dnParent( &op->o_req_ndn, &pdn );
1266 op->o_req_ndn = pdn;
1267 op->o_callback = &cb;
1268 op->o_bd->be_delete( op, &rs_delete );
1274 op->o_managedsait = org_managedsait;
1276 op->o_ndn = org_ndn;
1277 op->o_req_dn = org_req_dn;
1278 op->o_req_ndn = org_req_ndn;
1279 op->o_delete_glue_parent = 0;
1281 op->o_no_psearch = 0;
1284 switch ( syncstate ) {
1286 case LDAP_SYNC_MODIFY:
1287 if ( rs_search.sr_err == LDAP_SUCCESS ||
1288 rs_search.sr_err == LDAP_REFERRAL ||
1289 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1290 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1292 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1293 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1294 &syncUUID_strrep, syncUUID );
1296 op->o_tag = LDAP_REQ_ADD;
1298 op->o_req_dn = entry->e_name;
1299 op->o_req_ndn = entry->e_nname;
1301 rc = be->be_add( op, &rs_add );
1303 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1304 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1305 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1307 Modifications *modtail = modlist;
1311 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1315 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1316 ber_dupbv( &uuid_bv, syncUUID );
1317 mod->sml_op = LDAP_MOD_REPLACE;
1318 mod->sml_desc = slap_schema.si_ad_entryUUID;
1319 mod->sml_type = mod->sml_desc->ad_cname;
1320 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1321 modtail->sml_next = mod;
1323 op->o_tag = LDAP_REQ_MODIFY;
1324 op->orm_modlist = modlist;
1325 op->o_req_dn = entry->e_name;
1326 op->o_req_ndn = entry->e_nname;
1328 rc = be->be_modify( op, &rs_modify );
1329 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1330 Debug( LDAP_DEBUG_ANY,
1331 "syncrepl_entry : be_modify failed (%d)\n",
1332 rs_modify.sr_err, 0, 0 );
1336 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1337 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1338 syncrepl_add_glue( op, entry );
1342 Debug( LDAP_DEBUG_ANY,
1343 "syncrepl_entry : be_add failed (%d)\n",
1344 rs_add.sr_err, 0, 0 );
1349 be_entry_release_w( op, entry );
1354 Debug( LDAP_DEBUG_ANY,
1355 "syncrepl_entry : be_search failed (%d)\n",
1356 rs_search.sr_err, 0, 0 );
1361 case LDAP_SYNC_DELETE :
1362 /* Already deleted */
1367 Debug( LDAP_DEBUG_ANY,
1368 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1375 if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1376 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1377 BER_BVZERO( &syncUUID_strrep );
1379 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1380 ch_free( si->si_syncUUID_ndn.bv_val );
1381 BER_BVZERO( &si->si_syncUUID_ndn );
1386 static struct berval gcbva[] = {
1393 syncrepl_del_nonpresent(
1397 Backend* be = op->o_bd;
1398 slap_callback cb = { NULL };
1399 SlapReply rs_search = {REP_RESULT};
1400 SlapReply rs_delete = {REP_RESULT};
1401 SlapReply rs_modify = {REP_RESULT};
1402 struct nonpresent_entry *np_list, *np_prev;
1405 Modifications *mlnext;
1407 Modifications *modlist = NULL;
1408 Modifications **modtail = &modlist;
1410 AttributeName an[2];
1412 struct berval pdn = BER_BVNULL;
1413 struct berval org_req_dn = BER_BVNULL;
1414 struct berval org_req_ndn = BER_BVNULL;
1415 struct berval org_dn = BER_BVNULL;
1416 struct berval org_ndn = BER_BVNULL;
1417 int org_managedsait;
1419 op->o_req_dn = si->si_base;
1420 op->o_req_ndn = si->si_base;
1422 cb.sc_response = nonpresent_callback;
1425 op->o_callback = &cb;
1426 op->o_tag = LDAP_REQ_SEARCH;
1427 op->ors_scope = si->si_scope;
1428 op->ors_deref = LDAP_DEREF_NEVER;
1429 op->o_time = slap_get_time();
1430 op->ors_tlimit = SLAP_NO_LIMIT;
1431 op->ors_slimit = SLAP_NO_LIMIT;
1433 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1434 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1435 an[0].an_desc = slap_schema.si_ad_entryUUID;
1438 op->ors_attrsonly = 0;
1439 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1440 op->ors_filterstr = si->si_filterstr;
1442 op->o_nocaching = 1;
1443 op->o_managedsait = 0;
1445 if ( limits_check( op, &rs_search ) == 0 ) {
1446 rc = be->be_search( op, &rs_search );
1449 op->o_managedsait = 1;
1450 op->o_nocaching = 0;
1452 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1454 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1455 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1456 while ( np_list != NULL ) {
1457 LDAP_LIST_REMOVE( np_list, npe_link );
1459 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1460 op->o_tag = LDAP_REQ_DELETE;
1461 op->o_callback = &cb;
1462 cb.sc_response = null_callback;
1464 op->o_req_dn = *np_prev->npe_name;
1465 op->o_req_ndn = *np_prev->npe_nname;
1466 rc = op->o_bd->be_delete( op, &rs_delete );
1468 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1469 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1470 mod->sml_op = LDAP_MOD_REPLACE;
1471 mod->sml_desc = slap_schema.si_ad_objectClass;
1472 mod->sml_type = mod->sml_desc->ad_cname;
1473 mod->sml_values = &gcbva[0];
1475 modtail = &mod->sml_next;
1477 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1478 mod->sml_op = LDAP_MOD_REPLACE;
1479 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1480 mod->sml_type = mod->sml_desc->ad_cname;
1481 mod->sml_values = &gcbva[1];
1483 modtail = &mod->sml_next;
1485 op->o_tag = LDAP_REQ_MODIFY;
1486 op->orm_modlist = modlist;
1488 rc = be->be_modify( op, &rs_modify );
1490 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1491 mlnext = ml->sml_next;
1496 org_req_dn = op->o_req_dn;
1497 org_req_ndn = op->o_req_ndn;
1499 org_ndn = op->o_ndn;
1500 org_managedsait = get_manageDSAit( op );
1501 op->o_dn = op->o_bd->be_rootdn;
1502 op->o_ndn = op->o_bd->be_rootndn;
1503 op->o_managedsait = 1;
1505 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1506 op->o_delete_glue_parent ) {
1507 op->o_delete_glue_parent = 0;
1508 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1509 slap_callback cb = { NULL };
1510 cb.sc_response = slap_null_cb;
1511 dnParent( &op->o_req_ndn, &pdn );
1513 op->o_req_ndn = pdn;
1514 op->o_callback = &cb;
1515 /* give it a root privil ? */
1516 op->o_bd->be_delete( op, &rs_delete );
1522 op->o_managedsait = org_managedsait;
1524 op->o_ndn = org_ndn;
1525 op->o_req_dn = org_req_dn;
1526 op->o_req_ndn = org_req_ndn;
1527 op->o_delete_glue_parent = 0;
1529 ber_bvfree( np_prev->npe_name );
1530 ber_bvfree( np_prev->npe_nname );
1531 BER_BVZERO( &op->o_req_dn );
1532 BER_BVZERO( &op->o_req_ndn );
1545 Backend *be = op->o_bd;
1546 slap_callback cb = { NULL };
1551 struct berval dn = {0, NULL};
1552 struct berval ndn = {0, NULL};
1554 SlapReply rs_add = {REP_RESULT};
1557 op->o_tag = LDAP_REQ_ADD;
1558 op->o_callback = &cb;
1559 cb.sc_response = null_callback;
1560 cb.sc_private = NULL;
1565 /* count RDNs in suffix */
1566 if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
1567 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
1577 /* Start with BE suffix */
1578 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1579 comma = strrchr( dn.bv_val, ',' );
1580 if ( ptr ) *ptr = ',';
1581 if ( comma ) *comma = '\0';
1586 dn.bv_len -= ptr - dn.bv_val;
1589 /* the normalizedDNs are always the same length, no counting
1592 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1593 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1594 ndn.bv_len = be->be_nsuffix[0].bv_len;
1597 while ( ndn.bv_val > e->e_nname.bv_val ) {
1598 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1599 ber_dupbv( &glue->e_name, &dn );
1600 ber_dupbv( &glue->e_nname, &ndn );
1602 a = ch_calloc( 1, sizeof( Attribute ));
1603 a->a_desc = slap_schema.si_ad_objectClass;
1605 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1606 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1607 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1608 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1610 a->a_nvals = a->a_vals;
1612 a->a_next = glue->e_attrs;
1615 a = ch_calloc( 1, sizeof( Attribute ));
1616 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1618 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1619 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1620 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1622 a->a_nvals = a->a_vals;
1624 a->a_next = glue->e_attrs;
1627 op->o_req_dn = glue->e_name;
1628 op->o_req_ndn = glue->e_nname;
1630 rc = be->be_add ( op, &rs_add );
1631 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1632 be_entry_release_w( op, glue );
1634 /* incl. ALREADY EXIST */
1638 /* Move to next child */
1639 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1642 if ( ptr == e->e_name.bv_val ) break;
1644 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1645 for( ptr = ndn.bv_val-2;
1646 ptr > e->e_nname.bv_val && *ptr != ',';
1652 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1655 op->o_req_dn = e->e_name;
1656 op->o_req_ndn = e->e_nname;
1658 rc = be->be_add ( op, &rs_add );
1659 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1660 be_entry_release_w( op, e );
1668 static struct berval ocbva[] = {
1670 BER_BVC("subentry"),
1671 BER_BVC("syncConsumerSubentry"),
1675 static struct berval cnbva[] = {
1680 static struct berval ssbva[] = {
1685 static struct berval scbva[] = {
1691 syncrepl_updateCookie(
1695 struct sync_cookie *syncCookie )
1697 Backend *be = op->o_bd;
1699 Modifications *mlnext;
1701 Modifications *modlist = NULL;
1702 Modifications **modtail = &modlist;
1705 char txtbuf[SLAP_TEXT_BUFLEN];
1706 size_t textlen = sizeof txtbuf;
1711 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1712 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1713 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1715 slap_callback cb = { NULL };
1716 SlapReply rs_add = {REP_RESULT};
1717 SlapReply rs_modify = {REP_RESULT};
1719 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1720 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1722 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1723 mod->sml_op = LDAP_MOD_REPLACE;
1724 mod->sml_desc = slap_schema.si_ad_objectClass;
1725 mod->sml_type = mod->sml_desc->ad_cname;
1726 mod->sml_values = ocbva;
1728 modtail = &mod->sml_next;
1730 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1731 assert( si->si_rid < 1000 );
1732 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1733 slap_syncrepl_bvc.bv_len + 1,
1734 "syncrepl%ld", si->si_rid );
1735 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1736 mod->sml_op = LDAP_MOD_REPLACE;
1737 mod->sml_desc = slap_schema.si_ad_cn;
1738 mod->sml_type = mod->sml_desc->ad_cname;
1739 mod->sml_values = cnbva;
1741 modtail = &mod->sml_next;
1743 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1744 mod->sml_op = LDAP_MOD_REPLACE;
1745 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1746 mod->sml_type = mod->sml_desc->ad_cname;
1747 mod->sml_values = ssbva;
1749 modtail = &mod->sml_next;
1751 /* Keep this last, so we can avoid touching the previous
1752 * attributes unnecessarily.
1754 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1755 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1756 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1757 mod->sml_op = LDAP_MOD_REPLACE;
1758 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1759 mod->sml_type = mod->sml_desc->ad_cname;
1760 mod->sml_values = scbva;
1762 modtail = &mod->sml_next;
1766 op->o_tag = LDAP_REQ_ADD;
1767 rc = slap_mods_opattrs( op, modlist, modtail,
1768 &text, txtbuf, textlen, 0 );
1770 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1771 ml->sml_op = LDAP_MOD_REPLACE;
1774 if( rc != LDAP_SUCCESS ) {
1775 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1779 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1781 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1782 assert( si->si_rid < 1000 );
1783 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1784 slap_syncrepl_cn_bvc.bv_len + 1,
1785 "cn=syncrepl%ld", si->si_rid );
1787 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1789 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1790 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1792 if ( !BER_BVISNULL( &slap_syncrepl_dn_bv ) ) {
1793 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1798 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1800 if( rc != LDAP_SUCCESS ) {
1801 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1805 cb.sc_response = null_callback;
1808 op->o_callback = &cb;
1809 op->o_req_dn = e->e_name;
1810 op->o_req_ndn = e->e_nname;
1812 /* update persistent cookie */
1813 update_cookie_retry:
1814 op->o_tag = LDAP_REQ_MODIFY;
1815 /* Just modify the cookie value, not the entire entry */
1816 op->orm_modlist = mod;
1817 rc = be->be_modify( op, &rs_modify );
1819 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1820 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1821 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1822 op->o_tag = LDAP_REQ_ADD;
1824 rc = be->be_add( op, &rs_add );
1825 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1826 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1827 goto update_cookie_retry;
1828 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1829 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1830 Debug( LDAP_DEBUG_ANY,
1831 "cookie will be non-persistent\n",
1834 Debug( LDAP_DEBUG_ANY,
1835 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1838 be_entry_release_w( op, e );
1842 Debug( LDAP_DEBUG_ANY,
1843 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1853 if ( !BER_BVISNULL( &cnbva[0] ) ) {
1854 ch_free( cnbva[0].bv_val );
1855 BER_BVZERO( &cnbva[0] );
1857 if ( !BER_BVISNULL( &scbva[0] ) ) {
1858 ch_free( scbva[0].bv_val );
1859 BER_BVZERO( &scbva[0] );
1862 if ( mlnext->sml_next ) {
1863 slap_mods_free( mlnext->sml_next );
1864 mlnext->sml_next = NULL;
1867 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1868 mlnext = ml->sml_next;
1876 syncrepl_isupdate( Operation *op )
1878 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1882 syncrepl_isupdate_dn(
1884 struct berval* ndn )
1889 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1890 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1891 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1904 syncinfo_t *si = op->o_callback->sc_private;
1906 if ( rs->sr_type == REP_SEARCH ) {
1907 if ( !BER_BVISNULL( &si->si_syncUUID_ndn ) ) {
1908 Debug( LDAP_DEBUG_ANY,
1909 "dn_callback : consistency error - "
1910 "entryUUID is not unique\n", 0, 0, 0 );
1912 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1914 } else if ( rs->sr_type == REP_RESULT ) {
1915 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1916 Debug( LDAP_DEBUG_ANY,
1917 "dn_callback : consistency error - "
1918 "entryUUID is not unique\n", 0, 0, 0 );
1922 return LDAP_SUCCESS;
1926 nonpresent_callback(
1930 syncinfo_t *si = op->o_callback->sc_private;
1933 struct berval* present_uuid = NULL;
1934 struct nonpresent_entry *np_entry;
1936 if ( rs->sr_type == REP_RESULT ) {
1937 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1938 si->si_presentlist = NULL;
1940 } else if ( rs->sr_type == REP_SEARCH ) {
1941 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1943 if ( a == NULL ) return 0;
1945 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1948 if ( present_uuid == NULL ) {
1949 np_entry = (struct nonpresent_entry *)
1950 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1951 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1952 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1953 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1956 avl_delete( &si->si_presentlist,
1957 &a->a_nvals[0], syncuuid_cmp );
1958 ch_free( present_uuid->bv_val );
1959 ch_free( present_uuid );
1962 return LDAP_SUCCESS;
1970 if ( rs->sr_err != LDAP_SUCCESS &&
1971 rs->sr_err != LDAP_REFERRAL &&
1972 rs->sr_err != LDAP_ALREADY_EXISTS &&
1973 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1974 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1976 Debug( LDAP_DEBUG_ANY,
1977 "null_callback : error code 0x%x\n",
1980 return LDAP_SUCCESS;
1984 slap_create_syncrepl_entry(
1986 struct berval *context_csn,
1994 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1996 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1998 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2001 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2003 if ( context_csn ) {
2004 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2005 context_csn, NULL );
2008 BER_BVSTR( &bv, "{}" );
2009 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2011 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2012 ber_dupbv( &e->e_nname, &e->e_name );
2018 slap_uuidstr_from_normalized(
2019 struct berval* uuidstr,
2020 struct berval* normalized,
2024 unsigned char nibble;
2027 if ( normalized == NULL ) return NULL;
2028 if ( normalized->bv_len != 16 ) return NULL;
2033 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2034 if ( new == NULL ) {
2041 if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2042 if ( new != uuidstr ) {
2043 slap_sl_free( new, ctx );
2048 for ( i = 0; i < 16; i++ ) {
2049 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2050 new->bv_val[(i<<1)+d] = '-';
2054 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2055 if ( nibble < 10 ) {
2056 new->bv_val[(i<<1)+d] = nibble + '0';
2058 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2061 nibble = (normalized->bv_val[i]) & 0xF;
2062 if ( nibble < 10 ) {
2063 new->bv_val[(i<<1)+d+1] = nibble + '0';
2065 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2069 new->bv_val[new->bv_len] = '\0';
2074 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2076 const struct berval *uuid1 = v_uuid1;
2077 const struct berval *uuid2 = v_uuid2;
2078 int rc = uuid1->bv_len - uuid2->bv_len;
2079 if ( rc ) return rc;
2080 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2084 avl_ber_bvfree( void *v_bv )
2086 struct berval *bv = (struct berval *)v_bv;
2088 if( v_bv == NULL ) return;
2089 if ( !BER_BVISNULL( bv ) ) {
2090 ch_free( bv->bv_val );
2092 ch_free( (char *) bv );
2096 syncinfo_free( syncinfo_t *sie )
2098 if ( sie->si_provideruri ) {
2099 ch_free( sie->si_provideruri );
2101 if ( sie->si_provideruri_bv ) {
2102 ber_bvarray_free( sie->si_provideruri_bv );
2104 if ( sie->si_updatedn.bv_val ) {
2105 ch_free( sie->si_updatedn.bv_val );
2107 if ( sie->si_binddn ) {
2108 ch_free( sie->si_binddn );
2110 if ( sie->si_passwd ) {
2111 ch_free( sie->si_passwd );
2113 if ( sie->si_saslmech ) {
2114 ch_free( sie->si_saslmech );
2116 if ( sie->si_secprops ) {
2117 ch_free( sie->si_secprops );
2119 if ( sie->si_realm ) {
2120 ch_free( sie->si_realm );
2122 if ( sie->si_authcId ) {
2123 ch_free( sie->si_authcId );
2125 if ( sie->si_authzId ) {
2126 ch_free( sie->si_authzId );
2128 if ( sie->si_filterstr.bv_val ) {
2129 ch_free( sie->si_filterstr.bv_val );
2131 if ( sie->si_base.bv_val ) {
2132 ch_free( sie->si_base.bv_val );
2134 if ( sie->si_attrs ) {
2136 while ( sie->si_attrs[i] != NULL ) {
2137 ch_free( sie->si_attrs[i] );
2140 ch_free( sie->si_attrs );
2142 if ( sie->si_exattrs ) {
2144 while ( sie->si_exattrs[i] != NULL ) {
2145 ch_free( sie->si_exattrs[i] );
2148 ch_free( sie->si_exattrs );
2150 if ( sie->si_anlist ) {
2152 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2153 ch_free( sie->si_anlist[i].an_name.bv_val );
2156 ch_free( sie->si_anlist );
2158 if ( sie->si_exanlist ) {
2160 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2161 ch_free( sie->si_exanlist[i].an_name.bv_val );
2164 ch_free( sie->si_exanlist );
2166 if ( sie->si_retryinterval ) {
2167 ch_free( sie->si_retryinterval );
2169 if ( sie->si_retrynum ) {
2170 ch_free( sie->si_retrynum );
2172 if ( sie->si_retrynum_init ) {
2173 ch_free( sie->si_retrynum_init );
2175 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2176 if ( sie->si_syncUUID_ndn.bv_val ) {
2177 ch_free( sie->si_syncUUID_ndn.bv_val );
2179 if ( sie->si_presentlist ) {
2180 avl_free( sie->si_presentlist, avl_ber_bvfree );
2183 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2185 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2186 struct nonpresent_entry* npe;
2187 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2188 LDAP_LIST_REMOVE( npe, npe_link );
2189 if ( npe->npe_name ) {
2190 if ( npe->npe_name->bv_val ) {
2191 ch_free( npe->npe_name->bv_val );
2193 ch_free( npe->npe_name );
2195 if ( npe->npe_nname ) {
2196 if ( npe->npe_nname->bv_val ) {
2197 ch_free( npe->npe_nname->bv_val );
2199 ch_free( npe->npe_nname );