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 AttributeDescription *sync_descs[4];
50 struct runqueue_s syncrepl_rq;
53 init_syncrepl(syncinfo_t *si)
58 if ( !sync_descs[0] ) {
59 sync_descs[0] = slap_schema.si_ad_objectClass;
60 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61 sync_descs[2] = slap_schema.si_ad_entryCSN;
65 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
68 /* Delete Attributes */
69 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70 for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
74 ch_free( si->si_attrs[j] );
75 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 si->si_attrs[k] = si->si_attrs[k+1];
81 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
84 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
88 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
89 tmp[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
94 tmp = ( char ** ) ch_realloc( si->si_attrs, 3 * sizeof( char * ));
96 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
98 tmp[ n++ ] = ch_strdup( "*" );
99 tmp[ n++ ] = ch_strdup( "+" );
105 for ( n = 0; si->si_exattrs[ n ] != NULL; n++ ) /* empty */;
107 /* Delete Attributes from exattrs list */
108 for ( i = 0; sync_descs[i] != NULL; i++ ) {
109 for ( j = 0; si->si_exattrs[j] != NULL; j++ ) {
110 if ( strcmp( si->si_exattrs[j], sync_descs[i]->ad_cname.bv_val )
113 ch_free( si->si_exattrs[j] );
114 for ( k = j; si->si_exattrs[k] != NULL; k++ ) {
115 si->si_exattrs[k] = si->si_exattrs[k+1];
128 BerElementBuffer berbuf;
129 BerElement *ber = (BerElement *)&berbuf;
130 LDAPControl c[2], *ctrls[3];
131 struct timeval timeout;
135 /* setup LDAP SYNC control */
136 ber_init2( ber, NULL, LBER_USE_DER );
137 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
139 if ( si->si_syncCookie.octet_str &&
140 si->si_syncCookie.octet_str[0].bv_val )
142 ber_printf( ber, "{eO}",
144 &si->si_syncCookie.octet_str[0] );
146 ber_printf( ber, "{e}",
150 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
155 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
156 c[0].ldctl_iscritical = si->si_type < 0;
159 if ( si->si_authzId ) {
160 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
161 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
162 c[1].ldctl_iscritical = 1;
169 timeout.tv_sec = si->si_tlimit;
172 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
173 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
174 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
175 si->si_slimit, &msgid );
186 int cmdline_cookie_found = 0;
188 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
189 struct berval syncrepl_cn_bv;
190 struct sync_cookie *sc = NULL;
191 struct sync_cookie syncCookie = { NULL, -1, NULL };
197 psub = &si->si_be->be_nsuffix[0];
199 /* Init connection to master */
200 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
201 if ( rc != LDAP_SUCCESS ) {
202 Debug( LDAP_DEBUG_ANY,
203 "do_syncrep1: ldap_initialize failed (%s)\n",
204 si->si_provideruri, 0, 0 );
208 op->o_protocol = LDAP_VERSION3;
209 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
214 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
215 if( rc != LDAP_SUCCESS ) {
216 Debug( LDAP_DEBUG_ANY,
217 "%s: ldap_start_tls failed (%d)\n",
218 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
220 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
224 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
225 #ifdef HAVE_CYRUS_SASL
228 if ( si->si_secprops != NULL ) {
229 rc = ldap_set_option( si->si_ld,
230 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
232 if( rc != LDAP_OPT_SUCCESS ) {
233 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
234 "(%s,SECPROPS,\"%s\") failed!\n",
235 si->si_provideruri, si->si_secprops, 0 );
240 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
241 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
243 rc = ldap_sasl_interactive_bind_s( si->si_ld,
251 lutil_sasl_freedefs( defaults );
253 /* FIXME: different error behaviors according to
255 * 2) on err policy : exit, retry, backoff ...
257 if ( rc != LDAP_SUCCESS ) {
258 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
259 "ldap_sasl_interactive_bind_s failed (%d)\n",
262 /* FIXME (see above comment) */
263 /* if Kerberos credentials cache is not active, retry */
264 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
265 rc == LDAP_LOCAL_ERROR )
267 rc = LDAP_SERVER_DOWN;
272 #else /* HAVE_CYRUS_SASL */
273 /* Should never get here, we trapped this at config time */
275 fprintf( stderr, "not compiled with SASL support\n" );
281 rc = ldap_bind_s( si->si_ld,
282 si->si_binddn, si->si_passwd, si->si_bindmethod );
283 if ( rc != LDAP_SUCCESS ) {
284 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
285 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
290 /* Set SSF to strongest of TLS, SASL SSFs */
293 op->o_transport_ssf = 0;
295 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
296 == LDAP_SUCCESS && ssl != NULL )
298 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
300 #endif /* HAVE_TLS */
301 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
302 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
303 ? op->o_sasl_ssf : op->o_tls_ssf;
305 /* get syncrepl cookie of shadow replica from subentry */
306 assert( si->si_rid < 1000 );
307 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
308 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
309 CN_STR "syncrepl%ld", si->si_rid );
310 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
311 op->o_req_dn = op->o_req_ndn;
313 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
314 if ( si->si_rid == sc->rid ) {
315 cmdline_cookie_found = 1;
320 if ( cmdline_cookie_found ) {
321 /* cookie is supplied in the command line */
322 BerVarray cookie = NULL;
323 struct berval cookie_bv;
325 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
326 slap_sync_cookie_free( &si->si_syncCookie, 0 );
328 /* read stored cookie if it exists */
329 backend_attribute( op, NULL, &op->o_req_ndn,
330 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
333 /* no stored cookie */
334 if ( sc->ctxcsn == NULL ||
335 sc->ctxcsn->bv_val == NULL ) {
336 /* if cmdline cookie does not have ctxcsn */
337 /* component, set it to an initial value */
338 slap_init_sync_cookie_ctxcsn( sc );
340 slap_dup_sync_cookie( &si->si_syncCookie, sc );
341 slap_sync_cookie_free( sc, 1 );
346 struct berval newcookie = BER_BVNULL;
347 ber_dupbv( &cookie_bv, &cookie[0] );
348 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
349 slap_parse_sync_cookie( &si->si_syncCookie );
350 ber_bvarray_free( si->si_syncCookie.octet_str );
351 si->si_syncCookie.octet_str = NULL;
352 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
353 if ( sc->sid != -1 ) {
354 /* command line cookie wins */
355 si->si_syncCookie.sid = sc->sid;
357 if ( sc->ctxcsn != NULL ) {
358 /* command line cookie wins */
359 if ( si->si_syncCookie.ctxcsn ) {
360 ber_bvarray_free( si->si_syncCookie.ctxcsn );
361 si->si_syncCookie.ctxcsn = NULL;
363 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
364 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
366 if ( sc->rid != -1 ) {
367 /* command line cookie wins */
368 si->si_syncCookie.rid = sc->rid;
370 slap_sync_cookie_free( sc, 1 );
372 slap_compose_sync_cookie( NULL, &newcookie,
373 &si->si_syncCookie.ctxcsn[0],
374 si->si_syncCookie.sid, si->si_syncCookie.rid );
375 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
379 /* no command line cookie is specified */
380 if ( si->si_syncCookie.octet_str == NULL ) {
381 BerVarray cookie = NULL;
382 struct berval cookie_bv;
383 /* try to read stored cookie */
384 backend_attribute( op, NULL, &op->o_req_ndn,
385 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
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_x( cookie, op->o_tmpmemctx );
395 rc = ldap_sync_search( si, op->o_tmpmemctx );
397 if( rc != LDAP_SUCCESS ) {
398 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
399 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
405 ldap_unbind( si->si_ld );
410 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
420 LDAPControl **rctrls = NULL;
423 BerElementBuffer berbuf;
424 BerElement *ber = (BerElement *)&berbuf;
426 LDAPMessage *res = NULL;
427 LDAPMessage *msg = NULL;
430 struct berval *retdata = NULL;
435 struct berval syncUUID = BER_BVNULL;
436 struct sync_cookie syncCookie = { NULL, -1, NULL };
437 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
438 struct berval cookie = BER_BVNULL;
446 Modifications *modlist = NULL;
451 struct timeval *tout_p = NULL;
452 struct timeval tout = { 0, 0 };
454 int refreshDeletes = 0;
456 BerVarray syncUUIDs = NULL;
459 if ( slapd_shutdown ) {
464 ber_init2( ber, NULL, LBER_USE_DER );
465 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
467 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
469 psub = &si->si_be->be_nsuffix[0];
471 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
473 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
479 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
480 tout_p, &res )) > 0 )
482 if ( slapd_shutdown ) {
486 for( msg = ldap_first_message( si->si_ld, res );
488 msg = ldap_next_message( si->si_ld, msg ) )
490 switch( ldap_msgtype( msg ) ) {
491 case LDAP_RES_SEARCH_ENTRY:
492 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
493 /* we can't work without the control */
499 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
500 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
501 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
502 ber_scanf( ber, /*"{"*/ "m}", &cookie );
503 if ( cookie.bv_val ) {
504 struct berval tmp_bv;
505 ber_dupbv( &tmp_bv, &cookie );
506 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
508 if ( syncCookie.octet_str &&
509 syncCookie.octet_str[0].bv_val )
510 slap_parse_sync_cookie( &syncCookie );
512 if ( syncrepl_message_to_entry( si, op, msg,
513 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
514 rc_efree = syncrepl_entry( si, op, entry, modlist,
515 syncstate, &syncUUID, &syncCookie_req );
516 if ( syncCookie.octet_str &&
517 syncCookie.octet_str[0].bv_val )
519 syncrepl_updateCookie( si, op, psub, &syncCookie );
522 ldap_controls_free( rctrls );
524 slap_mods_free( modlist );
526 if ( rc_efree && entry ) {
532 case LDAP_RES_SEARCH_REFERENCE:
533 Debug( LDAP_DEBUG_ANY,
534 "do_syncrep2 : reference received\n", 0, 0, 0 );
537 case LDAP_RES_SEARCH_RESULT:
538 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
542 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
544 ber_scanf( ber, "{" /*"}"*/);
545 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
546 ber_scanf( ber, "m", &cookie );
547 if ( cookie.bv_val ) {
548 struct berval tmp_bv;
549 ber_dupbv( &tmp_bv, &cookie );
550 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
552 if ( syncCookie.octet_str &&
553 syncCookie.octet_str[0].bv_val )
555 slap_parse_sync_cookie( &syncCookie );
558 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
560 ber_scanf( ber, "b", &refreshDeletes );
562 ber_scanf( ber, /*"{"*/ "}" );
564 if ( syncCookie_req.ctxcsn == NULL ) {
566 } else if ( syncCookie.ctxcsn == NULL ) {
569 value_match( &match, slap_schema.si_ad_entryCSN,
570 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
571 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
572 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
575 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
576 match < 0 && err == LDAP_SUCCESS )
578 syncrepl_updateCookie( si, op, psub, &syncCookie );
581 ldap_controls_free( rctrls );
583 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
584 /* FIXME : different error behaviors according to
585 * 1) err code : LDAP_BUSY ...
586 * 2) on err policy : stop service, stop sync, retry
588 if ( refreshDeletes == 0 && match < 0 &&
589 err == LDAP_SUCCESS )
591 syncrepl_del_nonpresent( op, si );
593 avl_free( si->si_presentlist, avl_ber_bvfree );
594 si->si_presentlist = NULL;
601 case LDAP_RES_INTERMEDIATE:
602 rc = ldap_parse_intermediate( si->si_ld, msg,
603 &retoid, &retdata, NULL, 0 );
604 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
605 int si_refreshDelete = 0;
606 int si_refreshPresent = 0;
607 ber_init2( ber, retdata, LBER_USE_DER );
609 switch ( si_tag = ber_peek_tag( ber, &len )) {
611 case LDAP_TAG_SYNC_NEW_COOKIE:
612 ber_scanf( ber, "tm", &tag, &cookie );
614 case LDAP_TAG_SYNC_REFRESH_DELETE:
615 si_refreshDelete = 1;
616 case LDAP_TAG_SYNC_REFRESH_PRESENT:
617 si_refreshPresent = 1;
618 ber_scanf( ber, "t{" /*"}"*/, &tag );
619 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
621 ber_scanf( ber, "m", &cookie );
622 if ( cookie.bv_val ) {
623 struct berval tmp_bv;
624 ber_dupbv( &tmp_bv, &cookie );
625 ber_bvarray_add( &syncCookie.octet_str,
628 if ( syncCookie.octet_str &&
629 syncCookie.octet_str[0].bv_val )
631 slap_parse_sync_cookie( &syncCookie );
634 if ( ber_peek_tag( ber, &len ) ==
635 LDAP_TAG_REFRESHDONE )
637 ber_scanf( ber, "b", &refreshDone );
639 ber_scanf( ber, /*"{"*/ "}" );
641 case LDAP_TAG_SYNC_ID_SET:
642 ber_scanf( ber, "t{" /*"}"*/, &tag );
643 if ( ber_peek_tag( ber, &len ) ==
644 LDAP_TAG_SYNC_COOKIE )
646 ber_scanf( ber, "m", &cookie );
647 if ( cookie.bv_val ) {
648 struct berval tmp_bv;
649 ber_dupbv( &tmp_bv, &cookie );
650 ber_bvarray_add( &syncCookie.octet_str,
653 if ( syncCookie.octet_str &&
654 syncCookie.octet_str[0].bv_val )
656 slap_parse_sync_cookie( &syncCookie );
659 if ( ber_peek_tag( ber, &len ) ==
660 LDAP_TAG_REFRESHDELETES )
662 ber_scanf( ber, "b", &refreshDeletes );
664 ber_scanf( ber, "[W]", &syncUUIDs );
665 ber_scanf( ber, /*"{"*/ "}" );
666 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
667 struct berval *syncuuid_bv;
668 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
669 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
670 avl_insert( &si->si_presentlist,
671 (caddr_t) syncuuid_bv,
672 syncuuid_cmp, avl_dup_error );
674 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
677 Debug( LDAP_DEBUG_ANY,
678 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
679 (long) si_tag, 0, 0 );
680 ldap_memfree( retoid );
681 ber_bvfree( retdata );
685 if ( syncCookie_req.ctxcsn == NULL ) {
687 } else if ( syncCookie.ctxcsn == NULL ) {
690 value_match( &match, slap_schema.si_ad_entryCSN,
691 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
692 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
693 &syncCookie_req.ctxcsn[0],
694 &syncCookie.ctxcsn[0], &text );
697 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
700 syncrepl_updateCookie( si, op, psub, &syncCookie);
703 if ( si_refreshPresent == 1 ) {
705 syncrepl_del_nonpresent( op, si );
709 ldap_memfree( retoid );
710 ber_bvfree( retdata );
714 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
715 "unknown intermediate response (%d)\n",
717 ldap_memfree( retoid );
718 ber_bvfree( retdata );
724 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
725 "unknown message\n", 0, 0, 0 );
729 if ( syncCookie.octet_str ) {
730 slap_sync_cookie_free( &syncCookie_req, 0 );
731 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
732 slap_sync_cookie_free( &syncCookie, 0 );
742 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
743 errstr = ldap_err2string( rc );
745 Debug( LDAP_DEBUG_ANY,
746 "do_syncrep2 : %s\n", errstr, 0, 0 );
750 slap_sync_cookie_free( &syncCookie, 0 );
751 slap_sync_cookie_free( &syncCookie_req, 0 );
753 if ( res ) ldap_msgfree( res );
755 if ( rc && si->si_ld ) {
756 ldap_unbind( si->si_ld );
768 struct re_s* rtask = arg;
769 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
770 Connection conn = {0};
772 int rc = LDAP_SUCCESS;
779 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
784 switch( abs( si->si_type )) {
785 case LDAP_SYNC_REFRESH_ONLY:
786 case LDAP_SYNC_REFRESH_AND_PERSIST:
792 if ( slapd_shutdown && si->si_ld ) {
793 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
794 connection_client_stop( s );
795 ldap_unbind( si->si_ld );
800 connection_fake_init( &conn, &op, ctx );
802 /* use global malloc for now */
803 op.o_tmpmemctx = NULL;
804 op.o_tmpmfuncs = &ch_mfuncs;
806 op.o_dn = si->si_updatedn;
807 op.o_ndn = si->si_updatedn;
808 op.o_managedsait = 1;
809 op.o_bd = be = si->si_be;
811 op.o_sync_state.ctxcsn = NULL;
812 op.o_sync_state.sid = -1;
813 op.o_sync_state.octet_str = NULL;
814 op.o_sync_slog_size = -1;
815 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
816 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
818 /* Establish session, do search */
821 rc = do_syncrep1( &op, si );
824 /* Process results */
825 if ( rc == LDAP_SUCCESS ) {
826 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
828 rc = do_syncrep2( &op, si );
830 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
831 /* If we succeeded, enable the connection for further listening.
832 * If we failed, tear down the connection and reschedule.
834 if ( rc == LDAP_SUCCESS ) {
836 rc = connection_client_setup( s, do_syncrepl, arg );
838 connection_client_enable( s );
840 } else if ( !first ) {
844 if ( rc == -2 ) rc = 0;
848 /* At this point, we have 4 cases:
849 * 1) for any hard failure, give up and remove this task
850 * 2) for ServerDown, reschedule this task to run
851 * 3) for Refresh and Success, reschedule to run
852 * 4) for Persist and Success, reschedule to defer
854 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
856 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
857 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
861 connection_client_stop( s );
864 if ( rc == LDAP_SUCCESS ) {
865 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
868 rtask->interval.tv_sec = si->si_interval;
869 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
870 if ( si->si_retrynum ) {
871 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
872 si->si_retrynum[i] = si->si_retrynum_init[i];
874 si->si_retrynum[i] = -2;
877 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
878 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
882 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
883 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
884 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
886 } else if ( si->si_retrynum[i] >= -1 ) {
887 if ( si->si_retrynum[i] > 0 )
888 si->si_retrynum[i]--;
889 rtask->interval.tv_sec = si->si_retryinterval[i];
890 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
891 slap_wake_listener();
895 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
901 syncrepl_message_to_entry(
905 Modifications **modlist,
911 BerElement *ber = NULL;
914 Modifications **modtail = modlist;
917 char txtbuf[SLAP_TEXT_BUFLEN];
918 size_t textlen = sizeof txtbuf;
920 struct berval bdn = {0, NULL}, dn, ndn;
925 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
926 Debug( LDAP_DEBUG_ANY,
927 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
931 op->o_tag = LDAP_REQ_ADD;
933 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
935 if ( rc != LDAP_SUCCESS ) {
936 Debug( LDAP_DEBUG_ANY,
937 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
941 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
942 ber_dupbv( &op->o_req_dn, &dn );
943 ber_dupbv( &op->o_req_ndn, &ndn );
944 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
945 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
947 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
953 if ( entry == NULL ) {
957 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
959 e->e_name = op->o_req_dn;
960 e->e_nname = op->o_req_ndn;
962 while ( ber_remaining( ber ) ) {
963 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
964 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
969 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
971 mod->sml_op = LDAP_MOD_REPLACE;
972 mod->sml_next = NULL;
973 mod->sml_desc = NULL;
974 mod->sml_type = tmp.sml_type;
975 mod->sml_values = tmp.sml_values;
976 mod->sml_nvalues = NULL;
979 modtail = &mod->sml_next;
982 if ( *modlist == NULL ) {
983 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
989 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
991 if ( rc != LDAP_SUCCESS ) {
992 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
997 /* Strip out dynamically generated attrs */
998 for ( modtail = modlist; *modtail ; ) {
1000 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1001 *modtail = mod->sml_next;
1002 slap_mod_free( &mod->sml_mod, 0 );
1005 modtail = &mod->sml_next;
1009 /* Strip out attrs in exattrs list */
1010 for ( modtail = modlist; *modtail ; ) {
1012 if ( ldap_charray_inlist( si->si_exattrs,
1013 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1014 *modtail = mod->sml_next;
1015 slap_mod_free( &mod->sml_mod, 0 );
1018 modtail = &mod->sml_next;
1022 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1023 if( rc != LDAP_SUCCESS ) {
1024 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1029 ber_free ( ber, 0 );
1030 if ( rc != LDAP_SUCCESS ) {
1045 Modifications* modlist,
1047 struct berval* syncUUID,
1048 struct sync_cookie* syncCookie_req )
1050 Backend *be = op->o_bd;
1051 slap_callback cb = { NULL };
1052 struct berval *syncuuid_bv = NULL;
1053 struct berval syncUUID_strrep = BER_BVNULL;
1054 struct berval uuid_bv = BER_BVNULL;
1056 SlapReply rs_search = {REP_RESULT};
1057 SlapReply rs_delete = {REP_RESULT};
1058 SlapReply rs_add = {REP_RESULT};
1059 SlapReply rs_modify = {REP_RESULT};
1061 AttributeAssertion ava = {0};
1062 int rc = LDAP_SUCCESS;
1063 int ret = LDAP_SUCCESS;
1066 struct berval pdn = BER_BVNULL;
1067 struct berval org_req_dn = BER_BVNULL;
1068 struct berval org_req_ndn = BER_BVNULL;
1069 struct berval org_dn = BER_BVNULL;
1070 struct berval org_ndn = BER_BVNULL;
1071 int org_managedsait;
1073 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1074 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1075 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1076 syncuuid_cmp, avl_dup_error );
1079 if ( syncstate == LDAP_SYNC_PRESENT ) {
1081 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1082 if ( entry == NULL ) {
1087 f.f_choice = LDAP_FILTER_EQUALITY;
1089 ava.aa_desc = slap_schema.si_ad_entryUUID;
1090 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1091 ava.aa_value = *syncUUID;
1092 op->ors_filter = &f;
1094 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1095 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1096 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1097 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1098 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1099 syncUUID->bv_val, syncUUID->bv_len );
1100 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1102 op->o_tag = LDAP_REQ_SEARCH;
1103 op->ors_scope = LDAP_SCOPE_SUBTREE;
1105 /* get syncrepl cookie of shadow replica from subentry */
1106 op->o_req_dn = si->si_base;
1107 op->o_req_ndn = si->si_base;
1109 op->o_time = slap_get_time();
1110 op->ors_tlimit = SLAP_NO_LIMIT;
1113 op->ors_attrs = slap_anlist_no_attrs;
1114 op->ors_attrsonly = 1;
1116 /* set callback function */
1117 op->o_callback = &cb;
1118 cb.sc_response = dn_callback;
1121 si->si_syncUUID_ndn.bv_val = NULL;
1123 if ( limits_check( op, &rs_search ) == 0 ) {
1124 rc = be->be_search( op, &rs_search );
1127 if ( op->ors_filterstr.bv_val ) {
1128 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1131 cb.sc_response = null_callback;
1134 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1137 if ( syncstate != LDAP_SYNC_DELETE ) {
1138 op->o_no_psearch = 1;
1141 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1142 if ( op->o_sync_csn.bv_val ) {
1143 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1148 op->o_req_dn = si->si_syncUUID_ndn;
1149 op->o_req_ndn = si->si_syncUUID_ndn;
1150 op->o_tag = LDAP_REQ_DELETE;
1151 rc = be->be_delete( op, &rs_delete );
1153 org_req_dn = op->o_req_dn;
1154 org_req_ndn = op->o_req_ndn;
1156 org_ndn = op->o_ndn;
1157 org_managedsait = get_manageDSAit( op );
1158 op->o_dn = op->o_bd->be_rootdn;
1159 op->o_ndn = op->o_bd->be_rootndn;
1160 op->o_managedsait = 1;
1162 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1163 op->o_delete_glue_parent = 0;
1164 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1165 slap_callback cb = { NULL };
1166 cb.sc_response = slap_null_cb;
1167 dnParent( &op->o_req_ndn, &pdn );
1169 op->o_req_ndn = pdn;
1170 op->o_callback = &cb;
1171 op->o_bd->be_delete( op, &rs_delete );
1177 op->o_managedsait = org_managedsait;
1179 op->o_ndn = org_ndn;
1180 op->o_req_dn = org_req_dn;
1181 op->o_req_ndn = org_req_ndn;
1182 op->o_delete_glue_parent = 0;
1184 op->o_no_psearch = 0;
1187 switch ( syncstate ) {
1189 case LDAP_SYNC_MODIFY:
1190 if ( rs_search.sr_err == LDAP_SUCCESS ||
1191 rs_search.sr_err == LDAP_REFERRAL ||
1192 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1193 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1195 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1196 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1197 &syncUUID_strrep, syncUUID );
1199 op->o_tag = LDAP_REQ_ADD;
1201 op->o_req_dn = entry->e_name;
1202 op->o_req_ndn = entry->e_nname;
1204 rc = be->be_add( op, &rs_add );
1206 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1207 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1208 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1210 Modifications *modtail = modlist;
1214 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1218 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1219 ber_dupbv( &uuid_bv, syncUUID );
1220 mod->sml_op = LDAP_MOD_REPLACE;
1221 mod->sml_desc = slap_schema.si_ad_entryUUID;
1222 mod->sml_type = mod->sml_desc->ad_cname;
1223 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1224 modtail->sml_next = mod;
1226 op->o_tag = LDAP_REQ_MODIFY;
1227 op->orm_modlist = modlist;
1228 op->o_req_dn = entry->e_name;
1229 op->o_req_ndn = entry->e_nname;
1231 rc = be->be_modify( op, &rs_modify );
1232 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1233 Debug( LDAP_DEBUG_ANY,
1234 "syncrepl_entry : be_modify failed (%d)\n",
1235 rs_modify.sr_err, 0, 0 );
1239 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1240 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1241 syncrepl_add_glue( op, entry );
1245 Debug( LDAP_DEBUG_ANY,
1246 "syncrepl_entry : be_add failed (%d)\n",
1247 rs_add.sr_err, 0, 0 );
1252 be_entry_release_w( op, entry );
1257 Debug( LDAP_DEBUG_ANY,
1258 "syncrepl_entry : be_search failed (%d)\n",
1259 rs_search.sr_err, 0, 0 );
1264 case LDAP_SYNC_DELETE :
1265 /* Already deleted */
1270 Debug( LDAP_DEBUG_ANY,
1271 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1278 if ( syncUUID_strrep.bv_val ) {
1279 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1281 if ( si->si_syncUUID_ndn.bv_val ) {
1282 ch_free( si->si_syncUUID_ndn.bv_val );
1283 si->si_syncUUID_ndn.bv_val = NULL;
1288 static struct berval gcbva[] = {
1295 syncrepl_del_nonpresent(
1299 Backend* be = op->o_bd;
1300 slap_callback cb = { NULL };
1301 SlapReply rs_search = {REP_RESULT};
1302 SlapReply rs_delete = {REP_RESULT};
1303 SlapReply rs_modify = {REP_RESULT};
1304 struct nonpresent_entry *np_list, *np_prev;
1307 Modifications *mlnext;
1309 Modifications *modlist = NULL;
1310 Modifications **modtail = &modlist;
1312 AttributeName an[2];
1314 struct berval pdn = BER_BVNULL;
1315 struct berval org_req_dn = BER_BVNULL;
1316 struct berval org_req_ndn = BER_BVNULL;
1317 struct berval org_dn = BER_BVNULL;
1318 struct berval org_ndn = BER_BVNULL;
1319 int org_managedsait;
1321 op->o_req_dn = si->si_base;
1322 op->o_req_ndn = si->si_base;
1324 cb.sc_response = nonpresent_callback;
1327 op->o_callback = &cb;
1328 op->o_tag = LDAP_REQ_SEARCH;
1329 op->ors_scope = si->si_scope;
1330 op->ors_deref = LDAP_DEREF_NEVER;
1331 op->o_time = slap_get_time();
1332 op->ors_tlimit = SLAP_NO_LIMIT;
1333 op->ors_slimit = SLAP_NO_LIMIT;
1335 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1336 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1337 an[0].an_desc = slap_schema.si_ad_entryUUID;
1340 op->ors_attrsonly = 0;
1341 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1342 op->ors_filterstr = si->si_filterstr;
1344 op->o_nocaching = 1;
1345 op->o_managedsait = 0;
1347 if ( limits_check( op, &rs_search ) == 0 ) {
1348 rc = be->be_search( op, &rs_search );
1351 op->o_managedsait = 1;
1352 op->o_nocaching = 0;
1354 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1356 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1357 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1358 while ( np_list != NULL ) {
1359 LDAP_LIST_REMOVE( np_list, npe_link );
1361 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1362 op->o_tag = LDAP_REQ_DELETE;
1363 op->o_callback = &cb;
1364 cb.sc_response = null_callback;
1366 op->o_req_dn = *np_prev->npe_name;
1367 op->o_req_ndn = *np_prev->npe_nname;
1368 rc = op->o_bd->be_delete( op, &rs_delete );
1370 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1371 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1372 mod->sml_op = LDAP_MOD_REPLACE;
1373 mod->sml_desc = slap_schema.si_ad_objectClass;
1374 mod->sml_type = mod->sml_desc->ad_cname;
1375 mod->sml_values = &gcbva[0];
1377 modtail = &mod->sml_next;
1379 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1380 mod->sml_op = LDAP_MOD_REPLACE;
1381 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1382 mod->sml_type = mod->sml_desc->ad_cname;
1383 mod->sml_values = &gcbva[1];
1385 modtail = &mod->sml_next;
1387 op->o_tag = LDAP_REQ_MODIFY;
1388 op->orm_modlist = modlist;
1390 rc = be->be_modify( op, &rs_modify );
1392 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1393 mlnext = ml->sml_next;
1398 org_req_dn = op->o_req_dn;
1399 org_req_ndn = op->o_req_ndn;
1401 org_ndn = op->o_ndn;
1402 org_managedsait = get_manageDSAit( op );
1403 op->o_dn = op->o_bd->be_rootdn;
1404 op->o_ndn = op->o_bd->be_rootndn;
1405 op->o_managedsait = 1;
1407 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1408 op->o_delete_glue_parent ) {
1409 op->o_delete_glue_parent = 0;
1410 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1411 slap_callback cb = { NULL };
1412 cb.sc_response = slap_null_cb;
1413 dnParent( &op->o_req_ndn, &pdn );
1415 op->o_req_ndn = pdn;
1416 op->o_callback = &cb;
1417 /* give it a root privil ? */
1418 op->o_bd->be_delete( op, &rs_delete );
1424 op->o_managedsait = org_managedsait;
1426 op->o_ndn = org_ndn;
1427 op->o_req_dn = org_req_dn;
1428 op->o_req_ndn = org_req_ndn;
1429 op->o_delete_glue_parent = 0;
1431 ber_bvfree( np_prev->npe_name );
1432 ber_bvfree( np_prev->npe_nname );
1433 op->o_req_dn.bv_val = NULL;
1434 op->o_req_ndn.bv_val = NULL;
1447 Backend *be = op->o_bd;
1448 slap_callback cb = { NULL };
1453 struct berval dn = {0, NULL};
1454 struct berval ndn = {0, NULL};
1456 SlapReply rs_add = {REP_RESULT};
1459 op->o_tag = LDAP_REQ_ADD;
1460 op->o_callback = &cb;
1461 cb.sc_response = null_callback;
1462 cb.sc_private = NULL;
1467 /* count RDNs in suffix */
1468 if ( be->be_nsuffix[0].bv_len ) {
1469 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1479 /* Start with BE suffix */
1480 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1481 comma = strrchr(dn.bv_val, ',');
1482 if ( ptr ) *ptr = ',';
1483 if ( comma ) *comma = '\0';
1488 dn.bv_len -= ptr - dn.bv_val;
1491 /* the normalizedDNs are always the same length, no counting
1494 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1495 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1496 ndn.bv_len = be->be_nsuffix[0].bv_len;
1499 while ( ndn.bv_val > e->e_nname.bv_val ) {
1500 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1501 ber_dupbv( &glue->e_name, &dn );
1502 ber_dupbv( &glue->e_nname, &ndn );
1504 a = ch_calloc( 1, sizeof( Attribute ));
1505 a->a_desc = slap_schema.si_ad_objectClass;
1507 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1508 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1509 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1510 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1512 a->a_nvals = a->a_vals;
1514 a->a_next = glue->e_attrs;
1517 a = ch_calloc( 1, sizeof( Attribute ));
1518 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1520 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1521 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1522 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1524 a->a_nvals = a->a_vals;
1526 a->a_next = glue->e_attrs;
1529 op->o_req_dn = glue->e_name;
1530 op->o_req_ndn = glue->e_nname;
1532 rc = be->be_add ( op, &rs_add );
1533 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1534 be_entry_release_w( op, glue );
1536 /* incl. ALREADY EXIST */
1540 /* Move to next child */
1541 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1544 if ( ptr == e->e_name.bv_val ) break;
1546 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1547 for( ptr = ndn.bv_val-2;
1548 ptr > e->e_nname.bv_val && *ptr != ',';
1554 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1557 op->o_req_dn = e->e_name;
1558 op->o_req_ndn = e->e_nname;
1560 rc = be->be_add ( op, &rs_add );
1561 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1562 be_entry_release_w( op, e );
1570 static struct berval ocbva[] = {
1572 BER_BVC("subentry"),
1573 BER_BVC("syncConsumerSubentry"),
1577 static struct berval cnbva[] = {
1582 static struct berval ssbva[] = {
1587 static struct berval scbva[] = {
1593 syncrepl_updateCookie(
1597 struct sync_cookie *syncCookie )
1599 Backend *be = op->o_bd;
1601 Modifications *mlnext;
1603 Modifications *modlist = NULL;
1604 Modifications **modtail = &modlist;
1607 char txtbuf[SLAP_TEXT_BUFLEN];
1608 size_t textlen = sizeof txtbuf;
1613 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1614 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1615 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1617 slap_callback cb = { NULL };
1618 SlapReply rs_add = {REP_RESULT};
1619 SlapReply rs_modify = {REP_RESULT};
1621 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1622 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1624 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1625 mod->sml_op = LDAP_MOD_REPLACE;
1626 mod->sml_desc = slap_schema.si_ad_objectClass;
1627 mod->sml_type = mod->sml_desc->ad_cname;
1628 mod->sml_values = ocbva;
1630 modtail = &mod->sml_next;
1632 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1633 assert( si->si_rid < 1000 );
1634 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1635 slap_syncrepl_bvc.bv_len + 1,
1636 "syncrepl%ld", si->si_rid );
1637 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1638 mod->sml_op = LDAP_MOD_REPLACE;
1639 mod->sml_desc = slap_schema.si_ad_cn;
1640 mod->sml_type = mod->sml_desc->ad_cname;
1641 mod->sml_values = cnbva;
1643 modtail = &mod->sml_next;
1645 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1646 mod->sml_op = LDAP_MOD_REPLACE;
1647 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1648 mod->sml_type = mod->sml_desc->ad_cname;
1649 mod->sml_values = ssbva;
1651 modtail = &mod->sml_next;
1653 /* Keep this last, so we can avoid touching the previous
1654 * attributes unnecessarily.
1656 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1657 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1658 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1659 mod->sml_op = LDAP_MOD_REPLACE;
1660 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1661 mod->sml_type = mod->sml_desc->ad_cname;
1662 mod->sml_values = scbva;
1664 modtail = &mod->sml_next;
1668 op->o_tag = LDAP_REQ_ADD;
1669 rc = slap_mods_opattrs( op, modlist, modtail,
1670 &text, txtbuf, textlen, 0 );
1672 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1673 ml->sml_op = LDAP_MOD_REPLACE;
1676 if( rc != LDAP_SUCCESS ) {
1677 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1681 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1683 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1684 assert( si->si_rid < 1000 );
1685 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1686 slap_syncrepl_cn_bvc.bv_len + 1,
1687 "cn=syncrepl%ld", si->si_rid );
1689 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1691 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1692 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1694 if ( slap_syncrepl_dn_bv.bv_val ) {
1695 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1700 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1702 if( rc != LDAP_SUCCESS ) {
1703 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1707 cb.sc_response = null_callback;
1710 op->o_callback = &cb;
1711 op->o_req_dn = e->e_name;
1712 op->o_req_ndn = e->e_nname;
1714 /* update persistent cookie */
1715 update_cookie_retry:
1716 op->o_tag = LDAP_REQ_MODIFY;
1717 /* Just modify the cookie value, not the entire entry */
1718 op->orm_modlist = mod;
1719 rc = be->be_modify( op, &rs_modify );
1721 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1722 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1723 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1724 op->o_tag = LDAP_REQ_ADD;
1726 rc = be->be_add( op, &rs_add );
1727 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1728 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1729 goto update_cookie_retry;
1730 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1731 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1732 Debug( LDAP_DEBUG_ANY,
1733 "cookie will be non-persistent\n",
1736 Debug( LDAP_DEBUG_ANY,
1737 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1740 be_entry_release_w( op, e );
1744 Debug( LDAP_DEBUG_ANY,
1745 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1755 if ( cnbva[0].bv_val ) {
1756 ch_free( cnbva[0].bv_val );
1757 cnbva[0].bv_val = NULL;
1759 if ( scbva[0].bv_val ) {
1760 ch_free( scbva[0].bv_val );
1761 scbva[0].bv_val = NULL;
1764 if ( mlnext->sml_next ) {
1765 slap_mods_free( mlnext->sml_next );
1766 mlnext->sml_next = NULL;
1769 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1770 mlnext = ml->sml_next;
1778 syncrepl_isupdate( Operation *op )
1780 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1784 syncrepl_isupdate_dn(
1786 struct berval* ndn )
1791 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1792 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1793 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1806 syncinfo_t *si = op->o_callback->sc_private;
1808 if ( rs->sr_type == REP_SEARCH ) {
1809 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1810 Debug( LDAP_DEBUG_ANY,
1811 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1813 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1815 } else if ( rs->sr_type == REP_RESULT ) {
1816 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1817 Debug( LDAP_DEBUG_ANY,
1818 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1822 return LDAP_SUCCESS;
1826 nonpresent_callback(
1830 syncinfo_t *si = op->o_callback->sc_private;
1833 struct berval* present_uuid = NULL;
1834 struct nonpresent_entry *np_entry;
1836 if ( rs->sr_type == REP_RESULT ) {
1837 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1838 si->si_presentlist = NULL;
1840 } else if ( rs->sr_type == REP_SEARCH ) {
1841 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1843 if ( a == NULL ) return 0;
1845 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1848 if ( present_uuid == NULL ) {
1849 np_entry = (struct nonpresent_entry *)
1850 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1851 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1852 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1853 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1856 avl_delete( &si->si_presentlist,
1857 &a->a_nvals[0], syncuuid_cmp );
1858 ch_free( present_uuid->bv_val );
1859 ch_free( present_uuid );
1862 return LDAP_SUCCESS;
1870 if ( rs->sr_err != LDAP_SUCCESS &&
1871 rs->sr_err != LDAP_REFERRAL &&
1872 rs->sr_err != LDAP_ALREADY_EXISTS &&
1873 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1874 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1876 Debug( LDAP_DEBUG_ANY,
1877 "null_callback : error code 0x%x\n",
1880 return LDAP_SUCCESS;
1884 slap_create_syncrepl_entry(
1886 struct berval *context_csn,
1894 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1896 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1898 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1901 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1903 if ( context_csn ) {
1904 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1905 context_csn, NULL );
1909 bv.bv_len = sizeof("{}")-1;
1910 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1912 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1913 ber_dupbv( &e->e_nname, &e->e_name );
1919 slap_uuidstr_from_normalized(
1920 struct berval* uuidstr,
1921 struct berval* normalized,
1925 unsigned char nibble;
1928 if ( normalized == NULL ) return NULL;
1929 if ( normalized->bv_len != 16 ) return NULL;
1934 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
1939 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1940 if ( !uuidstr ) slap_sl_free( new, ctx );
1944 for ( i = 0; i < 16; i++ ) {
1945 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1946 new->bv_val[(i<<1)+d] = '-';
1950 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1951 if ( nibble < 10 ) {
1952 new->bv_val[(i<<1)+d] = nibble + '0';
1954 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1957 nibble = (normalized->bv_val[i]) & 0xF;
1958 if ( nibble < 10 ) {
1959 new->bv_val[(i<<1)+d+1] = nibble + '0';
1961 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1965 new->bv_val[new->bv_len] = '\0';
1970 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1972 const struct berval *uuid1 = v_uuid1;
1973 const struct berval *uuid2 = v_uuid2;
1974 int rc = uuid1->bv_len - uuid2->bv_len;
1975 if ( rc ) return rc;
1976 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
1980 avl_ber_bvfree( void *bv )
1982 if( bv == NULL ) return;
1983 if ( ((struct berval *)bv)->bv_val != NULL ) {
1984 ch_free( ((struct berval *)bv)->bv_val );
1986 ch_free( (char *) bv );
1990 syncinfo_free( syncinfo_t *sie )
1992 if ( sie->si_provideruri ) {
1993 ch_free( sie->si_provideruri );
1995 if ( sie->si_provideruri_bv ) {
1996 ber_bvarray_free( sie->si_provideruri_bv );
1998 if ( sie->si_updatedn.bv_val ) {
1999 ch_free( sie->si_updatedn.bv_val );
2001 if ( sie->si_binddn ) {
2002 ch_free( sie->si_binddn );
2004 if ( sie->si_passwd ) {
2005 ch_free( sie->si_passwd );
2007 if ( sie->si_saslmech ) {
2008 ch_free( sie->si_saslmech );
2010 if ( sie->si_secprops ) {
2011 ch_free( sie->si_secprops );
2013 if ( sie->si_realm ) {
2014 ch_free( sie->si_realm );
2016 if ( sie->si_authcId ) {
2017 ch_free( sie->si_authcId );
2019 if ( sie->si_authzId ) {
2020 ch_free( sie->si_authzId );
2022 if ( sie->si_filterstr.bv_val ) {
2023 ch_free( sie->si_filterstr.bv_val );
2025 if ( sie->si_base.bv_val ) {
2026 ch_free( sie->si_base.bv_val );
2028 if ( sie->si_attrs ) {
2030 while ( sie->si_attrs[i] != NULL ) {
2031 ch_free( sie->si_attrs[i] );
2034 ch_free( sie->si_attrs );
2036 if ( sie->si_exattrs ) {
2038 while ( sie->si_exattrs[i] != NULL ) {
2039 ch_free( sie->si_exattrs[i] );
2042 ch_free( sie->si_exattrs );
2044 if ( sie->si_retryinterval ) {
2045 ch_free( sie->si_retryinterval );
2047 if ( sie->si_retrynum ) {
2048 ch_free( sie->si_retrynum );
2050 if ( sie->si_retrynum_init ) {
2051 ch_free( sie->si_retrynum_init );
2053 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2054 if ( sie->si_syncUUID_ndn.bv_val ) {
2055 ch_free( sie->si_syncUUID_ndn.bv_val );
2057 if ( sie->si_presentlist ) {
2058 avl_free( sie->si_presentlist, avl_ber_bvfree );
2061 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2063 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2064 struct nonpresent_entry* npe;
2065 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2066 LDAP_LIST_REMOVE( npe, npe_link );
2067 if ( npe->npe_name ) {
2068 if ( npe->npe_name->bv_val ) {
2069 ch_free( npe->npe_name->bv_val );
2071 ch_free( npe->npe_name );
2073 if ( npe->npe_nname ) {
2074 if ( npe->npe_nname->bv_val ) {
2075 ch_free( npe->npe_nname->bv_val );
2077 ch_free( npe->npe_nname );