]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
Fix format errors: %d -> %ld, and add (long) casts as appropriate.
[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 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         slap_callback   cb;
438
439         int rc_efree;
440
441         struct berval   *psub;
442         Modifications   *modlist = NULL;
443
444         const char              *text;
445         int                             match;
446
447         struct timeval *tout_p = NULL;
448         struct timeval tout = { 0, 0 };
449
450         int             refreshDeletes = 0;
451         int             refreshDone = 1;
452         BerVarray syncUUIDs = NULL;
453         ber_tag_t si_tag;
454
455         if ( slapd_abrupt_shutdown ) {
456                 rc = -2;
457                 goto done;
458         }
459
460         ber_init2( ber, NULL, LBER_USE_DER );
461         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
462
463 #ifdef NEW_LOGGING
464         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
465 #else
466         Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
467 #endif
468
469         op->o_callback = &cb;
470
471         psub = &si->si_be->be_nsuffix[0];
472
473         slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
474
475         if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
476                 tout_p = &tout;
477         } else {
478                 tout_p = NULL;
479         }
480
481         while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
482                 > 0 )
483         {
484                 if ( slapd_abrupt_shutdown ) {
485                         rc = -2;
486                         goto done;
487                 }
488                 for( msg = ldap_first_message( si->si_ld, res );
489                   msg != NULL;
490                   msg = ldap_next_message( si->si_ld, msg ) )
491                 {
492                         switch( ldap_msgtype( msg ) ) {
493                         case LDAP_RES_SEARCH_ENTRY:
494                                 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
495                                 /* we can't work without the control */
496                                 if ( !rctrls ) {
497                                         rc = -1;
498                                         goto done;
499                                 }
500                                 rctrlp = *rctrls;
501                                 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
502                                 ber_scanf( ber, "{em", &syncstate, &syncUUID );
503                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
504                                         ber_scanf( ber, "m}", &cookie );
505                                         if ( cookie.bv_val ) {
506                                                 struct berval tmp_bv;
507                                                 ber_dupbv( &tmp_bv, &cookie );
508                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
509                                         }
510                                         if ( syncCookie.octet_str &&
511                                                         syncCookie.octet_str[0].bv_val )
512                                                 slap_parse_sync_cookie( &syncCookie );
513                                 }
514                                 entry = syncrepl_message_to_entry( si, op, msg,
515                                         &modlist, syncstate );
516                                 rc_efree = syncrepl_entry( si, op, entry, modlist, syncstate,
517                                                         &syncUUID, &syncCookie_req );
518                                 if ( syncCookie.octet_str && syncCookie.octet_str[0].bv_val ) {
519                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
520                                 }
521                                 ldap_controls_free( rctrls );
522                                 if ( modlist ) {
523                                         slap_mods_free( modlist );
524                                 }
525                                 if ( rc_efree && entry ) {
526                                         entry_free( entry );
527                                 }
528                                 break;
529
530                         case LDAP_RES_SEARCH_REFERENCE:
531 #ifdef NEW_LOGGING
532                                 LDAP_LOG( OPERATION, ERR,
533                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
534 #else
535                                 Debug( LDAP_DEBUG_ANY,
536                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
537 #endif
538                                 break;
539
540                         case LDAP_RES_SEARCH_RESULT:
541                                 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
542                                         &rctrls, 0 );
543                                 if ( rctrls ) {
544                                         rctrlp = *rctrls;
545                                         ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
546
547                                         ber_scanf( ber, "{" /*"}"*/);
548                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
549                                         {
550                                                 ber_scanf( ber, "m", &cookie );
551                                                 if ( cookie.bv_val ) {
552                                                         struct berval tmp_bv;
553                                                         ber_dupbv( &tmp_bv, &cookie );
554                                                         ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
555                                                 }
556                                                 if ( syncCookie.octet_str &&
557                                                                  syncCookie.octet_str[0].bv_val )
558                                                         slap_parse_sync_cookie( &syncCookie );
559                                         }
560                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
561                                         {
562                                                 ber_scanf( ber, "b", &refreshDeletes );
563                                         }
564                                         ber_scanf( ber, "}" );
565                                 }
566                                 if ( syncCookie_req.ctxcsn == NULL ) {
567                                         match = -1;
568                                 } else if ( syncCookie.ctxcsn == NULL ) {
569                                         match = 1;
570                                 } else {
571                                         value_match( &match, slap_schema.si_ad_entryCSN,
572                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
573                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
574                                                 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
575                                 }
576                                 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
577                                          && match < 0 ) {
578                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
579                                 }
580                                 if ( rctrls ) {
581                                         ldap_controls_free( rctrls );
582                                 }
583                                 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
584                                         /* FIXME : different error behaviors according to
585                                          *      1) err code : LDAP_BUSY ...
586                                          *      2) on err policy : stop service, stop sync, retry
587                                          */
588                                         if ( refreshDeletes == 0 && match < 0 ) {
589                                                 syncrepl_del_nonpresent( op, si );
590                                         } else {
591                                                 avl_free( si->si_presentlist, avl_ber_bvfree );
592                                                 si->si_presentlist = NULL;
593                                         }
594                                 }
595                                 rc = -2;
596                                 goto done;
597                                 break;
598
599                         case LDAP_RES_INTERMEDIATE:
600                                 rc = ldap_parse_intermediate( si->si_ld, msg,
601                                         &retoid, &retdata, NULL, 0 );
602                                 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
603                                         int             si_refreshDelete = 0;
604                                         int             si_refreshPresent = 0;
605                                         ber_init2( ber, retdata, LBER_USE_DER );
606
607                                         switch ( si_tag = ber_peek_tag( ber, &len )) {
608                                         ber_tag_t tag;
609                                         case LDAP_TAG_SYNC_NEW_COOKIE:
610                                                 ber_scanf( ber, "tm", &tag, &cookie );
611                                                 break;
612                                         case LDAP_TAG_SYNC_REFRESH_DELETE:
613                                                 si_refreshDelete = 1;
614                                         case LDAP_TAG_SYNC_REFRESH_PRESENT:
615                                                 si_refreshPresent = 1;
616                                                 ber_scanf( ber, "t{", &tag );
617                                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
618                                                 {
619                                                         ber_scanf( ber, "m", &cookie );
620                                                         if ( cookie.bv_val ) {
621                                                                 struct berval tmp_bv;
622                                                                 ber_dupbv( &tmp_bv, &cookie );
623                                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
624                                                         }
625                                                         if ( syncCookie.octet_str &&
626                                                                          syncCookie.octet_str[0].bv_val )
627                                                                 slap_parse_sync_cookie( &syncCookie );
628                                                 }
629                                                 if ( ber_peek_tag( ber, &len ) ==
630                                                                         LDAP_TAG_REFRESHDONE )
631                                                 {
632                                                         ber_scanf( ber, "b", &refreshDone );
633                                                 }
634                                                 ber_scanf( ber, "}" );
635                                                 break;
636                                         case LDAP_TAG_SYNC_ID_SET:
637                                                 ber_scanf( ber, "t{", &tag );
638                                                 if ( ber_peek_tag( ber, &len ) ==
639                                                                 LDAP_TAG_SYNC_COOKIE ) {
640                                                         ber_scanf( ber, "m", &cookie );
641                                                         if ( cookie.bv_val ) {
642                                                                 struct berval tmp_bv;
643                                                                 ber_dupbv( &tmp_bv, &cookie );
644                                                                 ber_bvarray_add( &syncCookie.octet_str,
645                                                                                                  &tmp_bv );
646                                                         }
647                                                         if ( syncCookie.octet_str &&
648                                                                          syncCookie.octet_str[0].bv_val )
649                                                                 slap_parse_sync_cookie( &syncCookie );
650                                                 }
651                                                 if ( ber_peek_tag( ber, &len ) ==
652                                                                         LDAP_TAG_REFRESHDELETES )
653                                                 {
654                                                         ber_scanf( ber, "b", &refreshDeletes );
655                                                 }
656                                                 ber_scanf( ber, "[W]", &syncUUIDs );
657                                                 ber_scanf( ber, "}" );
658                                                 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
659                                                         struct berval *syncuuid_bv;
660                                                         syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
661                                                         avl_insert( &si->si_presentlist,
662                                                                         (caddr_t) syncuuid_bv,
663                                                                         syncuuid_cmp, avl_dup_error );
664                                                 }
665                                                 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
666                                                 break;
667                                         default:
668 #ifdef NEW_LOGGING
669                                         LDAP_LOG( OPERATION, ERR,
670                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
671                                                 (long) si_tag, 0, 0 );
672 #else
673                                         Debug( LDAP_DEBUG_ANY,
674                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
675                                                 (long) si_tag, 0, 0 );
676 #endif
677                                                 ldap_memfree( retoid );
678                                                 ber_bvfree( retdata );
679                                                 continue;
680                                         }
681
682                                         if ( syncCookie_req.ctxcsn == NULL ) {
683                                                 match = -1;
684                                         } else if ( syncCookie.ctxcsn == NULL ) {
685                                                 match = 1;
686                                         } else {
687                                                 value_match( &match, slap_schema.si_ad_entryCSN,
688                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
689                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
690                                                         &syncCookie_req.ctxcsn[0],
691                                                         &syncCookie.ctxcsn[0], &text );
692                                         }
693
694                                         if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
695                                                  && match < 0 ) {
696                                                 syncrepl_updateCookie( si, op, psub, &syncCookie);
697                                         }
698
699                                         if ( si_refreshPresent == 1 ) {
700                                                 if ( match < 0 ) {
701                                                         syncrepl_del_nonpresent( op, si );
702                                                 }
703                                         } 
704
705                                         ldap_memfree( retoid );
706                                         ber_bvfree( retdata );
707                                         break;
708                                 } else {
709 #ifdef NEW_LOGGING
710                                         LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
711                                                 " unknown intermediate "
712                                                 "response\n", 0, 0, 0 );
713 #else
714                                         Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
715                                                 "unknown intermediate response (%d)\n",
716                                                 rc, 0, 0 );
717 #endif
718                                         ldap_memfree( retoid );
719                                         ber_bvfree( retdata );
720                                         break;
721                                 }
722                                 break;
723                         default:
724 #ifdef NEW_LOGGING
725                                 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
726                                         "unknown message\n", 0, 0, 0 );
727 #else
728                                 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
729                                         "unknown message\n", 0, 0, 0 );
730 #endif
731                                 break;
732
733                         }
734                         if ( syncCookie.octet_str ) {
735                                 slap_sync_cookie_free( &syncCookie_req, 0 );
736                                 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
737                                 slap_sync_cookie_free( &syncCookie, 0 );
738                         }
739                 }
740                 ldap_msgfree( res );
741                 res = NULL;
742         }
743
744         if ( rc == -1 ) {
745                 const char *errstr;
746
747                 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
748                 errstr = ldap_err2string( rc );
749                 
750 #ifdef NEW_LOGGING
751                 LDAP_LOG( OPERATION, ERR,
752                         "do_syncrep2 : %s\n", errstr, 0, 0 );
753 #else
754                 Debug( LDAP_DEBUG_ANY,
755                         "do_syncrep2 : %s\n", errstr, 0, 0 );
756 #endif
757         }
758
759 done:
760         slap_sync_cookie_free( &syncCookie, 0 );
761         slap_sync_cookie_free( &syncCookie_req, 0 );
762
763         if ( res ) ldap_msgfree( res );
764
765         if ( rc && si->si_ld ) {
766                 ldap_unbind( si->si_ld );
767                 si->si_ld = NULL;
768         }
769
770         return rc;
771 }
772
773 void *
774 do_syncrepl(
775         void    *ctx,
776         void    *arg )
777 {
778         struct re_s* rtask = arg;
779         syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
780         Connection conn = {0};
781         Operation op = {0};
782         int rc = LDAP_SUCCESS;
783         int first = 0;
784         int dostop = 0;
785         ber_socket_t s;
786
787 #ifdef NEW_LOGGING
788         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
789 #else
790         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
791 #endif
792
793         if ( si == NULL )
794                 return NULL;
795
796         switch( abs( si->si_type )) {
797         case LDAP_SYNC_REFRESH_ONLY:
798         case LDAP_SYNC_REFRESH_AND_PERSIST:
799                 break;
800         default:
801                 return NULL;
802         }
803
804         if ( slapd_abrupt_shutdown && si->si_ld ) {
805                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
806                 connection_client_stop( s );
807                 ldap_unbind( si->si_ld );
808                 si->si_ld = NULL;
809                 return NULL;
810         }
811
812         conn.c_connid = -1;
813         conn.c_send_ldap_result = slap_send_ldap_result;
814         conn.c_send_search_entry = slap_send_search_entry;
815         conn.c_send_search_reference = slap_send_search_reference;
816         conn.c_listener = (Listener *)&dummy_list;
817         conn.c_peer_name = slap_empty_bv;
818
819         /* set memory context */
820 #define SLAB_SIZE 1048576
821         op.o_tmpmemctx = sl_mem_create( SLAB_SIZE, ctx );
822         op.o_tmpmfuncs = &sl_mfuncs;
823
824         op.o_dn = si->si_updatedn;
825         op.o_ndn = si->si_updatedn;
826         op.o_time = slap_get_time();
827         op.o_threadctx = ctx;
828         op.o_managedsait = 1;
829         op.o_bd = si->si_be;
830         op.o_conn = &conn;
831         op.o_connid = op.o_conn->c_connid;
832
833         op.o_sync_state.ctxcsn = NULL;
834         op.o_sync_state.sid = -1;
835         op.o_sync_state.octet_str = NULL;
836         op.o_sync_slog_size = -1;
837         LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
838         op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
839
840         /* Establish session, do search */
841         if ( !si->si_ld ) {
842                 first = 1;
843                 rc = do_syncrep1( &op, si );
844         }
845
846         /* Process results */
847         if ( rc == LDAP_SUCCESS ) {
848                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
849
850                 rc = do_syncrep2( &op, si );
851
852                 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
853                         /* If we succeeded, enable the connection for further listening.
854                          * If we failed, tear down the connection and reschedule.
855                          */
856                         if ( rc == LDAP_SUCCESS ) {
857                                 if ( first ) {
858                                         rc = connection_client_setup( s, (Listener *)&dummy_list, do_syncrepl,
859                                                 arg );
860                                 } else {
861                                         connection_client_enable( s );
862                                 }
863                         } else if ( !first ) {
864                                 dostop = 1;
865                         }
866                 } else {
867                         if ( rc == -2 ) rc = 0;
868                 }
869         }
870
871         /* At this point, we have 4 cases:
872          * 1) for any hard failure, give up and remove this task
873          * 2) for ServerDown, reschedule this task to run
874          * 3) for Refresh and Success, reschedule to run
875          * 4) for Persist and Success, reschedule to defer
876          */
877         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
878         if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
879                 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
880         }
881
882         if ( dostop ) {
883                 connection_client_stop( s );
884         }
885
886         if ( rc && rc != LDAP_SERVER_DOWN ) {
887                 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
888         } else {
889                 if ( rc == LDAP_SERVER_DOWN ||
890                         si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
891                         rc = 0;
892                 } else {
893                         rc = 1;
894                 }
895                 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
896         }
897         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
898
899         return NULL;
900 }
901
902 Entry*
903 syncrepl_message_to_entry(
904         syncinfo_t      *si,
905         Operation       *op,
906         LDAPMessage     *msg,
907         Modifications   **modlist,
908         int             syncstate
909 )
910 {
911         Entry           *e = NULL;
912         BerElement      *ber = NULL;
913         Modifications   tmp;
914         Modifications   *mod;
915         Modifications   **modtail = modlist;
916
917         const char      *text;
918         char txtbuf[SLAP_TEXT_BUFLEN];
919         size_t textlen = sizeof txtbuf;
920
921         struct berval   bdn = {0, NULL}, dn, ndn;
922         int             rc;
923
924         *modlist = NULL;
925
926         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
927 #ifdef NEW_LOGGING
928                 LDAP_LOG( OPERATION, ERR,
929                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
930 #else
931                 Debug( LDAP_DEBUG_ANY,
932                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
933 #endif
934                 return NULL;
935         }
936
937         op->o_tag = LDAP_REQ_ADD;
938
939         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
940
941         if ( rc != LDAP_SUCCESS ) {
942 #ifdef NEW_LOGGING
943                 LDAP_LOG( OPERATION, ERR,
944                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
945 #else
946                 Debug( LDAP_DEBUG_ANY,
947                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
948 #endif
949                 return NULL;
950         }
951
952         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
953         ber_dupbv( &op->o_req_dn, &dn );
954         ber_dupbv( &op->o_req_ndn, &ndn );
955         sl_free( ndn.bv_val, op->o_tmpmemctx );
956         sl_free( dn.bv_val, op->o_tmpmemctx );
957
958         if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
959         {
960                 return NULL;
961         }
962
963         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
964         e->e_name = op->o_req_dn;
965         e->e_nname = op->o_req_ndn;
966
967         while ( ber_remaining( ber ) ) {
968                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
969                         LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
970                 {
971                         break;
972                 }
973
974                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
975
976                 mod->sml_op = LDAP_MOD_REPLACE;
977                 mod->sml_next = NULL;
978                 mod->sml_desc = NULL;
979                 mod->sml_type = tmp.sml_type;
980                 mod->sml_bvalues = tmp.sml_bvalues;
981                 mod->sml_nvalues = NULL;
982
983                 *modtail = mod;
984                 modtail = &mod->sml_next;
985         }
986
987         if ( *modlist == NULL ) {
988 #ifdef NEW_LOGGING
989                 LDAP_LOG( OPERATION, ERR,
990                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
991 #else
992                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
993                                 0, 0, 0 );
994 #endif
995         }
996
997         rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
998
999         if ( rc != LDAP_SUCCESS ) {
1000 #ifdef NEW_LOGGING
1001                 LDAP_LOG( OPERATION, ERR,
1002                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1003 #else
1004                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1005                                 text, 0, 0 );
1006 #endif
1007                 goto done;
1008         }
1009         
1010         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1011         if( rc != LDAP_SUCCESS ) {
1012 #ifdef NEW_LOGGING
1013                 LDAP_LOG( OPERATION, ERR,
1014                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1015 #else
1016                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1017                                 text, 0, 0 );
1018 #endif
1019         }
1020
1021 done:
1022         ber_free ( ber, 0 );
1023         if ( rc != LDAP_SUCCESS ) {
1024                 entry_free( e );
1025                 e = NULL;
1026         }
1027
1028         return e;
1029 }
1030
1031 int
1032 syncrepl_entry(
1033         syncinfo_t* si,
1034         Operation *op,
1035         Entry* e,
1036         Modifications* modlist,
1037         int syncstate,
1038         struct berval* syncUUID,
1039         struct sync_cookie* syncCookie_req
1040 )
1041 {
1042         Backend *be = op->o_bd;
1043         slap_callback   cb;
1044         struct berval   *syncuuid_bv = NULL;
1045         struct berval   syncUUID_strrep = { 0, NULL };
1046
1047         SlapReply       rs = {REP_RESULT};
1048         Filter f = {0};
1049         AttributeAssertion ava = {0};
1050         int rc = LDAP_SUCCESS;
1051         int ret = LDAP_SUCCESS;
1052         const char *text;
1053
1054         if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1055         {
1056                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1057                 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1058                         syncuuid_cmp, avl_dup_error );
1059         }
1060
1061         if ( syncstate == LDAP_SYNC_PRESENT ) {
1062                 return e ? 1 : 0;
1063         }
1064
1065         f.f_choice = LDAP_FILTER_EQUALITY;
1066         f.f_ava = &ava;
1067         ava.aa_desc = slap_schema.si_ad_entryUUID;
1068         slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1069         ava.aa_value = *syncUUID;
1070         op->ors_filter = &f;
1071
1072         op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1073         op->ors_filterstr.bv_val = (char *) sl_malloc(
1074                 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx ); 
1075         AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1076         AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1077                 syncUUID->bv_val, syncUUID->bv_len );
1078         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1079
1080         op->ors_scope = LDAP_SCOPE_SUBTREE;
1081
1082         /* get syncrepl cookie of shadow replica from subentry */
1083         op->o_req_dn = si->si_base;
1084         op->o_req_ndn = si->si_base;
1085
1086         /* set callback function */
1087         op->o_callback = &cb;
1088         cb.sc_response = dn_callback;
1089         cb.sc_private = si;
1090
1091         si->si_syncUUID_ndn.bv_val = NULL;
1092
1093         rc = be->be_search( op, &rs );
1094
1095         if ( op->ors_filterstr.bv_val ) {
1096                 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1097         }
1098
1099         cb.sc_response = null_callback;
1100         cb.sc_private = si;
1101
1102         if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1103         {
1104                 char *subseq_ptr;
1105
1106                 if ( syncstate != LDAP_SYNC_DELETE ) {
1107                         op->o_no_psearch = 1;
1108                 }
1109
1110                 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1111                 if ( op->o_sync_csn.bv_val ) {
1112                         subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1113                         subseq_ptr += 4;
1114                         *subseq_ptr = '1';
1115                 }
1116                 
1117                 op->o_req_dn = si->si_syncUUID_ndn;
1118                 op->o_req_ndn = si->si_syncUUID_ndn;
1119                 op->o_tag = LDAP_REQ_DELETE;
1120                 rc = be->be_delete( op, &rs );
1121                 op->o_no_psearch = 0;
1122         }
1123
1124         switch ( syncstate ) {
1125         case LDAP_SYNC_ADD:
1126         case LDAP_SYNC_MODIFY:
1127                 if ( rc == LDAP_SUCCESS ||
1128                          rc == LDAP_REFERRAL ||
1129                          rc == LDAP_NO_SUCH_OBJECT )
1130                 {
1131                         attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1132                         attr_merge_one( e, slap_schema.si_ad_entryUUID,
1133                                 syncUUID, &ava.aa_value );
1134
1135                         op->o_tag = LDAP_REQ_ADD;
1136                         op->ora_e = e;
1137                         op->o_req_dn = e->e_name;
1138                         op->o_req_ndn = e->e_nname;
1139                         rc = be->be_add( op, &rs );
1140
1141                         if ( rc != LDAP_SUCCESS ) {
1142                                 if ( rc == LDAP_ALREADY_EXISTS ) {      
1143                                         op->o_tag = LDAP_REQ_MODIFY;
1144                                         op->orm_modlist = modlist;
1145                                         op->o_req_dn = e->e_name;
1146                                         op->o_req_ndn = e->e_nname;
1147                                         rc = be->be_modify( op, &rs );
1148                                         if ( rc != LDAP_SUCCESS ) {
1149 #ifdef NEW_LOGGING
1150                                                 LDAP_LOG( OPERATION, ERR,
1151                                                         "syncrepl_entry : be_modify failed (%d)\n",
1152                                                         rc, 0, 0 );
1153 #else
1154                                                 Debug( LDAP_DEBUG_ANY,
1155                                                         "syncrepl_entry : be_modify failed (%d)\n",
1156                                                         rc, 0, 0 );
1157 #endif
1158                                         }
1159                                         ret = 1;
1160                                         goto done;
1161                                 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1162                                         syncrepl_add_glue( op, e );
1163                                         ret = 0;
1164                                         goto done;
1165                                 } else {
1166 #ifdef NEW_LOGGING
1167                                         LDAP_LOG( OPERATION, ERR,
1168                                                 "syncrepl_entry : be_add failed (%d)\n",
1169                                                 rc, 0, 0 );
1170 #else
1171                                         Debug( LDAP_DEBUG_ANY,
1172                                                 "syncrepl_entry : be_add failed (%d)\n",
1173                                                 rc, 0, 0 );
1174 #endif
1175                                         ret = 1;
1176                                         goto done;
1177                                 }
1178                         } else {
1179                                 be_entry_release_w( op, e );
1180                                 ret = 0;
1181                                 goto done;
1182                         }
1183                 } else {
1184 #ifdef NEW_LOGGING
1185                         LDAP_LOG( OPERATION, ERR,
1186                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1187 #else
1188                         Debug( LDAP_DEBUG_ANY,
1189                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1190 #endif
1191                         ret = 1;
1192                         goto done;
1193                 }
1194
1195         case LDAP_SYNC_DELETE :
1196                 /* Already deleted */
1197                 ret = 1;
1198                 goto done;
1199
1200         default :
1201 #ifdef NEW_LOGGING
1202                 LDAP_LOG( OPERATION, ERR,
1203                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1204 #else
1205                 Debug( LDAP_DEBUG_ANY,
1206                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1207 #endif
1208                 ret = 1;
1209                 goto done;
1210         }
1211
1212 done :
1213
1214         if ( syncUUID_strrep.bv_val ) {
1215                 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1216         }
1217         if ( si->si_syncUUID_ndn.bv_val ) {
1218                 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1219         }
1220         return ret;
1221 }
1222
1223 static void
1224 syncrepl_del_nonpresent(
1225         Operation *op,
1226         syncinfo_t *si
1227 )
1228 {
1229         Backend* be = op->o_bd;
1230         slap_callback   cb;
1231         SlapReply       rs = {REP_RESULT};
1232         struct nonpresent_entry *np_list, *np_prev;
1233
1234         op->o_req_dn = si->si_base;
1235         op->o_req_ndn = si->si_base;
1236
1237         cb.sc_response = nonpresent_callback;
1238         cb.sc_private = si;
1239
1240         op->o_callback = &cb;
1241         op->o_tag = LDAP_REQ_SEARCH;
1242         op->ors_scope = si->si_scope;
1243         op->ors_deref = LDAP_DEREF_NEVER;
1244         op->ors_slimit = 0;
1245         op->ors_tlimit = 0;
1246         op->ors_attrsonly = 0;
1247         op->ors_attrs = NULL;
1248         op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1249         op->ors_filterstr = si->si_filterstr;
1250
1251         op->o_nocaching = 1;
1252         be->be_search( op, &rs );
1253         op->o_nocaching = 0;
1254
1255         if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1256
1257         if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1258                 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1259                 while ( np_list != NULL ) {
1260                         LDAP_LIST_REMOVE( np_list, npe_link );
1261                         np_prev = np_list;
1262                         np_list = LDAP_LIST_NEXT( np_list, npe_link );
1263                         op->o_tag = LDAP_REQ_DELETE;
1264                         op->o_callback = &cb;
1265                         cb.sc_response = null_callback;
1266                         cb.sc_private = si;
1267                         op->o_req_dn = *np_prev->npe_name;
1268                         op->o_req_ndn = *np_prev->npe_nname;
1269                         op->o_bd->be_delete( op, &rs );
1270                         ber_bvfree( np_prev->npe_name );
1271                         ber_bvfree( np_prev->npe_nname );
1272                         op->o_req_dn.bv_val = NULL;
1273                         op->o_req_ndn.bv_val = NULL;
1274                         ch_free( np_prev );
1275                 }
1276         }
1277
1278         return;
1279 }
1280
1281
1282 static struct berval gcbva[] = {
1283         BER_BVC("top"),
1284         BER_BVC("glue")
1285 };
1286
1287 void
1288 syncrepl_add_glue(
1289         Operation* op,
1290         Entry *e
1291 )
1292 {
1293         Backend *be = op->o_bd;
1294         slap_callback cb;
1295         Attribute       *a;
1296         int     rc;
1297         int suffrdns;
1298         int i;
1299         struct berval dn = {0, NULL};
1300         struct berval ndn = {0, NULL};
1301         Entry   *glue;
1302         SlapReply       rs = {REP_RESULT};
1303         char    *ptr, *comma;
1304
1305         op->o_tag = LDAP_REQ_ADD;
1306         op->o_callback = &cb;
1307         cb.sc_response = null_callback;
1308         cb.sc_private = NULL;
1309
1310         dn = e->e_name;
1311         ndn = e->e_nname;
1312
1313         /* count RDNs in suffix */
1314         if ( be->be_nsuffix[0].bv_len ) {
1315                 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1316                         ptr++;
1317                         i++;
1318                 }
1319                 suffrdns = i;
1320         } else {
1321                 /* suffix is "" */
1322                 suffrdns = 0;
1323         }
1324
1325         /* Start with BE suffix */
1326         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1327                 comma = strrchr(dn.bv_val, ',');
1328                 if ( ptr ) *ptr = ',';
1329                 if ( comma ) *comma = '\0';
1330                 ptr = comma;
1331         }
1332         if ( ptr ) {
1333                 *ptr++ = ',';
1334                 dn.bv_len -= ptr - dn.bv_val;
1335                 dn.bv_val = ptr;
1336         }
1337         /* the normalizedDNs are always the same length, no counting
1338          * required.
1339          */
1340         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1341                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1342                 ndn.bv_len = be->be_nsuffix[0].bv_len;
1343         }
1344
1345         while ( ndn.bv_val > e->e_nname.bv_val ) {
1346                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1347                 ber_dupbv( &glue->e_name, &dn );
1348                 ber_dupbv( &glue->e_nname, &ndn );
1349
1350                 a = ch_calloc( 1, sizeof( Attribute ));
1351                 a->a_desc = slap_schema.si_ad_objectClass;
1352
1353                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1354                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1355                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1356                 a->a_vals[2].bv_len = 0;
1357                 a->a_vals[2].bv_val = NULL;
1358
1359                 a->a_nvals = a->a_vals;
1360
1361                 a->a_next = glue->e_attrs;
1362                 glue->e_attrs = a;
1363
1364                 a = ch_calloc( 1, sizeof( Attribute ));
1365                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1366
1367                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1368                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1369                 a->a_vals[1].bv_len = 0;
1370                 a->a_vals[1].bv_val = NULL;
1371
1372                 a->a_nvals = a->a_vals;
1373
1374                 a->a_next = glue->e_attrs;
1375                 glue->e_attrs = a;
1376
1377                 op->o_req_dn = glue->e_name;
1378                 op->o_req_ndn = glue->e_nname;
1379                 op->ora_e = glue;
1380                 rc = be->be_add ( op, &rs );
1381                 if ( rc == LDAP_SUCCESS ) {
1382                         be_entry_release_w( op, glue );
1383                 } else {
1384                 /* incl. ALREADY EXIST */
1385                         entry_free( glue );
1386                 }
1387
1388                 /* Move to next child */
1389                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1390                         /* empty */
1391                 }
1392                 if ( ptr == e->e_name.bv_val ) break;
1393                 dn.bv_val = ++ptr;
1394                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1395                 for( ptr = ndn.bv_val-2;
1396                         ptr > e->e_nname.bv_val && *ptr != ',';
1397                         ptr--)
1398                 {
1399                         /* empty */
1400                 }
1401                 ndn.bv_val = ++ptr;
1402                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1403         }
1404
1405         op->o_req_dn = e->e_name;
1406         op->o_req_ndn = e->e_nname;
1407         op->ora_e = e;
1408         rc = be->be_add ( op, &rs );
1409         if ( rc == LDAP_SUCCESS ) {
1410                 be_entry_release_w( op, e );
1411         } else {
1412                 entry_free( e );
1413         }
1414
1415         return;
1416 }
1417
1418 static struct berval ocbva[] = {
1419         BER_BVC("top"),
1420         BER_BVC("subentry"),
1421         BER_BVC("syncConsumerSubentry"),
1422         BER_BVNULL
1423 };
1424
1425 static struct berval cnbva[] = {
1426         BER_BVNULL,
1427         BER_BVNULL
1428 };
1429
1430 static struct berval ssbva[] = {
1431         BER_BVC("{}"),
1432         BER_BVNULL
1433 };
1434
1435 static struct berval scbva[] = {
1436         BER_BVNULL,
1437         BER_BVNULL
1438 };
1439
1440 void
1441 syncrepl_updateCookie(
1442         syncinfo_t *si,
1443         Operation *op,
1444         struct berval *pdn,
1445         struct sync_cookie *syncCookie
1446 )
1447 {
1448         Backend *be = op->o_bd;
1449         Modifications *ml;
1450         Modifications *mlnext;
1451         Modifications *mod;
1452         Modifications *modlist = NULL;
1453         Modifications **modtail = &modlist;
1454
1455         const char      *text;
1456         char txtbuf[SLAP_TEXT_BUFLEN];
1457         size_t textlen = sizeof txtbuf;
1458
1459         Entry* e = NULL;
1460         int rc;
1461
1462         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1463         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1464         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1465         
1466         slap_callback cb;
1467         SlapReply       rs = {REP_RESULT};
1468
1469         slap_sync_cookie_free( &si->si_syncCookie, 0 );
1470         slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1471
1472         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1473         mod->sml_op = LDAP_MOD_REPLACE;
1474         mod->sml_desc = slap_schema.si_ad_objectClass;
1475         mod->sml_type = mod->sml_desc->ad_cname;
1476         mod->sml_bvalues = ocbva;
1477         *modtail = mod;
1478         modtail = &mod->sml_next;
1479
1480         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1481         assert( si->si_rid < 1000 );
1482         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1483                 slap_syncrepl_bvc.bv_len,
1484                 "syncrepl%ld", si->si_rid );
1485         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1486         mod->sml_op = LDAP_MOD_REPLACE;
1487         mod->sml_desc = slap_schema.si_ad_cn;
1488         mod->sml_type = mod->sml_desc->ad_cname;
1489         mod->sml_bvalues = cnbva;
1490         *modtail = mod;
1491         modtail = &mod->sml_next;
1492
1493         if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1494         ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1495         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1496         mod->sml_op = LDAP_MOD_REPLACE;
1497         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1498         mod->sml_type = mod->sml_desc->ad_cname;
1499         mod->sml_bvalues = scbva;
1500         *modtail = mod;
1501         modtail = &mod->sml_next;
1502
1503         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1504         mod->sml_op = LDAP_MOD_REPLACE;
1505         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1506         mod->sml_type = mod->sml_desc->ad_cname;
1507         mod->sml_bvalues = ssbva;
1508         *modtail = mod;
1509         modtail = &mod->sml_next;
1510
1511         mlnext = mod;
1512
1513         op->o_tag = LDAP_REQ_ADD;
1514         rc = slap_mods_opattrs( op, modlist, modtail,
1515                                                          &text,txtbuf, textlen );
1516
1517         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1518                 ml->sml_op = LDAP_MOD_REPLACE;
1519         }
1520
1521         if( rc != LDAP_SUCCESS ) {
1522 #ifdef NEW_LOGGING
1523                 LDAP_LOG( OPERATION, ERR,
1524                         "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1525 #else
1526                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1527                          text, 0, 0 );
1528 #endif
1529         }
1530
1531         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1532
1533         slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1534         assert( si->si_rid < 1000 );
1535         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1536                 slap_syncrepl_cn_bvc.bv_len,
1537                 "cn=syncrepl%ld", si->si_rid );
1538
1539         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1540                 op->o_tmpmemctx );
1541         ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1542         ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1543
1544         if ( slap_syncrepl_dn_bv.bv_val ) {
1545                 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1546         }
1547
1548         e->e_attrs = NULL;
1549
1550         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1551
1552         if( rc != LDAP_SUCCESS ) {
1553 #ifdef NEW_LOGGING
1554                 LDAP_LOG( OPERATION, ERR,
1555                         "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1556 #else
1557                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1558                          text, 0, 0 );
1559 #endif
1560         }
1561
1562         cb.sc_response = null_callback;
1563         cb.sc_private = si;
1564
1565         op->o_callback = &cb;
1566         op->o_req_dn = e->e_name;
1567         op->o_req_ndn = e->e_nname;
1568
1569         /* update persistent cookie */
1570 update_cookie_retry:
1571         op->o_tag = LDAP_REQ_MODIFY;
1572         op->orm_modlist = modlist;
1573         rc = be->be_modify( op, &rs );
1574
1575         if ( rc != LDAP_SUCCESS ) {
1576                 if ( rc == LDAP_REFERRAL ||
1577                          rc == LDAP_NO_SUCH_OBJECT ) {
1578                         op->o_tag = LDAP_REQ_ADD;
1579                         op->ora_e = e;
1580                         rc = be->be_add( op, &rs );
1581                         if ( rc != LDAP_SUCCESS ) {
1582                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1583                                         goto update_cookie_retry;
1584                                 } else if ( rc == LDAP_REFERRAL ||
1585                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1586 #ifdef NEW_LOGGING
1587                                         LDAP_LOG( OPERATION, ERR,
1588                                                 "cookie will be non-persistent\n",
1589                                                 0, 0, 0 );
1590 #else
1591                                         Debug( LDAP_DEBUG_ANY,
1592                                                 "cookie will be non-persistent\n",
1593                                                 0, 0, 0 );
1594 #endif
1595                                 } else {
1596 #ifdef NEW_LOGGING
1597                                         LDAP_LOG( OPERATION, ERR,
1598                                                 "be_add failed (%d)\n",
1599                                                 rc, 0, 0 );
1600 #else
1601                                         Debug( LDAP_DEBUG_ANY,
1602                                                 "be_add failed (%d)\n",
1603                                                 rc, 0, 0 );
1604 #endif
1605                                 }
1606                         } else {
1607                                 be_entry_release_w( op, e );
1608                                 goto done;
1609                         }
1610                 } else {
1611 #ifdef NEW_LOGGING
1612                         LDAP_LOG( OPERATION, ERR,
1613                                 "be_modify failed (%d)\n", rc, 0, 0 );
1614 #else
1615                         Debug( LDAP_DEBUG_ANY,
1616                                 "be_modify failed (%d)\n", rc, 0, 0 );
1617 #endif
1618                 }
1619         }
1620
1621         if ( e != NULL ) {
1622                 entry_free( e );
1623         }
1624
1625 done :
1626
1627         if ( cnbva[0].bv_val ) {
1628                 ch_free( cnbva[0].bv_val );
1629                 cnbva[0].bv_val = NULL;
1630         }
1631         if ( scbva[0].bv_val ) {
1632                 ch_free( scbva[0].bv_val );
1633                 scbva[0].bv_val = NULL;
1634         }
1635
1636         if ( mlnext->sml_next ) {
1637                 slap_mods_free( mlnext->sml_next );
1638                 mlnext->sml_next = NULL;
1639         }
1640
1641         for (ml = modlist ; ml != NULL; ml = mlnext ) {
1642                 mlnext = ml->sml_next;
1643                 free( ml );
1644         }
1645
1646         return;
1647 }
1648
1649 static int
1650 dn_callback(
1651         Operation*      op,
1652         SlapReply*      rs
1653 )
1654 {
1655         syncinfo_t *si = op->o_callback->sc_private;
1656
1657         if ( rs->sr_type == REP_SEARCH ) {
1658                 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1659 #ifdef NEW_LOGGING
1660                         LDAP_LOG( OPERATION, ERR,
1661                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1662 #else
1663                         Debug( LDAP_DEBUG_ANY,
1664                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1665 #endif
1666                 } else {
1667                         ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1668                 }
1669         }
1670
1671         return LDAP_SUCCESS;
1672 }
1673
1674 static int
1675 nonpresent_callback(
1676         Operation*      op,
1677         SlapReply*      rs
1678 )
1679 {
1680         syncinfo_t *si = op->o_callback->sc_private;
1681         Attribute *a;
1682         int count = 0;
1683         struct berval* present_uuid = NULL;
1684         struct nonpresent_entry *np_entry;
1685
1686         if ( rs->sr_type == REP_RESULT ) {
1687                 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1688                 si->si_presentlist = NULL;
1689
1690         } else if ( rs->sr_type == REP_SEARCH ) {
1691                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1692
1693                 if ( a == NULL ) return 0;
1694
1695                 present_uuid = avl_find( si->si_presentlist, &a->a_vals[0],
1696                         syncuuid_cmp );
1697
1698                 if ( present_uuid == NULL ) {
1699                         np_entry = (struct nonpresent_entry *)
1700                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1701                         np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1702                         np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1703                         LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1704
1705                 } else {
1706                         avl_delete( &si->si_presentlist,
1707                                         &a->a_vals[0], syncuuid_cmp );
1708                         ch_free( present_uuid->bv_val );
1709                         ch_free( present_uuid );
1710                 }
1711         }
1712         return LDAP_SUCCESS;
1713 }
1714
1715 static int
1716 null_callback(
1717         Operation*      op,
1718         SlapReply*      rs
1719 )
1720 {
1721         if ( rs->sr_err != LDAP_SUCCESS &&
1722                 rs->sr_err != LDAP_REFERRAL &&
1723                 rs->sr_err != LDAP_ALREADY_EXISTS &&
1724                 rs->sr_err != LDAP_NO_SUCH_OBJECT )
1725         {
1726 #ifdef NEW_LOGGING
1727                 LDAP_LOG( OPERATION, ERR,
1728                         "null_callback : error code 0x%x\n",
1729                         rs->sr_err, 0, 0 );
1730 #else
1731                 Debug( LDAP_DEBUG_ANY,
1732                         "null_callback : error code 0x%x\n",
1733                         rs->sr_err, 0, 0 );
1734 #endif
1735         }
1736         return LDAP_SUCCESS;
1737 }
1738
1739 Entry *
1740 slap_create_syncrepl_entry(
1741         Backend *be,
1742         struct berval *context_csn,
1743         struct berval *rdn,
1744         struct berval *cn
1745 )
1746 {
1747         Entry* e;
1748
1749         struct berval bv;
1750
1751         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1752
1753         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1754
1755         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1756                 &ocbva[1], NULL );
1757
1758         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1759
1760         if ( context_csn ) {
1761                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1762                         context_csn, NULL );
1763         }
1764
1765         bv.bv_val = "{}";
1766         bv.bv_len = sizeof("{}")-1;
1767         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1768
1769         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1770         ber_dupbv( &e->e_nname, &e->e_name );
1771
1772         return e;
1773 }
1774
1775 struct berval *
1776 slap_uuidstr_from_normalized(
1777         struct berval* uuidstr,
1778         struct berval* normalized,
1779         void *ctx )
1780 {
1781         struct berval *new;
1782         unsigned char nibble;
1783         int i, d = 0;
1784
1785         if ( normalized == NULL )
1786                 return NULL;
1787
1788         if ( normalized->bv_len != 16 ) {
1789                 return NULL;
1790         }
1791
1792         if ( uuidstr ) {
1793                 new = uuidstr;
1794         } else {
1795                 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1796         }
1797
1798         new->bv_len = 36;
1799
1800         if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1801                 if ( !uuidstr )
1802                         sl_free( new, ctx );
1803                 return NULL;
1804         }
1805
1806         for ( i = 0; i < 16; i++ ) {
1807                 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1808                         new->bv_val[(i<<1)+d] = '-';
1809                         d += 1;
1810                 }
1811
1812                 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1813                 if ( nibble < 10 ) {
1814                         new->bv_val[(i<<1)+d] = nibble + '0';
1815                 } else {
1816                         new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1817                 }
1818
1819                 nibble = (normalized->bv_val[i]) & 0xF;
1820                 if ( nibble < 10 ) {
1821                         new->bv_val[(i<<1)+d+1] = nibble + '0';
1822                 } else {
1823                         new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1824                 }
1825         }
1826
1827         new->bv_val[new->bv_len] = '\0';
1828
1829         return new;
1830 }
1831
1832 static int
1833 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1834 {
1835         const struct berval *uuid1 = v_uuid1;
1836         const struct berval *uuid2 = v_uuid2;
1837         int rc = uuid1->bv_len - uuid2->bv_len;
1838         if ( rc ) return rc;
1839         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
1840 }
1841
1842 static void
1843 avl_ber_bvfree( void *bv )
1844 {
1845         if( bv == NULL ) {
1846                 return;
1847         }
1848         if ( ((struct berval *)bv)->bv_val != NULL ) {
1849                 ch_free ( ((struct berval *)bv)->bv_val );
1850         }
1851         ch_free ( (char *) bv );
1852 }
1853