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