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