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