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