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