]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
057afa5c78ef30ecf190cb6ea4755004b5bcf78a
[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 = NULL;
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         si->syncUUID_ndn = NULL;
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                                         if ( rc != LDAP_SUCCESS ) {
1059 #ifdef NEW_LOGGING
1060                                                 LDAP_LOG( OPERATION, ERR,
1061                                                         "syncrepl_entry : be_modify failed (%d)\n",
1062                                                         rc, 0, 0 );
1063 #else
1064                                                 Debug( LDAP_DEBUG_ANY,
1065                                                         "syncrepl_entry : be_modify failed (%d)\n",
1066                                                         rc, 0, 0 );
1067 #endif
1068                                                 return 1;
1069                                         }
1070                                         return 0;
1071                                 } else if ( rc == LDAP_REFERRAL ||
1072                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1073                                         syncrepl_add_glue( si, ld, op, e,
1074                                                 modlist, syncstate,
1075                                                 syncUUID, syncCookie);
1076                                         si->e = NULL;
1077                                         return 0;
1078                                 } else {
1079 #ifdef NEW_LOGGING
1080                                         LDAP_LOG( OPERATION, ERR,
1081                                                 "syncrepl_entry : be_add failed (%d)\n",
1082                                                 rc, 0, 0 );
1083 #else
1084                                         Debug( LDAP_DEBUG_ANY,
1085                                                 "syncrepl_entry : be_add failed (%d)\n",
1086                                                 rc, 0, 0 );
1087 #endif
1088                                         si->e = NULL;
1089                                         return 1;
1090                                 }
1091                         } else {
1092                                 si->e = NULL;
1093                                 be_entry_release_w( op, e );
1094                                 return 0;
1095                         }
1096                 } else {
1097 #ifdef NEW_LOGGING
1098                         LDAP_LOG( OPERATION, ERR,
1099                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1100 #else
1101                         Debug( LDAP_DEBUG_ANY,
1102                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1103 #endif
1104                         si->e = NULL;
1105                         return 1;
1106                 }
1107
1108         case LDAP_SYNC_DELETE :
1109                 if ( si->sync_mode == LDAP_SYNC_LOG_MODE ) {
1110                         op->o_req_dn = *si->syncUUID_ndn;
1111                         op->o_req_ndn = *si->syncUUID_ndn;
1112                         op->o_tag = LDAP_REQ_DELETE;
1113                         rc = be->be_delete( op, &rs );
1114                 }
1115                 /* Already deleted otherwise */
1116                 return 1;
1117
1118         default :
1119 #ifdef NEW_LOGGING
1120                 LDAP_LOG( OPERATION, ERR,
1121                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1122 #else
1123                 Debug( LDAP_DEBUG_ANY,
1124                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1125 #endif
1126                 return 1;
1127         }
1128 }
1129
1130 static void
1131 syncrepl_del_nonpresent(
1132         LDAP *ld,
1133         Operation *op
1134 )
1135 {
1136         Backend* be = op->o_bd;
1137         syncinfo_t *si = op->o_si;
1138         slap_callback   cb;
1139         struct berval   base_bv = {0, NULL};
1140         Filter *filter;
1141         SlapReply       rs = {REP_RESULT};
1142         struct berval   filterstr_bv = {0, NULL};
1143         struct nonpresent_entry *np_list, *np_prev;
1144
1145         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1146         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1147         ch_free( base_bv.bv_val );
1148
1149         filter = str2filter( si->filterstr );
1150
1151         cb.sc_response = nonpresent_callback;
1152         cb.sc_private = si;
1153
1154         op->o_callback = &cb;
1155         op->o_tag = LDAP_REQ_SEARCH;
1156         op->ors_scope = si->scope;
1157         op->ors_deref = LDAP_DEREF_NEVER;
1158         op->ors_slimit = 0;
1159         op->ors_tlimit = 0;
1160         op->ors_attrsonly = 0;
1161         op->ors_attrs = NULL;
1162         op->ors_filter = filter;
1163         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1164
1165         be->be_search( op, &rs );
1166
1167         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1168                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1169                 while ( np_list != NULL ) {
1170                         LDAP_LIST_REMOVE( np_list, np_link );
1171                         np_prev = np_list;
1172                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1173                         op->o_tag = LDAP_REQ_DELETE;
1174                         op->o_callback = &cb;
1175                         cb.sc_response = null_callback;
1176                         cb.sc_private = si;
1177                         op->o_req_dn = *np_prev->dn;
1178                         op->o_req_ndn = *np_prev->ndn;
1179                         op->o_bd->be_delete( op, &rs );
1180                         ber_bvfree( np_prev->dn );
1181                         ber_bvfree( np_prev->ndn );
1182                         op->o_req_dn.bv_val = NULL;
1183                         op->o_req_ndn.bv_val = NULL;
1184                         ch_free( np_prev );
1185                 }
1186         }
1187
1188         if ( op->o_req_dn.bv_val )
1189                 ch_free( op->o_req_dn.bv_val );
1190         if ( op->o_req_ndn.bv_val )
1191                 ch_free( op->o_req_ndn.bv_val );
1192         filter_free( op->ors_filter );
1193         ch_free( op->ors_filterstr.bv_val );
1194
1195         return;
1196 }
1197
1198
1199 void
1200 syncrepl_add_glue(
1201         syncinfo_t *si,
1202         LDAP *ld,
1203         Operation* op,
1204         Entry *e,
1205         Modifications* modlist,
1206         int syncstate,
1207         struct berval* syncUUID,
1208         struct berval* syncCookie
1209 )
1210 {
1211         Backend *be = op->o_bd;
1212         struct berval   uuid_bv = {0, NULL};
1213         slap_callback cb;
1214         Attribute       *a;
1215         int     rc;
1216         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1217         int levels = 0;
1218         int i, j, k;
1219         struct berval dn = {0, NULL};
1220         struct berval pdn = {0, NULL};
1221         struct berval ndn = {0, NULL};
1222         struct berval rdn = {0, NULL};
1223         Entry   *glue;
1224         SlapReply       rs = {REP_RESULT};
1225         Connection *conn = op->o_conn;
1226         char* ptr;
1227
1228         op->o_tag = LDAP_REQ_ADD;
1229         op->o_callback = &cb;
1230         cb.sc_response = null_callback;
1231         cb.sc_private = si;
1232
1233         ber_dupbv( &dn, &e->e_nname );
1234         ber_dupbv( &pdn, &e->e_nname );
1235
1236         ptr = dn.bv_val;
1237         while ( !be_issuffix ( be, &pdn )) {
1238                 dnParent( &dn, &pdn );
1239                 dn.bv_val = pdn.bv_val;
1240                 dn.bv_len = pdn.bv_len;
1241                 levels++;
1242         }
1243         ch_free( ptr );
1244
1245         for ( i = 0; i <= levels; i++ ) {
1246                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1247                 ber_dupbv( &dn, &e->e_nname );
1248                 j = levels - i;
1249
1250                 ptr = dn.bv_val;
1251                 for ( k = 0; k < j; k++ ) {
1252                         dnParent( &dn, &pdn );
1253                         dn.bv_val = pdn.bv_val;
1254                         dn.bv_len = pdn.bv_len;
1255                 }
1256
1257                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1258                 ber_dupbv( &glue->e_name, &pdn );
1259                 ber_dupbv( &glue->e_nname, &ndn );
1260                 ch_free( ptr );
1261                 ch_free( pdn.bv_val );
1262                 ch_free( ndn.bv_val );
1263
1264                 a = ch_calloc( 1, sizeof( Attribute ));
1265                 a->a_desc = slap_schema.si_ad_objectClass;
1266
1267                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1268                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1269                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1270                 a->a_vals[2].bv_len = 0;
1271                 a->a_vals[2].bv_val = NULL;
1272
1273                 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1274                 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1275                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1276                 a->a_nvals[2].bv_len = 0;
1277                 a->a_nvals[2].bv_val = NULL;
1278
1279                 a->a_next = glue->e_attrs;
1280                 glue->e_attrs = a;
1281
1282                 a = ch_calloc( 1, sizeof( Attribute ));
1283                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1284
1285                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1286                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1287                 a->a_vals[1].bv_len = 0;
1288                 a->a_vals[1].bv_val = NULL;
1289
1290                 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1291                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1292                 a->a_nvals[1].bv_len = 0;
1293                 a->a_nvals[1].bv_val = NULL;
1294
1295                 a->a_next = glue->e_attrs;
1296                 glue->e_attrs = a;
1297
1298                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1299                         op->o_req_dn = e->e_name;
1300                         op->o_req_ndn = e->e_nname;
1301                         op->ora_e = e;
1302                         rc = be->be_add ( op, &rs );
1303                         if ( rc == LDAP_SUCCESS )
1304                                 be_entry_release_w( op, e );
1305                         else 
1306                                 entry_free( e );
1307                         entry_free( glue );
1308                 } else {
1309                         op->o_req_dn = glue->e_name;
1310                         op->o_req_ndn = glue->e_nname;
1311                         op->ora_e = glue;
1312                         rc = be->be_add ( op, &rs );
1313                         if ( rc == LDAP_SUCCESS ) {
1314                                 be_entry_release_w( op, glue );
1315                         } else {
1316                         /* incl. ALREADY EXIST */
1317                                 entry_free( glue );
1318                         }
1319                 }
1320         }
1321
1322         return;
1323 }
1324
1325 void
1326 syncrepl_updateCookie(
1327         syncinfo_t *si,
1328         LDAP *ld,
1329         Operation *op,
1330         struct berval *pdn,
1331         struct berval *syncCookie
1332 )
1333 {
1334         Backend *be = op->o_bd;
1335         Modifications *ml;
1336         Modifications *mlnext;
1337         Modifications *mod;
1338         Modifications *modlist;
1339         Modifications **modtail = &modlist;
1340
1341         struct berval* ocbva = NULL;
1342         struct berval* cnbva = NULL;
1343         struct berval* ssbva = NULL;
1344         struct berval* scbva = NULL;
1345
1346         char substr[64];
1347         char rdnstr[67];
1348         const char      *text;
1349         char txtbuf[SLAP_TEXT_BUFLEN];
1350         size_t textlen = sizeof txtbuf;
1351
1352         Entry* e;
1353         int rc;
1354
1355         struct berval sub_bv = { 0, NULL };
1356         struct berval psubrdn = { 0, NULL };
1357         
1358         slap_callback cb;
1359         SlapReply       rs = {REP_RESULT};
1360
1361         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1362         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1363         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1364         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1365
1366         /* update in memory cookie */
1367         if ( si->syncCookie != NULL ) {
1368                 ber_bvfree( si->syncCookie );
1369         }
1370         si->syncCookie = ber_dupbv( NULL, syncCookie );
1371         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1372         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1373         ber_str2bv( "syncConsumerSubentry",
1374                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1375         ocbva[3].bv_len = 0;
1376         ocbva[3].bv_val = NULL;
1377
1378         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1379         mod->sml_op = LDAP_MOD_REPLACE;
1380         mod->sml_next = NULL;
1381         mod->sml_desc = NULL;
1382         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1383         mod->sml_bvalues = ocbva;
1384         mod->sml_nvalues = NULL;
1385         *modtail = mod;
1386         modtail = &mod->sml_next;
1387
1388         sprintf( substr, "syncrepl%d", si->id );
1389         sprintf( rdnstr, "cn=%s", substr );
1390         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1391         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1392         cnbva[1].bv_len = 0;
1393         cnbva[1].bv_val = NULL;
1394         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1395         mod->sml_op = LDAP_MOD_REPLACE;
1396         mod->sml_next = NULL;
1397         mod->sml_desc = NULL;
1398         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1399         mod->sml_bvalues = cnbva;
1400         mod->sml_nvalues = NULL;
1401         *modtail = mod;
1402         modtail = &mod->sml_next;
1403
1404         ber_dupbv( &scbva[0], si->syncCookie );
1405         scbva[1].bv_len = 0;
1406         scbva[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( "syncreplCookie", strlen("syncreplCookie"),
1412                                                 1, &mod->sml_type );
1413         mod->sml_bvalues = scbva;
1414         mod->sml_nvalues = NULL;
1415         *modtail = mod;
1416         modtail = &mod->sml_next;
1417
1418         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1419         ssbva[1].bv_len = 0;
1420         ssbva[1].bv_val = NULL;
1421         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1422         mod->sml_op = LDAP_MOD_REPLACE;
1423         mod->sml_next = NULL;
1424         mod->sml_desc = NULL;
1425         ber_str2bv( "subtreeSpecification",
1426                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1427         mod->sml_bvalues = ssbva;
1428         mod->sml_nvalues = NULL;
1429         *modtail = mod;
1430         modtail = &mod->sml_next;
1431
1432         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
1433
1434         if ( rc != LDAP_SUCCESS ) {
1435 #ifdef NEW_LOGGING
1436                 LDAP_LOG( OPERATION, ERR,
1437                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1438 #else
1439                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1440                          text, 0, 0 );
1441 #endif
1442         }
1443
1444         op->o_tag = LDAP_REQ_ADD;
1445         rc = slap_mods_opattrs( op, modlist, modtail,
1446                                                          &text,txtbuf, textlen );
1447
1448         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1449                 mlnext = ml->sml_next;
1450                 if ( ml->sml_desc == slap_schema.si_ad_structuralObjectClass )
1451                         ml->sml_op = LDAP_MOD_REPLACE;
1452         }
1453
1454         if( rc != LDAP_SUCCESS ) {
1455 #ifdef NEW_LOGGING
1456                 LDAP_LOG( OPERATION, ERR,
1457                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1458 #else
1459                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1460                          text, 0, 0 );
1461 #endif
1462         }
1463
1464         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1465
1466         build_new_dn( &sub_bv, pdn, &psubrdn );
1467         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1468         ch_free( sub_bv.bv_val );
1469         ch_free( psubrdn.bv_val );
1470
1471         e->e_attrs = NULL;
1472
1473         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1474
1475         if( rc != LDAP_SUCCESS ) {
1476 #ifdef NEW_LOGGING
1477                 LDAP_LOG( OPERATION, ERR,
1478                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1479 #else
1480                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1481                          text, 0, 0 );
1482 #endif
1483         }
1484
1485         cb.sc_response = null_callback;
1486         cb.sc_private = si;
1487
1488         op->o_callback = &cb;
1489         op->o_req_dn = e->e_name;
1490         op->o_req_ndn = e->e_nname;
1491
1492         /* update persistent cookie */
1493 update_cookie_retry:
1494         op->o_tag = LDAP_REQ_MODIFY;
1495         op->orm_modlist = modlist;
1496         rc = be->be_modify( op, &rs );
1497         if ( rc != LDAP_SUCCESS ) {
1498                 if ( rc == LDAP_REFERRAL ||
1499                          rc == LDAP_NO_SUCH_OBJECT ) {
1500                         op->o_tag = LDAP_REQ_ADD;
1501                         op->ora_e = e;
1502                         rc = be->be_add( op, &rs );
1503                         if ( rc != LDAP_SUCCESS ) {
1504                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1505                                         goto update_cookie_retry;
1506                                 } else if ( rc == LDAP_REFERRAL ||
1507                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1508 #ifdef NEW_LOGGING
1509                                         LDAP_LOG( OPERATION, ERR,
1510                                                 "cookie will be non-persistent\n",
1511                                                 0, 0, 0 );
1512 #else
1513                                         Debug( LDAP_DEBUG_ANY,
1514                                                 "cookie will be non-persistent\n",
1515                                                 0, 0, 0 );
1516 #endif
1517                                 } else {
1518 #ifdef NEW_LOGGING
1519                                         LDAP_LOG( OPERATION, ERR,
1520                                                 "be_add failed (%d)\n",
1521                                                 rc, 0, 0 );
1522 #else
1523                                         Debug( LDAP_DEBUG_ANY,
1524                                                 "be_add failed (%d)\n",
1525                                                 rc, 0, 0 );
1526 #endif
1527                                 }
1528                         } else {
1529                                 be_entry_release_w( op, e );
1530                                 goto done;
1531                         }
1532                 } else {
1533 #ifdef NEW_LOGGING
1534                         LDAP_LOG( OPERATION, ERR,
1535                                 "be_modify failed (%d)\n", rc, 0, 0 );
1536 #else
1537                         Debug( LDAP_DEBUG_ANY,
1538                                 "be_modify failed (%d)\n", rc, 0, 0 );
1539 #endif
1540                 }
1541         }
1542
1543         if ( e != NULL )
1544                 entry_free( e );
1545
1546 done :
1547
1548         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1549                 mlnext = ml->sml_next;
1550                 free( ml );
1551         }
1552
1553         return;
1554 }
1555
1556 void
1557 avl_ber_bvfree( void *bv )
1558 {
1559         if( bv == NULL ) {
1560                 return;
1561         }
1562         if ( ((struct berval *)bv)->bv_val != NULL ) {
1563                 ber_memfree ( ((struct berval *)bv)->bv_val );
1564         }
1565         ber_memfree ( (char *) bv );
1566 }
1567
1568 static int
1569 cookie_callback(
1570         Operation* op,
1571         SlapReply* rs
1572 )
1573 {
1574         syncinfo_t *si = op->o_callback->sc_private;
1575         Attribute *a;
1576
1577         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1578
1579         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1580
1581         if ( a == NULL ) {
1582                 si->syncCookie = NULL;
1583         } else {
1584                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1585         }
1586         return LDAP_SUCCESS;
1587 }
1588
1589 static int
1590 dn_callback(
1591         Operation*      op,
1592         SlapReply*      rs
1593 )
1594 {
1595         syncinfo_t *si = op->o_callback->sc_private;
1596
1597         if ( rs->sr_type == REP_SEARCH ) {
1598                 if ( si->syncUUID_ndn != NULL ) {
1599 #ifdef NEW_LOGGING
1600                         LDAP_LOG( OPERATION, ERR,
1601                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1602 #else
1603                         Debug( LDAP_DEBUG_ANY,
1604                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1605 #endif
1606                 } else {
1607                         if ( rs->sr_entry == NULL ) {
1608                                 si->syncUUID_ndn = NULL;
1609                         } else {
1610                                 si->syncUUID_ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1611                         }
1612                 }
1613         }
1614
1615         return LDAP_SUCCESS;
1616 }
1617
1618 static int
1619 nonpresent_callback(
1620         Operation*      op,
1621         SlapReply*      rs
1622 )
1623 {
1624         syncinfo_t *si = op->o_callback->sc_private;
1625         Attribute *a;
1626         int count = 0;
1627         struct berval* present_uuid = NULL;
1628         slap_callback cb;
1629         SlapReply       rs_cb = {REP_RESULT};
1630         struct nonpresent_entry *np_entry;
1631
1632         if ( rs->sr_type == REP_RESULT ) {
1633                 count = avl_free( si->presentlist, avl_ber_bvfree );
1634                 si->presentlist = NULL;
1635                 return LDAP_SUCCESS;
1636         } else if ( rs->sr_type == REP_SEARCH ) {
1637                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1638
1639                 if ( a == NULL )
1640                         return 0;
1641
1642                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1643
1644                 if ( present_uuid == NULL ) {
1645                         np_entry = (struct nonpresent_entry *)
1646                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1647                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1648                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1649                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1650                 } else {
1651                         avl_delete( &si->presentlist,
1652                                         &a->a_vals[0], syncuuid_cmp );
1653                 }
1654                 return LDAP_SUCCESS;
1655         } else {
1656                 return LDAP_SUCCESS;
1657         }
1658
1659 }
1660
1661 static int
1662 null_callback(
1663         Operation*      op,
1664         SlapReply*      rs
1665 )
1666 {
1667         syncinfo_t *si = op->o_callback->sc_private;
1668
1669         if ( rs->sr_err != LDAP_SUCCESS &&
1670                  rs->sr_err != LDAP_REFERRAL &&
1671                  rs->sr_err != LDAP_ALREADY_EXISTS &&
1672                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
1673 #ifdef NEW_LOGGING
1674                 LDAP_LOG( OPERATION, ERR,
1675                         "null_callback : error code 0x%x\n",
1676                         rs->sr_err, 0, 0 );
1677 #else
1678                 Debug( LDAP_DEBUG_ANY,
1679                         "null_callback : error code 0x%x\n",
1680                         rs->sr_err, 0, 0 );
1681 #endif
1682         }
1683         return LDAP_SUCCESS;
1684 }
1685
1686
1687 char **
1688 str2clist( char ***out, char *in, const char *brkstr )
1689 {
1690         char    *str;
1691         char    *s;
1692         char    *lasts;
1693         int     i, j;
1694         const char *text;
1695         char    **new;
1696
1697         /* find last element in list */
1698         for (i = 0; *out && *out[i]; i++);
1699         
1700         /* protect the input string from strtok */
1701         str = ch_strdup( in );
1702
1703         /* Count words in string */
1704         j=1;
1705         for ( s = str; *s; s++ ) {
1706                 if ( strchr( brkstr, *s ) != NULL ) {
1707                         j++;
1708                 }
1709         }
1710
1711         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
1712         new = *out + i;
1713         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
1714                 s != NULL;
1715                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
1716         {
1717                 *new = ch_strdup( s );
1718                 new++;
1719         }
1720
1721         *new = NULL;
1722         free( str );
1723         return( *out );
1724 }