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