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 * ));
85 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
87 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
92 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
95 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
97 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
100 tmp[ n++ ] = ch_strdup( "*" );
106 for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108 si->si_attrs[ n ] = NULL;
117 BerElementBuffer berbuf;
118 BerElement *ber = (BerElement *)&berbuf;
119 LDAPControl c[2], *ctrls[3];
120 struct timeval timeout;
124 /* setup LDAP SYNC control */
125 ber_init2( ber, NULL, LBER_USE_DER );
126 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
128 if ( si->si_syncCookie.octet_str &&
129 si->si_syncCookie.octet_str[0].bv_val )
131 ber_printf( ber, "{eO}",
133 &si->si_syncCookie.octet_str[0] );
135 ber_printf( ber, "{e}",
139 if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
144 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
145 c[0].ldctl_iscritical = si->si_type < 0;
148 if ( si->si_authzId ) {
149 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
150 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
151 c[1].ldctl_iscritical = 1;
158 timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
161 rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
162 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
163 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
164 si->si_slimit, &msgid );
175 int cmdline_cookie_found = 0;
177 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
178 struct berval syncrepl_cn_bv;
179 struct sync_cookie *sc = NULL;
180 struct sync_cookie syncCookie = { NULL, -1, NULL };
183 psub = &si->si_be->be_nsuffix[0];
185 /* Init connection to master */
186 rc = ldap_initialize( &si->si_ld, si->si_provideruri );
187 if ( rc != LDAP_SUCCESS ) {
189 LDAP_LOG( OPERATION, ERR,
190 "do_syncrep1: ldap_initialize failed (%s)\n",
191 si->si_provideruri, 0, 0 );
193 Debug( LDAP_DEBUG_ANY,
194 "do_syncrep1: ldap_initialize failed (%s)\n",
195 si->si_provideruri, 0, 0 );
200 op->o_protocol = LDAP_VERSION3;
201 ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
206 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
207 if( rc != LDAP_SUCCESS ) {
209 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
210 "%s: ldap_start_tls failed (%d)\n",
211 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
214 Debug( LDAP_DEBUG_ANY,
215 "%s: ldap_start_tls failed (%d)\n",
216 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
219 if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
223 if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
224 #ifdef HAVE_CYRUS_SASL
227 if ( si->si_secprops != NULL ) {
228 rc = ldap_set_option( si->si_ld,
229 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
231 if( rc != LDAP_OPT_SUCCESS ) {
233 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
234 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
235 si->si_provideruri, si->si_secprops, 0 );
237 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
238 "(%s,SECPROPS,\"%s\") failed!\n",
239 si->si_provideruri, si->si_secprops, 0 );
245 defaults = lutil_sasl_defaults( si->si_ld, si->si_saslmech,
246 si->si_realm, si->si_authcId, si->si_passwd, si->si_authzId );
248 rc = ldap_sasl_interactive_bind_s( si->si_ld,
256 lutil_sasl_freedefs( defaults );
258 /* FIXME: different error behaviors according to
260 * 2) on err policy : exit, retry, backoff ...
262 if ( rc != LDAP_SUCCESS ) {
264 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
265 "ldap_sasl_interactive_bind_s failed (%d)\n",
268 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
269 "ldap_sasl_interactive_bind_s failed (%d)\n",
273 /* FIXME (see above comment) */
274 /* if Kerberos credentials cache is not active, retry */
275 if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
276 rc == LDAP_LOCAL_ERROR )
278 rc = LDAP_SERVER_DOWN;
283 #else /* HAVE_CYRUS_SASL */
284 /* Should never get here, we trapped this at config time */
286 fprintf( stderr, "not compiled with SASL support\n" );
292 rc = ldap_bind_s( si->si_ld,
293 si->si_binddn, si->si_passwd, si->si_bindmethod );
294 if ( rc != LDAP_SUCCESS ) {
296 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
297 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
299 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
300 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
306 /* set SSF for local authorization */
307 ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_ssf );
309 /* get syncrepl cookie of shadow replica from subentry */
310 assert( si->si_rid < 1000 );
311 syncrepl_cn_bv.bv_val = syncrepl_cbuf;
312 syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
313 CN_STR "syncrepl%ld", si->si_rid );
314 build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
315 op->o_req_dn = op->o_req_ndn;
317 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
318 if ( si->si_rid == sc->rid ) {
319 cmdline_cookie_found = 1;
324 if ( cmdline_cookie_found ) {
325 /* cookie is supplied in the command line */
326 BerVarray cookie = NULL;
327 struct berval cookie_bv;
329 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
330 slap_sync_cookie_free( &si->si_syncCookie, 0 );
332 /* read stored cookie if it exists */
333 backend_attribute( op, NULL, &op->o_req_ndn,
334 slap_schema.si_ad_syncreplCookie, &cookie );
337 /* no stored cookie */
338 if ( sc->ctxcsn == NULL ||
339 sc->ctxcsn->bv_val == NULL ) {
340 /* if cmdline cookie does not have ctxcsn */
341 /* component, set it to an initial value */
342 slap_init_sync_cookie_ctxcsn( sc );
344 slap_dup_sync_cookie( &si->si_syncCookie, sc );
345 slap_sync_cookie_free( sc, 1 );
350 struct berval newcookie = BER_BVNULL;
351 ber_dupbv( &cookie_bv, &cookie[0] );
352 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
353 slap_parse_sync_cookie( &si->si_syncCookie );
354 ber_bvarray_free( si->si_syncCookie.octet_str );
355 si->si_syncCookie.octet_str = NULL;
356 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
357 if ( sc->sid != -1 ) {
358 /* command line cookie wins */
359 si->si_syncCookie.sid = sc->sid;
361 if ( sc->ctxcsn != NULL ) {
362 /* command line cookie wins */
363 if ( si->si_syncCookie.ctxcsn ) {
364 ber_bvarray_free( si->si_syncCookie.ctxcsn );
365 si->si_syncCookie.ctxcsn = NULL;
367 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
368 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
370 if ( sc->rid != -1 ) {
371 /* command line cookie wins */
372 si->si_syncCookie.rid = sc->rid;
374 slap_sync_cookie_free( sc, 1 );
376 slap_compose_sync_cookie( NULL, &newcookie,
377 &si->si_syncCookie.ctxcsn[0],
378 si->si_syncCookie.sid, si->si_syncCookie.rid );
379 ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
383 /* no command line cookie is specified */
384 if ( si->si_syncCookie.octet_str == NULL ) {
385 BerVarray cookie = NULL;
386 struct berval cookie_bv;
387 /* try to read stored cookie */
388 backend_attribute( op, NULL, &op->o_req_ndn,
389 slap_schema.si_ad_syncreplCookie, &cookie );
391 ber_dupbv( &cookie_bv, &cookie[0] );
392 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
393 slap_parse_sync_cookie( &si->si_syncCookie );
394 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
399 rc = ldap_sync_search( si, op->o_tmpmemctx );
401 if( rc != LDAP_SUCCESS ) {
403 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
404 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
406 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
407 "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
414 ldap_unbind( si->si_ld );
427 LDAPControl **rctrls = NULL;
430 BerElementBuffer berbuf;
431 BerElement *ber = (BerElement *)&berbuf;
433 LDAPMessage *res = NULL;
434 LDAPMessage *msg = NULL;
437 struct berval *retdata = NULL;
442 struct berval syncUUID = BER_BVNULL;
443 struct sync_cookie syncCookie = { NULL, -1, NULL };
444 struct sync_cookie syncCookie_req = { NULL, -1, NULL };
445 struct berval cookie = BER_BVNULL;
453 Modifications *modlist = NULL;
458 struct timeval *tout_p = NULL;
459 struct timeval tout = { 0, 0 };
461 int refreshDeletes = 0;
463 BerVarray syncUUIDs = NULL;
466 if ( slapd_shutdown ) {
471 ber_init2( ber, NULL, LBER_USE_DER );
472 ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
475 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
477 Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
480 psub = &si->si_be->be_nsuffix[0];
482 slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
484 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
490 while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
491 tout_p, &res )) > 0 )
493 if ( slapd_shutdown ) {
497 for( msg = ldap_first_message( si->si_ld, res );
499 msg = ldap_next_message( si->si_ld, msg ) )
501 switch( ldap_msgtype( msg ) ) {
502 case LDAP_RES_SEARCH_ENTRY:
503 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
504 /* we can't work without the control */
510 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
511 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
512 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
513 ber_scanf( ber, /*"{"*/ "m}", &cookie );
514 if ( cookie.bv_val ) {
515 struct berval tmp_bv;
516 ber_dupbv( &tmp_bv, &cookie );
517 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
519 if ( syncCookie.octet_str &&
520 syncCookie.octet_str[0].bv_val )
521 slap_parse_sync_cookie( &syncCookie );
523 if ( syncrepl_message_to_entry( si, op, msg,
524 &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
525 rc_efree = syncrepl_entry( si, op, entry, modlist,
526 syncstate, &syncUUID, &syncCookie_req );
527 if ( syncCookie.octet_str &&
528 syncCookie.octet_str[0].bv_val )
530 syncrepl_updateCookie( si, op, psub, &syncCookie );
533 ldap_controls_free( rctrls );
535 slap_mods_free( modlist );
537 if ( rc_efree && entry ) {
543 case LDAP_RES_SEARCH_REFERENCE:
545 LDAP_LOG( OPERATION, ERR,
546 "do_syncrep2 : reference received\n", 0, 0, 0 );
548 Debug( LDAP_DEBUG_ANY,
549 "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 avl_insert( &si->si_presentlist,
686 (caddr_t) syncuuid_bv,
687 syncuuid_cmp, avl_dup_error );
689 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
693 LDAP_LOG( OPERATION, ERR,
694 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
695 (long) si_tag, 0, 0 );
697 Debug( LDAP_DEBUG_ANY,
698 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
699 (long) si_tag, 0, 0 );
701 ldap_memfree( retoid );
702 ber_bvfree( retdata );
706 if ( syncCookie_req.ctxcsn == NULL ) {
708 } else if ( syncCookie.ctxcsn == NULL ) {
711 value_match( &match, slap_schema.si_ad_entryCSN,
712 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
713 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
714 &syncCookie_req.ctxcsn[0],
715 &syncCookie.ctxcsn[0], &text );
718 if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val &&
721 syncrepl_updateCookie( si, op, psub, &syncCookie);
724 if ( si_refreshPresent == 1 ) {
726 syncrepl_del_nonpresent( op, si );
730 ldap_memfree( retoid );
731 ber_bvfree( retdata );
736 LDAP_LOG( OPERATION, ERR, "do_syncrep2 :"
737 " unknown intermediate "
738 "response\n", 0, 0, 0 );
740 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
741 "unknown intermediate response (%d)\n",
744 ldap_memfree( retoid );
745 ber_bvfree( retdata );
752 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
753 "unknown message\n", 0, 0, 0 );
755 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
756 "unknown message\n", 0, 0, 0 );
761 if ( syncCookie.octet_str ) {
762 slap_sync_cookie_free( &syncCookie_req, 0 );
763 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
764 slap_sync_cookie_free( &syncCookie, 0 );
774 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
775 errstr = ldap_err2string( rc );
778 LDAP_LOG( OPERATION, ERR,
779 "do_syncrep2 : %s\n", errstr, 0, 0 );
781 Debug( LDAP_DEBUG_ANY,
782 "do_syncrep2 : %s\n", errstr, 0, 0 );
787 slap_sync_cookie_free( &syncCookie, 0 );
788 slap_sync_cookie_free( &syncCookie_req, 0 );
790 if ( res ) ldap_msgfree( res );
792 if ( rc && si->si_ld ) {
793 ldap_unbind( si->si_ld );
805 struct re_s* rtask = arg;
806 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
807 Connection conn = {0};
809 int rc = LDAP_SUCCESS;
815 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
817 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
823 switch( abs( si->si_type )) {
824 case LDAP_SYNC_REFRESH_ONLY:
825 case LDAP_SYNC_REFRESH_AND_PERSIST:
831 if ( slapd_shutdown && si->si_ld ) {
832 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
833 connection_client_stop( s );
834 ldap_unbind( si->si_ld );
839 connection_fake_init( &conn, &op, ctx );
841 op.o_dn = si->si_updatedn;
842 op.o_ndn = si->si_updatedn;
843 op.o_managedsait = 1;
846 op.o_sync_state.ctxcsn = NULL;
847 op.o_sync_state.sid = -1;
848 op.o_sync_state.octet_str = NULL;
849 op.o_sync_slog_size = -1;
850 LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
851 op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
853 /* Establish session, do search */
856 rc = do_syncrep1( &op, si );
859 /* Process results */
860 if ( rc == LDAP_SUCCESS ) {
861 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
863 rc = do_syncrep2( &op, si );
865 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
866 /* If we succeeded, enable the connection for further listening.
867 * If we failed, tear down the connection and reschedule.
869 if ( rc == LDAP_SUCCESS ) {
871 rc = connection_client_setup( s, do_syncrepl,
874 connection_client_enable( s );
876 } else if ( !first ) {
880 if ( rc == -2 ) rc = 0;
884 /* At this point, we have 4 cases:
885 * 1) for any hard failure, give up and remove this task
886 * 2) for ServerDown, reschedule this task to run
887 * 3) for Refresh and Success, reschedule to run
888 * 4) for Persist and Success, reschedule to defer
890 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
891 if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
892 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
896 connection_client_stop( s );
899 if ( rc && rc != LDAP_SERVER_DOWN ) {
900 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
902 if ( rc == LDAP_SERVER_DOWN ||
903 si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
908 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
910 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
916 syncrepl_message_to_entry(
920 Modifications **modlist,
926 BerElement *ber = NULL;
929 Modifications **modtail = modlist;
932 char txtbuf[SLAP_TEXT_BUFLEN];
933 size_t textlen = sizeof txtbuf;
935 struct berval bdn = {0, NULL}, dn, ndn;
940 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
942 LDAP_LOG( OPERATION, ERR,
943 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
945 Debug( LDAP_DEBUG_ANY,
946 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
951 op->o_tag = LDAP_REQ_ADD;
953 rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
955 if ( rc != LDAP_SUCCESS ) {
957 LDAP_LOG( OPERATION, ERR,
958 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
960 Debug( LDAP_DEBUG_ANY,
961 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
966 dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
967 ber_dupbv( &op->o_req_dn, &dn );
968 ber_dupbv( &op->o_req_ndn, &ndn );
969 slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
970 slap_sl_free( dn.bv_val, op->o_tmpmemctx );
972 if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
976 if ( entry == NULL ) {
980 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
982 e->e_name = op->o_req_dn;
983 e->e_nname = op->o_req_ndn;
985 while ( ber_remaining( ber ) ) {
986 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
987 LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
992 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
994 mod->sml_op = LDAP_MOD_REPLACE;
995 mod->sml_next = NULL;
996 mod->sml_desc = NULL;
997 mod->sml_type = tmp.sml_type;
998 mod->sml_values = tmp.sml_values;
999 mod->sml_nvalues = NULL;
1002 modtail = &mod->sml_next;
1005 if ( *modlist == NULL ) {
1007 LDAP_LOG( OPERATION, ERR,
1008 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
1010 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1015 rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
1017 if ( rc != LDAP_SUCCESS ) {
1019 LDAP_LOG( OPERATION, ERR,
1020 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1022 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1028 rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1029 if( rc != LDAP_SUCCESS ) {
1031 LDAP_LOG( OPERATION, ERR,
1032 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1034 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1040 ber_free ( ber, 0 );
1041 if ( rc != LDAP_SUCCESS ) {
1054 Modifications* modlist,
1056 struct berval* syncUUID,
1057 struct sync_cookie* syncCookie_req )
1059 Backend *be = op->o_bd;
1060 slap_callback cb = { NULL };
1061 struct berval *syncuuid_bv = NULL;
1062 struct berval syncUUID_strrep = BER_BVNULL;
1063 struct berval uuid_bv = BER_BVNULL;
1065 SlapReply rs = {REP_RESULT};
1067 AttributeAssertion ava = {0};
1068 int rc = LDAP_SUCCESS;
1069 int ret = LDAP_SUCCESS;
1072 struct berval pdn = BER_BVNULL;
1073 struct berval org_req_dn = BER_BVNULL;
1074 struct berval org_req_ndn = BER_BVNULL;
1075 struct berval org_dn = BER_BVNULL;
1076 struct berval org_ndn = BER_BVNULL;
1077 int org_managedsait;
1079 if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1080 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1081 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1082 syncuuid_cmp, avl_dup_error );
1085 if ( syncstate == LDAP_SYNC_PRESENT ) {
1089 f.f_choice = LDAP_FILTER_EQUALITY;
1091 ava.aa_desc = slap_schema.si_ad_entryUUID;
1092 slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1093 ava.aa_value = *syncUUID;
1094 op->ors_filter = &f;
1096 op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1097 op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1098 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx );
1099 AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1100 AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1101 syncUUID->bv_val, syncUUID->bv_len );
1102 op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1104 op->o_tag = LDAP_REQ_SEARCH;
1105 op->ors_scope = LDAP_SCOPE_SUBTREE;
1107 /* get syncrepl cookie of shadow replica from subentry */
1108 op->o_req_dn = si->si_base;
1109 op->o_req_ndn = si->si_base;
1111 /* set callback function */
1112 op->o_callback = &cb;
1113 cb.sc_response = dn_callback;
1116 si->si_syncUUID_ndn.bv_val = NULL;
1118 if ( limits_check( op, &rs ) == 0 ) {
1119 rc = be->be_search( op, &rs );
1122 if ( op->ors_filterstr.bv_val ) {
1123 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1126 cb.sc_response = null_callback;
1129 if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val ) {
1132 if ( syncstate != LDAP_SYNC_DELETE ) {
1133 op->o_no_psearch = 1;
1136 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1137 if ( op->o_sync_csn.bv_val ) {
1138 subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1143 op->o_req_dn = si->si_syncUUID_ndn;
1144 op->o_req_ndn = si->si_syncUUID_ndn;
1145 op->o_tag = LDAP_REQ_DELETE;
1146 rc = be->be_delete( op, &rs );
1148 org_req_dn = op->o_req_dn;
1149 org_req_ndn = op->o_req_ndn;
1151 org_ndn = op->o_ndn;
1152 org_managedsait = get_manageDSAit( op );
1153 op->o_dn = op->o_bd->be_rootdn;
1154 op->o_ndn = op->o_bd->be_rootndn;
1155 op->o_managedsait = 1;
1157 while ( rs.sr_err == LDAP_SUCCESS && op->o_delete_glue_parent ) {
1158 op->o_delete_glue_parent = 0;
1159 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1160 slap_callback cb = { NULL };
1161 cb.sc_response = slap_null_cb;
1162 dnParent( &op->o_req_ndn, &pdn );
1164 op->o_req_ndn = pdn;
1165 op->o_callback = &cb;
1166 op->o_bd->be_delete( op, &rs );
1172 op->o_managedsait = org_managedsait;
1174 op->o_ndn = org_ndn;
1175 op->o_req_dn = org_req_dn;
1176 op->o_req_ndn = org_req_ndn;
1177 op->o_delete_glue_parent = 0;
1179 op->o_no_psearch = 0;
1182 switch ( syncstate ) {
1184 case LDAP_SYNC_MODIFY:
1185 if ( rc == LDAP_SUCCESS ||
1186 rc == LDAP_REFERRAL ||
1187 rc == LDAP_NO_SUCH_OBJECT ||
1188 rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1190 attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1191 attr_merge_one( e, slap_schema.si_ad_entryUUID,
1192 syncUUID, &ava.aa_value );
1194 op->o_tag = LDAP_REQ_ADD;
1196 op->o_req_dn = e->e_name;
1197 op->o_req_ndn = e->e_nname;
1198 rc = be->be_add( op, &rs );
1200 if ( rc != LDAP_SUCCESS ) {
1201 if ( rc == LDAP_ALREADY_EXISTS ) {
1203 Modifications *modtail = modlist;
1207 for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1211 mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1212 ber_dupbv( &uuid_bv, syncUUID );
1213 mod->sml_op = LDAP_MOD_REPLACE;
1214 mod->sml_desc = slap_schema.si_ad_entryUUID;
1215 mod->sml_type = mod->sml_desc->ad_cname;
1216 ber_bvarray_add( &mod->sml_values, &uuid_bv );
1217 modtail->sml_next = mod;
1219 op->o_tag = LDAP_REQ_MODIFY;
1220 op->orm_modlist = modlist;
1221 op->o_req_dn = e->e_name;
1222 op->o_req_ndn = e->e_nname;
1224 rc = be->be_modify( op, &rs );
1225 if ( rc != LDAP_SUCCESS ) {
1227 LDAP_LOG( OPERATION, ERR,
1228 "syncrepl_entry : be_modify failed (%d)\n",
1231 Debug( LDAP_DEBUG_ANY,
1232 "syncrepl_entry : be_modify failed (%d)\n",
1238 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1239 syncrepl_add_glue( op, e );
1244 LDAP_LOG( OPERATION, ERR,
1245 "syncrepl_entry : be_add failed (%d)\n",
1248 Debug( LDAP_DEBUG_ANY,
1249 "syncrepl_entry : be_add failed (%d)\n",
1256 be_entry_release_w( op, e );
1262 LDAP_LOG( OPERATION, ERR,
1263 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1265 Debug( LDAP_DEBUG_ANY,
1266 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1272 case LDAP_SYNC_DELETE :
1273 /* Already deleted */
1279 LDAP_LOG( OPERATION, ERR,
1280 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1282 Debug( LDAP_DEBUG_ANY,
1283 "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1291 if ( syncUUID_strrep.bv_val ) {
1292 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1294 if ( si->si_syncUUID_ndn.bv_val ) {
1295 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1300 static struct berval gcbva[] = {
1307 syncrepl_del_nonpresent(
1311 Backend* be = op->o_bd;
1312 slap_callback cb = { NULL };
1313 SlapReply rs = {REP_RESULT};
1314 struct nonpresent_entry *np_list, *np_prev;
1317 Modifications *mlnext;
1319 Modifications *modlist = NULL;
1320 Modifications **modtail = &modlist;
1323 struct berval pdn = BER_BVNULL;
1324 struct berval org_req_dn = BER_BVNULL;
1325 struct berval org_req_ndn = BER_BVNULL;
1326 struct berval org_dn = BER_BVNULL;
1327 struct berval org_ndn = BER_BVNULL;
1328 int org_managedsait;
1330 op->o_req_dn = si->si_base;
1331 op->o_req_ndn = si->si_base;
1333 cb.sc_response = nonpresent_callback;
1336 op->o_callback = &cb;
1337 op->o_tag = LDAP_REQ_SEARCH;
1338 op->ors_scope = si->si_scope;
1339 op->ors_deref = LDAP_DEREF_NEVER;
1342 op->ors_attrsonly = 0;
1343 op->ors_attrs = NULL;
1344 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1345 op->ors_filterstr = si->si_filterstr;
1347 op->o_nocaching = 1;
1348 op->o_managedsait = 0;
1350 if ( limits_check( op, &rs ) == 0 ) {
1351 be->be_search( op, &rs );
1354 op->o_managedsait = 1;
1355 op->o_nocaching = 0;
1357 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1359 if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1360 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1361 while ( np_list != NULL ) {
1362 LDAP_LIST_REMOVE( np_list, npe_link );
1364 np_list = LDAP_LIST_NEXT( np_list, npe_link );
1365 op->o_tag = LDAP_REQ_DELETE;
1366 op->o_callback = &cb;
1367 cb.sc_response = null_callback;
1369 op->o_req_dn = *np_prev->npe_name;
1370 op->o_req_ndn = *np_prev->npe_nname;
1371 rc = op->o_bd->be_delete( op, &rs );
1373 if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1374 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1375 mod->sml_op = LDAP_MOD_REPLACE;
1376 mod->sml_desc = slap_schema.si_ad_objectClass;
1377 mod->sml_type = mod->sml_desc->ad_cname;
1378 mod->sml_values = &gcbva[0];
1380 modtail = &mod->sml_next;
1382 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1383 mod->sml_op = LDAP_MOD_REPLACE;
1384 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1385 mod->sml_type = mod->sml_desc->ad_cname;
1386 mod->sml_values = &gcbva[1];
1388 modtail = &mod->sml_next;
1390 op->o_tag = LDAP_REQ_MODIFY;
1391 op->orm_modlist = modlist;
1393 rc = be->be_modify( op, &rs );
1395 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1396 mlnext = ml->sml_next;
1401 org_req_dn = op->o_req_dn;
1402 org_req_ndn = op->o_req_ndn;
1404 org_ndn = op->o_ndn;
1405 org_managedsait = get_manageDSAit( op );
1406 op->o_dn = op->o_bd->be_rootdn;
1407 op->o_ndn = op->o_bd->be_rootndn;
1408 op->o_managedsait = 1;
1410 while ( rs.sr_err == LDAP_SUCCESS &&
1411 op->o_delete_glue_parent ) {
1412 op->o_delete_glue_parent = 0;
1413 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1414 slap_callback cb = { NULL };
1415 cb.sc_response = slap_null_cb;
1416 dnParent( &op->o_req_ndn, &pdn );
1418 op->o_req_ndn = pdn;
1419 op->o_callback = &cb;
1420 /* give it a root privil ? */
1421 op->o_bd->be_delete( op, &rs );
1427 op->o_managedsait = org_managedsait;
1429 op->o_ndn = org_ndn;
1430 op->o_req_dn = org_req_dn;
1431 op->o_req_ndn = org_req_ndn;
1432 op->o_delete_glue_parent = 0;
1434 ber_bvfree( np_prev->npe_name );
1435 ber_bvfree( np_prev->npe_nname );
1436 op->o_req_dn.bv_val = NULL;
1437 op->o_req_ndn.bv_val = NULL;
1450 Backend *be = op->o_bd;
1451 slap_callback cb = { NULL };
1456 struct berval dn = {0, NULL};
1457 struct berval ndn = {0, NULL};
1459 SlapReply rs = {REP_RESULT};
1462 op->o_tag = LDAP_REQ_ADD;
1463 op->o_callback = &cb;
1464 cb.sc_response = null_callback;
1465 cb.sc_private = NULL;
1470 /* count RDNs in suffix */
1471 if ( be->be_nsuffix[0].bv_len ) {
1472 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1482 /* Start with BE suffix */
1483 for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1484 comma = strrchr(dn.bv_val, ',');
1485 if ( ptr ) *ptr = ',';
1486 if ( comma ) *comma = '\0';
1491 dn.bv_len -= ptr - dn.bv_val;
1494 /* the normalizedDNs are always the same length, no counting
1497 if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1498 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1499 ndn.bv_len = be->be_nsuffix[0].bv_len;
1502 while ( ndn.bv_val > e->e_nname.bv_val ) {
1503 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1504 ber_dupbv( &glue->e_name, &dn );
1505 ber_dupbv( &glue->e_nname, &ndn );
1507 a = ch_calloc( 1, sizeof( Attribute ));
1508 a->a_desc = slap_schema.si_ad_objectClass;
1510 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1511 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1512 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1513 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1515 a->a_nvals = a->a_vals;
1517 a->a_next = glue->e_attrs;
1520 a = ch_calloc( 1, sizeof( Attribute ));
1521 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1523 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1524 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1525 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1527 a->a_nvals = a->a_vals;
1529 a->a_next = glue->e_attrs;
1532 op->o_req_dn = glue->e_name;
1533 op->o_req_ndn = glue->e_nname;
1535 rc = be->be_add ( op, &rs );
1536 if ( rc == LDAP_SUCCESS ) {
1537 be_entry_release_w( op, glue );
1539 /* incl. ALREADY EXIST */
1543 /* Move to next child */
1544 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1547 if ( ptr == e->e_name.bv_val ) break;
1549 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1550 for( ptr = ndn.bv_val-2;
1551 ptr > e->e_nname.bv_val && *ptr != ',';
1557 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1560 op->o_req_dn = e->e_name;
1561 op->o_req_ndn = e->e_nname;
1563 rc = be->be_add ( op, &rs );
1564 if ( rc == LDAP_SUCCESS ) {
1565 be_entry_release_w( op, e );
1573 static struct berval ocbva[] = {
1575 BER_BVC("subentry"),
1576 BER_BVC("syncConsumerSubentry"),
1580 static struct berval cnbva[] = {
1585 static struct berval ssbva[] = {
1590 static struct berval scbva[] = {
1596 syncrepl_updateCookie(
1600 struct sync_cookie *syncCookie )
1602 Backend *be = op->o_bd;
1604 Modifications *mlnext;
1606 Modifications *modlist = NULL;
1607 Modifications **modtail = &modlist;
1610 char txtbuf[SLAP_TEXT_BUFLEN];
1611 size_t textlen = sizeof txtbuf;
1616 char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1617 struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1618 struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1620 slap_callback cb = { NULL };
1621 SlapReply rs = {REP_RESULT};
1623 slap_sync_cookie_free( &si->si_syncCookie, 0 );
1624 slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1626 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1627 mod->sml_op = LDAP_MOD_REPLACE;
1628 mod->sml_desc = slap_schema.si_ad_objectClass;
1629 mod->sml_type = mod->sml_desc->ad_cname;
1630 mod->sml_values = ocbva;
1632 modtail = &mod->sml_next;
1634 ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1635 assert( si->si_rid < 1000 );
1636 cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1637 slap_syncrepl_bvc.bv_len + 1,
1638 "syncrepl%ld", si->si_rid );
1639 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1640 mod->sml_op = LDAP_MOD_REPLACE;
1641 mod->sml_desc = slap_schema.si_ad_cn;
1642 mod->sml_type = mod->sml_desc->ad_cname;
1643 mod->sml_values = cnbva;
1645 modtail = &mod->sml_next;
1647 if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1648 ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1649 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1650 mod->sml_op = LDAP_MOD_REPLACE;
1651 mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1652 mod->sml_type = mod->sml_desc->ad_cname;
1653 mod->sml_values = scbva;
1655 modtail = &mod->sml_next;
1657 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1658 mod->sml_op = LDAP_MOD_REPLACE;
1659 mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1660 mod->sml_type = mod->sml_desc->ad_cname;
1661 mod->sml_values = ssbva;
1663 modtail = &mod->sml_next;
1667 op->o_tag = LDAP_REQ_ADD;
1668 rc = slap_mods_opattrs( op, modlist, modtail,
1669 &text, txtbuf, textlen );
1671 for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1672 ml->sml_op = LDAP_MOD_REPLACE;
1675 if( rc != LDAP_SUCCESS ) {
1677 LDAP_LOG( OPERATION, ERR,
1678 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1680 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1685 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1687 slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1688 assert( si->si_rid < 1000 );
1689 slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1690 slap_syncrepl_cn_bvc.bv_len + 1,
1691 "cn=syncrepl%ld", si->si_rid );
1693 build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1695 ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1696 ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1698 if ( slap_syncrepl_dn_bv.bv_val ) {
1699 slap_sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1704 rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1706 if( rc != LDAP_SUCCESS ) {
1708 LDAP_LOG( OPERATION, ERR,
1709 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1711 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1716 cb.sc_response = null_callback;
1719 op->o_callback = &cb;
1720 op->o_req_dn = e->e_name;
1721 op->o_req_ndn = e->e_nname;
1723 /* update persistent cookie */
1724 update_cookie_retry:
1725 op->o_tag = LDAP_REQ_MODIFY;
1726 op->orm_modlist = modlist;
1727 rc = be->be_modify( op, &rs );
1729 if ( rc != LDAP_SUCCESS ) {
1730 if ( rc == LDAP_REFERRAL ||
1731 rc == LDAP_NO_SUCH_OBJECT ) {
1732 op->o_tag = LDAP_REQ_ADD;
1734 rc = be->be_add( op, &rs );
1735 if ( rc != LDAP_SUCCESS ) {
1736 if ( rc == LDAP_ALREADY_EXISTS ) {
1737 goto update_cookie_retry;
1738 } else if ( rc == LDAP_REFERRAL ||
1739 rc == LDAP_NO_SUCH_OBJECT ) {
1741 LDAP_LOG( OPERATION, ERR,
1742 "cookie will be non-persistent\n",
1745 Debug( LDAP_DEBUG_ANY,
1746 "cookie will be non-persistent\n",
1751 LDAP_LOG( OPERATION, ERR,
1752 "be_add failed (%d)\n",
1755 Debug( LDAP_DEBUG_ANY,
1756 "be_add failed (%d)\n",
1761 be_entry_release_w( op, e );
1766 LDAP_LOG( OPERATION, ERR,
1767 "be_modify failed (%d)\n", rc, 0, 0 );
1769 Debug( LDAP_DEBUG_ANY,
1770 "be_modify failed (%d)\n", rc, 0, 0 );
1781 if ( cnbva[0].bv_val ) {
1782 ch_free( cnbva[0].bv_val );
1783 cnbva[0].bv_val = NULL;
1785 if ( scbva[0].bv_val ) {
1786 ch_free( scbva[0].bv_val );
1787 scbva[0].bv_val = NULL;
1790 if ( mlnext->sml_next ) {
1791 slap_mods_free( mlnext->sml_next );
1792 mlnext->sml_next = NULL;
1795 for (ml = modlist ; ml != NULL; ml = mlnext ) {
1796 mlnext = ml->sml_next;
1804 syncrepl_isupdate( Operation *op )
1806 return ( syncrepl_isupdate_dn( op->o_bd, &op->o_ndn ));
1810 syncrepl_isupdate_dn(
1812 struct berval* ndn )
1817 if ( !LDAP_STAILQ_EMPTY( &be->be_syncinfo )) {
1818 LDAP_STAILQ_FOREACH( si, &be->be_syncinfo, si_next ) {
1819 if ( ( ret = dn_match( &si->si_updatedn, ndn ) ) ) {
1832 syncinfo_t *si = op->o_callback->sc_private;
1834 if ( rs->sr_type == REP_SEARCH ) {
1835 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1837 LDAP_LOG( OPERATION, ERR,
1838 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1840 Debug( LDAP_DEBUG_ANY,
1841 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1844 ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1848 return LDAP_SUCCESS;
1852 nonpresent_callback(
1856 syncinfo_t *si = op->o_callback->sc_private;
1859 struct berval* present_uuid = NULL;
1860 struct nonpresent_entry *np_entry;
1862 if ( rs->sr_type == REP_RESULT ) {
1863 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1864 si->si_presentlist = NULL;
1866 } else if ( rs->sr_type == REP_SEARCH ) {
1867 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1869 if ( a == NULL ) return 0;
1871 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1874 if ( present_uuid == NULL ) {
1875 np_entry = (struct nonpresent_entry *)
1876 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1877 np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1878 np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1879 LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1882 avl_delete( &si->si_presentlist,
1883 &a->a_nvals[0], syncuuid_cmp );
1884 ch_free( present_uuid->bv_val );
1885 ch_free( present_uuid );
1888 return LDAP_SUCCESS;
1896 if ( rs->sr_err != LDAP_SUCCESS &&
1897 rs->sr_err != LDAP_REFERRAL &&
1898 rs->sr_err != LDAP_ALREADY_EXISTS &&
1899 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1900 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1903 LDAP_LOG( OPERATION, ERR,
1904 "null_callback : error code 0x%x\n",
1907 Debug( LDAP_DEBUG_ANY,
1908 "null_callback : error code 0x%x\n",
1912 return LDAP_SUCCESS;
1916 slap_create_syncrepl_entry(
1918 struct berval *context_csn,
1926 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1928 attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1930 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1933 attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1935 if ( context_csn ) {
1936 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1937 context_csn, NULL );
1941 bv.bv_len = sizeof("{}")-1;
1942 attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1944 build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1945 ber_dupbv( &e->e_nname, &e->e_name );
1951 slap_uuidstr_from_normalized(
1952 struct berval* uuidstr,
1953 struct berval* normalized,
1957 unsigned char nibble;
1960 if ( normalized == NULL ) return NULL;
1961 if ( normalized->bv_len != 16 ) return NULL;
1966 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
1971 if (( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1972 if ( !uuidstr ) slap_sl_free( new, ctx );
1976 for ( i = 0; i < 16; i++ ) {
1977 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1978 new->bv_val[(i<<1)+d] = '-';
1982 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1983 if ( nibble < 10 ) {
1984 new->bv_val[(i<<1)+d] = nibble + '0';
1986 new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1989 nibble = (normalized->bv_val[i]) & 0xF;
1990 if ( nibble < 10 ) {
1991 new->bv_val[(i<<1)+d+1] = nibble + '0';
1993 new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1997 new->bv_val[new->bv_len] = '\0';
2002 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2004 const struct berval *uuid1 = v_uuid1;
2005 const struct berval *uuid2 = v_uuid2;
2006 int rc = uuid1->bv_len - uuid2->bv_len;
2007 if ( rc ) return rc;
2008 return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2012 avl_ber_bvfree( void *bv )
2014 if( bv == NULL ) return;
2015 if ( ((struct berval *)bv)->bv_val != NULL ) {
2016 ch_free( ((struct berval *)bv)->bv_val );
2018 ch_free( (char *) bv );