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 for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76 ch_free( si->si_attrs[k] );
77 si->si_attrs[k] = si->si_attrs[k+1];
82 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
83 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
85 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
89 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
90 tmp[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
95 tmp = ( char ** ) ch_realloc( si->si_attrs, 3 * sizeof( char * ));
97 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
99 tmp[ n++ ] = ch_strdup( "*" );
100 tmp[ n++ ] = ch_strdup( "+" );
106 for ( n = 0; si->si_exattrs[ n ] != NULL; n++ ) /* empty */;
108 /* Delete Attributes from exattrs list */
109 for ( i = 0; sync_descs[i] != NULL; i++ ) {
110 for ( j = 0; si->si_exattrs[j] != NULL; j++ ) {
111 if ( strcmp( si->si_exattrs[j], sync_descs[i]->ad_cname.bv_val )
114 for ( k = j; si->si_exattrs[k] != NULL; k++ ) {
116 ch_free( si->si_exattrs[k] );
117 si->si_exattrs[k] = si->si_exattrs[k+1];
122 for ( i = 0; si->si_exattrs[i] != NULL; i++ ) {
123 for ( j = 0; si->si_ocs[j]; j++ ) {
124 for ( k = 0; si->si_ocs[j]->soc_required[k]; k++ ) {
125 if (!strcmp( si->si_exattrs[i],
126 si->si_ocs[j]->soc_required[k]->sat_cname.bv_val )) {
127 for ( l = i; si->si_exattrs[l] != NULL; l++ ) {
129 ch_free( si->si_exattrs[l] );
130 si->si_exattrs[l] = si->si_exattrs[l+1];
144 BerElementBuffer berbuf;
145 BerElement *ber = (BerElement *)&berbuf;
146 LDAPControl c[2], *ctrls[3];
147 struct timeval timeout;
151 /* setup LDAP SYNC control */
152 ber_init2( ber, NULL, LBER_USE_DER );
153 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
155 if ( si->si_syncCookie.octet_str &&
156 si->si_syncCookie.octet_str[0].bv_val )
158 ber_printf( ber, "{eO}",
160 &si->si_syncCookie.octet_str[0] );
162 ber_printf( ber, "{e}",
166 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
171 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
172 c[0].ldctl_iscritical = si->si_type < 0;
175 if ( si->si_authzId ) {
176 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
177 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
178 c[1].ldctl_iscritical = 1;
185 timeout.tv_sec = si->si_tlimit;
188 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
189 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
190 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
191 si->si_slimit, &msgid );
202 int cmdline_cookie_found = 0;
204 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
205 struct berval syncrepl_cn_bv;
206 struct sync_cookie *sc = NULL;
207 struct sync_cookie syncCookie = { NULL, -1, NULL };
213 psub = &si->si_be->be_nsuffix[0];
215 /* Init connection to master */
216 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
217 if ( rc != LDAP_SUCCESS ) {
218 Debug( LDAP_DEBUG_ANY,
219 "do_syncrep1: ldap_initialize failed (%s)\n",
220 si->si_provideruri, 0, 0 );
224 op->o_protocol = LDAP_VERSION3;
225 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
230 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
231 if( rc != LDAP_SUCCESS ) {
232 Debug( LDAP_DEBUG_ANY,
233 "%s: ldap_start_tls failed (%d)\n",
234 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
236 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
240 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
241 #ifdef HAVE_CYRUS_SASL
244 if ( si->si_secprops != NULL ) {
245 rc = ldap_set_option( si->si_ld,
246 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
248 if( rc != LDAP_OPT_SUCCESS ) {
249 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
250 "(%s,SECPROPS,\"%s\") failed!\n",
251 si->si_provideruri, si->si_secprops, 0 );
256 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
257 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
259 rc = ldap_sasl_interactive_bind_s( si->si_ld,
267 lutil_sasl_freedefs( defaults );
269 /* FIXME: different error behaviors according to
271 * 2) on err policy : exit, retry, backoff ...
273 if ( rc != LDAP_SUCCESS ) {
274 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
275 "ldap_sasl_interactive_bind_s failed (%d)\n",
278 /* FIXME (see above comment) */
279 /* if Kerberos credentials cache is not active, retry */
280 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
281 rc == LDAP_LOCAL_ERROR )
283 rc = LDAP_SERVER_DOWN;
288 #else /* HAVE_CYRUS_SASL */
289 /* Should never get here, we trapped this at config time */
291 fprintf( stderr, "not compiled with SASL support\n" );
297 rc = ldap_bind_s( si->si_ld,
298 si->si_binddn, si->si_passwd, si->si_bindmethod );
299 if ( rc != LDAP_SUCCESS ) {
300 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
301 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
306 /* Set SSF to strongest of TLS, SASL SSFs */
309 op->o_transport_ssf = 0;
311 if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
312 == LDAP_SUCCESS && ssl != NULL )
314 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
316 #endif /* HAVE_TLS */
317 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
318 op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
319 ? op->o_sasl_ssf : op->o_tls_ssf;
321 /* get syncrepl cookie of shadow replica from subentry */
322 assert( si->si_rid < 1000 );
323 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
324 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
325 CN_STR "syncrepl%ld", si->si_rid );
326 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
327 op->o_req_dn = op->o_req_ndn;
329 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
330 if ( si->si_rid == sc->rid ) {
331 cmdline_cookie_found = 1;
336 if ( cmdline_cookie_found ) {
337 /* cookie is supplied in the command line */
338 BerVarray cookie = NULL;
339 struct berval cookie_bv;
341 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
342 slap_sync_cookie_free( &si->si_syncCookie, 0 );
344 /* read stored cookie if it exists */
345 backend_attribute( op, NULL, &op->o_req_ndn,
346 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
349 /* no stored cookie */
350 if ( sc->ctxcsn == NULL ||
351 sc->ctxcsn->bv_val == NULL ) {
352 /* if cmdline cookie does not have ctxcsn */
353 /* component, set it to an initial value */
354 slap_init_sync_cookie_ctxcsn( sc );
356 slap_dup_sync_cookie( &si->si_syncCookie, sc );
357 slap_sync_cookie_free( sc, 1 );
362 struct berval newcookie = BER_BVNULL;
363 ber_dupbv( &cookie_bv, &cookie[0] );
364 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
365 slap_parse_sync_cookie( &si->si_syncCookie );
366 ber_bvarray_free( si->si_syncCookie.octet_str );
367 si->si_syncCookie.octet_str = NULL;
368 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
369 if ( sc->sid != -1 ) {
370 /* command line cookie wins */
371 si->si_syncCookie.sid = sc->sid;
373 if ( sc->ctxcsn != NULL ) {
374 /* command line cookie wins */
375 if ( si->si_syncCookie.ctxcsn ) {
376 ber_bvarray_free( si->si_syncCookie.ctxcsn );
377 si->si_syncCookie.ctxcsn = NULL;
379 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
380 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
382 if ( sc->rid != -1 ) {
383 /* command line cookie wins */
384 si->si_syncCookie.rid = sc->rid;
386 slap_sync_cookie_free( sc, 1 );
388 slap_compose_sync_cookie( NULL, &newcookie,
389 &si->si_syncCookie.ctxcsn[0],
390 si->si_syncCookie.sid, si->si_syncCookie.rid );
391 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
395 /* no command line cookie is specified */
396 if ( si->si_syncCookie.octet_str == NULL ) {
397 BerVarray cookie = NULL;
398 struct berval cookie_bv;
399 /* try to read stored cookie */
400 backend_attribute( op, NULL, &op->o_req_ndn,
401 slap_schema.si_ad_syncreplCookie, &cookie, ACL_READ );
403 ber_dupbv( &cookie_bv, &cookie[0] );
404 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
405 slap_parse_sync_cookie( &si->si_syncCookie );
406 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
411 rc = ldap_sync_search( si, op->o_tmpmemctx );
413 if( rc != LDAP_SUCCESS ) {
414 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
415 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
421 ldap_unbind( si->si_ld );
426 slap_sl_free( op->o_req_ndn.bv_val, op->o_tmpmemctx );
436 LDAPControl **rctrls = NULL;
439 BerElementBuffer berbuf;
440 BerElement *ber = (BerElement *)&berbuf;
442 LDAPMessage *res = NULL;
443 LDAPMessage *msg = NULL;
446 struct berval *retdata = NULL;
451 struct berval syncUUID = BER_BVNULL;
452 struct sync_cookie syncCookie = { NULL, -1, NULL };
453 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
454 struct berval cookie = BER_BVNULL;
462 Modifications *modlist = NULL;
467 struct timeval *tout_p = NULL;
468 struct timeval tout = { 0, 0 };
470 int refreshDeletes = 0;
472 BerVarray syncUUIDs = NULL;
475 if ( slapd_shutdown ) {
480 ber_init2( ber, NULL, LBER_USE_DER );
481 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
483 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
485 psub = &si->si_be->be_nsuffix[0];
487 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
489 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
495 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
496 tout_p, &res )) > 0 )
498 if ( slapd_shutdown ) {
502 for( msg = ldap_first_message( si->si_ld, res );
504 msg = ldap_next_message( si->si_ld, msg ) )
506 switch( ldap_msgtype( msg ) ) {
507 case LDAP_RES_SEARCH_ENTRY:
508 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
509 /* we can't work without the control */
515 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
516 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
517 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
518 ber_scanf( ber, /*"{"*/ "m}", &cookie );
519 if ( cookie.bv_val ) {
520 struct berval tmp_bv;
521 ber_dupbv( &tmp_bv, &cookie );
522 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
524 if ( syncCookie.octet_str &&
525 syncCookie.octet_str[0].bv_val )
526 slap_parse_sync_cookie( &syncCookie );
528 if ( syncrepl_message_to_entry( si, op, msg,
529 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
530 rc_efree = syncrepl_entry( si, op, entry, modlist,
531 syncstate, &syncUUID, &syncCookie_req );
532 if ( syncCookie.octet_str &&
533 syncCookie.octet_str[0].bv_val )
535 syncrepl_updateCookie( si, op, psub, &syncCookie );
538 ldap_controls_free( rctrls );
540 slap_mods_free( modlist );
542 if ( rc_efree && entry ) {
548 case LDAP_RES_SEARCH_REFERENCE:
549 Debug( LDAP_DEBUG_ANY,
550 "do_syncrep2 : reference received\n", 0, 0, 0 );
553 case LDAP_RES_SEARCH_RESULT:
554 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
558 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
560 ber_scanf( ber, "{" /*"}"*/);
561 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
562 ber_scanf( ber, "m", &cookie );
563 if ( cookie.bv_val ) {
564 struct berval tmp_bv;
565 ber_dupbv( &tmp_bv, &cookie );
566 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
568 if ( syncCookie.octet_str &&
569 syncCookie.octet_str[0].bv_val )
571 slap_parse_sync_cookie( &syncCookie );
574 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
576 ber_scanf( ber, "b", &refreshDeletes );
578 ber_scanf( ber, /*"{"*/ "}" );
580 if ( syncCookie_req.ctxcsn == NULL ) {
582 } else if ( syncCookie.ctxcsn == NULL ) {
585 value_match( &match, slap_schema.si_ad_entryCSN,
586 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
587 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
588 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0],
591 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val &&
592 match < 0 && err == LDAP_SUCCESS )
594 syncrepl_updateCookie( si, op, psub, &syncCookie );
597 ldap_controls_free( rctrls );
599 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
600 /* FIXME : different error behaviors according to
601 * 1) err code : LDAP_BUSY ...
602 * 2) on err policy : stop service, stop sync, retry
604 if ( refreshDeletes == 0 && match < 0 &&
605 err == LDAP_SUCCESS )
607 syncrepl_del_nonpresent( op, si );
609 avl_free( si->si_presentlist, avl_ber_bvfree );
610 si->si_presentlist = NULL;
617 case LDAP_RES_INTERMEDIATE:
618 rc = ldap_parse_intermediate( si->si_ld, msg,
619 &retoid, &retdata, NULL, 0 );
620 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
621 int si_refreshDelete = 0;
622 int si_refreshPresent = 0;
623 ber_init2( ber, retdata, LBER_USE_DER );
625 switch ( si_tag = ber_peek_tag( ber, &len )) {
627 case LDAP_TAG_SYNC_NEW_COOKIE:
628 ber_scanf( ber, "tm", &tag, &cookie );
630 case LDAP_TAG_SYNC_REFRESH_DELETE:
631 si_refreshDelete = 1;
632 case LDAP_TAG_SYNC_REFRESH_PRESENT:
633 si_refreshPresent = 1;
634 ber_scanf( ber, "t{" /*"}"*/, &tag );
635 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
637 ber_scanf( ber, "m", &cookie );
638 if ( cookie.bv_val ) {
639 struct berval tmp_bv;
640 ber_dupbv( &tmp_bv, &cookie );
641 ber_bvarray_add( &syncCookie.octet_str,
644 if ( syncCookie.octet_str &&
645 syncCookie.octet_str[0].bv_val )
647 slap_parse_sync_cookie( &syncCookie );
650 if ( ber_peek_tag( ber, &len ) ==
651 LDAP_TAG_REFRESHDONE )
653 ber_scanf( ber, "b", &refreshDone );
655 ber_scanf( ber, /*"{"*/ "}" );
657 case LDAP_TAG_SYNC_ID_SET:
658 ber_scanf( ber, "t{" /*"}"*/, &tag );
659 if ( ber_peek_tag( ber, &len ) ==
660 LDAP_TAG_SYNC_COOKIE )
662 ber_scanf( ber, "m", &cookie );
663 if ( cookie.bv_val ) {
664 struct berval tmp_bv;
665 ber_dupbv( &tmp_bv, &cookie );
666 ber_bvarray_add( &syncCookie.octet_str,
669 if ( syncCookie.octet_str &&
670 syncCookie.octet_str[0].bv_val )
672 slap_parse_sync_cookie( &syncCookie );
675 if ( ber_peek_tag( ber, &len ) ==
676 LDAP_TAG_REFRESHDELETES )
678 ber_scanf( ber, "b", &refreshDeletes );
680 ber_scanf( ber, "[W]", &syncUUIDs );
681 ber_scanf( ber, /*"{"*/ "}" );
682 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
683 struct berval *syncuuid_bv;
684 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
685 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
686 avl_insert( &si->si_presentlist,
687 (caddr_t) syncuuid_bv,
688 syncuuid_cmp, avl_dup_error );
690 slap_sl_free( syncUUIDs, op->o_tmpmemctx );
693 Debug( LDAP_DEBUG_ANY,
694 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
695 (long) si_tag, 0, 0 );
696 ldap_memfree( retoid );
697 ber_bvfree( retdata );
701 if ( syncCookie_req.ctxcsn == NULL ) {
703 } else if ( syncCookie.ctxcsn == NULL ) {
706 value_match( &match, slap_schema.si_ad_entryCSN,
707 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
708 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
709 &syncCookie_req.ctxcsn[0],
710 &syncCookie.ctxcsn[0], &text );
713 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
716 syncrepl_updateCookie( si, op, psub, &syncCookie);
719 if ( si_refreshPresent == 1 ) {
721 syncrepl_del_nonpresent( op, si );
725 ldap_memfree( retoid );
726 ber_bvfree( retdata );
730 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
731 "unknown intermediate response (%d)\n",
733 ldap_memfree( retoid );
734 ber_bvfree( retdata );
740 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
741 "unknown message\n", 0, 0, 0 );
745 if ( syncCookie.octet_str ) {
746 slap_sync_cookie_free( &syncCookie_req, 0 );
747 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
748 slap_sync_cookie_free( &syncCookie, 0 );
758 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
759 errstr = ldap_err2string( rc );
761 Debug( LDAP_DEBUG_ANY,
762 "do_syncrep2 : %s\n", errstr, 0, 0 );
766 slap_sync_cookie_free( &syncCookie, 0 );
767 slap_sync_cookie_free( &syncCookie_req, 0 );
769 if ( res ) ldap_msgfree( res );
771 if ( rc && si->si_ld ) {
772 ldap_unbind( si->si_ld );
784 struct re_s* rtask = arg;
785 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
786 Connection conn = {0};
788 int rc = LDAP_SUCCESS;
795 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
800 switch( abs( si->si_type )) {
801 case LDAP_SYNC_REFRESH_ONLY:
802 case LDAP_SYNC_REFRESH_AND_PERSIST:
808 if ( slapd_shutdown && si->si_ld ) {
809 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
810 connection_client_stop( s );
811 ldap_unbind( si->si_ld );
816 connection_fake_init( &conn, &op, ctx );
818 /* use global malloc for now */
819 op.o_tmpmemctx = NULL;
820 op.o_tmpmfuncs = &ch_mfuncs;
822 op.o_dn = si->si_updatedn;
823 op.o_ndn = si->si_updatedn;
824 op.o_managedsait = 1;
825 op.o_bd = be = si->si_be;
827 op.o_sync_state.ctxcsn = NULL;
828 op.o_sync_state.sid = -1;
829 op.o_sync_state.octet_str = NULL;
830 op.o_sync_slog_size = -1;
831 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
832 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
834 /* Establish session, do search */
837 rc = do_syncrep1( &op, si );
840 /* Process results */
841 if ( rc == LDAP_SUCCESS ) {
842 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
844 rc = do_syncrep2( &op, si );
846 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
847 /* If we succeeded, enable the connection for further listening.
848 * If we failed, tear down the connection and reschedule.
850 if ( rc == LDAP_SUCCESS ) {
852 rc = connection_client_setup( s, do_syncrepl, arg );
854 connection_client_enable( s );
856 } else if ( !first ) {
860 if ( rc == -2 ) rc = 0;
864 /* At this point, we have 4 cases:
865 * 1) for any hard failure, give up and remove this task
866 * 2) for ServerDown, reschedule this task to run
867 * 3) for Refresh and Success, reschedule to run
868 * 4) for Persist and Success, reschedule to defer
870 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
872 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
873 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
877 connection_client_stop( s );
880 if ( rc == LDAP_SUCCESS ) {
881 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
884 rtask->interval.tv_sec = si->si_interval;
885 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, defer );
886 if ( si->si_retrynum ) {
887 for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
888 si->si_retrynum[i] = si->si_retrynum_init[i];
890 si->si_retrynum[i] = -2;
893 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
894 if ( si->si_retrynum[i] == -1 || si->si_retrynum[i] == -2 )
898 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
899 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
900 LDAP_STAILQ_REMOVE( &be->be_syncinfo, si, syncinfo_s, si_next );
902 } else if ( si->si_retrynum[i] >= -1 ) {
903 if ( si->si_retrynum[i] > 0 )
904 si->si_retrynum[i]--;
905 rtask->interval.tv_sec = si->si_retryinterval[i];
906 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, 0 );
907 slap_wake_listener();
911 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
917 syncrepl_message_to_entry(
921 Modifications **modlist,
927 BerElement *ber = NULL;
930 Modifications **modtail = modlist;
933 char txtbuf[SLAP_TEXT_BUFLEN];
934 size_t textlen = sizeof txtbuf;
936 struct berval bdn = {0, NULL}, dn, ndn;
941 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
942 Debug( LDAP_DEBUG_ANY,
943 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
947 op->o_tag = LDAP_REQ_ADD;
949 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
951 if ( rc != LDAP_SUCCESS ) {
952 Debug( LDAP_DEBUG_ANY,
953 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
957 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
958 ber_dupbv( &op->o_req_dn, &dn );
959 ber_dupbv( &op->o_req_ndn, &ndn );
960 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
961 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
963 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
969 if ( entry == NULL ) {
973 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
975 e->e_name = op->o_req_dn;
976 e->e_nname = op->o_req_ndn;
978 while ( ber_remaining( ber ) ) {
979 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
980 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
985 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
987 mod->sml_op = LDAP_MOD_REPLACE;
988 mod->sml_next = NULL;
989 mod->sml_desc = NULL;
990 mod->sml_type = tmp.sml_type;
991 mod->sml_values = tmp.sml_values;
992 mod->sml_nvalues = NULL;
995 modtail = &mod->sml_next;
998 if ( *modlist == NULL ) {
999 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1005 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1007 if ( rc != LDAP_SUCCESS ) {
1008 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1013 /* Strip out dynamically generated attrs */
1014 for ( modtail = modlist; *modtail ; ) {
1016 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1017 *modtail = mod->sml_next;
1018 slap_mod_free( &mod->sml_mod, 0 );
1021 modtail = &mod->sml_next;
1025 /* Strip out attrs in exattrs list */
1026 for ( modtail = modlist; *modtail ; ) {
1028 if ( ldap_charray_inlist( si->si_exattrs,
1029 mod->sml_desc->ad_type->sat_cname.bv_val )) {
1030 *modtail = mod->sml_next;
1031 slap_mod_free( &mod->sml_mod, 0 );
1034 modtail = &mod->sml_next;
1038 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1039 if( rc != LDAP_SUCCESS ) {
1040 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1045 ber_free ( ber, 0 );
1046 if ( rc != LDAP_SUCCESS ) {
1061 Modifications* modlist,
1063 struct berval* syncUUID,
1064 struct sync_cookie* syncCookie_req )
1066 Backend *be = op->o_bd;
1067 slap_callback cb = { NULL };
1068 struct berval *syncuuid_bv = NULL;
1069 struct berval syncUUID_strrep = BER_BVNULL;
1070 struct berval uuid_bv = BER_BVNULL;
1072 SlapReply rs_search = {REP_RESULT};
1073 SlapReply rs_delete = {REP_RESULT};
1074 SlapReply rs_add = {REP_RESULT};
1075 SlapReply rs_modify = {REP_RESULT};
1077 AttributeAssertion ava = {0};
1078 int rc = LDAP_SUCCESS;
1079 int ret = LDAP_SUCCESS;
1082 struct berval pdn = BER_BVNULL;
1083 struct berval org_req_dn = BER_BVNULL;
1084 struct berval org_req_ndn = BER_BVNULL;
1085 struct berval org_dn = BER_BVNULL;
1086 struct berval org_ndn = BER_BVNULL;
1087 int org_managedsait;
1089 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1090 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1091 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1092 syncuuid_cmp, avl_dup_error );
1095 if ( syncstate == LDAP_SYNC_PRESENT ) {
1097 } else if ( syncstate != LDAP_SYNC_DELETE ) {
1098 if ( entry == NULL ) {
1103 f.f_choice = LDAP_FILTER_EQUALITY;
1105 ava.aa_desc = slap_schema.si_ad_entryUUID;
1106 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1107 ava.aa_value = *syncUUID;
1108 op->ors_filter = &f;
1110 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1111 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1112 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1113 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1114 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1115 syncUUID->bv_val, syncUUID->bv_len );
1116 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1118 op->o_tag = LDAP_REQ_SEARCH;
1119 op->ors_scope = LDAP_SCOPE_SUBTREE;
1121 /* get syncrepl cookie of shadow replica from subentry */
1122 op->o_req_dn = si->si_base;
1123 op->o_req_ndn = si->si_base;
1125 op->o_time = slap_get_time();
1126 op->ors_tlimit = SLAP_NO_LIMIT;
1129 op->ors_attrs = slap_anlist_no_attrs;
1130 op->ors_attrsonly = 1;
1132 /* set callback function */
1133 op->o_callback = &cb;
1134 cb.sc_response = dn_callback;
1137 si->si_syncUUID_ndn.bv_val = NULL;
1139 if ( limits_check( op, &rs_search ) == 0 ) {
1140 rc = be->be_search( op, &rs_search );
1143 if ( op->ors_filterstr.bv_val ) {
1144 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1147 cb.sc_response = null_callback;
1150 if ( rs_search.sr_err == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1153 if ( syncstate != LDAP_SYNC_DELETE ) {
1154 op->o_no_psearch = 1;
1157 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1158 if ( op->o_sync_csn.bv_val ) {
1159 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1164 op->o_req_dn = si->si_syncUUID_ndn;
1165 op->o_req_ndn = si->si_syncUUID_ndn;
1166 op->o_tag = LDAP_REQ_DELETE;
1167 rc = be->be_delete( op, &rs_delete );
1169 org_req_dn = op->o_req_dn;
1170 org_req_ndn = op->o_req_ndn;
1172 org_ndn = op->o_ndn;
1173 org_managedsait = get_manageDSAit( op );
1174 op->o_dn = op->o_bd->be_rootdn;
1175 op->o_ndn = op->o_bd->be_rootndn;
1176 op->o_managedsait = 1;
1178 while ( rs_delete.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1179 op->o_delete_glue_parent = 0;
1180 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1181 slap_callback cb = { NULL };
1182 cb.sc_response = slap_null_cb;
1183 dnParent( &op->o_req_ndn, &pdn );
1185 op->o_req_ndn = pdn;
1186 op->o_callback = &cb;
1187 op->o_bd->be_delete( op, &rs_delete );
1193 op->o_managedsait = org_managedsait;
1195 op->o_ndn = org_ndn;
1196 op->o_req_dn = org_req_dn;
1197 op->o_req_ndn = org_req_ndn;
1198 op->o_delete_glue_parent = 0;
1200 op->o_no_psearch = 0;
1203 switch ( syncstate ) {
1205 case LDAP_SYNC_MODIFY:
1206 if ( rs_search.sr_err == LDAP_SUCCESS ||
1207 rs_search.sr_err == LDAP_REFERRAL ||
1208 rs_search.sr_err == LDAP_NO_SUCH_OBJECT ||
1209 rs_search.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF )
1211 attr_delete( &entry->e_attrs, slap_schema.si_ad_entryUUID );
1212 attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1213 &syncUUID_strrep, syncUUID );
1215 op->o_tag = LDAP_REQ_ADD;
1217 op->o_req_dn = entry->e_name;
1218 op->o_req_ndn = entry->e_nname;
1220 rc = be->be_add( op, &rs_add );
1222 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1223 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS &&
1224 rs_search.sr_err != LDAP_NO_SUCH_OBJECT ) {
1226 Modifications *modtail = modlist;
1230 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1234 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1235 ber_dupbv( &uuid_bv, syncUUID );
1236 mod->sml_op = LDAP_MOD_REPLACE;
1237 mod->sml_desc = slap_schema.si_ad_entryUUID;
1238 mod->sml_type = mod->sml_desc->ad_cname;
1239 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1240 modtail->sml_next = mod;
1242 op->o_tag = LDAP_REQ_MODIFY;
1243 op->orm_modlist = modlist;
1244 op->o_req_dn = entry->e_name;
1245 op->o_req_ndn = entry->e_nname;
1247 rc = be->be_modify( op, &rs_modify );
1248 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1249 Debug( LDAP_DEBUG_ANY,
1250 "syncrepl_entry : be_modify failed (%d)\n",
1251 rs_modify.sr_err, 0, 0 );
1255 } else if ( rs_modify.sr_err == LDAP_REFERRAL ||
1256 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1257 syncrepl_add_glue( op, entry );
1261 Debug( LDAP_DEBUG_ANY,
1262 "syncrepl_entry : be_add failed (%d)\n",
1263 rs_add.sr_err, 0, 0 );
1268 be_entry_release_w( op, entry );
1273 Debug( LDAP_DEBUG_ANY,
1274 "syncrepl_entry : be_search failed (%d)\n",
1275 rs_search.sr_err, 0, 0 );
1280 case LDAP_SYNC_DELETE :
1281 /* Already deleted */
1286 Debug( LDAP_DEBUG_ANY,
1287 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1294 if ( syncUUID_strrep.bv_val ) {
1295 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1297 if ( si->si_syncUUID_ndn.bv_val ) {
1298 ch_free( si->si_syncUUID_ndn.bv_val );
1299 si->si_syncUUID_ndn.bv_val = NULL;
1304 static struct berval gcbva[] = {
1311 syncrepl_del_nonpresent(
1315 Backend* be = op->o_bd;
1316 slap_callback cb = { NULL };
1317 SlapReply rs_search = {REP_RESULT};
1318 SlapReply rs_delete = {REP_RESULT};
1319 SlapReply rs_modify = {REP_RESULT};
1320 struct nonpresent_entry *np_list, *np_prev;
1323 Modifications *mlnext;
1325 Modifications *modlist = NULL;
1326 Modifications **modtail = &modlist;
1328 AttributeName an[2];
1330 struct berval pdn = BER_BVNULL;
1331 struct berval org_req_dn = BER_BVNULL;
1332 struct berval org_req_ndn = BER_BVNULL;
1333 struct berval org_dn = BER_BVNULL;
1334 struct berval org_ndn = BER_BVNULL;
1335 int org_managedsait;
1337 op->o_req_dn = si->si_base;
1338 op->o_req_ndn = si->si_base;
1340 cb.sc_response = nonpresent_callback;
1343 op->o_callback = &cb;
1344 op->o_tag = LDAP_REQ_SEARCH;
1345 op->ors_scope = si->si_scope;
1346 op->ors_deref = LDAP_DEREF_NEVER;
1347 op->o_time = slap_get_time();
1348 op->ors_tlimit = SLAP_NO_LIMIT;
1349 op->ors_slimit = SLAP_NO_LIMIT;
1351 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1352 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1353 an[0].an_desc = slap_schema.si_ad_entryUUID;
1356 op->ors_attrsonly = 0;
1357 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1358 op->ors_filterstr = si->si_filterstr;
1360 op->o_nocaching = 1;
1361 op->o_managedsait = 0;
1363 if ( limits_check( op, &rs_search ) == 0 ) {
1364 rc = be->be_search( op, &rs_search );
1367 op->o_managedsait = 1;
1368 op->o_nocaching = 0;
1370 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1372 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1373 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1374 while ( np_list != NULL ) {
1375 LDAP_LIST_REMOVE( np_list, npe_link );
1377 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1378 op->o_tag = LDAP_REQ_DELETE;
1379 op->o_callback = &cb;
1380 cb.sc_response = null_callback;
1382 op->o_req_dn = *np_prev->npe_name;
1383 op->o_req_ndn = *np_prev->npe_nname;
1384 rc = op->o_bd->be_delete( op, &rs_delete );
1386 if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1387 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1388 mod->sml_op = LDAP_MOD_REPLACE;
1389 mod->sml_desc = slap_schema.si_ad_objectClass;
1390 mod->sml_type = mod->sml_desc->ad_cname;
1391 mod->sml_values = &gcbva[0];
1393 modtail = &mod->sml_next;
1395 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1396 mod->sml_op = LDAP_MOD_REPLACE;
1397 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1398 mod->sml_type = mod->sml_desc->ad_cname;
1399 mod->sml_values = &gcbva[1];
1401 modtail = &mod->sml_next;
1403 op->o_tag = LDAP_REQ_MODIFY;
1404 op->orm_modlist = modlist;
1406 rc = be->be_modify( op, &rs_modify );
1408 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1409 mlnext = ml->sml_next;
1414 org_req_dn = op->o_req_dn;
1415 org_req_ndn = op->o_req_ndn;
1417 org_ndn = op->o_ndn;
1418 org_managedsait = get_manageDSAit( op );
1419 op->o_dn = op->o_bd->be_rootdn;
1420 op->o_ndn = op->o_bd->be_rootndn;
1421 op->o_managedsait = 1;
1423 while ( rs_delete.sr_err == LDAP_SUCCESS &&
1424 op->o_delete_glue_parent ) {
1425 op->o_delete_glue_parent = 0;
1426 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1427 slap_callback cb = { NULL };
1428 cb.sc_response = slap_null_cb;
1429 dnParent( &op->o_req_ndn, &pdn );
1431 op->o_req_ndn = pdn;
1432 op->o_callback = &cb;
1433 /* give it a root privil ? */
1434 op->o_bd->be_delete( op, &rs_delete );
1440 op->o_managedsait = org_managedsait;
1442 op->o_ndn = org_ndn;
1443 op->o_req_dn = org_req_dn;
1444 op->o_req_ndn = org_req_ndn;
1445 op->o_delete_glue_parent = 0;
1447 ber_bvfree( np_prev->npe_name );
1448 ber_bvfree( np_prev->npe_nname );
1449 op->o_req_dn.bv_val = NULL;
1450 op->o_req_ndn.bv_val = NULL;
1463 Backend *be = op->o_bd;
1464 slap_callback cb = { NULL };
1469 struct berval dn = {0, NULL};
1470 struct berval ndn = {0, NULL};
1472 SlapReply rs_add = {REP_RESULT};
1475 op->o_tag = LDAP_REQ_ADD;
1476 op->o_callback = &cb;
1477 cb.sc_response = null_callback;
1478 cb.sc_private = NULL;
1483 /* count RDNs in suffix */
1484 if ( be->be_nsuffix[0].bv_len ) {
1485 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1495 /* Start with BE suffix */
1496 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1497 comma = strrchr(dn.bv_val, ',');
1498 if ( ptr ) *ptr = ',';
1499 if ( comma ) *comma = '\0';
1504 dn.bv_len -= ptr - dn.bv_val;
1507 /* the normalizedDNs are always the same length, no counting
1510 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1511 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1512 ndn.bv_len = be->be_nsuffix[0].bv_len;
1515 while ( ndn.bv_val > e->e_nname.bv_val ) {
1516 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1517 ber_dupbv( &glue->e_name, &dn );
1518 ber_dupbv( &glue->e_nname, &ndn );
1520 a = ch_calloc( 1, sizeof( Attribute ));
1521 a->a_desc = slap_schema.si_ad_objectClass;
1523 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1524 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1525 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1526 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1528 a->a_nvals = a->a_vals;
1530 a->a_next = glue->e_attrs;
1533 a = ch_calloc( 1, sizeof( Attribute ));
1534 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1536 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1537 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1538 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1540 a->a_nvals = a->a_vals;
1542 a->a_next = glue->e_attrs;
1545 op->o_req_dn = glue->e_name;
1546 op->o_req_ndn = glue->e_nname;
1548 rc = be->be_add ( op, &rs_add );
1549 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1550 be_entry_release_w( op, glue );
1552 /* incl. ALREADY EXIST */
1556 /* Move to next child */
1557 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1560 if ( ptr == e->e_name.bv_val ) break;
1562 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1563 for( ptr = ndn.bv_val-2;
1564 ptr > e->e_nname.bv_val && *ptr != ',';
1570 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1573 op->o_req_dn = e->e_name;
1574 op->o_req_ndn = e->e_nname;
1576 rc = be->be_add ( op, &rs_add );
1577 if ( rs_add.sr_err == LDAP_SUCCESS ) {
1578 be_entry_release_w( op, e );
1586 static struct berval ocbva[] = {
1588 BER_BVC("subentry"),
1589 BER_BVC("syncConsumerSubentry"),
1593 static struct berval cnbva[] = {
1598 static struct berval ssbva[] = {
1603 static struct berval scbva[] = {
1609 syncrepl_updateCookie(
1613 struct sync_cookie *syncCookie )
1615 Backend *be = op->o_bd;
1617 Modifications *mlnext;
1619 Modifications *modlist = NULL;
1620 Modifications **modtail = &modlist;
1623 char txtbuf[SLAP_TEXT_BUFLEN];
1624 size_t textlen = sizeof txtbuf;
1629 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1630 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1631 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1633 slap_callback cb = { NULL };
1634 SlapReply rs_add = {REP_RESULT};
1635 SlapReply rs_modify = {REP_RESULT};
1637 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1638 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1640 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1641 mod->sml_op = LDAP_MOD_REPLACE;
1642 mod->sml_desc = slap_schema.si_ad_objectClass;
1643 mod->sml_type = mod->sml_desc->ad_cname;
1644 mod->sml_values = ocbva;
1646 modtail = &mod->sml_next;
1648 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1649 assert( si->si_rid < 1000 );
1650 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1651 slap_syncrepl_bvc.bv_len + 1,
1652 "syncrepl%ld", si->si_rid );
1653 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1654 mod->sml_op = LDAP_MOD_REPLACE;
1655 mod->sml_desc = slap_schema.si_ad_cn;
1656 mod->sml_type = mod->sml_desc->ad_cname;
1657 mod->sml_values = cnbva;
1659 modtail = &mod->sml_next;
1661 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1662 mod->sml_op = LDAP_MOD_REPLACE;
1663 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1664 mod->sml_type = mod->sml_desc->ad_cname;
1665 mod->sml_values = ssbva;
1667 modtail = &mod->sml_next;
1669 /* Keep this last, so we can avoid touching the previous
1670 * attributes unnecessarily.
1672 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1673 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1674 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1675 mod->sml_op = LDAP_MOD_REPLACE;
1676 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1677 mod->sml_type = mod->sml_desc->ad_cname;
1678 mod->sml_values = scbva;
1680 modtail = &mod->sml_next;
1684 op->o_tag = LDAP_REQ_ADD;
1685 rc = slap_mods_opattrs( op, modlist, modtail,
1686 &text, txtbuf, textlen, 0 );
1688 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1689 ml->sml_op = LDAP_MOD_REPLACE;
1692 if( rc != LDAP_SUCCESS ) {
1693 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1697 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1699 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1700 assert( si->si_rid < 1000 );
1701 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1702 slap_syncrepl_cn_bvc.bv_len + 1,
1703 "cn=syncrepl%ld", si->si_rid );
1705 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1707 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1708 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1710 if ( slap_syncrepl_dn_bv.bv_val ) {
1711 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1716 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1718 if( rc != LDAP_SUCCESS ) {
1719 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1723 cb.sc_response = null_callback;
1726 op->o_callback = &cb;
1727 op->o_req_dn = e->e_name;
1728 op->o_req_ndn = e->e_nname;
1730 /* update persistent cookie */
1731 update_cookie_retry:
1732 op->o_tag = LDAP_REQ_MODIFY;
1733 /* Just modify the cookie value, not the entire entry */
1734 op->orm_modlist = mod;
1735 rc = be->be_modify( op, &rs_modify );
1737 if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1738 if ( rs_modify.sr_err == LDAP_REFERRAL ||
1739 rs_modify.sr_err == LDAP_NO_SUCH_OBJECT ) {
1740 op->o_tag = LDAP_REQ_ADD;
1742 rc = be->be_add( op, &rs_add );
1743 if ( rs_add.sr_err != LDAP_SUCCESS ) {
1744 if ( rs_add.sr_err == LDAP_ALREADY_EXISTS ) {
1745 goto update_cookie_retry;
1746 } else if ( rs_add.sr_err == LDAP_REFERRAL ||
1747 rs_add.sr_err == LDAP_NO_SUCH_OBJECT ) {
1748 Debug( LDAP_DEBUG_ANY,
1749 "cookie will be non-persistent\n",
1752 Debug( LDAP_DEBUG_ANY,
1753 "be_add failed (%d)\n", rs_add.sr_err, 0, 0 );
1756 be_entry_release_w( op, e );
1760 Debug( LDAP_DEBUG_ANY,
1761 "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
1771 if ( cnbva[0].bv_val ) {
1772 ch_free( cnbva[0].bv_val );
1773 cnbva[0].bv_val = NULL;
1775 if ( scbva[0].bv_val ) {
1776 ch_free( scbva[0].bv_val );
1777 scbva[0].bv_val = NULL;
1780 if ( mlnext->sml_next ) {
1781 slap_mods_free( mlnext->sml_next );
1782 mlnext->sml_next = NULL;
1785 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1786 mlnext = ml->sml_next;
1794 syncrepl_isupdate( Operation *op )
1796 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1800 syncrepl_isupdate_dn(
1802 struct berval* ndn )
1807 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1808 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1809 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1822 syncinfo_t *si = op->o_callback->sc_private;
1824 if ( rs->sr_type == REP_SEARCH ) {
1825 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1826 Debug( LDAP_DEBUG_ANY,
1827 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1829 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, NULL );
1831 } else if ( rs->sr_type == REP_RESULT ) {
1832 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
1833 Debug( LDAP_DEBUG_ANY,
1834 "dn_callback : consistency error - entryUUID is not unique\n", 0, 0, 0 );
1838 return LDAP_SUCCESS;
1842 nonpresent_callback(
1846 syncinfo_t *si = op->o_callback->sc_private;
1849 struct berval* present_uuid = NULL;
1850 struct nonpresent_entry *np_entry;
1852 if ( rs->sr_type == REP_RESULT ) {
1853 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1854 si->si_presentlist = NULL;
1856 } else if ( rs->sr_type == REP_SEARCH ) {
1857 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1859 if ( a == NULL ) return 0;
1861 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1864 if ( present_uuid == NULL ) {
1865 np_entry = (struct nonpresent_entry *)
1866 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1867 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1868 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1869 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1872 avl_delete( &si->si_presentlist,
1873 &a->a_nvals[0], syncuuid_cmp );
1874 ch_free( present_uuid->bv_val );
1875 ch_free( present_uuid );
1878 return LDAP_SUCCESS;
1886 if ( rs->sr_err != LDAP_SUCCESS &&
1887 rs->sr_err != LDAP_REFERRAL &&
1888 rs->sr_err != LDAP_ALREADY_EXISTS &&
1889 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1890 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1892 Debug( LDAP_DEBUG_ANY,
1893 "null_callback : error code 0x%x\n",
1896 return LDAP_SUCCESS;
1900 slap_create_syncrepl_entry(
1902 struct berval *context_csn,
1910 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1912 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1914 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1917 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1919 if ( context_csn ) {
1920 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1921 context_csn, NULL );
1925 bv.bv_len = sizeof("{}")-1;
1926 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1928 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1929 ber_dupbv( &e->e_nname, &e->e_name );
1935 slap_uuidstr_from_normalized(
1936 struct berval* uuidstr,
1937 struct berval* normalized,
1941 unsigned char nibble;
1944 if ( normalized == NULL ) return NULL;
1945 if ( normalized->bv_len != 16 ) return NULL;
1950 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
1955 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1956 if ( !uuidstr ) slap_sl_free( new, ctx );
1960 for ( i = 0; i < 16; i++ ) {
1961 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1962 new->bv_val[(i<<1)+d] = '-';
1966 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1967 if ( nibble < 10 ) {
1968 new->bv_val[(i<<1)+d] = nibble + '0';
1970 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1973 nibble = (normalized->bv_val[i]) & 0xF;
1974 if ( nibble < 10 ) {
1975 new->bv_val[(i<<1)+d+1] = nibble + '0';
1977 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1981 new->bv_val[new->bv_len] = '\0';
1986 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1988 const struct berval *uuid1 = v_uuid1;
1989 const struct berval *uuid2 = v_uuid2;
1990 int rc = uuid1->bv_len - uuid2->bv_len;
1991 if ( rc ) return rc;
1992 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
1996 avl_ber_bvfree( void *bv )
1998 if( bv == NULL ) return;
1999 if ( ((struct berval *)bv)->bv_val != NULL ) {
2000 ch_free( ((struct berval *)bv)->bv_val );
2002 ch_free( (char *) bv );
2006 syncinfo_free( syncinfo_t *sie )
2008 if ( sie->si_provideruri ) {
2009 ch_free( sie->si_provideruri );
2011 if ( sie->si_provideruri_bv ) {
2012 ber_bvarray_free( sie->si_provideruri_bv );
2014 if ( sie->si_updatedn.bv_val ) {
2015 ch_free( sie->si_updatedn.bv_val );
2017 if ( sie->si_binddn ) {
2018 ch_free( sie->si_binddn );
2020 if ( sie->si_passwd ) {
2021 ch_free( sie->si_passwd );
2023 if ( sie->si_saslmech ) {
2024 ch_free( sie->si_saslmech );
2026 if ( sie->si_secprops ) {
2027 ch_free( sie->si_secprops );
2029 if ( sie->si_realm ) {
2030 ch_free( sie->si_realm );
2032 if ( sie->si_authcId ) {
2033 ch_free( sie->si_authcId );
2035 if ( sie->si_authzId ) {
2036 ch_free( sie->si_authzId );
2038 if ( sie->si_filterstr.bv_val ) {
2039 ch_free( sie->si_filterstr.bv_val );
2041 if ( sie->si_base.bv_val ) {
2042 ch_free( sie->si_base.bv_val );
2044 if ( sie->si_attrs ) {
2046 while ( sie->si_attrs[i] != NULL ) {
2047 ch_free( sie->si_attrs[i] );
2050 ch_free( sie->si_attrs );
2052 if ( sie->si_ocs ) {
2053 ch_free( sie->si_ocs );
2055 if ( sie->si_exattrs ) {
2057 while ( sie->si_exattrs[i] != NULL ) {
2058 ch_free( sie->si_exattrs[i] );
2061 ch_free( sie->si_exattrs );
2063 if ( sie->si_retryinterval ) {
2064 ch_free( sie->si_retryinterval );
2066 if ( sie->si_retrynum ) {
2067 ch_free( sie->si_retrynum );
2069 if ( sie->si_retrynum_init ) {
2070 ch_free( sie->si_retrynum_init );
2072 slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2073 if ( sie->si_syncUUID_ndn.bv_val ) {
2074 ch_free( sie->si_syncUUID_ndn.bv_val );
2076 if ( sie->si_presentlist ) {
2077 avl_free( sie->si_presentlist, avl_ber_bvfree );
2080 ldap_ld_free( sie->si_ld, 1, NULL, NULL );
2082 while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2083 struct nonpresent_entry* npe;
2084 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2085 LDAP_LIST_REMOVE( npe, npe_link );
2086 if ( npe->npe_name ) {
2087 if ( npe->npe_name->bv_val ) {
2088 ch_free( npe->npe_name->bv_val );
2090 ch_free( npe->npe_name );
2092 if ( npe->npe_nname ) {
2093 if ( npe->npe_nname->bv_val ) {
2094 ch_free( npe->npe_nname->bv_val );
2096 ch_free( npe->npe_nname );