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