]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
sync with HEAD
[openldap] / servers / slapd / syncrepl.c
1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2003 by IBM Corporation.
7  * Portions Copyright 2003 by Howard Chu, Symas Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
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>.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "ldap_pvt.h"
27 #include "lutil.h"
28 #include "slap.h"
29 #include "lutil_ldap.h"
30
31 #include "ldap_rq.h"
32
33 #define SYNCREPL_STR    "syncreplxxx"
34 #define CN_STR  "cn="
35
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);
38
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 * );
42
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 * );
47
48 static AttributeDescription *sync_descs[4];
49
50 struct runqueue_s syncrepl_rq;
51
52 void
53 init_syncrepl(syncinfo_t *si)
54 {
55         int i, j, k, n;
56         char **tmp;
57
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;
62                 sync_descs[3] = NULL;
63         }
64
65         for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
66
67         if ( n ) {
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 )
72                                         == 0 )
73                                 {
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];
77                                         }
78                                 }
79                         }
80                 }
81                 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82                 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
83                 if ( tmp == NULL ) {
84 #ifdef NEW_LOGGING
85                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
86 #else
87                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
88 #endif
89                 }
90         } else {
91                 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
92                 if ( tmp == NULL ) {
93 #ifdef NEW_LOGGING
94                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
95 #else
96                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
97 #endif
98                 }
99                 tmp[ n++ ] = ch_strdup( "*" );
100         }
101         
102         si->si_attrs = tmp;
103
104         /* Add Attributes */
105
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;
109         }
110 }
111
112 static int
113 ldap_sync_search(
114         syncinfo_t *si,
115         void *ctx
116 )
117 {
118         BerElementBuffer berbuf;
119         BerElement *ber = (BerElement *)&berbuf;
120         LDAPControl c[2], *ctrls[3];
121         struct timeval timeout;
122         ber_int_t       msgid;
123         int rc;
124
125         /* setup LDAP SYNC control */
126         ber_init2( ber, NULL, LBER_USE_DER );
127         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
128
129         if ( si->si_syncCookie.octet_str &&
130                  si->si_syncCookie.octet_str[0].bv_val ) {
131                 ber_printf( ber, "{eO}", abs(si->si_type),
132                                         &si->si_syncCookie.octet_str[0] );
133         } else {
134                 ber_printf( ber, "{e}", abs(si->si_type) );
135         }
136
137         if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
138                 ber_free_buf( ber );
139                 return rc;
140         }
141
142         c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143         c[0].ldctl_iscritical = si->si_type < 0;
144         ctrls[0] = &c[0];
145
146         if ( si->si_authzId ) {
147                 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148                 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149                 c[1].ldctl_iscritical = 1;
150                 ctrls[1] = &c[1];
151                 ctrls[2] = NULL;
152         } else {
153                 ctrls[1] = NULL;
154         }
155
156         timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
157         timeout.tv_usec = 0;
158
159         rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160                 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161                 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
162                 si->si_slimit, &msgid );
163         ber_free_buf( ber );
164
165         return rc;
166 }
167
168 static const Listener dummy_list = { {0, ""}, {0, ""} };
169
170 static int
171 do_syncrep1(
172         Operation *op,
173         syncinfo_t *si )
174 {
175         int     rc;
176         int cmdline_cookie_found = 0;
177
178         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
179         struct berval syncrepl_cn_bv;
180         struct sync_cookie      *sc = NULL;
181         struct sync_cookie      syncCookie = { NULL, -1, NULL };
182         struct berval   *psub;
183
184         psub = &si->si_be->be_nsuffix[0];
185
186         /* Init connection to master */
187
188         rc = ldap_initialize( &si->si_ld, si->si_provideruri );
189         if ( rc != LDAP_SUCCESS ) {
190 #ifdef NEW_LOGGING
191                 LDAP_LOG( OPERATION, ERR,
192                         "do_syncrep1: ldap_initialize failed (%s)\n",
193                         si->si_provideruri, 0, 0 );
194 #else
195                 Debug( LDAP_DEBUG_ANY,
196                         "do_syncrep1: ldap_initialize failed (%s)\n",
197                         si->si_provideruri, 0, 0 );
198 #endif
199                 return rc;
200         }
201
202         op->o_protocol = LDAP_VERSION3;
203         ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
204
205         /* Bind to master */
206
207         if ( si->si_tls ) {
208                 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
209                 if( rc != LDAP_SUCCESS ) {
210 #ifdef NEW_LOGGING
211                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
212                                 "%s: ldap_start_tls failed (%d)\n",
213                                 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
214                                 rc, 0 );
215 #else
216                         Debug( LDAP_DEBUG_ANY,
217                                 "%s: ldap_start_tls failed (%d)\n",
218                                 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
219                                 rc, 0 );
220 #endif
221                         if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
222                 }
223         }
224
225         if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
226 #ifdef HAVE_CYRUS_SASL
227                 void *defaults;
228
229                 if ( si->si_secprops != NULL ) {
230                         rc = ldap_set_option( si->si_ld,
231                                 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
232
233                         if( rc != LDAP_OPT_SUCCESS ) {
234 #ifdef NEW_LOGGING
235                                 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
236                                         "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
237                                         si->si_provideruri, si->si_secprops, 0 );
238 #else
239                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
240                                         "(%s,SECPROPS,\"%s\") failed!\n",
241                                         si->si_provideruri, si->si_secprops, 0 );
242 #endif
243                                 goto done;
244                         }
245                 }
246
247                 defaults = lutil_sasl_defaults( si->si_ld,
248                         si->si_saslmech, si->si_realm,
249                         si->si_authcId, si->si_passwd, si->si_authzId );
250
251                 rc = ldap_sasl_interactive_bind_s( si->si_ld,
252                                 si->si_binddn,
253                                 si->si_saslmech,
254                                 NULL, NULL,
255                                 LDAP_SASL_QUIET,
256                                 lutil_sasl_interact,
257                                 defaults );
258
259                 lutil_sasl_freedefs( defaults );
260
261                 /* FIXME : different error behaviors according to
262                  *      1) return code
263                  *      2) on err policy : exit, retry, backoff ...
264                  */
265                 if ( rc != LDAP_SUCCESS ) {
266 #ifdef NEW_LOGGING
267                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
268                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
269                                 rc, 0, 0 );
270 #else
271                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
272                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
273                                 rc, 0, 0 );
274 #endif
275                         goto done;
276                 }
277 #else /* HAVE_CYRUS_SASL */
278                 /* Should never get here, we trapped this at config time */
279                 fprintf( stderr, "not compiled with SASL support\n" );
280                 rc = LDAP_OTHER;
281                 goto done;
282 #endif
283         } else {
284                 rc = ldap_bind_s( si->si_ld, si->si_binddn, si->si_passwd, si->si_bindmethod );
285                 if ( rc != LDAP_SUCCESS ) {
286 #ifdef NEW_LOGGING
287                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
288                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
289 #else
290                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
291                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
292 #endif
293                         goto done;
294                 }
295         }
296
297         /* get syncrepl cookie of shadow replica from subentry */
298
299         assert( si->si_rid < 1000 );
300         syncrepl_cn_bv.bv_val = syncrepl_cbuf;
301         syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
302                 CN_STR "syncrepl%ld", si->si_rid );
303         build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
304         op->o_req_dn = op->o_req_ndn;
305
306         LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
307                 if ( si->si_rid == sc->rid ) {
308                         cmdline_cookie_found = 1;
309                         break;
310                 }
311         }
312
313         if ( cmdline_cookie_found ) {
314                 /* cookie is supplied in the command line */
315                 BerVarray cookie = NULL;
316                 struct berval cookie_bv;
317
318                 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
319                 slap_sync_cookie_free( &si->si_syncCookie, 0 );
320
321                 /* read stored cookie if it exists */
322                 backend_attribute( op, NULL, &op->o_req_ndn,
323                         slap_schema.si_ad_syncreplCookie, &cookie );
324
325                 if ( !cookie ) {
326                         /* no stored cookie */
327                         if ( sc->ctxcsn == NULL ||
328                                  sc->ctxcsn->bv_val == NULL ) {
329                                 /* if cmdline cookie does not have ctxcsn */
330                                 /* component, set it to an initial value */
331                                 slap_init_sync_cookie_ctxcsn( sc );
332                         }
333                         slap_dup_sync_cookie( &si->si_syncCookie, sc );
334                         slap_sync_cookie_free( sc, 1 );
335                         sc = NULL;
336                 } else {
337                         /* stored cookie */
338                         struct berval newcookie = { 0, NULL };
339                         ber_dupbv( &cookie_bv, &cookie[0] );
340                         ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
341                         slap_parse_sync_cookie( &si->si_syncCookie );
342                         ber_bvarray_free( si->si_syncCookie.octet_str );
343                         si->si_syncCookie.octet_str = NULL;
344                         ber_bvarray_free_x( cookie, op->o_tmpmemctx );
345                         if ( sc->sid != -1 ) {
346                                 /* command line cookie wins */
347                                 si->si_syncCookie.sid = sc->sid;
348                         }
349                         if ( sc->ctxcsn != NULL ) {
350                                 /* command line cookie wins */
351                                 if ( si->si_syncCookie.ctxcsn ) {
352                                         ber_bvarray_free( si->si_syncCookie.ctxcsn );
353                                         si->si_syncCookie.ctxcsn = NULL;
354                                 }
355                                 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
356                                 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
357                         }
358                         if ( sc->rid != -1 ) {
359                                 /* command line cookie wins */
360                                 si->si_syncCookie.rid = sc->rid;
361                         }
362                         slap_sync_cookie_free( sc, 1 );
363                         sc = NULL;
364                         slap_compose_sync_cookie( NULL, &newcookie,
365                                         &si->si_syncCookie.ctxcsn[0],
366                                         si->si_syncCookie.sid, si->si_syncCookie.rid );
367                         ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
368                 }
369         } else {
370                 /* no command line cookie is specified */
371                 if ( si->si_syncCookie.octet_str == NULL ) {
372                         BerVarray cookie = NULL;
373                         struct berval cookie_bv;
374                         /* try to read stored cookie */
375                         backend_attribute( op, NULL, &op->o_req_ndn,
376                                 slap_schema.si_ad_syncreplCookie, &cookie );
377                         if ( cookie ) {
378                                 ber_dupbv( &cookie_bv, &cookie[0] );
379                                 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
380                                 slap_parse_sync_cookie( &si->si_syncCookie );
381                                 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
382                         }
383                 }
384         }
385
386         rc = ldap_sync_search( si, op->o_tmpmemctx );
387
388         if( rc != LDAP_SUCCESS ) {
389 #ifdef NEW_LOGGING
390                 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
391                         "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
392 #else
393                 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
394                         "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
395 #endif
396         }
397
398 done:
399         if ( rc ) {
400                 if ( si->si_ld ) {
401                         ldap_unbind( si->si_ld );
402                         si->si_ld = NULL;
403                 }
404         }
405
406         return rc;
407 }
408
409 static int
410 do_syncrep2(
411         Operation *op,
412         syncinfo_t *si )
413 {
414         LDAPControl     **rctrls = NULL;
415         LDAPControl     *rctrlp;
416
417         BerElementBuffer berbuf;
418         BerElement      *ber = (BerElement *)&berbuf;
419
420         LDAPMessage     *res = NULL;
421         LDAPMessage     *msg = NULL;
422
423         char            *retoid = NULL;
424         struct berval   *retdata = NULL;
425
426         Entry           *entry = NULL;
427
428         int             syncstate;
429         struct berval   syncUUID = { 0, NULL };
430         struct sync_cookie      syncCookie = { NULL, -1, NULL };
431         struct sync_cookie      syncCookie_req = { NULL, -1, NULL };
432         struct berval           cookie = { 0, NULL };
433
434         int     rc, err, i;
435         ber_len_t       len;
436
437         int rc_efree;
438
439         struct berval   *psub;
440         Modifications   *modlist = NULL;
441
442         const char              *text;
443         int                             match;
444
445         struct timeval *tout_p = NULL;
446         struct timeval tout = { 0, 0 };
447
448         int             refreshDeletes = 0;
449         int             refreshDone = 1;
450         BerVarray syncUUIDs = NULL;
451         ber_tag_t si_tag;
452
453         if ( slapd_shutdown ) {
454                 rc = -2;
455                 goto done;
456         }
457
458         ber_init2( ber, NULL, LBER_USE_DER );
459         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
460
461 #ifdef NEW_LOGGING
462         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
463 #else
464         Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
465 #endif
466
467         psub = &si->si_be->be_nsuffix[0];
468
469         slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
470
471         if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
472                 tout_p = &tout;
473         } else {
474                 tout_p = NULL;
475         }
476
477         while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
478                 > 0 )
479         {
480                 if ( slapd_shutdown ) {
481                         rc = -2;
482                         goto done;
483                 }
484                 for( msg = ldap_first_message( si->si_ld, res );
485                   msg != NULL;
486                   msg = ldap_next_message( si->si_ld, msg ) )
487                 {
488                         switch( ldap_msgtype( msg ) ) {
489                         case LDAP_RES_SEARCH_ENTRY:
490                                 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
491                                 /* we can't work without the control */
492                                 if ( !rctrls ) {
493                                         rc = -1;
494                                         goto done;
495                                 }
496                                 rctrlp = *rctrls;
497                                 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
498                                 ber_scanf( ber, "{em", &syncstate, &syncUUID );
499                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
500                                         ber_scanf( ber, "m}", &cookie );
501                                         if ( cookie.bv_val ) {
502                                                 struct berval tmp_bv;
503                                                 ber_dupbv( &tmp_bv, &cookie );
504                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
505                                         }
506                                         if ( syncCookie.octet_str &&
507                                                         syncCookie.octet_str[0].bv_val )
508                                                 slap_parse_sync_cookie( &syncCookie );
509                                 }
510                                 entry = syncrepl_message_to_entry( si, op, msg,
511                                         &modlist, syncstate );
512                                 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
513                                                         &syncUUID, &syncCookie_req );
514                                 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
515                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
516                                 }
517                                 ldap_controls_free( rctrls );
518                                 if ( modlist ) {
519                                         slap_mods_free( modlist );
520                                 }
521                                 if ( rc_efree && entry ) {
522                                         entry_free( entry );
523                                 }
524                                 break;
525
526                         case LDAP_RES_SEARCH_REFERENCE:
527 #ifdef NEW_LOGGING
528                                 LDAP_LOG( OPERATION, ERR,
529                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
530 #else
531                                 Debug( LDAP_DEBUG_ANY,
532                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
533 #endif
534                                 break;
535
536                         case LDAP_RES_SEARCH_RESULT:
537                                 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
538                                         &rctrls, 0 );
539                                 if ( rctrls ) {
540                                         rctrlp = *rctrls;
541                                         ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
542
543                                         ber_scanf( ber, "{" /*"}"*/);
544                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
545                                         {
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);
551                                                 }
552                                                 if ( syncCookie.octet_str &&
553                                                                  syncCookie.octet_str[0].bv_val )
554                                                         slap_parse_sync_cookie( &syncCookie );
555                                         }
556                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
557                                         {
558                                                 ber_scanf( ber, "b", &refreshDeletes );
559                                         }
560                                         ber_scanf( ber, "}" );
561                                 }
562                                 if ( syncCookie_req.ctxcsn == NULL ) {
563                                         match = -1;
564                                 } else if ( syncCookie.ctxcsn == NULL ) {
565                                         match = 1;
566                                 } else {
567                                         value_match( &match, slap_schema.si_ad_entryCSN,
568                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
569                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
570                                                 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
571                                 }
572                                 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
573                                          && match < 0 ) {
574                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
575                                 }
576                                 if ( rctrls ) {
577                                         ldap_controls_free( rctrls );
578                                 }
579                                 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
580                                         /* FIXME : different error behaviors according to
581                                          *      1) err code : LDAP_BUSY ...
582                                          *      2) on err policy : stop service, stop sync, retry
583                                          */
584                                         if ( refreshDeletes == 0 && match < 0 ) {
585                                                 syncrepl_del_nonpresent( op, si );
586                                         } else {
587                                                 avl_free( si->si_presentlist, avl_ber_bvfree );
588                                                 si->si_presentlist = NULL;
589                                         }
590                                 }
591                                 rc = -2;
592                                 goto done;
593                                 break;
594
595                         case LDAP_RES_INTERMEDIATE:
596                                 rc = ldap_parse_intermediate( si->si_ld, msg,
597                                         &retoid, &retdata, NULL, 0 );
598                                 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
599                                         int             si_refreshDelete = 0;
600                                         int             si_refreshPresent = 0;
601                                         ber_init2( ber, retdata, LBER_USE_DER );
602
603                                         switch ( si_tag = ber_peek_tag( ber, &len )) {
604                                         ber_tag_t tag;
605                                         case LDAP_TAG_SYNC_NEW_COOKIE:
606                                                 ber_scanf( ber, "tm", &tag, &cookie );
607                                                 break;
608                                         case LDAP_TAG_SYNC_REFRESH_DELETE:
609                                                 si_refreshDelete = 1;
610                                         case LDAP_TAG_SYNC_REFRESH_PRESENT:
611                                                 si_refreshPresent = 1;
612                                                 ber_scanf( ber, "t{", &tag );
613                                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
614                                                 {
615                                                         ber_scanf( ber, "m", &cookie );
616                                                         if ( cookie.bv_val ) {
617                                                                 struct berval tmp_bv;
618                                                                 ber_dupbv( &tmp_bv, &cookie );
619                                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
620                                                         }
621                                                         if ( syncCookie.octet_str &&
622                                                                          syncCookie.octet_str[0].bv_val )
623                                                                 slap_parse_sync_cookie( &syncCookie );
624                                                 }
625                                                 if ( ber_peek_tag( ber, &len ) ==
626                                                                         LDAP_TAG_REFRESHDONE )
627                                                 {
628                                                         ber_scanf( ber, "b", &refreshDone );
629                                                 }
630                                                 ber_scanf( ber, "}" );
631                                                 break;
632                                         case LDAP_TAG_SYNC_ID_SET:
633                                                 ber_scanf( ber, "t{", &tag );
634                                                 if ( ber_peek_tag( ber, &len ) ==
635                                                                 LDAP_TAG_SYNC_COOKIE ) {
636                                                         ber_scanf( ber, "m", &cookie );
637                                                         if ( cookie.bv_val ) {
638                                                                 struct berval tmp_bv;
639                                                                 ber_dupbv( &tmp_bv, &cookie );
640                                                                 ber_bvarray_add( &syncCookie.octet_str,
641                                                                                                  &tmp_bv );
642                                                         }
643                                                         if ( syncCookie.octet_str &&
644                                                                          syncCookie.octet_str[0].bv_val )
645                                                                 slap_parse_sync_cookie( &syncCookie );
646                                                 }
647                                                 if ( ber_peek_tag( ber, &len ) ==
648                                                                         LDAP_TAG_REFRESHDELETES )
649                                                 {
650                                                         ber_scanf( ber, "b", &refreshDeletes );
651                                                 }
652                                                 ber_scanf( ber, "[W]", &syncUUIDs );
653                                                 ber_scanf( ber, "}" );
654                                                 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
655                                                         struct berval *syncuuid_bv;
656                                                         syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
657                                                         avl_insert( &si->si_presentlist,
658                                                                         (caddr_t) syncuuid_bv,
659                                                                         syncuuid_cmp, avl_dup_error );
660                                                 }
661                                                 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
662                                                 break;
663                                         default:
664 #ifdef NEW_LOGGING
665                                         LDAP_LOG( OPERATION, ERR,
666                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
667                                                 (long) si_tag, 0, 0 );
668 #else
669                                         Debug( LDAP_DEBUG_ANY,
670                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
671                                                 (long) si_tag, 0, 0 );
672 #endif
673                                                 ldap_memfree( retoid );
674                                                 ber_bvfree( retdata );
675                                                 continue;
676                                         }
677
678                                         if ( syncCookie_req.ctxcsn == NULL ) {
679                                                 match = -1;
680                                         } else if ( syncCookie.ctxcsn == NULL ) {
681                                                 match = 1;
682                                         } else {
683                                                 value_match( &match, slap_schema.si_ad_entryCSN,
684                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
685                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
686                                                         &syncCookie_req.ctxcsn[0],
687                                                         &syncCookie.ctxcsn[0], &text );
688                                         }
689
690                                         if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
691                                                  && match < 0 ) {
692                                                 syncrepl_updateCookie( si, op, psub, &syncCookie);
693                                         }
694
695                                         if ( si_refreshPresent == 1 ) {
696                                                 if ( match < 0 ) {
697                                                         syncrepl_del_nonpresent( op, si );
698                                                 }
699                                         } 
700
701                                         ldap_memfree( retoid );
702                                         ber_bvfree( retdata );
703                                         break;
704                                 } else {
705 #ifdef NEW_LOGGING
706                                         LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
707                                                 " unknown intermediate "
708                                                 "response\n", 0, 0, 0 );
709 #else
710                                         Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
711                                                 "unknown intermediate response (%d)\n",
712                                                 rc, 0, 0 );
713 #endif
714                                         ldap_memfree( retoid );
715                                         ber_bvfree( retdata );
716                                         break;
717                                 }
718                                 break;
719                         default:
720 #ifdef NEW_LOGGING
721                                 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
722                                         "unknown message\n", 0, 0, 0 );
723 #else
724                                 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
725                                         "unknown message\n", 0, 0, 0 );
726 #endif
727                                 break;
728
729                         }
730                         if ( syncCookie.octet_str ) {
731                                 slap_sync_cookie_free( &syncCookie_req, 0 );
732                                 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
733                                 slap_sync_cookie_free( &syncCookie, 0 );
734                         }
735                 }
736                 ldap_msgfree( res );
737                 res = NULL;
738         }
739
740         if ( rc == -1 ) {
741                 const char *errstr;
742
743                 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
744                 errstr = ldap_err2string( rc );
745                 
746 #ifdef NEW_LOGGING
747                 LDAP_LOG( OPERATION, ERR,
748                         "do_syncrep2 : %s\n", errstr, 0, 0 );
749 #else
750                 Debug( LDAP_DEBUG_ANY,
751                         "do_syncrep2 : %s\n", errstr, 0, 0 );
752 #endif
753         }
754
755 done:
756         slap_sync_cookie_free( &syncCookie, 0 );
757         slap_sync_cookie_free( &syncCookie_req, 0 );
758
759         if ( res ) ldap_msgfree( res );
760
761         if ( rc && si->si_ld ) {
762                 ldap_unbind( si->si_ld );
763                 si->si_ld = NULL;
764         }
765
766         return rc;
767 }
768
769 void *
770 do_syncrepl(
771         void    *ctx,
772         void    *arg )
773 {
774         struct re_s* rtask = arg;
775         syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
776         Connection conn = {0};
777         Operation op = {0};
778         int rc = LDAP_SUCCESS;
779         int first = 0;
780         int dostop = 0;
781         ber_socket_t s;
782
783 #ifdef NEW_LOGGING
784         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
785 #else
786         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
787 #endif
788
789         if ( si == NULL )
790                 return NULL;
791
792         switch( abs( si->si_type )) {
793         case LDAP_SYNC_REFRESH_ONLY:
794         case LDAP_SYNC_REFRESH_AND_PERSIST:
795                 break;
796         default:
797                 return NULL;
798         }
799
800         if ( slapd_shutdown && si->si_ld ) {
801                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
802                 connection_client_stop( s );
803                 ldap_unbind( si->si_ld );
804                 si->si_ld = NULL;
805                 return NULL;
806         }
807
808         conn.c_connid = -1;
809         conn.c_send_ldap_result = slap_send_ldap_result;
810         conn.c_send_search_entry = slap_send_search_entry;
811         conn.c_send_search_reference = slap_send_search_reference;
812         conn.c_listener = (Listener *)&dummy_list;
813         conn.c_peer_name = slap_empty_bv;
814
815         /* set memory context */
816         op.o_tmpmemctx = sl_mem_create( SLMALLOC_SLAB_SIZE, ctx );
817         op.o_tmpmfuncs = &sl_mfuncs;
818
819         op.o_dn = si->si_updatedn;
820         op.o_ndn = si->si_updatedn;
821         op.o_time = slap_get_time();
822         op.o_threadctx = ctx;
823         op.o_managedsait = 1;
824         op.o_bd = si->si_be;
825         op.o_conn = &conn;
826         op.o_connid = op.o_conn->c_connid;
827
828         op.o_sync_state.ctxcsn = NULL;
829         op.o_sync_state.sid = -1;
830         op.o_sync_state.octet_str = NULL;
831         op.o_sync_slog_size = -1;
832         LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
833         op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
834
835         /* Establish session, do search */
836         if ( !si->si_ld ) {
837                 first = 1;
838                 rc = do_syncrep1( &op, si );
839         }
840
841         /* Process results */
842         if ( rc == LDAP_SUCCESS ) {
843                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
844
845                 rc = do_syncrep2( &op, si );
846
847                 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
848                         /* If we succeeded, enable the connection for further listening.
849                          * If we failed, tear down the connection and reschedule.
850                          */
851                         if ( rc == LDAP_SUCCESS ) {
852                                 if ( first ) {
853                                         rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
854                                                 arg );
855                                 } else {
856                                         connection_client_enable( s );
857                                 }
858                         } else if ( !first ) {
859                                 dostop = 1;
860                         }
861                 } else {
862                         if ( rc == -2 ) rc = 0;
863                 }
864         }
865
866         /* At this point, we have 4 cases:
867          * 1) for any hard failure, give up and remove this task
868          * 2) for ServerDown, reschedule this task to run
869          * 3) for Refresh and Success, reschedule to run
870          * 4) for Persist and Success, reschedule to defer
871          */
872         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
873         if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
874                 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
875         }
876
877         if ( dostop ) {
878                 connection_client_stop( s );
879         }
880
881         if ( rc && rc != LDAP_SERVER_DOWN ) {
882                 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
883         } else {
884                 if ( rc == LDAP_SERVER_DOWN ||
885                         si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
886                         rc = 0;
887                 } else {
888                         rc = 1;
889                 }
890                 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
891         }
892         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
893
894         return NULL;
895 }
896
897 Entry*
898 syncrepl_message_to_entry(
899         syncinfo_t      *si,
900         Operation       *op,
901         LDAPMessage     *msg,
902         Modifications   **modlist,
903         int             syncstate
904 )
905 {
906         Entry           *e = NULL;
907         BerElement      *ber = NULL;
908         Modifications   tmp;
909         Modifications   *mod;
910         Modifications   **modtail = modlist;
911
912         const char      *text;
913         char txtbuf[SLAP_TEXT_BUFLEN];
914         size_t textlen = sizeof txtbuf;
915
916         struct berval   bdn = {0, NULL}, dn, ndn;
917         int             rc;
918
919         *modlist = NULL;
920
921         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
922 #ifdef NEW_LOGGING
923                 LDAP_LOG( OPERATION, ERR,
924                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
925 #else
926                 Debug( LDAP_DEBUG_ANY,
927                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
928 #endif
929                 return NULL;
930         }
931
932         op->o_tag = LDAP_REQ_ADD;
933
934         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
935
936         if ( rc != LDAP_SUCCESS ) {
937 #ifdef NEW_LOGGING
938                 LDAP_LOG( OPERATION, ERR,
939                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
940 #else
941                 Debug( LDAP_DEBUG_ANY,
942                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
943 #endif
944                 return NULL;
945         }
946
947         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
948         ber_dupbv( &op->o_req_dn, &dn );
949         ber_dupbv( &op->o_req_ndn, &ndn );
950         sl_free( ndn.bv_val, op->o_tmpmemctx );
951         sl_free( dn.bv_val, op->o_tmpmemctx );
952
953         if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
954         {
955                 return NULL;
956         }
957
958         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
959         e->e_name = op->o_req_dn;
960         e->e_nname = op->o_req_ndn;
961
962         while ( ber_remaining( ber ) ) {
963                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
964                         LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
965                 {
966                         break;
967                 }
968
969                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
970
971                 mod->sml_op = LDAP_MOD_REPLACE;
972                 mod->sml_next = NULL;
973                 mod->sml_desc = NULL;
974                 mod->sml_type = tmp.sml_type;
975                 mod->sml_bvalues = tmp.sml_bvalues;
976                 mod->sml_nvalues = NULL;
977
978                 *modtail = mod;
979                 modtail = &mod->sml_next;
980         }
981
982         if ( *modlist == NULL ) {
983 #ifdef NEW_LOGGING
984                 LDAP_LOG( OPERATION, ERR,
985                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
986 #else
987                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
988                                 0, 0, 0 );
989 #endif
990         }
991
992         rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
993
994         if ( rc != LDAP_SUCCESS ) {
995 #ifdef NEW_LOGGING
996                 LDAP_LOG( OPERATION, ERR,
997                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
998 #else
999                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1000                                 text, 0, 0 );
1001 #endif
1002                 goto done;
1003         }
1004         
1005         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1006         if( rc != LDAP_SUCCESS ) {
1007 #ifdef NEW_LOGGING
1008                 LDAP_LOG( OPERATION, ERR,
1009                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1010 #else
1011                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1012                                 text, 0, 0 );
1013 #endif
1014         }
1015
1016 done:
1017         ber_free ( ber, 0 );
1018         if ( rc != LDAP_SUCCESS ) {
1019                 entry_free( e );
1020                 e = NULL;
1021         }
1022
1023         return e;
1024 }
1025
1026 int
1027 syncrepl_entry(
1028         syncinfo_t* si,
1029         Operation *op,
1030         Entry* e,
1031         Modifications* modlist,
1032         int syncstate,
1033         struct berval* syncUUID,
1034         struct sync_cookie* syncCookie_req
1035 )
1036 {
1037         Backend *be = op->o_bd;
1038         slap_callback   cb = { NULL };
1039         struct berval   *syncuuid_bv = NULL;
1040         struct berval   syncUUID_strrep = { 0, NULL };
1041
1042         SlapReply       rs = {REP_RESULT};
1043         Filter f = {0};
1044         AttributeAssertion ava = {0};
1045         int rc = LDAP_SUCCESS;
1046         int ret = LDAP_SUCCESS;
1047         const char *text;
1048
1049         if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1050         {
1051                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1052                 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1053                         syncuuid_cmp, avl_dup_error );
1054         }
1055
1056         if ( syncstate == LDAP_SYNC_PRESENT ) {
1057                 return e ? 1 : 0;
1058         }
1059
1060         f.f_choice = LDAP_FILTER_EQUALITY;
1061         f.f_ava = &ava;
1062         ava.aa_desc = slap_schema.si_ad_entryUUID;
1063         slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1064         ava.aa_value = *syncUUID;
1065         op->ors_filter = &f;
1066
1067         op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1068         op->ors_filterstr.bv_val = (char *) sl_malloc(
1069                 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx ); 
1070         AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1071         AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1072                 syncUUID->bv_val, syncUUID->bv_len );
1073         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1074
1075         op->ors_scope = LDAP_SCOPE_SUBTREE;
1076
1077         /* get syncrepl cookie of shadow replica from subentry */
1078         op->o_req_dn = si->si_base;
1079         op->o_req_ndn = si->si_base;
1080
1081         /* set callback function */
1082         op->o_callback = &cb;
1083         cb.sc_response = dn_callback;
1084         cb.sc_private = si;
1085
1086         si->si_syncUUID_ndn.bv_val = NULL;
1087
1088         rc = be->be_search( op, &rs );
1089
1090         if ( op->ors_filterstr.bv_val ) {
1091                 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1092         }
1093
1094         cb.sc_response = null_callback;
1095         cb.sc_private = si;
1096
1097         if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1098         {
1099                 char *subseq_ptr;
1100
1101                 if ( syncstate != LDAP_SYNC_DELETE ) {
1102                         op->o_no_psearch = 1;
1103                 }
1104
1105                 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1106                 if ( op->o_sync_csn.bv_val ) {
1107                         subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1108                         subseq_ptr += 4;
1109                         *subseq_ptr = '1';
1110                 }
1111                 
1112                 op->o_req_dn = si->si_syncUUID_ndn;
1113                 op->o_req_ndn = si->si_syncUUID_ndn;
1114                 op->o_tag = LDAP_REQ_DELETE;
1115                 rc = be->be_delete( op, &rs );
1116                 op->o_no_psearch = 0;
1117         }
1118
1119         switch ( syncstate ) {
1120         case LDAP_SYNC_ADD:
1121         case LDAP_SYNC_MODIFY:
1122                 if ( rc == LDAP_SUCCESS ||
1123                          rc == LDAP_REFERRAL ||
1124                          rc == LDAP_NO_SUCH_OBJECT )
1125                 {
1126                         attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1127                         attr_merge_one( e, slap_schema.si_ad_entryUUID,
1128                                 syncUUID, &ava.aa_value );
1129
1130                         op->o_tag = LDAP_REQ_ADD;
1131                         op->ora_e = e;
1132                         op->o_req_dn = e->e_name;
1133                         op->o_req_ndn = e->e_nname;
1134                         rc = be->be_add( op, &rs );
1135
1136                         if ( rc != LDAP_SUCCESS ) {
1137                                 if ( rc == LDAP_ALREADY_EXISTS ) {      
1138                                         op->o_tag = LDAP_REQ_MODIFY;
1139                                         op->orm_modlist = modlist;
1140                                         op->o_req_dn = e->e_name;
1141                                         op->o_req_ndn = e->e_nname;
1142                                         rc = be->be_modify( op, &rs );
1143                                         if ( rc != LDAP_SUCCESS ) {
1144 #ifdef NEW_LOGGING
1145                                                 LDAP_LOG( OPERATION, ERR,
1146                                                         "syncrepl_entry : be_modify failed (%d)\n",
1147                                                         rc, 0, 0 );
1148 #else
1149                                                 Debug( LDAP_DEBUG_ANY,
1150                                                         "syncrepl_entry : be_modify failed (%d)\n",
1151                                                         rc, 0, 0 );
1152 #endif
1153                                         }
1154                                         ret = 1;
1155                                         goto done;
1156                                 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1157                                         syncrepl_add_glue( op, e );
1158                                         ret = 0;
1159                                         goto done;
1160                                 } else {
1161 #ifdef NEW_LOGGING
1162                                         LDAP_LOG( OPERATION, ERR,
1163                                                 "syncrepl_entry : be_add failed (%d)\n",
1164                                                 rc, 0, 0 );
1165 #else
1166                                         Debug( LDAP_DEBUG_ANY,
1167                                                 "syncrepl_entry : be_add failed (%d)\n",
1168                                                 rc, 0, 0 );
1169 #endif
1170                                         ret = 1;
1171                                         goto done;
1172                                 }
1173                         } else {
1174                                 be_entry_release_w( op, e );
1175                                 ret = 0;
1176                                 goto done;
1177                         }
1178                 } else {
1179 #ifdef NEW_LOGGING
1180                         LDAP_LOG( OPERATION, ERR,
1181                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1182 #else
1183                         Debug( LDAP_DEBUG_ANY,
1184                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1185 #endif
1186                         ret = 1;
1187                         goto done;
1188                 }
1189
1190         case LDAP_SYNC_DELETE :
1191                 /* Already deleted */
1192                 ret = 1;
1193                 goto done;
1194
1195         default :
1196 #ifdef NEW_LOGGING
1197                 LDAP_LOG( OPERATION, ERR,
1198                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1199 #else
1200                 Debug( LDAP_DEBUG_ANY,
1201                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1202 #endif
1203                 ret = 1;
1204                 goto done;
1205         }
1206
1207 done :
1208
1209         if ( syncUUID_strrep.bv_val ) {
1210                 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1211         }
1212         if ( si->si_syncUUID_ndn.bv_val ) {
1213                 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1214         }
1215         return ret;
1216 }
1217
1218 static void
1219 syncrepl_del_nonpresent(
1220         Operation *op,
1221         syncinfo_t *si
1222 )
1223 {
1224         Backend* be = op->o_bd;
1225         slap_callback   cb = { NULL };
1226         SlapReply       rs = {REP_RESULT};
1227         struct nonpresent_entry *np_list, *np_prev;
1228
1229         op->o_req_dn = si->si_base;
1230         op->o_req_ndn = si->si_base;
1231
1232         cb.sc_response = nonpresent_callback;
1233         cb.sc_private = si;
1234
1235         op->o_callback = &cb;
1236         op->o_tag = LDAP_REQ_SEARCH;
1237         op->ors_scope = si->si_scope;
1238         op->ors_deref = LDAP_DEREF_NEVER;
1239         op->ors_slimit = 0;
1240         op->ors_tlimit = 0;
1241         op->ors_attrsonly = 0;
1242         op->ors_attrs = NULL;
1243         op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1244         op->ors_filterstr = si->si_filterstr;
1245
1246         op->o_nocaching = 1;
1247         be->be_search( op, &rs );
1248         op->o_nocaching = 0;
1249
1250         if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1251
1252         if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1253                 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1254                 while ( np_list != NULL ) {
1255                         LDAP_LIST_REMOVE( np_list, npe_link );
1256                         np_prev = np_list;
1257                         np_list = LDAP_LIST_NEXT( np_list, npe_link );
1258                         op->o_tag = LDAP_REQ_DELETE;
1259                         op->o_callback = &cb;
1260                         cb.sc_response = null_callback;
1261                         cb.sc_private = si;
1262                         op->o_req_dn = *np_prev->npe_name;
1263                         op->o_req_ndn = *np_prev->npe_nname;
1264                         op->o_bd->be_delete( op, &rs );
1265                         ber_bvfree( np_prev->npe_name );
1266                         ber_bvfree( np_prev->npe_nname );
1267                         op->o_req_dn.bv_val = NULL;
1268                         op->o_req_ndn.bv_val = NULL;
1269                         ch_free( np_prev );
1270                 }
1271         }
1272
1273         return;
1274 }
1275
1276
1277 static struct berval gcbva[] = {
1278         BER_BVC("top"),
1279         BER_BVC("glue")
1280 };
1281
1282 void
1283 syncrepl_add_glue(
1284         Operation* op,
1285         Entry *e
1286 )
1287 {
1288         Backend *be = op->o_bd;
1289         slap_callback cb = { NULL };
1290         Attribute       *a;
1291         int     rc;
1292         int suffrdns;
1293         int i;
1294         struct berval dn = {0, NULL};
1295         struct berval ndn = {0, NULL};
1296         Entry   *glue;
1297         SlapReply       rs = {REP_RESULT};
1298         char    *ptr, *comma;
1299
1300         op->o_tag = LDAP_REQ_ADD;
1301         op->o_callback = &cb;
1302         cb.sc_response = null_callback;
1303         cb.sc_private = NULL;
1304
1305         dn = e->e_name;
1306         ndn = e->e_nname;
1307
1308         /* count RDNs in suffix */
1309         if ( be->be_nsuffix[0].bv_len ) {
1310                 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1311                         ptr++;
1312                         i++;
1313                 }
1314                 suffrdns = i;
1315         } else {
1316                 /* suffix is "" */
1317                 suffrdns = 0;
1318         }
1319
1320         /* Start with BE suffix */
1321         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1322                 comma = strrchr(dn.bv_val, ',');
1323                 if ( ptr ) *ptr = ',';
1324                 if ( comma ) *comma = '\0';
1325                 ptr = comma;
1326         }
1327         if ( ptr ) {
1328                 *ptr++ = ',';
1329                 dn.bv_len -= ptr - dn.bv_val;
1330                 dn.bv_val = ptr;
1331         }
1332         /* the normalizedDNs are always the same length, no counting
1333          * required.
1334          */
1335         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1336                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1337                 ndn.bv_len = be->be_nsuffix[0].bv_len;
1338         }
1339
1340         while ( ndn.bv_val > e->e_nname.bv_val ) {
1341                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1342                 ber_dupbv( &glue->e_name, &dn );
1343                 ber_dupbv( &glue->e_nname, &ndn );
1344
1345                 a = ch_calloc( 1, sizeof( Attribute ));
1346                 a->a_desc = slap_schema.si_ad_objectClass;
1347
1348                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1349                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1350                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1351                 a->a_vals[2].bv_len = 0;
1352                 a->a_vals[2].bv_val = NULL;
1353
1354                 a->a_nvals = a->a_vals;
1355
1356                 a->a_next = glue->e_attrs;
1357                 glue->e_attrs = a;
1358
1359                 a = ch_calloc( 1, sizeof( Attribute ));
1360                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1361
1362                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1363                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1364                 a->a_vals[1].bv_len = 0;
1365                 a->a_vals[1].bv_val = NULL;
1366
1367                 a->a_nvals = a->a_vals;
1368
1369                 a->a_next = glue->e_attrs;
1370                 glue->e_attrs = a;
1371
1372                 op->o_req_dn = glue->e_name;
1373                 op->o_req_ndn = glue->e_nname;
1374                 op->ora_e = glue;
1375                 rc = be->be_add ( op, &rs );
1376                 if ( rc == LDAP_SUCCESS ) {
1377                         be_entry_release_w( op, glue );
1378                 } else {
1379                 /* incl. ALREADY EXIST */
1380                         entry_free( glue );
1381                 }
1382
1383                 /* Move to next child */
1384                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1385                         /* empty */
1386                 }
1387                 if ( ptr == e->e_name.bv_val ) break;
1388                 dn.bv_val = ++ptr;
1389                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1390                 for( ptr = ndn.bv_val-2;
1391                         ptr > e->e_nname.bv_val && *ptr != ',';
1392                         ptr--)
1393                 {
1394                         /* empty */
1395                 }
1396                 ndn.bv_val = ++ptr;
1397                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1398         }
1399
1400         op->o_req_dn = e->e_name;
1401         op->o_req_ndn = e->e_nname;
1402         op->ora_e = e;
1403         rc = be->be_add ( op, &rs );
1404         if ( rc == LDAP_SUCCESS ) {
1405                 be_entry_release_w( op, e );
1406         } else {
1407                 entry_free( e );
1408         }
1409
1410         return;
1411 }
1412
1413 static struct berval ocbva[] = {
1414         BER_BVC("top"),
1415         BER_BVC("subentry"),
1416         BER_BVC("syncConsumerSubentry"),
1417         BER_BVNULL
1418 };
1419
1420 static struct berval cnbva[] = {
1421         BER_BVNULL,
1422         BER_BVNULL
1423 };
1424
1425 static struct berval ssbva[] = {
1426         BER_BVC("{}"),
1427         BER_BVNULL
1428 };
1429
1430 static struct berval scbva[] = {
1431         BER_BVNULL,
1432         BER_BVNULL
1433 };
1434
1435 void
1436 syncrepl_updateCookie(
1437         syncinfo_t *si,
1438         Operation *op,
1439         struct berval *pdn,
1440         struct sync_cookie *syncCookie
1441 )
1442 {
1443         Backend *be = op->o_bd;
1444         Modifications *ml;
1445         Modifications *mlnext;
1446         Modifications *mod;
1447         Modifications *modlist = NULL;
1448         Modifications **modtail = &modlist;
1449
1450         const char      *text;
1451         char txtbuf[SLAP_TEXT_BUFLEN];
1452         size_t textlen = sizeof txtbuf;
1453
1454         Entry* e = NULL;
1455         int rc;
1456
1457         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1458         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1459         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1460         
1461         slap_callback cb = { NULL };
1462         SlapReply       rs = {REP_RESULT};
1463
1464         slap_sync_cookie_free( &si->si_syncCookie, 0 );
1465         slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1466
1467         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1468         mod->sml_op = LDAP_MOD_REPLACE;
1469         mod->sml_desc = slap_schema.si_ad_objectClass;
1470         mod->sml_type = mod->sml_desc->ad_cname;
1471         mod->sml_bvalues = ocbva;
1472         *modtail = mod;
1473         modtail = &mod->sml_next;
1474
1475         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1476         assert( si->si_rid < 1000 );
1477         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1478                 slap_syncrepl_bvc.bv_len,
1479                 "syncrepl%ld", si->si_rid );
1480         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1481         mod->sml_op = LDAP_MOD_REPLACE;
1482         mod->sml_desc = slap_schema.si_ad_cn;
1483         mod->sml_type = mod->sml_desc->ad_cname;
1484         mod->sml_bvalues = cnbva;
1485         *modtail = mod;
1486         modtail = &mod->sml_next;
1487
1488         if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1489         ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1490         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1491         mod->sml_op = LDAP_MOD_REPLACE;
1492         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1493         mod->sml_type = mod->sml_desc->ad_cname;
1494         mod->sml_bvalues = scbva;
1495         *modtail = mod;
1496         modtail = &mod->sml_next;
1497
1498         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1499         mod->sml_op = LDAP_MOD_REPLACE;
1500         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1501         mod->sml_type = mod->sml_desc->ad_cname;
1502         mod->sml_bvalues = ssbva;
1503         *modtail = mod;
1504         modtail = &mod->sml_next;
1505
1506         mlnext = mod;
1507
1508         op->o_tag = LDAP_REQ_ADD;
1509         rc = slap_mods_opattrs( op, modlist, modtail,
1510                                                          &text,txtbuf, textlen );
1511
1512         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1513                 ml->sml_op = LDAP_MOD_REPLACE;
1514         }
1515
1516         if( rc != LDAP_SUCCESS ) {
1517 #ifdef NEW_LOGGING
1518                 LDAP_LOG( OPERATION, ERR,
1519                         "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1520 #else
1521                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1522                          text, 0, 0 );
1523 #endif
1524         }
1525
1526         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1527
1528         slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1529         assert( si->si_rid < 1000 );
1530         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1531                 slap_syncrepl_cn_bvc.bv_len,
1532                 "cn=syncrepl%ld", si->si_rid );
1533
1534         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1535                 op->o_tmpmemctx );
1536         ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1537         ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1538
1539         if ( slap_syncrepl_dn_bv.bv_val ) {
1540                 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1541         }
1542
1543         e->e_attrs = NULL;
1544
1545         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1546
1547         if( rc != LDAP_SUCCESS ) {
1548 #ifdef NEW_LOGGING
1549                 LDAP_LOG( OPERATION, ERR,
1550                         "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1551 #else
1552                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1553                          text, 0, 0 );
1554 #endif
1555         }
1556
1557         cb.sc_response = null_callback;
1558         cb.sc_private = si;
1559
1560         op->o_callback = &cb;
1561         op->o_req_dn = e->e_name;
1562         op->o_req_ndn = e->e_nname;
1563
1564         /* update persistent cookie */
1565 update_cookie_retry:
1566         op->o_tag = LDAP_REQ_MODIFY;
1567         op->orm_modlist = modlist;
1568         rc = be->be_modify( op, &rs );
1569
1570         if ( rc != LDAP_SUCCESS ) {
1571                 if ( rc == LDAP_REFERRAL ||
1572                          rc == LDAP_NO_SUCH_OBJECT ) {
1573                         op->o_tag = LDAP_REQ_ADD;
1574                         op->ora_e = e;
1575                         rc = be->be_add( op, &rs );
1576                         if ( rc != LDAP_SUCCESS ) {
1577                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1578                                         goto update_cookie_retry;
1579                                 } else if ( rc == LDAP_REFERRAL ||
1580                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1581 #ifdef NEW_LOGGING
1582                                         LDAP_LOG( OPERATION, ERR,
1583                                                 "cookie will be non-persistent\n",
1584                                                 0, 0, 0 );
1585 #else
1586                                         Debug( LDAP_DEBUG_ANY,
1587                                                 "cookie will be non-persistent\n",
1588                                                 0, 0, 0 );
1589 #endif
1590                                 } else {
1591 #ifdef NEW_LOGGING
1592                                         LDAP_LOG( OPERATION, ERR,
1593                                                 "be_add failed (%d)\n",
1594                                                 rc, 0, 0 );
1595 #else
1596                                         Debug( LDAP_DEBUG_ANY,
1597                                                 "be_add failed (%d)\n",
1598                                                 rc, 0, 0 );
1599 #endif
1600                                 }
1601                         } else {
1602                                 be_entry_release_w( op, e );
1603                                 goto done;
1604                         }
1605                 } else {
1606 #ifdef NEW_LOGGING
1607                         LDAP_LOG( OPERATION, ERR,
1608                                 "be_modify failed (%d)\n", rc, 0, 0 );
1609 #else
1610                         Debug( LDAP_DEBUG_ANY,
1611                                 "be_modify failed (%d)\n", rc, 0, 0 );
1612 #endif
1613                 }
1614         }
1615
1616         if ( e != NULL ) {
1617                 entry_free( e );
1618         }
1619
1620 done :
1621
1622         if ( cnbva[0].bv_val ) {
1623                 ch_free( cnbva[0].bv_val );
1624                 cnbva[0].bv_val = NULL;
1625         }
1626         if ( scbva[0].bv_val ) {
1627                 ch_free( scbva[0].bv_val );
1628                 scbva[0].bv_val = NULL;
1629         }
1630
1631         if ( mlnext->sml_next ) {
1632                 slap_mods_free( mlnext->sml_next );
1633                 mlnext->sml_next = NULL;
1634         }
1635
1636         for (ml = modlist ; ml != NULL; ml = mlnext ) {
1637                 mlnext = ml->sml_next;
1638                 free( ml );
1639         }
1640
1641         return;
1642 }
1643
1644 static int
1645 dn_callback(
1646         Operation*      op,
1647         SlapReply*      rs
1648 )
1649 {
1650         syncinfo_t *si = op->o_callback->sc_private;
1651
1652         if ( rs->sr_type == REP_SEARCH ) {
1653                 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1654 #ifdef NEW_LOGGING
1655                         LDAP_LOG( OPERATION, ERR,
1656                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1657 #else
1658                         Debug( LDAP_DEBUG_ANY,
1659                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1660 #endif
1661                 } else {
1662                         ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1663                 }
1664         }
1665
1666         return LDAP_SUCCESS;
1667 }
1668
1669 static int
1670 nonpresent_callback(
1671         Operation*      op,
1672         SlapReply*      rs
1673 )
1674 {
1675         syncinfo_t *si = op->o_callback->sc_private;
1676         Attribute *a;
1677         int count = 0;
1678         struct berval* present_uuid = NULL;
1679         struct nonpresent_entry *np_entry;
1680
1681         if ( rs->sr_type == REP_RESULT ) {
1682                 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1683                 si->si_presentlist = NULL;
1684
1685         } else if ( rs->sr_type == REP_SEARCH ) {
1686                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1687
1688                 if ( a == NULL ) return 0;
1689
1690                 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1691                         syncuuid_cmp );
1692
1693                 if ( present_uuid == NULL ) {
1694                         np_entry = (struct nonpresent_entry *)
1695                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1696                         np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1697                         np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1698                         LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1699
1700                 } else {
1701                         avl_delete( &si->si_presentlist,
1702                                         &a->a_vals[0], syncuuid_cmp );
1703                         ch_free( present_uuid->bv_val );
1704                         ch_free( present_uuid );
1705                 }
1706         }
1707         return LDAP_SUCCESS;
1708 }
1709
1710 static int
1711 null_callback(
1712         Operation*      op,
1713         SlapReply*      rs )
1714 {
1715         if ( rs->sr_err != LDAP_SUCCESS &&
1716                 rs->sr_err != LDAP_REFERRAL &&
1717                 rs->sr_err != LDAP_ALREADY_EXISTS &&
1718                 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1719         {
1720 #ifdef NEW_LOGGING
1721                 LDAP_LOG( OPERATION, ERR,
1722                         "null_callback : error code 0x%x\n",
1723                         rs->sr_err, 0, 0 );
1724 #else
1725                 Debug( LDAP_DEBUG_ANY,
1726                         "null_callback : error code 0x%x\n",
1727                         rs->sr_err, 0, 0 );
1728 #endif
1729         }
1730         return LDAP_SUCCESS;
1731 }
1732
1733 Entry *
1734 slap_create_syncrepl_entry(
1735         Backend *be,
1736         struct berval *context_csn,
1737         struct berval *rdn,
1738         struct berval *cn )
1739 {
1740         Entry* e;
1741
1742         struct berval bv;
1743
1744         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1745
1746         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1747
1748         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1749                 &ocbva[1], NULL );
1750
1751         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1752
1753         if ( context_csn ) {
1754                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1755                         context_csn, NULL );
1756         }
1757
1758         bv.bv_val = "{}";
1759         bv.bv_len = sizeof("{}")-1;
1760         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1761
1762         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1763         ber_dupbv( &e->e_nname, &e->e_name );
1764
1765         return e;
1766 }
1767
1768 struct berval *
1769 slap_uuidstr_from_normalized(
1770         struct berval* uuidstr,
1771         struct berval* normalized,
1772         void *ctx )
1773 {
1774         struct berval *new;
1775         unsigned char nibble;
1776         int i, d = 0;
1777
1778         if ( normalized == NULL )
1779                 return NULL;
1780
1781         if ( normalized->bv_len != 16 ) {
1782                 return NULL;
1783         }
1784
1785         if ( uuidstr ) {
1786                 new = uuidstr;
1787         } else {
1788                 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1789         }
1790
1791         new->bv_len = 36;
1792
1793         if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1794                 if ( !uuidstr )
1795                         sl_free( new, ctx );
1796                 return NULL;
1797         }
1798
1799         for ( i = 0; i < 16; i++ ) {
1800                 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1801                         new->bv_val[(i<<1)+d] = '-';
1802                         d += 1;
1803                 }
1804
1805                 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1806                 if ( nibble < 10 ) {
1807                         new->bv_val[(i<<1)+d] = nibble + '0';
1808                 } else {
1809                         new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1810                 }
1811
1812                 nibble = (normalized->bv_val[i]) & 0xF;
1813                 if ( nibble < 10 ) {
1814                         new->bv_val[(i<<1)+d+1] = nibble + '0';
1815                 } else {
1816                         new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1817                 }
1818         }
1819
1820         new->bv_val[new->bv_len] = '\0';
1821
1822         return new;
1823 }
1824
1825 static int
1826 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1827 {
1828         const struct berval *uuid1 = v_uuid1;
1829         const struct berval *uuid2 = v_uuid2;
1830         int rc = uuid1->bv_len - uuid2->bv_len;
1831         if ( rc ) return rc;
1832         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1833 }
1834
1835 static void
1836 avl_ber_bvfree( void *bv )
1837 {
1838         if( bv == NULL ) {
1839                 return;
1840         }
1841         if ( ((struct berval *)bv)->bv_val != NULL ) {
1842                 ch_free ( ((struct berval *)bv)->bv_val );
1843         }
1844         ch_free ( (char *) bv );
1845 }
1846