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