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