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;
778 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
783 switch( abs( si->si_type )) {
784 case LDAP_SYNC_REFRESH_ONLY:
785 case LDAP_SYNC_REFRESH_AND_PERSIST:
791 if ( slapd_shutdown && si->si_ld ) {
792 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
793 connection_client_stop( s );
794 ldap_unbind( si->si_ld );
799 connection_fake_init( &conn, &op, ctx );
801 /* use global malloc for now */
802 op.o_tmpmemctx = NULL;
803 op.o_tmpmfuncs = &ch_mfuncs;
805 op.o_dn = si->si_updatedn;
806 op.o_ndn = si->si_updatedn;
807 op.o_managedsait = 1;
810 op.o_sync_state.ctxcsn = NULL;
811 op.o_sync_state.sid = -1;
812 op.o_sync_state.octet_str = NULL;
813 op.o_sync_slog_size = -1;
814 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
815 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
817 /* Establish session, do search */
820 rc = do_syncrep1( &op, si );
823 /* Process results */
824 if ( rc == LDAP_SUCCESS ) {
825 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
827 rc = do_syncrep2( &op, si );
829 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
830 /* If we succeeded, enable the connection for further listening.
831 * If we failed, tear down the connection and reschedule.
833 if ( rc == LDAP_SUCCESS ) {
835 rc = connection_client_setup( s, do_syncrepl, arg );
837 connection_client_enable( s );
839 } else if ( !first ) {
843 if ( rc == -2 ) rc = 0;
847 /* At this point, we have 4 cases:
848 * 1) for any hard failure, give up and remove this task
849 * 2) for ServerDown, reschedule this task to run
850 * 3) for Refresh and Success, reschedule to run
851 * 4) for Persist and Success, reschedule to defer
853 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
855 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
856 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
860 connection_client_stop( s );
863 if ( rc == LDAP_SUCCESS ) {
864 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
867 rtask->interval.tv_sec = si->si_interval;
868 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
869 if ( si->si_retrynum ) {
870 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
871 si->si_retrynum[i] = si->si_retrynum_init[i];
873 si->si_retrynum[i] = -2;
876 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
877 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
881 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
882 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
883 } else if ( si->si_retrynum[i] >= -1 ) {
884 if ( si->si_retrynum[i] > 0 )
885 si->si_retrynum[i]--;
886 rtask->interval.tv_sec = si->si_retryinterval[i];
887 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
888 slap_wake_listener();
892 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
898 syncrepl_message_to_entry(
902 Modifications **modlist,
908 BerElement *ber = NULL;
911 Modifications **modtail = modlist;
914 char txtbuf[SLAP_TEXT_BUFLEN];
915 size_t textlen = sizeof txtbuf;
917 struct berval bdn = {0, NULL}, dn, ndn;
922 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
923 Debug( LDAP_DEBUG_ANY,
924 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
928 op->o_tag = LDAP_REQ_ADD;
930 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
932 if ( rc != LDAP_SUCCESS ) {
933 Debug( LDAP_DEBUG_ANY,
934 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
938 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
939 ber_dupbv( &op->o_req_dn, &dn );
940 ber_dupbv( &op->o_req_ndn, &ndn );
941 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
942 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
944 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
950 if ( entry == NULL ) {
954 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
956 e->e_name = op->o_req_dn;
957 e->e_nname = op->o_req_ndn;
959 while ( ber_remaining( ber ) ) {
960 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
961 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
966 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
968 mod->sml_op = LDAP_MOD_REPLACE;
969 mod->sml_next = NULL;
970 mod->sml_desc = NULL;
971 mod->sml_type = tmp.sml_type;
972 mod->sml_values = tmp.sml_values;
973 mod->sml_nvalues = NULL;
976 modtail = &mod->sml_next;
979 if ( *modlist == NULL ) {
980 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
986 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
988 if ( rc != LDAP_SUCCESS ) {
989 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
994 /* Strip out dynamically generated attrs */
995 for ( modtail = modlist; *modtail ; ) {
997 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
998 *modtail = mod->sml_next;
999 slap_mod_free( &mod->sml_mod, 0 );
1002 modtail = &mod->sml_next;
1006 /* Strip out attrs in exattrs list */
1007 for ( modtail = modlist; *modtail ; ) {
1009 if ( ldap_charray_inlist( si->si_exattrs,
1010 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1011 *modtail = mod->sml_next;
1012 slap_mod_free( &mod->sml_mod, 0 );
1015 modtail = &mod->sml_next;
1019 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1020 if( rc != LDAP_SUCCESS ) {
1021 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1026 ber_free ( ber, 0 );
1027 if ( rc != LDAP_SUCCESS ) {
1042 Modifications* modlist,
1044 struct berval* syncUUID,
1045 struct sync_cookie* syncCookie_req )
1047 Backend *be = op->o_bd;
1048 slap_callback cb = { NULL };
1049 struct berval *syncuuid_bv = NULL;
1050 struct berval syncUUID_strrep = BER_BVNULL;
1051 struct berval uuid_bv = BER_BVNULL;
1053 SlapReply rs_search = {REP_RESULT};
1054 SlapReply rs_delete = {REP_RESULT};
1055 SlapReply rs_add = {REP_RESULT};
1056 SlapReply rs_modify = {REP_RESULT};
1058 AttributeAssertion ava = {0};
1059 int rc = LDAP_SUCCESS;
1060 int ret = LDAP_SUCCESS;
1063 struct berval pdn = BER_BVNULL;
1064 struct berval org_req_dn = BER_BVNULL;
1065 struct berval org_req_ndn = BER_BVNULL;
1066 struct berval org_dn = BER_BVNULL;
1067 struct berval org_ndn = BER_BVNULL;
1068 int org_managedsait;
1070 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1071 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1072 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1073 syncuuid_cmp, avl_dup_error );
1076 if ( syncstate == LDAP_SYNC_PRESENT ) {
1078 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1079 if ( entry == NULL ) {
1084 f.f_choice = LDAP_FILTER_EQUALITY;
1086 ava.aa_desc = slap_schema.si_ad_entryUUID;
1087 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1088 ava.aa_value = *syncUUID;
1089 op->ors_filter = &f;
1091 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1092 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1093 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1094 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1095 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1096 syncUUID->bv_val, syncUUID->bv_len );
1097 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1099 op->o_tag = LDAP_REQ_SEARCH;
1100 op->ors_scope = LDAP_SCOPE_SUBTREE;
1102 /* get syncrepl cookie of shadow replica from subentry */
1103 op->o_req_dn = si->si_base;
1104 op->o_req_ndn = si->si_base;
1106 op->o_time = slap_get_time();
1107 op->ors_tlimit = SLAP_NO_LIMIT;
1110 op->ors_attrs = slap_anlist_no_attrs;
1111 op->ors_attrsonly = 1;
1113 /* set callback function */
1114 op->o_callback = &cb;
1115 cb.sc_response = dn_callback;
1118 si->si_syncUUID_ndn.bv_val = NULL;
1120 if ( limits_check( op, &rs_search ) == 0 ) {
1121 rc = be->be_search( op, &rs_search );
1124 if ( op->ors_filterstr.bv_val ) {
1125 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1128 cb.sc_response = null_callback;
1131 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1134 if ( syncstate != LDAP_SYNC_DELETE ) {
1135 op->o_no_psearch = 1;
1138 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1139 if ( op->o_sync_csn.bv_val ) {
1140 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1145 op->o_req_dn = si->si_syncUUID_ndn;
1146 op->o_req_ndn = si->si_syncUUID_ndn;
1147 op->o_tag = LDAP_REQ_DELETE;
1148 rc = be->be_delete( op, &rs_delete );
1150 org_req_dn = op->o_req_dn;
1151 org_req_ndn = op->o_req_ndn;
1153 org_ndn = op->o_ndn;
1154 org_managedsait = get_manageDSAit( op );
1155 op->o_dn = op->o_bd->be_rootdn;
1156 op->o_ndn = op->o_bd->be_rootndn;
1157 op->o_managedsait = 1;
1159 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1160 op->o_delete_glue_parent = 0;
1161 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1162 slap_callback cb = { NULL };
1163 cb.sc_response = slap_null_cb;
1164 dnParent( &op->o_req_ndn, &pdn );
1166 op->o_req_ndn = pdn;
1167 op->o_callback = &cb;
1168 op->o_bd->be_delete( op, &rs_delete );
1174 op->o_managedsait = org_managedsait;
1176 op->o_ndn = org_ndn;
1177 op->o_req_dn = org_req_dn;
1178 op->o_req_ndn = org_req_ndn;
1179 op->o_delete_glue_parent = 0;
1181 op->o_no_psearch = 0;
1184 switch ( syncstate ) {
1186 case LDAP_SYNC_MODIFY:
1187 if ( rs_search.sr_err == LDAP_SUCCESS ||
1188 rs_search.sr_err == LDAP_REFERRAL ||
1189 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1190 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1192 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1193 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1194 &syncUUID_strrep, syncUUID );
1196 op->o_tag = LDAP_REQ_ADD;
1198 op->o_req_dn = entry->e_name;
1199 op->o_req_ndn = entry->e_nname;
1201 rc = be->be_add( op, &rs_add );
1203 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1204 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1205 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1207 Modifications *modtail = modlist;
1211 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1215 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1216 ber_dupbv( &uuid_bv, syncUUID );
1217 mod->sml_op = LDAP_MOD_REPLACE;
1218 mod->sml_desc = slap_schema.si_ad_entryUUID;
1219 mod->sml_type = mod->sml_desc->ad_cname;
1220 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1221 modtail->sml_next = mod;
1223 op->o_tag = LDAP_REQ_MODIFY;
1224 op->orm_modlist = modlist;
1225 op->o_req_dn = entry->e_name;
1226 op->o_req_ndn = entry->e_nname;
1228 rc = be->be_modify( op, &rs_modify );
1229 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1230 Debug( LDAP_DEBUG_ANY,
1231 "syncrepl_entry : be_modify failed (%d)\n",
1232 rs_modify.sr_err, 0, 0 );
1236 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1237 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1238 syncrepl_add_glue( op, entry );
1242 Debug( LDAP_DEBUG_ANY,
1243 "syncrepl_entry : be_add failed (%d)\n",
1244 rs_add.sr_err, 0, 0 );
1249 be_entry_release_w( op, entry );
1254 Debug( LDAP_DEBUG_ANY,
1255 "syncrepl_entry : be_search failed (%d)\n",
1256 rs_search.sr_err, 0, 0 );
1261 case LDAP_SYNC_DELETE :
1262 /* Already deleted */
1267 Debug( LDAP_DEBUG_ANY,
1268 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1275 if ( syncUUID_strrep.bv_val ) {
1276 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1278 if ( si->si_syncUUID_ndn.bv_val ) {
1279 ch_free( si->si_syncUUID_ndn.bv_val );
1284 static struct berval gcbva[] = {
1291 syncrepl_del_nonpresent(
1295 Backend* be = op->o_bd;
1296 slap_callback cb = { NULL };
1297 SlapReply rs_search = {REP_RESULT};
1298 SlapReply rs_delete = {REP_RESULT};
1299 SlapReply rs_modify = {REP_RESULT};
1300 struct nonpresent_entry *np_list, *np_prev;
1303 Modifications *mlnext;
1305 Modifications *modlist = NULL;
1306 Modifications **modtail = &modlist;
1308 AttributeName an[2];
1310 struct berval pdn = BER_BVNULL;
1311 struct berval org_req_dn = BER_BVNULL;
1312 struct berval org_req_ndn = BER_BVNULL;
1313 struct berval org_dn = BER_BVNULL;
1314 struct berval org_ndn = BER_BVNULL;
1315 int org_managedsait;
1317 op->o_req_dn = si->si_base;
1318 op->o_req_ndn = si->si_base;
1320 cb.sc_response = nonpresent_callback;
1323 op->o_callback = &cb;
1324 op->o_tag = LDAP_REQ_SEARCH;
1325 op->ors_scope = si->si_scope;
1326 op->ors_deref = LDAP_DEREF_NEVER;
1327 op->o_time = slap_get_time();
1328 op->ors_tlimit = SLAP_NO_LIMIT;
1329 op->ors_slimit = SLAP_NO_LIMIT;
1331 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1332 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1333 an[0].an_desc = slap_schema.si_ad_entryUUID;
1336 op->ors_attrsonly = 0;
1337 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1338 op->ors_filterstr = si->si_filterstr;
1340 op->o_nocaching = 1;
1341 op->o_managedsait = 0;
1343 if ( limits_check( op, &rs_search ) == 0 ) {
1344 rc = be->be_search( op, &rs_search );
1347 op->o_managedsait = 1;
1348 op->o_nocaching = 0;
1350 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1352 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1353 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1354 while ( np_list != NULL ) {
1355 LDAP_LIST_REMOVE( np_list, npe_link );
1357 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1358 op->o_tag = LDAP_REQ_DELETE;
1359 op->o_callback = &cb;
1360 cb.sc_response = null_callback;
1362 op->o_req_dn = *np_prev->npe_name;
1363 op->o_req_ndn = *np_prev->npe_nname;
1364 rc = op->o_bd->be_delete( op, &rs_delete );
1366 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1367 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1368 mod->sml_op = LDAP_MOD_REPLACE;
1369 mod->sml_desc = slap_schema.si_ad_objectClass;
1370 mod->sml_type = mod->sml_desc->ad_cname;
1371 mod->sml_values = &gcbva[0];
1373 modtail = &mod->sml_next;
1375 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1376 mod->sml_op = LDAP_MOD_REPLACE;
1377 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1378 mod->sml_type = mod->sml_desc->ad_cname;
1379 mod->sml_values = &gcbva[1];
1381 modtail = &mod->sml_next;
1383 op->o_tag = LDAP_REQ_MODIFY;
1384 op->orm_modlist = modlist;
1386 rc = be->be_modify( op, &rs_modify );
1388 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1389 mlnext = ml->sml_next;
1394 org_req_dn = op->o_req_dn;
1395 org_req_ndn = op->o_req_ndn;
1397 org_ndn = op->o_ndn;
1398 org_managedsait = get_manageDSAit( op );
1399 op->o_dn = op->o_bd->be_rootdn;
1400 op->o_ndn = op->o_bd->be_rootndn;
1401 op->o_managedsait = 1;
1403 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1404 op->o_delete_glue_parent ) {
1405 op->o_delete_glue_parent = 0;
1406 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1407 slap_callback cb = { NULL };
1408 cb.sc_response = slap_null_cb;
1409 dnParent( &op->o_req_ndn, &pdn );
1411 op->o_req_ndn = pdn;
1412 op->o_callback = &cb;
1413 /* give it a root privil ? */
1414 op->o_bd->be_delete( op, &rs_delete );
1420 op->o_managedsait = org_managedsait;
1422 op->o_ndn = org_ndn;
1423 op->o_req_dn = org_req_dn;
1424 op->o_req_ndn = org_req_ndn;
1425 op->o_delete_glue_parent = 0;
1427 ber_bvfree( np_prev->npe_name );
1428 ber_bvfree( np_prev->npe_nname );
1429 op->o_req_dn.bv_val = NULL;
1430 op->o_req_ndn.bv_val = NULL;
1443 Backend *be = op->o_bd;
1444 slap_callback cb = { NULL };
1449 struct berval dn = {0, NULL};
1450 struct berval ndn = {0, NULL};
1452 SlapReply rs_add = {REP_RESULT};
1455 op->o_tag = LDAP_REQ_ADD;
1456 op->o_callback = &cb;
1457 cb.sc_response = null_callback;
1458 cb.sc_private = NULL;
1463 /* count RDNs in suffix */
1464 if ( be->be_nsuffix[0].bv_len ) {
1465 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1475 /* Start with BE suffix */
1476 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1477 comma = strrchr(dn.bv_val, ',');
1478 if ( ptr ) *ptr = ',';
1479 if ( comma ) *comma = '\0';
1484 dn.bv_len -= ptr - dn.bv_val;
1487 /* the normalizedDNs are always the same length, no counting
1490 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1491 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1492 ndn.bv_len = be->be_nsuffix[0].bv_len;
1495 while ( ndn.bv_val > e->e_nname.bv_val ) {
1496 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1497 ber_dupbv( &glue->e_name, &dn );
1498 ber_dupbv( &glue->e_nname, &ndn );
1500 a = ch_calloc( 1, sizeof( Attribute ));
1501 a->a_desc = slap_schema.si_ad_objectClass;
1503 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1504 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1505 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1506 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1508 a->a_nvals = a->a_vals;
1510 a->a_next = glue->e_attrs;
1513 a = ch_calloc( 1, sizeof( Attribute ));
1514 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1516 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1517 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1518 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1520 a->a_nvals = a->a_vals;
1522 a->a_next = glue->e_attrs;
1525 op->o_req_dn = glue->e_name;
1526 op->o_req_ndn = glue->e_nname;
1528 rc = be->be_add ( op, &rs_add );
1529 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1530 be_entry_release_w( op, glue );
1532 /* incl. ALREADY EXIST */
1536 /* Move to next child */
1537 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1540 if ( ptr == e->e_name.bv_val ) break;
1542 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1543 for( ptr = ndn.bv_val-2;
1544 ptr > e->e_nname.bv_val && *ptr != ',';
1550 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1553 op->o_req_dn = e->e_name;
1554 op->o_req_ndn = e->e_nname;
1556 rc = be->be_add ( op, &rs_add );
1557 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1558 be_entry_release_w( op, e );
1566 static struct berval ocbva[] = {
1568 BER_BVC("subentry"),
1569 BER_BVC("syncConsumerSubentry"),
1573 static struct berval cnbva[] = {
1578 static struct berval ssbva[] = {
1583 static struct berval scbva[] = {
1589 syncrepl_updateCookie(
1593 struct sync_cookie *syncCookie )
1595 Backend *be = op->o_bd;
1597 Modifications *mlnext;
1599 Modifications *modlist = NULL;
1600 Modifications **modtail = &modlist;
1603 char txtbuf[SLAP_TEXT_BUFLEN];
1604 size_t textlen = sizeof txtbuf;
1609 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1610 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1611 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1613 slap_callback cb = { NULL };
1614 SlapReply rs_add = {REP_RESULT};
1615 SlapReply rs_modify = {REP_RESULT};
1617 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1618 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1620 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1621 mod->sml_op = LDAP_MOD_REPLACE;
1622 mod->sml_desc = slap_schema.si_ad_objectClass;
1623 mod->sml_type = mod->sml_desc->ad_cname;
1624 mod->sml_values = ocbva;
1626 modtail = &mod->sml_next;
1628 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1629 assert( si->si_rid < 1000 );
1630 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1631 slap_syncrepl_bvc.bv_len + 1,
1632 "syncrepl%ld", si->si_rid );
1633 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1634 mod->sml_op = LDAP_MOD_REPLACE;
1635 mod->sml_desc = slap_schema.si_ad_cn;
1636 mod->sml_type = mod->sml_desc->ad_cname;
1637 mod->sml_values = cnbva;
1639 modtail = &mod->sml_next;
1641 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1642 mod->sml_op = LDAP_MOD_REPLACE;
1643 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1644 mod->sml_type = mod->sml_desc->ad_cname;
1645 mod->sml_values = ssbva;
1647 modtail = &mod->sml_next;
1649 /* Keep this last, so we can avoid touching the previous
1650 * attributes unnecessarily.
1652 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1653 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1654 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1655 mod->sml_op = LDAP_MOD_REPLACE;
1656 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1657 mod->sml_type = mod->sml_desc->ad_cname;
1658 mod->sml_values = scbva;
1660 modtail = &mod->sml_next;
1664 op->o_tag = LDAP_REQ_ADD;
1665 rc = slap_mods_opattrs( op, modlist, modtail,
1666 &text, txtbuf, textlen, 0 );
1668 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1669 ml->sml_op = LDAP_MOD_REPLACE;
1672 if( rc != LDAP_SUCCESS ) {
1673 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1677 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1679 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1680 assert( si->si_rid < 1000 );
1681 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1682 slap_syncrepl_cn_bvc.bv_len + 1,
1683 "cn=syncrepl%ld", si->si_rid );
1685 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1687 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1688 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1690 if ( slap_syncrepl_dn_bv.bv_val ) {
1691 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1696 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1698 if( rc != LDAP_SUCCESS ) {
1699 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1703 cb.sc_response = null_callback;
1706 op->o_callback = &cb;
1707 op->o_req_dn = e->e_name;
1708 op->o_req_ndn = e->e_nname;
1710 /* update persistent cookie */
1711 update_cookie_retry:
1712 op->o_tag = LDAP_REQ_MODIFY;
1713 /* Just modify the cookie value, not the entire entry */
1714 op->orm_modlist = mod;
1715 rc = be->be_modify( op, &rs_modify );
1717 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1718 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1719 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1720 op->o_tag = LDAP_REQ_ADD;
1722 rc = be->be_add( op, &rs_add );
1723 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1724 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1725 goto update_cookie_retry;
1726 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1727 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1728 Debug( LDAP_DEBUG_ANY,
1729 "cookie will be non-persistent\n",
1732 Debug( LDAP_DEBUG_ANY,
1733 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1736 be_entry_release_w( op, e );
1740 Debug( LDAP_DEBUG_ANY,
1741 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1751 if ( cnbva[0].bv_val ) {
1752 ch_free( cnbva[0].bv_val );
1753 cnbva[0].bv_val = NULL;
1755 if ( scbva[0].bv_val ) {
1756 ch_free( scbva[0].bv_val );
1757 scbva[0].bv_val = NULL;
1760 if ( mlnext->sml_next ) {
1761 slap_mods_free( mlnext->sml_next );
1762 mlnext->sml_next = NULL;
1765 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1766 mlnext = ml->sml_next;
1774 syncrepl_isupdate( Operation *op )
1776 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1780 syncrepl_isupdate_dn(
1782 struct berval* ndn )
1787 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1788 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1789 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1802 syncinfo_t *si = op->o_callback->sc_private;
1804 if ( rs->sr_type == REP_SEARCH ) {
1805 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1806 Debug( LDAP_DEBUG_ANY,
1807 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1809 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1811 } else if ( rs->sr_type == REP_RESULT ) {
1812 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1813 Debug( LDAP_DEBUG_ANY,
1814 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1818 return LDAP_SUCCESS;
1822 nonpresent_callback(
1826 syncinfo_t *si = op->o_callback->sc_private;
1829 struct berval* present_uuid = NULL;
1830 struct nonpresent_entry *np_entry;
1832 if ( rs->sr_type == REP_RESULT ) {
1833 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1834 si->si_presentlist = NULL;
1836 } else if ( rs->sr_type == REP_SEARCH ) {
1837 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1839 if ( a == NULL ) return 0;
1841 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1844 if ( present_uuid == NULL ) {
1845 np_entry = (struct nonpresent_entry *)
1846 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1847 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1848 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1849 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1852 avl_delete( &si->si_presentlist,
1853 &a->a_nvals[0], syncuuid_cmp );
1854 ch_free( present_uuid->bv_val );
1855 ch_free( present_uuid );
1858 return LDAP_SUCCESS;
1866 if ( rs->sr_err != LDAP_SUCCESS &&
1867 rs->sr_err != LDAP_REFERRAL &&
1868 rs->sr_err != LDAP_ALREADY_EXISTS &&
1869 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1870 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1872 Debug( LDAP_DEBUG_ANY,
1873 "null_callback : error code 0x%x\n",
1876 return LDAP_SUCCESS;
1880 slap_create_syncrepl_entry(
1882 struct berval *context_csn,
1890 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1892 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1894 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1897 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1899 if ( context_csn ) {
1900 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1901 context_csn, NULL );
1905 bv.bv_len = sizeof("{}")-1;
1906 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1908 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1909 ber_dupbv( &e->e_nname, &e->e_name );
1915 slap_uuidstr_from_normalized(
1916 struct berval* uuidstr,
1917 struct berval* normalized,
1921 unsigned char nibble;
1924 if ( normalized == NULL ) return NULL;
1925 if ( normalized->bv_len != 16 ) return NULL;
1930 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
1935 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1936 if ( !uuidstr ) slap_sl_free( new, ctx );
1940 for ( i = 0; i < 16; i++ ) {
1941 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1942 new->bv_val[(i<<1)+d] = '-';
1946 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1947 if ( nibble < 10 ) {
1948 new->bv_val[(i<<1)+d] = nibble + '0';
1950 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1953 nibble = (normalized->bv_val[i]) & 0xF;
1954 if ( nibble < 10 ) {
1955 new->bv_val[(i<<1)+d+1] = nibble + '0';
1957 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1961 new->bv_val[new->bv_len] = '\0';
1966 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1968 const struct berval *uuid1 = v_uuid1;
1969 const struct berval *uuid2 = v_uuid2;
1970 int rc = uuid1->bv_len - uuid2->bv_len;
1971 if ( rc ) return rc;
1972 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
1976 avl_ber_bvfree( void *bv )
1978 if( bv == NULL ) return;
1979 if ( ((struct berval *)bv)->bv_val != NULL ) {
1980 ch_free( ((struct berval *)bv)->bv_val );
1982 ch_free( (char *) bv );
1986 syncinfo_free( syncinfo_t *sie )
1988 if ( sie->si_provideruri ) {
1989 ch_free( sie->si_provideruri );
1991 if ( sie->si_provideruri_bv ) {
1992 ber_bvarray_free( sie->si_provideruri_bv );
1994 if ( sie->si_updatedn.bv_val ) {
1995 ch_free( sie->si_updatedn.bv_val );
1997 if ( sie->si_binddn ) {
1998 ch_free( sie->si_binddn );
2000 if ( sie->si_passwd ) {
2001 ch_free( sie->si_passwd );
2003 if ( sie->si_saslmech ) {
2004 ch_free( sie->si_saslmech );
2006 if ( sie->si_secprops ) {
2007 ch_free( sie->si_secprops );
2009 if ( sie->si_realm ) {
2010 ch_free( sie->si_realm );
2012 if ( sie->si_authcId ) {
2013 ch_free( sie->si_authcId );
2015 if ( sie->si_authzId ) {
2016 ch_free( sie->si_authzId );
2018 if ( sie->si_filterstr.bv_val ) {
2019 ch_free( sie->si_filterstr.bv_val );
2021 if ( sie->si_base.bv_val ) {
2022 ch_free( sie->si_base.bv_val );
2024 if ( sie->si_attrs ) {
2026 while ( sie->si_attrs[i] != NULL ) {
2027 ch_free( sie->si_attrs[i] );
2030 ch_free( sie->si_attrs );
2032 if ( sie->si_exattrs ) {
2034 while ( sie->si_exattrs[i] != NULL ) {
2035 ch_free( sie->si_exattrs[i] );
2038 ch_free( sie->si_exattrs );
2040 if ( sie->si_retryinterval ) {
2041 ch_free( sie->si_retryinterval );
2043 if ( sie->si_retrynum ) {
2044 ch_free( sie->si_retrynum );
2046 if ( sie->si_retrynum_init ) {
2047 ch_free( sie->si_retrynum_init );
2049 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2050 if ( sie->si_syncUUID_ndn.bv_val ) {
2051 ch_free( sie->si_syncUUID_ndn.bv_val );
2053 if ( sie->si_presentlist ) {
2054 avl_free( sie->si_presentlist, avl_ber_bvfree );
2057 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2059 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2060 struct nonpresent_entry* npe;
2061 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2062 LDAP_LIST_REMOVE( npe, npe_link );
2063 if ( npe->npe_name ) {
2064 if ( npe->npe_name->bv_val ) {
2065 ch_free( npe->npe_name->bv_val );
2067 ch_free( npe->npe_name );
2069 if ( npe->npe_nname ) {
2070 if ( npe->npe_nname->bv_val ) {
2071 ch_free( npe->npe_nname->bv_val );
2073 ch_free( npe->npe_nname );