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