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