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