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