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