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