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