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>
29 #include "lutil_ldap.h"
33 #define SYNCREPL_STR "syncreplxxx"
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
48 static int si_refreshDelete = 0;
49 static int si_refreshPresent = 0;
51 static AttributeDescription *sync_descs[4];
53 struct runqueue_s syncrepl_rq;
56 init_syncrepl(syncinfo_t *si)
61 if ( !sync_descs[0] ) {
62 sync_descs[0] = slap_schema.si_ad_objectClass;
63 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
64 sync_descs[2] = slap_schema.si_ad_entryCSN;
68 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
71 /* Delete Attributes */
72 for ( i = 0; sync_descs[i] != NULL; i++ ) {
73 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
74 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
77 ch_free( si->si_attrs[j] );
78 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
79 si->si_attrs[k] = si->si_attrs[k+1];
84 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
85 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
88 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
90 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
95 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
96 tmp[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
101 tmp = ( char ** ) ch_realloc( si->si_attrs, 3 * sizeof( char * ));
104 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
106 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
109 tmp[ n++ ] = ch_strdup( "*" );
110 tmp[ n++ ] = ch_strdup( "+" );
116 for ( n = 0; si->si_exattrs[ n ] != NULL; n++ ) /* empty */;
118 /* Delete Attributes from exattrs list */
119 for ( i = 0; sync_descs[i] != NULL; i++ ) {
120 for ( j = 0; si->si_exattrs[j] != NULL; j++ ) {
121 if ( strcmp( si->si_exattrs[j], sync_descs[i]->ad_cname.bv_val )
124 ch_free( si->si_exattrs[j] );
125 for ( k = j; si->si_exattrs[k] != NULL; k++ ) {
126 si->si_exattrs[k] = si->si_exattrs[k+1];
140 BerElementBuffer berbuf;
141 BerElement *ber = (BerElement *)&berbuf;
142 LDAPControl c[2], *ctrls[3];
143 struct timeval timeout;
147 /* setup LDAP SYNC control */
148 ber_init2( ber, NULL, LBER_USE_DER );
149 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
151 if ( si->si_syncCookie.octet_str &&
152 si->si_syncCookie.octet_str[0].bv_val ) {
153 ber_printf( ber, "{eO}", abs(si->si_type),
154 &si->si_syncCookie.octet_str[0] );
156 ber_printf( ber, "{e}", abs(si->si_type) );
159 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
164 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
165 c[0].ldctl_iscritical = si->si_type < 0;
168 if ( si->si_authzId ) {
169 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
170 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
171 c[1].ldctl_iscritical = 1;
178 timeout.tv_sec = si->si_tlimit;
181 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
182 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
183 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
184 si->si_slimit, &msgid );
196 int cmdline_cookie_found = 0;
198 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
199 struct berval syncrepl_cn_bv;
200 struct sync_cookie *sc = NULL;
201 struct sync_cookie syncCookie = { NULL, -1, NULL };
207 psub = &si->si_be->be_nsuffix[0];
209 /* Init connection to master */
211 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
212 if ( rc != LDAP_SUCCESS ) {
214 LDAP_LOG( OPERATION, ERR,
215 "do_syncrep1: ldap_initialize failed (%s)\n",
216 si->si_provideruri, 0, 0 );
218 Debug( LDAP_DEBUG_ANY,
219 "do_syncrep1: ldap_initialize failed (%s)\n",
220 si->si_provideruri, 0, 0 );
225 op->o_protocol = LDAP_VERSION3;
226 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
231 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
232 if( rc != LDAP_SUCCESS ) {
234 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
235 "%s: ldap_start_tls failed (%d)\n",
236 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
239 Debug( LDAP_DEBUG_ANY,
240 "%s: ldap_start_tls failed (%d)\n",
241 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
244 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
248 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
249 #ifdef HAVE_CYRUS_SASL
252 if ( si->si_secprops != NULL ) {
253 rc = ldap_set_option( si->si_ld,
254 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
256 if( rc != LDAP_OPT_SUCCESS ) {
258 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
259 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
260 si->si_provideruri, si->si_secprops, 0 );
262 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
263 "(%s,SECPROPS,\"%s\") failed!\n",
264 si->si_provideruri, si->si_secprops, 0 );
270 defaults = lutil_sasl_defaults( si->si_ld,
271 si->si_saslmech, si->si_realm,
272 si->si_authcId, si->si_passwd, si->si_authzId );
274 rc = ldap_sasl_interactive_bind_s( si->si_ld,
282 lutil_sasl_freedefs( defaults );
284 /* FIXME: different error behaviors according to
286 * 2) on err policy : exit, retry, backoff ...
288 if ( rc != LDAP_SUCCESS ) {
290 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
291 "ldap_sasl_interactive_bind_s failed (%d)\n",
294 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
295 "ldap_sasl_interactive_bind_s failed (%d)\n",
299 /* FIXME (see above comment) */
300 /* if Kerberos credentials cache is not active, retry */
301 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
302 rc == LDAP_LOCAL_ERROR )
304 rc = LDAP_SERVER_DOWN;
309 #else /* HAVE_CYRUS_SASL */
310 /* Should never get here, we trapped this at config time */
311 fprintf( stderr, "not compiled with SASL support\n" );
316 rc = ldap_bind_s( si->si_ld,
317 si->si_binddn, si->si_passwd, si->si_bindmethod );
318 if ( rc != LDAP_SUCCESS ) {
320 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
321 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
323 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
324 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
330 /* Set SSF to strongest of TLS, SASL SSFs */
333 op->o_transport_ssf = 0;
335 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
336 == LDAP_SUCCESS && ssl != NULL )
338 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
340 #endif /* HAVE_TLS */
341 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
342 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
343 ? op->o_sasl_ssf : op->o_tls_ssf;
345 /* get syncrepl cookie of shadow replica from subentry */
347 assert( si->si_rid < 1000 );
348 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
349 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
350 CN_STR "syncrepl%ld", si->si_rid );
351 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
352 op->o_req_dn = op->o_req_ndn;
354 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
355 if ( si->si_rid == sc->rid ) {
356 cmdline_cookie_found = 1;
361 if ( cmdline_cookie_found ) {
362 /* cookie is supplied in the command line */
363 BerVarray cookie = NULL;
364 struct berval cookie_bv;
366 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
367 slap_sync_cookie_free( &si->si_syncCookie, 0 );
369 /* read stored cookie if it exists */
370 backend_attribute( op, NULL, &op->o_req_ndn,
371 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
374 /* no stored cookie */
375 if ( sc->ctxcsn == NULL ||
376 sc->ctxcsn->bv_val == NULL ) {
377 /* if cmdline cookie does not have ctxcsn */
378 /* component, set it to an initial value */
379 slap_init_sync_cookie_ctxcsn( sc );
381 slap_dup_sync_cookie( &si->si_syncCookie, sc );
382 slap_sync_cookie_free( sc, 1 );
386 struct berval newcookie = BER_BVNULL;
387 ber_dupbv( &cookie_bv, &cookie[0] );
388 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
389 slap_parse_sync_cookie( &si->si_syncCookie );
390 ber_bvarray_free( si->si_syncCookie.octet_str );
391 si->si_syncCookie.octet_str = NULL;
392 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
393 if ( sc->sid != -1 ) {
394 /* command line cookie wins */
395 si->si_syncCookie.sid = sc->sid;
397 if ( sc->ctxcsn != NULL ) {
398 /* command line cookie wins */
399 if ( si->si_syncCookie.ctxcsn ) {
400 ber_bvarray_free( si->si_syncCookie.ctxcsn );
401 si->si_syncCookie.ctxcsn = NULL;
403 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
404 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
406 if ( sc->rid != -1 ) {
407 /* command line cookie wins */
408 si->si_syncCookie.rid = sc->rid;
410 slap_sync_cookie_free( sc, 1 );
412 slap_compose_sync_cookie( NULL, &newcookie,
413 &si->si_syncCookie.ctxcsn[0],
414 si->si_syncCookie.sid, si->si_syncCookie.rid );
415 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
418 /* no command line cookie is specified */
419 if ( si->si_syncCookie.octet_str == NULL ) {
420 BerVarray cookie = NULL;
421 struct berval cookie_bv;
422 /* try to read stored cookie */
423 backend_attribute( op, NULL, &op->o_req_ndn,
424 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
426 ber_dupbv( &cookie_bv, &cookie[0] );
427 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
428 slap_parse_sync_cookie( &si->si_syncCookie );
429 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
434 rc = ldap_sync_search( si, op->o_tmpmemctx );
436 if( rc != LDAP_SUCCESS ) {
438 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
439 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
441 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
442 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
449 ldap_unbind( si->si_ld );
454 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
464 LDAPControl **rctrls = NULL;
467 BerElementBuffer berbuf;
468 BerElement *ber = (BerElement *)&berbuf;
470 LDAPMessage *res = NULL;
471 LDAPMessage *msg = NULL;
474 struct berval *retdata = NULL;
479 struct berval syncUUID = BER_BVNULL;
480 struct sync_cookie syncCookie = { NULL, -1, NULL };
481 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
482 struct berval cookie = BER_BVNULL;
490 Modifications *modlist = NULL;
495 struct timeval *tout_p = NULL;
496 struct timeval tout = { 0, 0 };
498 int refreshDeletes = 0;
500 BerVarray syncUUIDs = NULL;
503 if ( slapd_shutdown ) {
508 ber_init2( ber, NULL, LBER_USE_DER );
509 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
512 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
514 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
517 psub = &si->si_be->be_nsuffix[0];
519 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
521 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
527 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
530 if ( slapd_shutdown ) {
534 for( msg = ldap_first_message( si->si_ld, res );
536 msg = ldap_next_message( si->si_ld, msg ) )
538 switch( ldap_msgtype( msg ) ) {
539 case LDAP_RES_SEARCH_ENTRY:
540 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
541 /* we can't work without the control */
547 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
548 ber_scanf( ber, "{em", &syncstate, &syncUUID );
549 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
550 ber_scanf( ber, "m}", &cookie );
551 if ( cookie.bv_val ) {
552 struct berval tmp_bv;
553 ber_dupbv( &tmp_bv, &cookie );
554 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
556 if ( syncCookie.octet_str &&
557 syncCookie.octet_str[0].bv_val )
558 slap_parse_sync_cookie( &syncCookie );
560 if ( syncrepl_message_to_entry( si, op, msg,
561 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
562 rc_efree = syncrepl_entry( si, op, entry, modlist,
563 syncstate, &syncUUID, &syncCookie_req );
564 if ( syncCookie.octet_str &&
565 syncCookie.octet_str[0].bv_val ) {
566 syncrepl_updateCookie( si, op, psub, &syncCookie );
569 ldap_controls_free( rctrls );
571 slap_mods_free( modlist );
573 if ( rc_efree && entry ) {
579 case LDAP_RES_SEARCH_REFERENCE:
581 LDAP_LOG( OPERATION, ERR,
582 "do_syncrep2: reference received error\n", 0, 0, 0 );
584 Debug( LDAP_DEBUG_ANY,
585 "do_syncrep2: reference received error\n", 0, 0, 0 );
589 case LDAP_RES_SEARCH_RESULT:
590 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
594 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
596 ber_scanf( ber, "{" /*"}"*/);
597 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
599 ber_scanf( ber, "m", &cookie );
600 if ( cookie.bv_val ) {
601 struct berval tmp_bv;
602 ber_dupbv( &tmp_bv, &cookie );
603 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
605 if ( syncCookie.octet_str &&
606 syncCookie.octet_str[0].bv_val )
607 slap_parse_sync_cookie( &syncCookie );
609 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
611 ber_scanf( ber, "b", &refreshDeletes );
613 ber_scanf( ber, "}" );
615 if ( syncCookie_req.ctxcsn == NULL ) {
617 } else if ( syncCookie.ctxcsn == NULL ) {
620 value_match( &match, slap_schema.si_ad_entryCSN,
621 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
622 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
623 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
625 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
626 && match < 0 && err == LDAP_SUCCESS ) {
627 syncrepl_updateCookie( si, op, psub, &syncCookie );
630 ldap_controls_free( rctrls );
632 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
633 /* FIXME : different error behaviors according to
634 * 1) err code : LDAP_BUSY ...
635 * 2) on err policy : stop service, stop sync, retry
637 if ( refreshDeletes == 0 && match < 0 &&
638 err == LDAP_SUCCESS ) {
639 syncrepl_del_nonpresent( op, si );
641 avl_free( si->si_presentlist, avl_ber_bvfree );
642 si->si_presentlist = NULL;
649 case LDAP_RES_INTERMEDIATE:
650 rc = ldap_parse_intermediate( si->si_ld, msg,
651 &retoid, &retdata, NULL, 0 );
652 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
653 ber_init2( ber, retdata, LBER_USE_DER );
655 switch ( si_tag = ber_peek_tag( ber, &len )) {
657 case LDAP_TAG_SYNC_NEW_COOKIE:
658 ber_scanf( ber, "tm", &tag, &cookie );
660 case LDAP_TAG_SYNC_REFRESH_DELETE:
661 si_refreshDelete = 1;
662 case LDAP_TAG_SYNC_REFRESH_PRESENT:
663 si_refreshPresent = 1;
664 si_refreshPresent = 1;
665 ber_scanf( ber, "t{", &tag );
666 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
668 ber_scanf( ber, "m", &cookie );
669 if ( cookie.bv_val ) {
670 struct berval tmp_bv;
671 ber_dupbv( &tmp_bv, &cookie );
672 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
674 if ( syncCookie.octet_str &&
675 syncCookie.octet_str[0].bv_val )
676 slap_parse_sync_cookie( &syncCookie );
678 if ( ber_peek_tag( ber, &len ) ==
679 LDAP_TAG_REFRESHDONE )
681 ber_scanf( ber, "b", &refreshDone );
683 ber_scanf( ber, "}" );
685 case LDAP_TAG_SYNC_ID_SET:
686 ber_scanf( ber, "t{", &tag );
687 if ( ber_peek_tag( ber, &len ) ==
688 LDAP_TAG_SYNC_COOKIE ) {
689 ber_scanf( ber, "m", &cookie );
690 if ( cookie.bv_val ) {
691 struct berval tmp_bv;
692 ber_dupbv( &tmp_bv, &cookie );
693 ber_bvarray_add( &syncCookie.octet_str,
696 if ( syncCookie.octet_str &&
697 syncCookie.octet_str[0].bv_val )
698 slap_parse_sync_cookie( &syncCookie );
700 if ( ber_peek_tag( ber, &len ) ==
701 LDAP_TAG_REFRESHDELETES )
703 ber_scanf( ber, "b", &refreshDeletes );
705 ber_scanf( ber, "[W]", &syncUUIDs );
706 ber_scanf( ber, "}" );
707 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
708 struct berval *syncuuid_bv;
709 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
710 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
711 avl_insert( &si->si_presentlist,
712 (caddr_t) syncuuid_bv,
713 syncuuid_cmp, avl_dup_error );
715 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
719 LDAP_LOG( OPERATION, ERR,
720 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
721 (long) si_tag, 0, 0 );
723 Debug( LDAP_DEBUG_ANY,
724 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
725 (long) si_tag, 0, 0 );
727 ldap_memfree( retoid );
728 ber_bvfree( retdata );
732 if ( syncCookie_req.ctxcsn == NULL ) {
734 } else if ( syncCookie.ctxcsn == NULL ) {
737 value_match( &match, slap_schema.si_ad_entryCSN,
738 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
739 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
740 &syncCookie_req.ctxcsn[0],
741 &syncCookie.ctxcsn[0], &text );
744 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
746 syncrepl_updateCookie( si, op, psub, &syncCookie);
749 if ( si_refreshPresent == 1 ) {
751 syncrepl_del_nonpresent( op, si );
755 ldap_memfree( retoid );
756 ber_bvfree( retdata );
760 LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
761 " unknown intermediate "
762 "response\n", 0, 0, 0 );
764 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
765 "unknown intermediate response (%d)\n",
768 ldap_memfree( retoid );
769 ber_bvfree( retdata );
775 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
776 "unknown message\n", 0, 0, 0 );
778 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
779 "unknown message\n", 0, 0, 0 );
784 if ( syncCookie.octet_str ) {
785 slap_sync_cookie_free( &syncCookie_req, 0 );
786 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
787 slap_sync_cookie_free( &syncCookie, 0 );
797 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
798 errstr = ldap_err2string( rc );
801 LDAP_LOG( OPERATION, ERR,
802 "do_syncrep2 : %s\n", errstr, 0, 0 );
804 Debug( LDAP_DEBUG_ANY,
805 "do_syncrep2 : %s\n", errstr, 0, 0 );
810 slap_sync_cookie_free( &syncCookie, 0 );
811 slap_sync_cookie_free( &syncCookie_req, 0 );
813 if ( res ) ldap_msgfree( res );
815 if ( rc && si->si_ld ) {
816 ldap_unbind( si->si_ld );
828 struct re_s* rtask = arg;
829 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
830 Connection conn = {0};
832 int rc = LDAP_SUCCESS;
840 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
842 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
848 switch( abs( si->si_type )) {
849 case LDAP_SYNC_REFRESH_ONLY:
850 case LDAP_SYNC_REFRESH_AND_PERSIST:
856 if ( slapd_shutdown && si->si_ld ) {
857 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
858 connection_client_stop( s );
859 ldap_unbind( si->si_ld );
864 connection_fake_init( &conn, &op, ctx );
866 /* use global malloc for now */
867 op.o_tmpmemctx = NULL;
868 op.o_tmpmfuncs = &ch_mfuncs;
870 op.o_dn = si->si_updatedn;
871 op.o_ndn = si->si_updatedn;
872 op.o_managedsait = 1;
873 op.o_bd = be = si->si_be;
875 op.o_sync_state.ctxcsn = NULL;
876 op.o_sync_state.sid = -1;
877 op.o_sync_state.octet_str = NULL;
878 op.o_sync_slog_size = -1;
879 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
880 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
882 /* Establish session, do search */
885 si_refreshDelete = 0;
886 si_refreshPresent = 0;
887 rc = do_syncrep1( &op, si );
890 /* Process results */
891 if ( rc == LDAP_SUCCESS ) {
892 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
894 rc = do_syncrep2( &op, si );
896 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
897 /* If we succeeded, enable the connection for further listening.
898 * If we failed, tear down the connection and reschedule.
900 if ( rc == LDAP_SUCCESS ) {
902 rc = connection_client_setup( s, do_syncrepl, arg );
904 connection_client_enable( s );
906 } else if ( !first ) {
910 if ( rc == -2 ) rc = 0;
914 /* At this point, we have 4 cases:
915 * 1) for any hard failure, give up and remove this task
916 * 2) for ServerDown, reschedule this task to run
917 * 3) for Refresh and Success, reschedule to run
918 * 4) for Persist and Success, reschedule to defer
920 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
922 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
923 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
927 connection_client_stop( s );
930 if ( rc == LDAP_SUCCESS ) {
931 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
934 rtask->interval.tv_sec = si->si_interval;
935 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
936 if ( si->si_retrynum ) {
937 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
938 si->si_retrynum[i] = si->si_retrynum_init[i];
940 si->si_retrynum[i] = -2;
943 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
944 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
948 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
949 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
950 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
952 } else if ( si->si_retrynum[i] >= -1 ) {
953 if ( si->si_retrynum[i] > 0 )
954 si->si_retrynum[i]--;
955 rtask->interval.tv_sec = si->si_retryinterval[i];
956 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
957 slap_wake_listener();
961 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
967 syncrepl_message_to_entry(
971 Modifications **modlist,
977 BerElement *ber = NULL;
980 Modifications **modtail = modlist;
983 char txtbuf[SLAP_TEXT_BUFLEN];
984 size_t textlen = sizeof txtbuf;
986 struct berval bdn = {0, NULL}, dn, ndn;
991 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
993 LDAP_LOG( OPERATION, ERR,
994 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
996 Debug( LDAP_DEBUG_ANY,
997 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1002 op->o_tag = LDAP_REQ_ADD;
1004 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1006 if ( rc != LDAP_SUCCESS ) {
1008 LDAP_LOG( OPERATION, ERR,
1009 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1011 Debug( LDAP_DEBUG_ANY,
1012 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1017 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1018 ber_dupbv( &op->o_req_dn, &dn );
1019 ber_dupbv( &op->o_req_ndn, &ndn );
1020 sl_free( ndn.bv_val, op->o_tmpmemctx );
1021 sl_free( dn.bv_val, op->o_tmpmemctx );
1023 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1026 return LDAP_SUCCESS;
1029 if ( entry == NULL ) {
1033 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1035 e->e_name = op->o_req_dn;
1036 e->e_nname = op->o_req_ndn;
1038 while ( ber_remaining( ber ) ) {
1039 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1040 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
1045 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1047 mod->sml_op = LDAP_MOD_REPLACE;
1048 mod->sml_next = NULL;
1049 mod->sml_desc = NULL;
1050 mod->sml_type = tmp.sml_type;
1051 mod->sml_values = tmp.sml_values;
1052 mod->sml_nvalues = NULL;
1055 modtail = &mod->sml_next;
1058 if ( *modlist == NULL ) {
1060 LDAP_LOG( OPERATION, ERR,
1061 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1063 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1070 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1072 if ( rc != LDAP_SUCCESS ) {
1074 LDAP_LOG( OPERATION, ERR,
1075 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1077 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1083 /* Strip out dynamically generated attrs */
1084 for ( modtail = modlist; *modtail ; ) {
1086 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1087 *modtail = mod->sml_next;
1088 slap_mod_free( &mod->sml_mod, 0 );
1091 modtail = &mod->sml_next;
1095 /* Strip out attrs in exattrs list */
1096 for ( modtail = modlist; *modtail ; ) {
1098 if ( ldap_charray_inlist( si->si_exattrs,
1099 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1100 *modtail = mod->sml_next;
1101 slap_mod_free( &mod->sml_mod, 0 );
1104 modtail = &mod->sml_next;
1108 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1109 if( rc != LDAP_SUCCESS ) {
1111 LDAP_LOG( OPERATION, ERR,
1112 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1114 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1120 ber_free ( ber, 0 );
1121 if ( rc != LDAP_SUCCESS ) {
1136 Modifications* modlist,
1138 struct berval* syncUUID,
1139 struct sync_cookie* syncCookie_req
1142 Backend *be = op->o_bd;
1143 slap_callback cb = { NULL };
1144 struct berval *syncuuid_bv = NULL;
1145 struct berval syncUUID_strrep = BER_BVNULL;
1146 struct berval uuid_bv = BER_BVNULL;
1148 SlapReply rs_search = {REP_RESULT};
1149 SlapReply rs_delete = {REP_RESULT};
1150 SlapReply rs_add = {REP_RESULT};
1151 SlapReply rs_modify = {REP_RESULT};
1153 AttributeAssertion ava = {0};
1154 int rc = LDAP_SUCCESS;
1155 int ret = LDAP_SUCCESS;
1158 struct berval pdn = BER_BVNULL;
1159 struct berval org_req_dn = BER_BVNULL;
1160 struct berval org_req_ndn = BER_BVNULL;
1161 struct berval org_dn = BER_BVNULL;
1162 struct berval org_ndn = BER_BVNULL;
1163 int org_managedsait;
1165 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1167 if (!si_refreshPresent) {
1168 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1169 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1170 syncuuid_cmp, avl_dup_error );
1174 if ( syncstate == LDAP_SYNC_PRESENT ) {
1176 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1177 if ( entry == NULL ) {
1182 f.f_choice = LDAP_FILTER_EQUALITY;
1184 ava.aa_desc = slap_schema.si_ad_entryUUID;
1185 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1186 ava.aa_value = *syncUUID;
1187 op->ors_filter = &f;
1189 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1190 op->ors_filterstr.bv_val = (char *) sl_malloc(
1191 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1192 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1193 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1194 syncUUID->bv_val, syncUUID->bv_len );
1195 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1197 op->o_tag = LDAP_REQ_SEARCH;
1198 op->ors_scope = LDAP_SCOPE_SUBTREE;
1200 /* get syncrepl cookie of shadow replica from subentry */
1201 op->o_req_dn = si->si_base;
1202 op->o_req_ndn = si->si_base;
1204 op->o_time = slap_get_time();
1205 op->ors_tlimit = SLAP_NO_LIMIT;
1208 /* set callback function */
1209 op->o_callback = &cb;
1210 cb.sc_response = dn_callback;
1213 si->si_syncUUID_ndn.bv_val = NULL;
1215 if ( limits_check( op, &rs_search ) == 0 ) {
1216 rc = be->be_search( op, &rs_search );
1219 if ( op->ors_filterstr.bv_val ) {
1220 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1223 cb.sc_response = null_callback;
1226 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1229 if ( syncstate != LDAP_SYNC_DELETE ) {
1230 op->o_no_psearch = 1;
1233 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1234 if ( op->o_sync_csn.bv_val ) {
1235 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1240 op->o_req_dn = si->si_syncUUID_ndn;
1241 op->o_req_ndn = si->si_syncUUID_ndn;
1242 op->o_tag = LDAP_REQ_DELETE;
1243 rc = be->be_delete( op, &rs_delete );
1245 org_req_dn = op->o_req_dn;
1246 org_req_ndn = op->o_req_ndn;
1248 org_ndn = op->o_ndn;
1249 org_managedsait = get_manageDSAit( op );
1250 op->o_dn = op->o_bd->be_rootdn;
1251 op->o_ndn = op->o_bd->be_rootndn;
1252 op->o_managedsait = 1;
1254 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1255 op->o_delete_glue_parent = 0;
1256 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1257 slap_callback cb = { NULL };
1258 cb.sc_response = slap_null_cb;
1259 dnParent( &op->o_req_ndn, &pdn );
1261 op->o_req_ndn = pdn;
1262 op->o_callback = &cb;
1263 op->o_bd->be_delete( op, &rs_delete );
1269 op->o_managedsait = org_managedsait;
1271 op->o_ndn = org_ndn;
1272 op->o_req_dn = org_req_dn;
1273 op->o_req_ndn = org_req_ndn;
1274 op->o_delete_glue_parent = 0;
1276 op->o_no_psearch = 0;
1279 switch ( syncstate ) {
1281 case LDAP_SYNC_MODIFY:
1282 if ( rs_search.sr_err == LDAP_SUCCESS ||
1283 rs_search.sr_err == LDAP_REFERRAL ||
1284 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1285 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1287 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1288 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1289 &syncUUID_strrep, syncUUID );
1291 op->o_tag = LDAP_REQ_ADD;
1293 op->o_req_dn = entry->e_name;
1294 op->o_req_ndn = entry->e_nname;
1296 rc = be->be_add( op, &rs_add );
1298 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1299 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1300 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1302 Modifications *modtail = modlist;
1306 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1310 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1311 ber_dupbv( &uuid_bv, syncUUID );
1312 mod->sml_op = LDAP_MOD_REPLACE;
1313 mod->sml_desc = slap_schema.si_ad_entryUUID;
1314 mod->sml_type = mod->sml_desc->ad_cname;
1315 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1316 modtail->sml_next = mod;
1318 op->o_tag = LDAP_REQ_MODIFY;
1319 op->orm_modlist = modlist;
1320 op->o_req_dn = entry->e_name;
1321 op->o_req_ndn = entry->e_nname;
1323 rc = be->be_modify( op, &rs_modify );
1324 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1326 LDAP_LOG( OPERATION, ERR,
1327 "syncrepl_entry : be_modify failed (%d)\n",
1328 rs_modify.sr_err, 0, 0 );
1330 Debug( LDAP_DEBUG_ANY,
1331 "syncrepl_entry : be_modify failed (%d)\n",
1332 rs_modify.sr_err, 0, 0 );
1337 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1338 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1339 syncrepl_add_glue( op, entry );
1344 LDAP_LOG( OPERATION, ERR,
1345 "syncrepl_entry : be_add failed (%d)\n",
1346 rs_add.sr_err, 0, 0 );
1348 Debug( LDAP_DEBUG_ANY,
1349 "syncrepl_entry : be_add failed (%d)\n",
1350 rs_add.sr_err, 0, 0 );
1356 be_entry_release_w( op, entry );
1362 LDAP_LOG( OPERATION, ERR,
1363 "syncrepl_entry : be_search failed (%d)\n",
1364 rs_search.sr_err, 0, 0 );
1366 Debug( LDAP_DEBUG_ANY,
1367 "syncrepl_entry : be_search failed (%d)\n",
1368 rs_search.sr_err, 0, 0 );
1374 case LDAP_SYNC_DELETE :
1375 /* Already deleted */
1381 LDAP_LOG( OPERATION, ERR,
1382 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1384 Debug( LDAP_DEBUG_ANY,
1385 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1393 if ( syncUUID_strrep.bv_val ) {
1394 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1396 if ( si->si_syncUUID_ndn.bv_val ) {
1397 ch_free( si->si_syncUUID_ndn.bv_val );
1398 si->si_syncUUID_ndn.bv_val = NULL;
1403 static struct berval gcbva[] = {
1410 syncrepl_del_nonpresent(
1415 Backend* be = op->o_bd;
1416 slap_callback cb = { NULL };
1417 SlapReply rs_search = {REP_RESULT};
1418 SlapReply rs_delete = {REP_RESULT};
1419 SlapReply rs_modify = {REP_RESULT};
1420 struct nonpresent_entry *np_list, *np_prev;
1423 Modifications *mlnext;
1425 Modifications *modlist = NULL;
1426 Modifications **modtail;
1429 struct berval pdn = BER_BVNULL;
1430 struct berval org_req_dn = BER_BVNULL;
1431 struct berval org_req_ndn = BER_BVNULL;
1432 struct berval org_dn = BER_BVNULL;
1433 struct berval org_ndn = BER_BVNULL;
1434 int org_managedsait;
1436 op->o_req_dn = si->si_base;
1437 op->o_req_ndn = si->si_base;
1439 cb.sc_response = nonpresent_callback;
1442 op->o_callback = &cb;
1443 op->o_tag = LDAP_REQ_SEARCH;
1444 op->ors_scope = si->si_scope;
1445 op->ors_deref = LDAP_DEREF_NEVER;
1446 op->o_time = slap_get_time();
1447 op->ors_tlimit = SLAP_NO_LIMIT;
1448 op->ors_slimit = SLAP_NO_LIMIT;
1449 op->ors_attrsonly = 0;
1450 op->ors_attrs = NULL;
1451 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1452 op->ors_filterstr = si->si_filterstr;
1454 op->o_nocaching = 1;
1455 op->o_managedsait = 0;
1457 if ( limits_check( op, &rs_search ) == 0 ) {
1458 rc = be->be_search( op, &rs_search );
1461 op->o_managedsait = 1;
1462 op->o_nocaching = 0;
1464 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1466 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1467 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1468 while ( np_list != NULL ) {
1469 LDAP_LIST_REMOVE( np_list, npe_link );
1471 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1472 op->o_tag = LDAP_REQ_DELETE;
1473 op->o_callback = &cb;
1474 cb.sc_response = null_callback;
1476 op->o_req_dn = *np_prev->npe_name;
1477 op->o_req_ndn = *np_prev->npe_nname;
1478 rc = op->o_bd->be_delete( op, &rs_delete );
1480 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1482 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1483 mod->sml_op = LDAP_MOD_REPLACE;
1484 mod->sml_desc = slap_schema.si_ad_objectClass;
1485 mod->sml_type = mod->sml_desc->ad_cname;
1486 mod->sml_values = &gcbva[0];
1488 modtail = &mod->sml_next;
1490 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1491 mod->sml_op = LDAP_MOD_REPLACE;
1492 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1493 mod->sml_type = mod->sml_desc->ad_cname;
1494 mod->sml_values = &gcbva[1];
1496 modtail = &mod->sml_next;
1498 op->o_tag = LDAP_REQ_MODIFY;
1499 op->orm_modlist = modlist;
1501 rc = be->be_modify( op, &rs_modify );
1503 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1504 mlnext = ml->sml_next;
1509 org_req_dn = op->o_req_dn;
1510 org_req_ndn = op->o_req_ndn;
1512 org_ndn = op->o_ndn;
1513 org_managedsait = get_manageDSAit( op );
1514 op->o_dn = op->o_bd->be_rootdn;
1515 op->o_ndn = op->o_bd->be_rootndn;
1516 op->o_managedsait = 1;
1518 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1519 op->o_delete_glue_parent ) {
1520 op->o_delete_glue_parent = 0;
1521 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1522 slap_callback cb = { NULL };
1523 cb.sc_response = slap_null_cb;
1524 dnParent( &op->o_req_ndn, &pdn );
1526 op->o_req_ndn = pdn;
1527 op->o_callback = &cb;
1528 /* give it a root privil ? */
1529 op->o_bd->be_delete( op, &rs_delete );
1535 op->o_managedsait = org_managedsait;
1537 op->o_ndn = org_ndn;
1538 op->o_req_dn = org_req_dn;
1539 op->o_req_ndn = org_req_ndn;
1540 op->o_delete_glue_parent = 0;
1542 ber_bvfree( np_prev->npe_name );
1543 ber_bvfree( np_prev->npe_nname );
1544 op->o_req_dn.bv_val = NULL;
1545 op->o_req_ndn.bv_val = NULL;
1560 Backend *be = op->o_bd;
1561 slap_callback cb = { NULL };
1566 struct berval dn = {0, NULL};
1567 struct berval ndn = {0, NULL};
1569 SlapReply rs_add = {REP_RESULT};
1572 op->o_tag = LDAP_REQ_ADD;
1573 op->o_callback = &cb;
1574 cb.sc_response = null_callback;
1575 cb.sc_private = NULL;
1580 /* count RDNs in suffix */
1581 if ( be->be_nsuffix[0].bv_len ) {
1582 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1592 /* Start with BE suffix */
1593 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1594 comma = strrchr(dn.bv_val, ',');
1595 if ( ptr ) *ptr = ',';
1596 if ( comma ) *comma = '\0';
1601 dn.bv_len -= ptr - dn.bv_val;
1604 /* the normalizedDNs are always the same length, no counting
1607 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1608 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1609 ndn.bv_len = be->be_nsuffix[0].bv_len;
1612 while ( ndn.bv_val > e->e_nname.bv_val ) {
1613 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1614 ber_dupbv( &glue->e_name, &dn );
1615 ber_dupbv( &glue->e_nname, &ndn );
1617 a = ch_calloc( 1, sizeof( Attribute ));
1618 a->a_desc = slap_schema.si_ad_objectClass;
1620 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1621 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1622 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1623 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1625 a->a_nvals = a->a_vals;
1627 a->a_next = glue->e_attrs;
1630 a = ch_calloc( 1, sizeof( Attribute ));
1631 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1633 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1634 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1635 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1637 a->a_nvals = a->a_vals;
1639 a->a_next = glue->e_attrs;
1642 op->o_req_dn = glue->e_name;
1643 op->o_req_ndn = glue->e_nname;
1645 rc = be->be_add ( op, &rs_add );
1646 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1647 be_entry_release_w( op, glue );
1649 /* incl. ALREADY EXIST */
1653 /* Move to next child */
1654 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1657 if ( ptr == e->e_name.bv_val ) break;
1659 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1660 for( ptr = ndn.bv_val-2;
1661 ptr > e->e_nname.bv_val && *ptr != ',';
1667 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1670 op->o_req_dn = e->e_name;
1671 op->o_req_ndn = e->e_nname;
1673 rc = be->be_add ( op, &rs_add );
1674 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1675 be_entry_release_w( op, e );
1683 static struct berval ocbva[] = {
1685 BER_BVC("subentry"),
1686 BER_BVC("syncConsumerSubentry"),
1690 static struct berval cnbva[] = {
1695 static struct berval ssbva[] = {
1700 static struct berval scbva[] = {
1706 syncrepl_updateCookie(
1710 struct sync_cookie *syncCookie
1713 Backend *be = op->o_bd;
1715 Modifications *mlnext;
1717 Modifications *modlist = NULL;
1718 Modifications **modtail = &modlist;
1721 char txtbuf[SLAP_TEXT_BUFLEN];
1722 size_t textlen = sizeof txtbuf;
1727 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1728 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1729 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1731 slap_callback cb = { NULL };
1732 SlapReply rs_add = {REP_RESULT};
1733 SlapReply rs_modify = {REP_RESULT};
1735 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1736 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1738 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1739 mod->sml_op = LDAP_MOD_REPLACE;
1740 mod->sml_desc = slap_schema.si_ad_objectClass;
1741 mod->sml_type = mod->sml_desc->ad_cname;
1742 mod->sml_values = ocbva;
1744 modtail = &mod->sml_next;
1746 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1747 assert( si->si_rid < 1000 );
1748 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1749 slap_syncrepl_bvc.bv_len + 1,
1750 "syncrepl%ld", si->si_rid );
1751 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1752 mod->sml_op = LDAP_MOD_REPLACE;
1753 mod->sml_desc = slap_schema.si_ad_cn;
1754 mod->sml_type = mod->sml_desc->ad_cname;
1755 mod->sml_values = cnbva;
1757 modtail = &mod->sml_next;
1759 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1760 mod->sml_op = LDAP_MOD_REPLACE;
1761 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1762 mod->sml_type = mod->sml_desc->ad_cname;
1763 mod->sml_values = ssbva;
1765 modtail = &mod->sml_next;
1767 /* Keep this last, so we can avoid touching the previous
1768 * attributes unnecessarily.
1770 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1771 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1772 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1773 mod->sml_op = LDAP_MOD_REPLACE;
1774 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1775 mod->sml_type = mod->sml_desc->ad_cname;
1776 mod->sml_values = scbva;
1778 modtail = &mod->sml_next;
1782 op->o_tag = LDAP_REQ_ADD;
1783 rc = slap_mods_opattrs( op, modlist, modtail,
1784 &text, txtbuf, textlen, 0 );
1786 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1787 ml->sml_op = LDAP_MOD_REPLACE;
1790 if( rc != LDAP_SUCCESS ) {
1792 LDAP_LOG( OPERATION, ERR,
1793 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1795 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1800 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1802 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1803 assert( si->si_rid < 1000 );
1804 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1805 slap_syncrepl_cn_bvc.bv_len + 1,
1806 "cn=syncrepl%ld", si->si_rid );
1808 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1810 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1811 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1813 if ( slap_syncrepl_dn_bv.bv_val ) {
1814 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1819 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1821 if( rc != LDAP_SUCCESS ) {
1823 LDAP_LOG( OPERATION, ERR,
1824 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1826 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1831 cb.sc_response = null_callback;
1834 op->o_callback = &cb;
1835 op->o_req_dn = e->e_name;
1836 op->o_req_ndn = e->e_nname;
1838 /* update persistent cookie */
1839 update_cookie_retry:
1840 op->o_tag = LDAP_REQ_MODIFY;
1841 /* Just modify the cookie value, not the entire entry */
1842 op->orm_modlist = mod;
1843 rc = be->be_modify( op, &rs_modify );
1845 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1846 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1847 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1848 op->o_tag = LDAP_REQ_ADD;
1850 rc = be->be_add( op, &rs_add );
1851 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1852 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1853 goto update_cookie_retry;
1854 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1855 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1857 LDAP_LOG( OPERATION, ERR,
1858 "cookie will be non-persistent\n",
1861 Debug( LDAP_DEBUG_ANY,
1862 "cookie will be non-persistent\n",
1867 LDAP_LOG( OPERATION, ERR,
1868 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1870 Debug( LDAP_DEBUG_ANY,
1871 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1875 be_entry_release_w( op, e );
1880 LDAP_LOG( OPERATION, ERR,
1881 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1883 Debug( LDAP_DEBUG_ANY,
1884 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1895 if ( cnbva[0].bv_val ) {
1896 ch_free( cnbva[0].bv_val );
1897 cnbva[0].bv_val = NULL;
1899 if ( scbva[0].bv_val ) {
1900 ch_free( scbva[0].bv_val );
1901 scbva[0].bv_val = NULL;
1904 if ( mlnext->sml_next ) {
1905 slap_mods_free( mlnext->sml_next );
1906 mlnext->sml_next = NULL;
1909 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1910 mlnext = ml->sml_next;
1918 syncrepl_isupdate( Operation *op )
1920 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1924 syncrepl_isupdate_dn(
1932 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1933 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1934 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1948 syncinfo_t *si = op->o_callback->sc_private;
1950 if ( rs->sr_type == REP_SEARCH ) {
1951 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1953 LDAP_LOG( OPERATION, ERR,
1954 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1956 Debug( LDAP_DEBUG_ANY,
1957 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1960 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1962 } else if ( rs->sr_type == REP_RESULT ) {
1963 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1965 LDAP_LOG( OPERATION, ERR,
1966 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1968 Debug( LDAP_DEBUG_ANY,
1969 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1974 return LDAP_SUCCESS;
1978 nonpresent_callback(
1983 syncinfo_t *si = op->o_callback->sc_private;
1986 struct berval* present_uuid = NULL;
1987 struct nonpresent_entry *np_entry;
1989 if ( rs->sr_type == REP_RESULT ) {
1990 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1991 si->si_presentlist = NULL;
1993 } else if ( rs->sr_type == REP_SEARCH ) {
1994 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1996 if ( a == NULL ) return 0;
1998 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2001 if ( present_uuid == NULL ) {
2002 np_entry = (struct nonpresent_entry *)
2003 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2004 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2005 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2006 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2009 avl_delete( &si->si_presentlist,
2010 &a->a_nvals[0], syncuuid_cmp );
2011 ch_free( present_uuid->bv_val );
2012 ch_free( present_uuid );
2015 return LDAP_SUCCESS;
2023 if ( rs->sr_err != LDAP_SUCCESS &&
2024 rs->sr_err != LDAP_REFERRAL &&
2025 rs->sr_err != LDAP_ALREADY_EXISTS &&
2026 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2027 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2030 LDAP_LOG( OPERATION, ERR,
2031 "null_callback : error code 0x%x\n",
2034 Debug( LDAP_DEBUG_ANY,
2035 "null_callback : error code 0x%x\n",
2039 return LDAP_SUCCESS;
2043 slap_create_syncrepl_entry(
2045 struct berval *context_csn,
2053 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
2055 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
2057 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
2060 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
2062 if ( context_csn ) {
2063 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
2064 context_csn, NULL );
2068 bv.bv_len = sizeof("{}")-1;
2069 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
2071 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
2072 ber_dupbv( &e->e_nname, &e->e_name );
2078 slap_uuidstr_from_normalized(
2079 struct berval* uuidstr,
2080 struct berval* normalized,
2084 unsigned char nibble;
2087 if ( normalized == NULL )
2090 if ( normalized->bv_len != 16 ) {
2097 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
2102 if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
2104 sl_free( new, ctx );
2108 for ( i = 0; i < 16; i++ ) {
2109 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2110 new->bv_val[(i<<1)+d] = '-';
2114 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2115 if ( nibble < 10 ) {
2116 new->bv_val[(i<<1)+d] = nibble + '0';
2118 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2121 nibble = (normalized->bv_val[i]) & 0xF;
2122 if ( nibble < 10 ) {
2123 new->bv_val[(i<<1)+d+1] = nibble + '0';
2125 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2129 new->bv_val[new->bv_len] = '\0';
2135 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2137 const struct berval *uuid1 = v_uuid1;
2138 const struct berval *uuid2 = v_uuid2;
2139 int rc = uuid1->bv_len - uuid2->bv_len;
2140 if ( rc ) return rc;
2141 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2145 avl_ber_bvfree( void *bv )
2150 if ( ((struct berval *)bv)->bv_val != NULL ) {
2151 ch_free ( ((struct berval *)bv)->bv_val );
2153 ch_free ( (char *) bv );
2157 syncinfo_free( syncinfo_t *sie )
2159 if ( sie->si_provideruri ) {
2160 ch_free( sie->si_provideruri );
2162 if ( sie->si_provideruri_bv ) {
2163 ber_bvarray_free( sie->si_provideruri_bv );
2165 if ( sie->si_updatedn.bv_val ) {
2166 ch_free( sie->si_updatedn.bv_val );
2168 if ( sie->si_binddn ) {
2169 ch_free( sie->si_binddn );
2171 if ( sie->si_passwd ) {
2172 ch_free( sie->si_passwd );
2174 if ( sie->si_saslmech ) {
2175 ch_free( sie->si_saslmech );
2177 if ( sie->si_secprops ) {
2178 ch_free( sie->si_secprops );
2180 if ( sie->si_realm ) {
2181 ch_free( sie->si_realm );
2183 if ( sie->si_authcId ) {
2184 ch_free( sie->si_authcId );
2186 if ( sie->si_authzId ) {
2187 ch_free( sie->si_authzId );
2189 if ( sie->si_filterstr.bv_val ) {
2190 ch_free( sie->si_filterstr.bv_val );
2192 if ( sie->si_base.bv_val ) {
2193 ch_free( sie->si_base.bv_val );
2195 if ( sie->si_attrs ) {
2197 while ( sie->si_attrs[i] != NULL ) {
2198 ch_free( sie->si_attrs[i] );
2201 ch_free( sie->si_attrs );
2203 if ( sie->si_exattrs ) {
2205 while ( sie->si_exattrs[i] != NULL ) {
2206 ch_free( sie->si_exattrs[i] );
2209 ch_free( sie->si_exattrs );
2211 if ( sie->si_retryinterval ) {
2212 ch_free( sie->si_retryinterval );
2214 if ( sie->si_retrynum ) {
2215 ch_free( sie->si_retrynum );
2217 if ( sie->si_retrynum_init ) {
2218 ch_free( sie->si_retrynum_init );
2220 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2221 if ( sie->si_syncUUID_ndn.bv_val ) {
2222 ch_free( sie->si_syncUUID_ndn.bv_val );
2224 if ( sie->si_presentlist ) {
2225 avl_free( sie->si_presentlist, avl_ber_bvfree );
2228 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2230 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2231 struct nonpresent_entry* npe;
2232 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2233 LDAP_LIST_REMOVE( npe, npe_link );
2234 if ( npe->npe_name ) {
2235 if ( npe->npe_name->bv_val ) {
2236 ch_free( npe->npe_name->bv_val );
2238 ch_free( npe->npe_name );
2240 if ( npe->npe_nname ) {
2241 if ( npe->npe_nname->bv_val ) {
2242 ch_free( npe->npe_nname->bv_val );
2244 ch_free( npe->npe_nname );