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