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