]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
Tentative fix for last comment (tests still running)
[openldap] / servers / slapd / syncrepl.c
1 /* $OpenLDAP$ */
2 /*
3  * Replication Engine which uses the LDAP Sync protocol
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <db.h>
30
31 #include "ldap_pvt.h"
32 #include "lutil.h"
33 #include "slap.h"
34 #include "lutil_ldap.h"
35
36 #include "ldap_rq.h"
37
38 static const struct berval slap_syncrepl_bvc = BER_BVC("syncreplxxx");
39 static const struct berval slap_syncrepl_cn_bvc = BER_BVC("cn=syncreplxxx");
40
41 static void
42 syncrepl_del_nonpresent( LDAP *, Operation * );
43
44 /* callback functions */
45 static int cookie_callback( struct slap_op *, struct slap_rep * );
46 static int dn_callback( struct slap_op *, struct slap_rep * );
47 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
48 static int null_callback( struct slap_op *, struct slap_rep * );
49 static int contextcsn_callback( Operation*, SlapReply* );
50
51 static AttributeDescription **sync_descs;
52
53 struct runqueue_s syncrepl_rq;
54
55 void
56 init_syncrepl()
57 {
58         sync_descs = ch_malloc( 4 * sizeof( AttributeDescription * ));
59         sync_descs[0] = slap_schema.si_ad_objectClass;
60         sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61         sync_descs[2] = slap_schema.si_ad_entryCSN;
62         sync_descs[3] = NULL;
63 }
64
65 int
66 ldap_sync_search(
67         syncinfo_t *si,
68         LDAP *ld,
69         LDAPControl **sctrls,
70         LDAPControl **cctrls,
71         int *msgidp )
72 {
73         BerElement      *ber;
74         int timelimit;
75         ber_int_t id;
76
77         int rc;
78         BerElement      *sync_ber = NULL;
79         struct berval *sync_bvalp = NULL;
80         LDAPControl c[2];
81         LDAPControl **ctrls;
82         int err;
83         struct timeval timeout;
84
85     /* setup LDAP SYNC control */
86     sync_ber = ber_alloc_t( LBER_USE_DER );
87     ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, NULL );
88
89     if ( si->syncCookie ) {
90         ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
91     } else {
92         ber_printf( sync_ber, "{e}", abs(si->type) );
93     }
94
95     if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
96         ber_free( sync_ber, 1 );
97         return LBER_ERROR;
98     }
99     ber_free( sync_ber, 1 );
100
101     ctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), NULL );
102
103     c[0].ldctl_oid = LDAP_CONTROL_SYNC;
104     c[0].ldctl_value = (*sync_bvalp);
105     c[0].ldctl_iscritical = si->type < 0;
106     ctrls[0] = &c[0];
107
108     if ( si->authzId ) {
109         c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
110         c[1].ldctl_value.bv_val = si->authzId;
111         c[1].ldctl_value.bv_len = strlen( si->authzId );
112         c[1].ldctl_iscritical = 1;
113         ctrls[1] = &c[1];
114     } else {
115         ctrls[1] = NULL;
116     }
117
118     ctrls[2] = NULL;
119
120     err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
121
122     ber_bvfree( sync_bvalp );
123     ch_free( ctrls );
124
125     if ( err != LDAP_OPT_SUCCESS )
126         fprintf( stderr, "Could not set controls : %d\n", err );
127
128         timeout.tv_sec = si->tlimit > 0 ? si->tlimit : 1;
129
130         rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
131                                                   si->attrs, si->attrsonly, sctrls, cctrls,
132                                                   si->tlimit < 0 ? NULL : &timeout,
133                                                   si->slimit, msgidp );
134
135         return rc;
136 }
137
138 void *
139 do_syncrepl(
140         void    *ctx,
141         void    *arg )
142 {
143         struct re_s* rtask = arg;
144         syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
145         Backend *be = si->be;
146
147         SlapReply       rs = {REP_RESULT};
148
149         LDAPControl     c[2];
150         LDAPControl     **sctrls = NULL;
151         LDAPControl     **rctrls = NULL;
152         LDAPControl     *rctrlp = NULL;
153         BerElement      *sync_ber = NULL;
154         struct berval   *sync_bvalp = NULL;
155
156         BerElement      *ctrl_ber = NULL;
157         BerElement      *res_ber = NULL;
158
159         LDAP    *ld = NULL;
160         LDAPMessage     *res = NULL;
161         LDAPMessage     *msg = NULL;
162
163         ber_int_t       msgid;
164
165         int             nresponses, nreferences, nextended, npartial;
166         int             nresponses_psearch;
167
168         int             cancel_msgid = -1;
169         char            *retoid = NULL;
170         struct berval   *retdata = NULL;
171
172         int             sync_info_arrived = 0;
173         Entry           *entry = NULL;
174
175         int             syncstate;
176         struct berval   syncUUID = { 0, NULL };
177         struct berval   syncCookie = { 0, NULL };
178         struct berval   syncCookie_req = { 0, NULL };
179
180         int     rc;
181         int     err;
182         ber_len_t       len;
183         int     syncinfo_arrived = 0;
184
185         char **tmp = NULL;
186         AttributeDescription** descs = NULL;
187
188         Connection conn;
189         Operation op = {0};
190         slap_callback   cb;
191
192         void *memctx = NULL;
193         ber_len_t memsiz;
194         
195         int i, j, k, n;
196         int rc_efree;
197
198         struct berval base_bv = { 0, NULL };
199         struct berval pbase = { 0, NULL };
200         struct berval nbase = { 0, NULL };
201         struct berval psubrdn = { 0, NULL };
202         struct berval nsubrdn = { 0, NULL };
203         struct berval psub = { 0, NULL };
204         struct berval nsub = { 0, NULL };
205         Modifications   *modlist = NULL;
206         Modifications   *ml, *mlnext;
207         char *def_filter_str = NULL;
208
209         struct berval slap_syncrepl_bv = BER_BVNULL;
210
211         const char              *text;
212         int                             match;
213
214         struct timeval *tout_p = NULL;
215         struct timeval tout = { 10, 0 };
216
217 #ifdef NEW_LOGGING
218         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
219 #else
220         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
221 #endif
222
223         if ( si == NULL )
224                 return NULL;
225
226         if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
227              abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
228                 return NULL;
229         }
230
231         si->sync_mode = LDAP_SYNC_STATE_MODE;
232
233         /* Init connection to master */
234
235         rc = ldap_initialize( &ld, si->provideruri );
236         if ( rc != LDAP_SUCCESS ) {
237 #ifdef NEW_LOGGING
238                 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
239                         "ldap_initialize failed (%s)\n",
240                         si->provideruri, 0, 0 );
241 #else
242                 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
243                         "ldap_initialize failed (%s)\n",
244                         si->provideruri, 0, 0 );
245 #endif
246         }
247
248         op.o_protocol = LDAP_VERSION3;
249         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
250
251         /* Bind to master */
252
253         if ( si->tls ) {
254                 rc = ldap_start_tls_s( ld, NULL, NULL );
255                 if( rc != LDAP_SUCCESS ) {
256 #ifdef NEW_LOGGING
257                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
258                                 "%s: ldap_start_tls failed (%d)\n",
259                                 si->tls == TLS_CRITICAL ? "Error" : "Warning",
260                                 rc, 0 );
261 #else
262                         Debug( LDAP_DEBUG_ANY,
263                                 "%s: ldap_start_tls failed (%d)\n",
264                                 si->tls == TLS_CRITICAL ? "Error" : "Warning",
265                                 rc, 0 );
266 #endif
267                         if( si->tls == TLS_CRITICAL )
268                                 return NULL;
269                 }
270         }
271
272         if ( si->bindmethod == LDAP_AUTH_SASL ) {
273 #ifdef HAVE_CYRUS_SASL
274                 void *defaults;
275
276                 if ( si->secprops != NULL ) {
277                         int err = ldap_set_option( ld,
278                                         LDAP_OPT_X_SASL_SECPROPS, si->secprops);
279
280                         if( err != LDAP_OPT_SUCCESS ) {
281 #ifdef NEW_LOGGING
282                                 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
283                                         "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
284                                         si->provideruri, si->secprops, 0 );
285 #else
286                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
287                                         "(%s,SECPROPS,\"%s\") failed!\n",
288                                         si->provideruri, si->secprops, NULL );
289 #endif
290                                 return NULL;
291                         }
292                 }
293
294                 defaults = lutil_sasl_defaults( ld,
295                                 si->saslmech,
296                                 si->realm,
297                                 si->authcId,
298                                 si->passwd,
299                                 si->authzId );
300
301                 rc = ldap_sasl_interactive_bind_s( ld,
302                                 si->binddn,
303                                 si->saslmech,
304                                 NULL, NULL,
305                                 LDAP_SASL_QUIET,
306                                 lutil_sasl_interact,
307                                 defaults );
308
309                 /* FIXME : different error behaviors according to
310                         1) return code
311                         2) on err policy : exit, retry, backoff ...
312                 */
313                 if ( rc != LDAP_SUCCESS ) {
314 #ifdef NEW_LOGGING
315                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
316                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
317                                 rc, 0, 0 );
318 #else
319                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
320                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
321                                 rc, 0, 0 );
322 #endif
323                         return NULL;
324                 }
325 #else /* HAVE_CYRUS_SASL */
326                 fprintf( stderr, "not compiled with SASL support\n" );
327                 return NULL;
328 #endif
329         } else {
330                 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
331                 if ( rc != LDAP_SUCCESS ) {
332 #ifdef NEW_LOGGING
333                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
334                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
335 #else
336                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
337                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
338 #endif
339                         return NULL;
340                 }
341         }
342
343         /* set thread context in syncinfo */
344         si->ctx = ctx;
345
346         /* set memory context */
347 #define SLAB_SIZE 1048576
348         memsiz = SLAB_SIZE;
349         memctx = sl_mem_create( memsiz, ctx );
350         op.o_tmpmemctx = memctx;
351         op.o_tmpmfuncs = &sl_mfuncs;
352
353         op.o_si = si;
354         op.o_tag = LDAP_REQ_SEARCH;
355         op.o_dn = si->updatedn;
356         op.o_ndn = si->updatedn;
357         op.o_callback = &cb;
358         op.o_time = slap_get_time();
359         op.o_managedsait = 1;
360         op.o_threadctx = si->ctx;
361         op.o_bd = be;
362         op.o_conn = &conn;
363         op.o_connid = op.o_conn->c_connid;
364         op.ors_scope = LDAP_SCOPE_BASE;
365         op.ors_deref = LDAP_DEREF_NEVER;
366         op.ors_slimit = 0;
367         op.ors_tlimit = 0;
368         op.ors_attrsonly = 0;
369         op.ors_attrs = NULL;
370         op.ors_filter = str2filter_x( &op, def_filter_str = "(objectClass=*)" );
371         ber_str2bv( def_filter_str, 0, 0, &op.ors_filterstr );
372
373         si->conn = &conn;
374         conn.c_send_ldap_result = slap_send_ldap_result;
375         conn.c_send_search_entry = slap_send_search_entry;
376         conn.c_send_search_reference = slap_send_search_reference;
377
378         /* get syncrepl cookie of shadow replica from subentry */
379         ber_str2bv( si->base, 0, 0, &base_bv ); 
380         dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
381
382         ber_dupbv( &slap_syncrepl_bv, (struct berval *) &slap_syncrepl_bvc );
383         slap_syncrepl_bv.bv_len = snprintf( slap_syncrepl_bv.bv_val,
384                                                                         slap_syncrepl_bvc.bv_len,
385                                                                         "syncrepl%d", si->id );
386         build_new_dn( &op.o_req_dn, &pbase, &slap_syncrepl_bv, op.o_tmpmemctx );
387         build_new_dn( &op.o_req_ndn, &nbase, &slap_syncrepl_bv, op.o_tmpmemctx );
388
389         /* set callback function */
390         cb.sc_response = cookie_callback;
391         cb.sc_private = si;
392
393         /* search subentry to retrieve cookie */
394         si->syncCookie = NULL;
395         be->be_search( &op, &rs );
396
397         if ( op.o_req_dn.bv_val )
398                 ch_free( op.o_req_dn.bv_val );
399         if ( op.o_req_ndn.bv_val )
400                 ch_free( op.o_req_ndn.bv_val );
401         if ( op.ors_filter )
402                 filter_free( op.ors_filter );
403         if ( op.ors_filterstr.bv_val )
404                 ch_free( op.ors_filterstr.bv_val );
405         if ( slap_syncrepl_bv.bv_val )
406                 ch_free( slap_syncrepl_bv.bv_val );
407         if ( pbase.bv_val )
408                 ch_free( pbase.bv_val );
409         if ( nbase.bv_val )
410                 ch_free( nbase.bv_val );
411
412         ber_dupbv( &syncCookie_req, si->syncCookie );
413
414         psub = be->be_nsuffix[0];
415
416         for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
417
418         if ( n != 0 ) {
419                 /* Delete Attributes */
420                 descs = sync_descs;
421                 for ( i = 0; descs[i] != NULL; i++ ) {
422                         for ( j = 0; si->attrs[j] != NULL; j++ ) {
423                                 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
424                                         ch_free( si->attrs[j] );
425                                         for ( k = j; si->attrs[k] != NULL; k++ ) {
426                                                 si->attrs[k] = si->attrs[k+1];
427                                         }
428                                 }
429                         }
430                 }
431                 for ( n = 0; si->attrs[ n ] != NULL; n++ );
432                 tmp = ( char ** ) ch_realloc( si->attrs, ( n + 4 ) * sizeof( char * ));
433                 if ( tmp == NULL ) {
434 #ifdef NEW_LOGGING
435                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
436 #else
437                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
438 #endif
439                 }
440         } else {
441                 tmp = ( char ** ) ch_realloc( si->attrs, 5 * sizeof( char * ));
442                 if ( tmp == NULL ) {
443 #ifdef NEW_LOGGING
444                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
445 #else
446                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
447 #endif
448                 }
449                 tmp[ n++ ] = ch_strdup( "*" );
450         }
451         
452         descs = sync_descs;
453         si->attrs = tmp;
454
455         /* Add Attributes */
456
457         for ( i = 0; descs[ i ] != NULL; i++ ) {
458                 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
459                 si->attrs[ n ] = NULL;
460         }
461
462         rc = ldap_sync_search( si, ld, NULL, NULL, &msgid );
463         if( rc != LDAP_SUCCESS ) {
464                 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
465                                                         ldap_err2string( rc ), rc );
466                 return NULL;
467         }
468
469         if ( abs(si->type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
470                 tout_p = &tout;
471         } else {
472                 tout_p = NULL;
473         }
474
475         while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res )) >= 0 ) {
476
477                 if ( rc == 0 ) {
478                         if ( slapd_abrupt_shutdown ) {
479                                 break;
480                         } else {
481                                 continue;
482                         }
483                 }
484
485                 for ( msg = ldap_first_message( ld, res );
486                       msg != NULL;
487                       msg = ldap_next_message( ld, msg ) )
488                 {
489                         syncCookie.bv_len = 0; syncCookie.bv_val = NULL;
490                         switch( ldap_msgtype( msg ) ) {
491                         case LDAP_RES_SEARCH_ENTRY:
492                                 entry = syncrepl_message_to_entry( si, ld, &op, msg,
493                                         &modlist, &syncstate, &syncUUID, &syncCookie );
494                                 rc_efree = syncrepl_entry( si, ld, &op, entry, modlist,
495                                                 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
496                                 if ( syncCookie.bv_len ) {
497                                         syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
498                                 }
499                                 if ( modlist ) {
500                                         slap_mods_free( modlist );
501                                 }
502                                 if ( rc_efree ) {
503                                         entry_free( entry );
504                                 }
505                                 break;
506
507                         case LDAP_RES_SEARCH_REFERENCE:
508 #ifdef NEW_LOGGING
509                                 LDAP_LOG( OPERATION, ERR,
510                                         "do_syncrepl : reference received\n", 0, 0, 0 );
511 #else
512                                 Debug( LDAP_DEBUG_ANY,
513                                         "do_syncrepl : reference received\n", 0, 0, 0 );
514 #endif
515                                 break;
516
517                         case LDAP_RES_SEARCH_RESULT:
518                                 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
519                                 if ( rctrls ) {
520                                         rctrlp = *rctrls;
521                                         ctrl_ber = ber_alloc_t( LBER_USE_DER );
522                                         ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
523                                         ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
524                                         ber_reset( ctrl_ber, 1 );
525
526                                         ber_scanf( ctrl_ber, "{" /*"}"*/);
527                                         if ( ber_peek_tag( ctrl_ber, &len )
528                                                 == LDAP_SYNC_TAG_COOKIE ) {
529                                                 ber_scanf( ctrl_ber, "o", &syncCookie );
530                                         }
531                                 }
532                                 value_match( &match, slap_schema.si_ad_entryCSN,
533                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
534                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
535                                                         &syncCookie_req, &syncCookie, &text );
536                                 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
537                                         /* FIXME : different error behaviors according to
538                                                 1) err code : LDAP_BUSY ...
539                                                 2) on err policy : stop service, stop sync, retry
540                                         */
541                                         if ( syncCookie.bv_len && match < 0) {
542                                                 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
543                                         }
544                                         if ( ctrl_ber )
545                                                 ber_free( ctrl_ber, 1 );
546                                         goto done;
547                                 } else {
548                                         /* FIXME : different error behaviors according to
549                                                 1) err code : LDAP_BUSY ...
550                                                 2) on err policy : stop service, stop sync, retry
551                                         */
552                                         if ( syncCookie.bv_len && match < 0 ) {
553                                                 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
554                                         }
555                                         if ( si->sync_mode == LDAP_SYNC_STATE_MODE && match < 0 ) {
556                                                         syncrepl_del_nonpresent( ld, &op );
557                                         }
558                                         if ( ctrl_ber )
559                                                 ber_free( ctrl_ber, 1 );
560                                         goto done;
561                                 }
562                                 break;
563
564                         case LDAP_RES_INTERMEDIATE:
565                                 rc = ldap_parse_intermediate( ld, msg,
566                                         &retoid, &retdata, NULL, 0 );
567                                 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
568                                         sync_info_arrived = 1;
569                                         res_ber = ber_init( retdata );
570                                         ber_scanf( res_ber, "{e" /*"}"*/, &syncstate );
571
572                                         if ( ber_peek_tag( res_ber, &len )
573                                                                 == LDAP_SYNC_TAG_COOKIE ) {
574                                                 ber_scanf( res_ber, /*"{"*/ "o}", &syncCookie );
575                                         } else {
576                                                 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
577 #ifdef NEW_LOGGING
578                                                         LDAP_LOG( OPERATION, ERR,
579                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
580 #else
581                                                         Debug( LDAP_DEBUG_ANY,
582                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
583 #endif
584                                                 }
585                                         }
586
587                                         value_match( &match, slap_schema.si_ad_entryCSN,
588                                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
589                                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
590                                                                 &syncCookie_req, &syncCookie, &text );
591
592                                         if ( syncCookie.bv_len && match < 0 ) {
593                                                 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
594                                         }
595
596                                         if ( syncstate == LDAP_SYNC_STATE_MODE_DONE ) {
597                                                 if ( match < 0 ) {
598                                                         syncrepl_del_nonpresent( ld, &op );
599                                                 }
600                                                 si->sync_mode = LDAP_SYNC_LOG_MODE;
601                                         } else if ( syncstate == LDAP_SYNC_LOG_MODE_DONE ) {
602                                                 si->sync_mode = LDAP_SYNC_PERSIST_MODE;
603                                         } else if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
604                                                 si->sync_mode = LDAP_SYNC_PERSIST_MODE;
605                                         } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ||
606                                                                 syncstate != LDAP_SYNC_LOG_MODE_DONE ) {
607 #ifdef NEW_LOGGING
608                                                 LDAP_LOG( OPERATION, ERR,
609                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
610 #else
611                                                 Debug( LDAP_DEBUG_ANY,
612                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
613 #endif
614                                         }
615
616                                         ldap_memfree( retoid );
617                                         ber_bvfree( retdata );
618                                         ber_free( res_ber, 1 );
619                                         break;
620                                 } else {
621 #ifdef NEW_LOGGING
622                                         LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
623                                                 " unknown intermediate "
624                                                 "response\n", 0, 0, 0 );
625 #else
626                                         Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
627                                                 "unknown intermediate response (%d)\n",
628                                                 rc, 0, 0 );
629 #endif
630                                         ldap_memfree( retoid );
631                                         ber_bvfree( retdata );
632                                         break;
633                                 }
634                                 break;
635                         default:
636 #ifdef NEW_LOGGING
637                                 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
638                                         "unknown message\n", 0, 0, 0 );
639 #else
640                                 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
641                                         "unknown message\n", 0, 0, 0 );
642 #endif
643                                 break;
644
645                         }
646                         if ( syncCookie.bv_val )
647                                 ch_free( syncCookie.bv_val );
648                         if ( syncUUID.bv_val )
649                                 ch_free( syncUUID.bv_val );
650                 }
651                 ldap_msgfree( res );
652         }
653
654         if ( rc == -1 ) {
655                 int errno;
656                 const char *errstr;
657
658                 ldap_get_option( ld, LDAP_OPT_ERROR_NUMBER, &errno );
659                 errstr = ldap_err2string( errno );
660                 
661 #ifdef NEW_LOGGING
662                 LDAP_LOG( OPERATION, ERR,
663                         "do_syncrepl : %s\n", errstr, 0, 0 );
664 #else
665                 Debug( LDAP_DEBUG_ANY,
666                         "do_syncrepl : %s\n", errstr, 0, 0 );
667 #endif
668         }
669
670 done:
671         if ( syncCookie.bv_val )
672                 ch_free( syncCookie.bv_val );
673         if ( syncCookie_req.bv_val )
674                 ch_free( syncCookie_req.bv_val );
675         if ( syncUUID.bv_val )
676                 ch_free( syncUUID.bv_val );
677
678         if ( res )
679                 ldap_msgfree( res );
680
681         ldap_unbind( ld );
682
683         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
684         ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
685         if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
686                 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
687         } else {
688                 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
689         }
690         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
691
692         return NULL;
693 }
694
695 Entry*
696 syncrepl_message_to_entry(
697         syncinfo_t      *si,
698         LDAP            *ld,
699         Operation       *op,
700         LDAPMessage     *msg,
701         Modifications   **modlist,
702         int             *syncstate,
703         struct berval   *syncUUID,
704         struct berval   *syncCookie
705 )
706 {
707         Entry           *e;
708         BerElement      *ber = NULL;
709         BerElement      *tmpber;
710         struct berval   bv = {0, NULL};
711         Modifications   tmp;
712         Modifications   *mod;
713         Modifications   **modtail = modlist;
714         Backend         *be = op->o_bd;
715
716         const char      *text;
717         char txtbuf[SLAP_TEXT_BUFLEN];
718         size_t textlen = sizeof txtbuf;
719
720         struct berval   **bvals = NULL;
721         char            *dn;
722         struct berval   bdn = {0, NULL};
723         Attribute       *attr;
724         struct berval   empty_bv = { 0, NULL };
725         int             rc;
726         char            *a;
727
728         ber_len_t       len;
729         LDAPControl*    rctrlp;
730         LDAPControl**   rctrls = NULL;
731         BerElement*     ctrl_ber;
732
733         ber_tag_t       tag;
734
735         Modifications *ml = NULL;
736         AttributeDescription** descs;
737         int i;
738
739         *modlist = NULL;
740
741         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
742 #ifdef NEW_LOGGING
743                 LDAP_LOG( OPERATION, ERR,
744                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
745 #else
746                 Debug( LDAP_DEBUG_ANY,
747                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
748 #endif
749                 return NULL;
750         }
751
752         op->o_tag = LDAP_REQ_ADD;
753
754         rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
755
756         if ( rc != LDAP_SUCCESS ) {
757 #ifdef NEW_LOGGING
758                 LDAP_LOG( OPERATION, ERR,
759                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
760 #else
761                 Debug( LDAP_DEBUG_ANY,
762                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
763 #endif
764                 return NULL;
765         }
766
767         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
768         dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, NULL );
769
770         e->e_attrs = NULL;
771
772         while ( ber_remaining( ber ) ) {
773                 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
774
775                 if ( tag == LBER_ERROR ) break;
776                 if ( tmp.sml_type.bv_val == NULL ) break;
777
778                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
779
780                 mod->sml_op = LDAP_MOD_REPLACE;
781                 mod->sml_next = NULL;
782                 mod->sml_desc = NULL;
783                 mod->sml_type = tmp.sml_type;
784                 mod->sml_bvalues = tmp.sml_bvalues;
785                 mod->sml_nvalues = NULL;
786
787                 *modtail = mod;
788                 modtail = &mod->sml_next;
789         }
790
791         if ( ber_scanf( ber, "}") == LBER_ERROR ) {
792 #ifdef NEW_LOGGING
793                 LDAP_LOG( OPERATION, ERR,
794                                 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
795 #else
796                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
797                                 0, 0, 0 );
798 #endif
799                 return NULL;
800         }
801
802         ber_free( ber, 0 );
803         tmpber = ldap_get_message_ber( msg );
804         ber = ber_dup( tmpber );
805
806         ber_scanf( ber, "{xx" );
807
808         rc = ldap_int_get_controls( ber, &rctrls );
809
810         if ( rc != LDAP_SUCCESS ) {
811 #ifdef NEW_LOGGING
812                 LDAP_LOG( OPERATION, ERR,
813                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
814 #else
815                 Debug( LDAP_DEBUG_ANY,
816                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
817 #endif
818                 return NULL;
819         }
820
821         if ( rctrls ) {
822                 rctrlp = *rctrls;
823                 ctrl_ber = ber_alloc_t( LBER_USE_DER );
824                 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
825                 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
826                 ber_reset( ctrl_ber, 1 );
827                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
828                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
829                         ber_scanf( ctrl_ber, "o}", syncCookie );
830                 }
831                 ber_free( ctrl_ber, 1 );
832                 ldap_controls_free( rctrls );
833         } else {
834 #ifdef NEW_LOGGING
835                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
836                         " rctrls absent\n", 0, 0, 0 );
837 #else
838                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
839                         " rctrls absent\n", 0, 0, 0 );
840 #endif
841         }
842
843         if ( *syncstate == LDAP_SYNC_PRESENT || *syncstate == LDAP_SYNC_DELETE ) {
844                 goto done;
845         }
846
847         if ( *modlist == NULL ) {
848 #ifdef NEW_LOGGING
849                 LDAP_LOG( OPERATION, ERR,
850                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
851 #else
852                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
853                                 0, 0, 0 );
854 #endif
855         }
856
857         ml = *modlist;
858         while ( ml != NULL ) {
859                 AttributeDescription *ad = NULL;
860         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, &text );
861
862                 if( rc != LDAP_SUCCESS ) {
863                         e = NULL;
864                         goto done;
865                 }
866
867                 ad = ml->sml_desc;
868                 ml->sml_desc = NULL;
869                 ml = ml->sml_next;
870         }
871
872         rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
873
874         if ( rc != LDAP_SUCCESS ) {
875 #ifdef NEW_LOGGING
876                 LDAP_LOG( OPERATION, ERR,
877                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
878 #else
879                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
880                                 text, 0, 0 );
881 #endif
882                 return NULL;
883         }
884         
885         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
886         if( rc != LDAP_SUCCESS ) {
887 #ifdef NEW_LOGGING
888                 LDAP_LOG( OPERATION, ERR,
889                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
890 #else
891                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
892                                 text, 0, 0 );
893 #endif
894         }
895
896 done:
897
898         ber_free ( ber, 0 );
899
900         return e;
901 }
902
903 int
904 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
905 {
906         const struct berval *uuid1 = v_uuid1;
907         const struct berval *uuid2 = v_uuid2;
908         int rc = uuid1->bv_len - uuid2->bv_len;
909         if ( rc ) return rc;
910         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
911 }
912
913 int
914 syncrepl_entry(
915         syncinfo_t* si,
916         LDAP *ld,
917         Operation *op,
918         Entry* e,
919         Modifications* modlist,
920         int syncstate,
921         struct berval* syncUUID,
922         struct berval* syncCookie,
923         int refresh
924 )
925 {
926         Backend *be = op->o_bd;
927         slap_callback   cb;
928         struct berval   csn_bv = {0, NULL};
929         struct berval   *syncuuid_bv = NULL;
930         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
931
932         SlapReply       rs = {REP_RESULT};
933         int rc = LDAP_SUCCESS;
934
935         struct berval base_bv = {0, NULL};
936
937         char *filterstr;
938         Filter *filter;
939
940         Attribute *a;
941
942         if ( refresh &&
943                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
944                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
945                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
946                                                 syncuuid_cmp, avl_dup_error );
947         }
948
949         if ( syncstate == LDAP_SYNC_PRESENT ) {
950                 if ( e ) {
951                         return 1;
952                 } else {
953                         return 0;
954                 }
955         }
956
957         filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
958                                                                         op->o_tmpmemctx ); 
959         strcpy( filterstr, "entryUUID=" );
960         strcat( filterstr, syncUUID->bv_val );
961
962         si->e = e;
963         si->syncUUID_ndn = NULL;
964
965         filter = str2filter( filterstr );
966         ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
967         ch_free( filterstr );
968         op->ors_filter = filter;
969         op->ors_scope = LDAP_SCOPE_SUBTREE;
970
971         /* get syncrepl cookie of shadow replica from subentry */
972         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
973         dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
974         ch_free( base_bv.bv_val );
975
976         /* set callback function */
977         op->o_callback = &cb;
978         cb.sc_response = dn_callback;
979         cb.sc_private = si;
980
981         si->syncUUID_ndn = NULL;
982
983         rc = be->be_search( op, &rs );
984
985         if ( op->o_req_dn.bv_val )
986                 ch_free( op->o_req_dn.bv_val );
987         if ( op->o_req_ndn.bv_val )
988                 ch_free( op->o_req_ndn.bv_val );
989         if ( op->ors_filter )
990                 filter_free( op->ors_filter );
991         if ( op->ors_filterstr.bv_val )
992                 ch_free( op->ors_filterstr.bv_val );
993
994         cb.sc_response = null_callback;
995         cb.sc_private = si;
996
997         if ( rc == LDAP_SUCCESS && si->syncUUID_ndn && si->sync_mode != LDAP_SYNC_LOG_MODE ) {
998                 op->o_req_dn = *si->syncUUID_ndn;
999                 op->o_req_ndn = *si->syncUUID_ndn;
1000                 op->o_tag = LDAP_REQ_DELETE;
1001                 rc = be->be_delete( op, &rs );
1002         }
1003
1004         if ( si->syncUUID_ndn ) {
1005                 ber_bvfree( si->syncUUID_ndn );
1006         }
1007
1008         switch ( syncstate ) {
1009         case LDAP_SYNC_ADD :
1010         case LDAP_SYNC_MODIFY :
1011
1012                 if ( rc == LDAP_SUCCESS ||
1013                          rc == LDAP_REFERRAL ||
1014                          rc == LDAP_NO_SUCH_OBJECT ) {
1015
1016                         attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1017                         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, syncUUID, op->o_tmpmemctx );
1018
1019                         op->o_tag = LDAP_REQ_ADD;
1020                         op->ora_e = e;
1021                         op->o_req_dn = e->e_name;
1022                         op->o_req_ndn = e->e_nname;
1023                         rc = be->be_add( op, &rs );
1024
1025                         if ( rc != LDAP_SUCCESS ) {
1026                                 if ( rc == LDAP_ALREADY_EXISTS ) {      
1027                                         op->o_tag = LDAP_REQ_MODIFY;
1028                                         op->orm_modlist = modlist;
1029                                         op->o_req_dn = e->e_name;
1030                                         op->o_req_ndn = e->e_nname;
1031                                         rc = be->be_modify( op, &rs );
1032                                         si->e = NULL;
1033                                         if ( rc != LDAP_SUCCESS ) {
1034 #ifdef NEW_LOGGING
1035                                                 LDAP_LOG( OPERATION, ERR,
1036                                                         "syncrepl_entry : be_modify failed (%d)\n",
1037                                                         rc, 0, 0 );
1038 #else
1039                                                 Debug( LDAP_DEBUG_ANY,
1040                                                         "syncrepl_entry : be_modify failed (%d)\n",
1041                                                         rc, 0, 0 );
1042 #endif
1043                                         }
1044                                         return 1;
1045                                 } else if ( rc == LDAP_REFERRAL ||
1046                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1047                                         syncrepl_add_glue( si, ld, op, e,
1048                                                 modlist, syncstate,
1049                                                 syncUUID, syncCookie);
1050                                         si->e = NULL;
1051                                         return 0;
1052                                 } else {
1053 #ifdef NEW_LOGGING
1054                                         LDAP_LOG( OPERATION, ERR,
1055                                                 "syncrepl_entry : be_add failed (%d)\n",
1056                                                 rc, 0, 0 );
1057 #else
1058                                         Debug( LDAP_DEBUG_ANY,
1059                                                 "syncrepl_entry : be_add failed (%d)\n",
1060                                                 rc, 0, 0 );
1061 #endif
1062                                         si->e = NULL;
1063                                         return 1;
1064                                 }
1065                         } else {
1066                                 si->e = NULL;
1067                                 be_entry_release_w( op, e );
1068                                 return 0;
1069                         }
1070                 } else {
1071 #ifdef NEW_LOGGING
1072                         LDAP_LOG( OPERATION, ERR,
1073                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1074 #else
1075                         Debug( LDAP_DEBUG_ANY,
1076                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1077 #endif
1078                         si->e = NULL;
1079                         return 1;
1080                 }
1081
1082         case LDAP_SYNC_DELETE :
1083                 if ( si->sync_mode == LDAP_SYNC_LOG_MODE ) {
1084                         op->o_req_dn = *si->syncUUID_ndn;
1085                         op->o_req_ndn = *si->syncUUID_ndn;
1086                         op->o_tag = LDAP_REQ_DELETE;
1087                         rc = be->be_delete( op, &rs );
1088                 }
1089                 /* Already deleted otherwise */
1090                 return 1;
1091
1092         default :
1093 #ifdef NEW_LOGGING
1094                 LDAP_LOG( OPERATION, ERR,
1095                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1096 #else
1097                 Debug( LDAP_DEBUG_ANY,
1098                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1099 #endif
1100                 return 1;
1101         }
1102 }
1103
1104 static void
1105 syncrepl_del_nonpresent(
1106         LDAP *ld,
1107         Operation *op
1108 )
1109 {
1110         Backend* be = op->o_bd;
1111         syncinfo_t *si = op->o_si;
1112         slap_callback   cb;
1113         struct berval   base_bv = {0, NULL};
1114         Filter *filter;
1115         SlapReply       rs = {REP_RESULT};
1116         struct berval   filterstr_bv = {0, NULL};
1117         struct nonpresent_entry *np_list, *np_prev;
1118
1119         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1120         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1121         ch_free( base_bv.bv_val );
1122
1123         filter = str2filter( si->filterstr );
1124
1125         cb.sc_response = nonpresent_callback;
1126         cb.sc_private = si;
1127
1128         op->o_callback = &cb;
1129         op->o_tag = LDAP_REQ_SEARCH;
1130         op->ors_scope = si->scope;
1131         op->ors_deref = LDAP_DEREF_NEVER;
1132         op->ors_slimit = 0;
1133         op->ors_tlimit = 0;
1134         op->ors_attrsonly = 0;
1135         op->ors_attrs = NULL;
1136         op->ors_filter = filter;
1137         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1138
1139         op->o_nocaching = 1;
1140         be->be_search( op, &rs );
1141         op->o_nocaching = 0;
1142
1143         if ( op->o_req_dn.bv_val )
1144                 ch_free( op->o_req_dn.bv_val );
1145         if ( op->o_req_ndn.bv_val )
1146                 ch_free( op->o_req_ndn.bv_val );
1147         if ( op->ors_filter )
1148                 filter_free( op->ors_filter );
1149         if ( op->ors_filterstr.bv_val )
1150                 ch_free( op->ors_filterstr.bv_val );
1151
1152         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1153                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1154                 while ( np_list != NULL ) {
1155                         LDAP_LIST_REMOVE( np_list, np_link );
1156                         np_prev = np_list;
1157                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1158                         op->o_tag = LDAP_REQ_DELETE;
1159                         op->o_callback = &cb;
1160                         cb.sc_response = null_callback;
1161                         cb.sc_private = si;
1162                         op->o_req_dn = *np_prev->dn;
1163                         op->o_req_ndn = *np_prev->ndn;
1164                         op->o_bd->be_delete( op, &rs );
1165                         ber_bvfree( np_prev->dn );
1166                         ber_bvfree( np_prev->ndn );
1167                         op->o_req_dn.bv_val = NULL;
1168                         op->o_req_ndn.bv_val = NULL;
1169                         ch_free( np_prev );
1170                 }
1171         }
1172
1173         return;
1174 }
1175
1176
1177 void
1178 syncrepl_add_glue(
1179         syncinfo_t *si,
1180         LDAP *ld,
1181         Operation* op,
1182         Entry *e,
1183         Modifications* modlist,
1184         int syncstate,
1185         struct berval* syncUUID,
1186         struct berval* syncCookie
1187 )
1188 {
1189         Backend *be = op->o_bd;
1190         struct berval   uuid_bv = {0, NULL};
1191         slap_callback cb;
1192         Attribute       *a;
1193         int     rc;
1194         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1195         int levels = 0;
1196         int i, j, k;
1197         struct berval dn = {0, NULL};
1198         struct berval pdn = {0, NULL};
1199         struct berval ndn = {0, NULL};
1200         struct berval rdn = {0, NULL};
1201         Entry   *glue;
1202         SlapReply       rs = {REP_RESULT};
1203         Connection *conn = op->o_conn;
1204         char* ptr;
1205
1206         op->o_tag = LDAP_REQ_ADD;
1207         op->o_callback = &cb;
1208         cb.sc_response = null_callback;
1209         cb.sc_private = si;
1210
1211         ber_dupbv( &dn, &e->e_nname );
1212         ber_dupbv( &pdn, &e->e_nname );
1213
1214         ptr = dn.bv_val;
1215         while ( !be_issuffix ( be, &pdn )) {
1216                 dnParent( &dn, &pdn );
1217                 dn.bv_val = pdn.bv_val;
1218                 dn.bv_len = pdn.bv_len;
1219                 levels++;
1220         }
1221         ch_free( ptr );
1222
1223         for ( i = 0; i <= levels; i++ ) {
1224                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1225                 ber_dupbv( &dn, &e->e_nname );
1226                 j = levels - i;
1227
1228                 ptr = dn.bv_val;
1229                 for ( k = 0; k < j; k++ ) {
1230                         dnParent( &dn, &pdn );
1231                         dn.bv_val = pdn.bv_val;
1232                         dn.bv_len = pdn.bv_len;
1233                 }
1234
1235                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1236                 ber_dupbv( &glue->e_name, &pdn );
1237                 ber_dupbv( &glue->e_nname, &ndn );
1238                 ch_free( ptr );
1239                 ch_free( pdn.bv_val );
1240                 ch_free( ndn.bv_val );
1241
1242                 a = ch_calloc( 1, sizeof( Attribute ));
1243                 a->a_desc = slap_schema.si_ad_objectClass;
1244
1245                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1246                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1247                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1248                 a->a_vals[2].bv_len = 0;
1249                 a->a_vals[2].bv_val = NULL;
1250
1251                 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1252                 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1253                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1254                 a->a_nvals[2].bv_len = 0;
1255                 a->a_nvals[2].bv_val = NULL;
1256
1257                 a->a_next = glue->e_attrs;
1258                 glue->e_attrs = a;
1259
1260                 a = ch_calloc( 1, sizeof( Attribute ));
1261                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1262
1263                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1264                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1265                 a->a_vals[1].bv_len = 0;
1266                 a->a_vals[1].bv_val = NULL;
1267
1268                 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1269                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1270                 a->a_nvals[1].bv_len = 0;
1271                 a->a_nvals[1].bv_val = NULL;
1272
1273                 a->a_next = glue->e_attrs;
1274                 glue->e_attrs = a;
1275
1276                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1277                         op->o_req_dn = e->e_name;
1278                         op->o_req_ndn = e->e_nname;
1279                         op->ora_e = e;
1280                         rc = be->be_add ( op, &rs );
1281                         if ( rc == LDAP_SUCCESS )
1282                                 be_entry_release_w( op, e );
1283                         else 
1284                                 entry_free( e );
1285                         entry_free( glue );
1286                 } else {
1287                         op->o_req_dn = glue->e_name;
1288                         op->o_req_ndn = glue->e_nname;
1289                         op->ora_e = glue;
1290                         rc = be->be_add ( op, &rs );
1291                         if ( rc == LDAP_SUCCESS ) {
1292                                 be_entry_release_w( op, glue );
1293                         } else {
1294                         /* incl. ALREADY EXIST */
1295                                 entry_free( glue );
1296                         }
1297                 }
1298         }
1299
1300         return;
1301 }
1302
1303 static struct berval ocbva[] = {
1304         BER_BVC("top"),
1305         BER_BVC("subentry"),
1306         BER_BVC("syncConsumerSubentry"),
1307         BER_BVNULL
1308 };
1309
1310 static struct berval cnbva[] = {
1311         BER_BVNULL,
1312         BER_BVNULL
1313 };
1314
1315 static struct berval ssbva[] = {
1316         BER_BVC("{}"),
1317         BER_BVNULL
1318 };
1319
1320 static struct berval scbva[] = {
1321         BER_BVC("subentry"),
1322         BER_BVNULL
1323 };
1324
1325 void
1326 syncrepl_updateCookie(
1327         syncinfo_t *si,
1328         LDAP *ld,
1329         Operation *op,
1330         struct berval *pdn,
1331         struct berval *syncCookie
1332 )
1333 {
1334         Backend *be = op->o_bd;
1335         Modifications *ml;
1336         Modifications *mlnext;
1337         Modifications *mod;
1338         Modifications *modlist = NULL;
1339         Modifications **modtail = &modlist;
1340
1341         const char      *text;
1342         char txtbuf[SLAP_TEXT_BUFLEN];
1343         size_t textlen = sizeof txtbuf;
1344
1345         Entry* e = NULL;
1346         int rc;
1347
1348         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1349         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1350         
1351         slap_callback cb;
1352         SlapReply       rs = {REP_RESULT};
1353
1354         /* update in memory cookie */
1355         if ( si->syncCookie != NULL ) {
1356                 ber_bvfree( si->syncCookie );
1357         }
1358         si->syncCookie = ber_dupbv( NULL, syncCookie );
1359         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1360         mod->sml_op = LDAP_MOD_REPLACE;
1361         mod->sml_desc = slap_schema.si_ad_objectClass;
1362         mod->sml_type = mod->sml_desc->ad_cname;
1363         mod->sml_bvalues = ocbva;
1364         *modtail = mod;
1365         modtail = &mod->sml_next;
1366
1367         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1368         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1369                                                                 slap_syncrepl_bvc.bv_len,
1370                                                                 "syncrepl%d", si->id );
1371         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1372         mod->sml_op = LDAP_MOD_REPLACE;
1373         mod->sml_desc = slap_schema.si_ad_cn;
1374         mod->sml_type = mod->sml_desc->ad_cname;
1375         mod->sml_bvalues = cnbva;
1376         *modtail = mod;
1377         modtail = &mod->sml_next;
1378
1379         ber_dupbv( &scbva[0], si->syncCookie );
1380         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1381         mod->sml_op = LDAP_MOD_REPLACE;
1382         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1383         mod->sml_type = mod->sml_desc->ad_cname;
1384         mod->sml_bvalues = scbva;
1385         *modtail = mod;
1386         modtail = &mod->sml_next;
1387
1388         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1389         mod->sml_op = LDAP_MOD_REPLACE;
1390         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1391         mod->sml_type = mod->sml_desc->ad_cname;
1392         mod->sml_bvalues = ssbva;
1393         *modtail = mod;
1394         modtail = &mod->sml_next;
1395
1396 #if 0
1397         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
1398
1399         if ( rc != LDAP_SUCCESS ) {
1400 #ifdef NEW_LOGGING
1401                 LDAP_LOG( OPERATION, ERR,
1402                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1403 #else
1404                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1405                          text, 0, 0 );
1406 #endif
1407         }
1408 #endif
1409
1410         op->o_tag = LDAP_REQ_ADD;
1411         rc = slap_mods_opattrs( op, modlist, modtail,
1412                                                          &text,txtbuf, textlen );
1413
1414         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1415                 mlnext = ml->sml_next;
1416                 ml->sml_op = LDAP_MOD_REPLACE;
1417         }
1418
1419         if( rc != LDAP_SUCCESS ) {
1420 #ifdef NEW_LOGGING
1421                 LDAP_LOG( OPERATION, ERR,
1422                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1423 #else
1424                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1425                          text, 0, 0 );
1426 #endif
1427         }
1428
1429         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1430
1431         ber_dupbv( &slap_syncrepl_cn_bv, (struct berval *) &slap_syncrepl_cn_bvc );
1432         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1433                                                                                 slap_syncrepl_cn_bvc.bv_len,
1434                                                                                 "cn=syncrepl%d", si->id );
1435
1436         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv, NULL );
1437         dnPrettyNormal( NULL, &slap_syncrepl_dn_bv, &e->e_name, &e->e_nname, NULL );
1438
1439         if ( slap_syncrepl_cn_bv.bv_val )
1440                 ch_free( slap_syncrepl_cn_bv.bv_val );
1441         if ( slap_syncrepl_dn_bv.bv_val )
1442                 ch_free( slap_syncrepl_dn_bv.bv_val );
1443
1444         e->e_attrs = NULL;
1445
1446         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1447
1448         if( rc != LDAP_SUCCESS ) {
1449 #ifdef NEW_LOGGING
1450                 LDAP_LOG( OPERATION, ERR,
1451                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1452 #else
1453                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1454                          text, 0, 0 );
1455 #endif
1456         }
1457
1458         cb.sc_response = null_callback;
1459         cb.sc_private = si;
1460
1461         op->o_callback = &cb;
1462         op->o_req_dn = e->e_name;
1463         op->o_req_ndn = e->e_nname;
1464
1465         /* update persistent cookie */
1466 update_cookie_retry:
1467         op->o_tag = LDAP_REQ_MODIFY;
1468         op->orm_modlist = modlist;
1469         rc = be->be_modify( op, &rs );
1470
1471         if ( rc != LDAP_SUCCESS ) {
1472                 if ( rc == LDAP_REFERRAL ||
1473                          rc == LDAP_NO_SUCH_OBJECT ) {
1474                         op->o_tag = LDAP_REQ_ADD;
1475                         op->ora_e = e;
1476                         rc = be->be_add( op, &rs );
1477                         if ( rc != LDAP_SUCCESS ) {
1478                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1479                                         goto update_cookie_retry;
1480                                 } else if ( rc == LDAP_REFERRAL ||
1481                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1482 #ifdef NEW_LOGGING
1483                                         LDAP_LOG( OPERATION, ERR,
1484                                                 "cookie will be non-persistent\n",
1485                                                 0, 0, 0 );
1486 #else
1487                                         Debug( LDAP_DEBUG_ANY,
1488                                                 "cookie will be non-persistent\n",
1489                                                 0, 0, 0 );
1490 #endif
1491                                 } else {
1492 #ifdef NEW_LOGGING
1493                                         LDAP_LOG( OPERATION, ERR,
1494                                                 "be_add failed (%d)\n",
1495                                                 rc, 0, 0 );
1496 #else
1497                                         Debug( LDAP_DEBUG_ANY,
1498                                                 "be_add failed (%d)\n",
1499                                                 rc, 0, 0 );
1500 #endif
1501                                 }
1502                         } else {
1503                                 be_entry_release_w( op, e );
1504                                 goto done;
1505                         }
1506                 } else {
1507 #ifdef NEW_LOGGING
1508                         LDAP_LOG( OPERATION, ERR,
1509                                 "be_modify failed (%d)\n", rc, 0, 0 );
1510 #else
1511                         Debug( LDAP_DEBUG_ANY,
1512                                 "be_modify failed (%d)\n", rc, 0, 0 );
1513 #endif
1514                 }
1515         }
1516
1517         if ( e != NULL ) {
1518                 entry_free( e );
1519         }
1520
1521 done :
1522
1523         if ( cnbva[0].bv_val )
1524                 ch_free( cnbva[0].bv_val );
1525
1526         for ( ; ml != NULL; ml = mlnext ) {
1527                 mlnext = ml->sml_next;
1528                 free( ml );
1529         }
1530
1531         return;
1532 }
1533
1534 void
1535 avl_ber_bvfree( void *bv )
1536 {
1537         if( bv == NULL ) {
1538                 return;
1539         }
1540         if ( ((struct berval *)bv)->bv_val != NULL ) {
1541                 ch_free ( ((struct berval *)bv)->bv_val );
1542         }
1543         ch_free ( (char *) bv );
1544 }
1545
1546 static int
1547 cookie_callback(
1548         Operation* op,
1549         SlapReply* rs
1550 )
1551 {
1552         syncinfo_t *si = op->o_callback->sc_private;
1553         Attribute *a;
1554
1555         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1556
1557         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1558
1559         if ( a == NULL ) {
1560                 si->syncCookie = NULL;
1561         } else {
1562                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1563         }
1564         return LDAP_SUCCESS;
1565 }
1566
1567 static int
1568 dn_callback(
1569         Operation*      op,
1570         SlapReply*      rs
1571 )
1572 {
1573         syncinfo_t *si = op->o_callback->sc_private;
1574
1575         if ( rs->sr_type == REP_SEARCH ) {
1576                 if ( si->syncUUID_ndn != NULL ) {
1577 #ifdef NEW_LOGGING
1578                         LDAP_LOG( OPERATION, ERR,
1579                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1580 #else
1581                         Debug( LDAP_DEBUG_ANY,
1582                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1583 #endif
1584                 } else {
1585                         if ( rs->sr_entry == NULL ) {
1586                                 si->syncUUID_ndn = NULL;
1587                         } else {
1588                                 si->syncUUID_ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1589                         }
1590                 }
1591         }
1592
1593         return LDAP_SUCCESS;
1594 }
1595
1596 static int
1597 nonpresent_callback(
1598         Operation*      op,
1599         SlapReply*      rs
1600 )
1601 {
1602         syncinfo_t *si = op->o_callback->sc_private;
1603         Attribute *a;
1604         int count = 0;
1605         struct berval* present_uuid = NULL;
1606         slap_callback cb;
1607         SlapReply       rs_cb = {REP_RESULT};
1608         struct nonpresent_entry *np_entry;
1609
1610         if ( rs->sr_type == REP_RESULT ) {
1611                 count = avl_free( si->presentlist, avl_ber_bvfree );
1612                 si->presentlist = NULL;
1613                 return LDAP_SUCCESS;
1614         } else if ( rs->sr_type == REP_SEARCH ) {
1615                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1616
1617                 if ( a == NULL )
1618                         return 0;
1619
1620                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1621
1622                 if ( present_uuid == NULL ) {
1623                         np_entry = (struct nonpresent_entry *)
1624                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1625                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1626                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1627                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1628                 } else {
1629                         avl_delete( &si->presentlist,
1630                                         &a->a_vals[0], syncuuid_cmp );
1631                         ch_free( present_uuid->bv_val );
1632                         ch_free( present_uuid );
1633                 }
1634                 return LDAP_SUCCESS;
1635         } else {
1636                 return LDAP_SUCCESS;
1637         }
1638
1639 }
1640
1641 static int
1642 null_callback(
1643         Operation*      op,
1644         SlapReply*      rs
1645 )
1646 {
1647         syncinfo_t *si = op->o_callback->sc_private;
1648
1649         if ( rs->sr_err != LDAP_SUCCESS &&
1650                  rs->sr_err != LDAP_REFERRAL &&
1651                  rs->sr_err != LDAP_ALREADY_EXISTS &&
1652                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
1653 #ifdef NEW_LOGGING
1654                 LDAP_LOG( OPERATION, ERR,
1655                         "null_callback : error code 0x%x\n",
1656                         rs->sr_err, 0, 0 );
1657 #else
1658                 Debug( LDAP_DEBUG_ANY,
1659                         "null_callback : error code 0x%x\n",
1660                         rs->sr_err, 0, 0 );
1661 #endif
1662         }
1663         return LDAP_SUCCESS;
1664 }
1665
1666 Entry *
1667 slap_create_syncrepl_entry(
1668         Backend *be,
1669         struct berval *context_csn,
1670         struct berval *rdn,
1671         struct berval *cn
1672 )
1673 {
1674         Entry* e;
1675         int rc;
1676
1677         struct berval bv;
1678
1679         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1680
1681         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1682
1683         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
1684
1685         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1686
1687         if ( context_csn ) {
1688                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1689                         context_csn, NULL );
1690         }
1691
1692         bv.bv_val = "{}";
1693         bv.bv_len = sizeof("{}")-1;
1694         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1695
1696         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1697         ber_dupbv( &e->e_nname, &e->e_name );
1698
1699         return e;
1700 }