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