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