]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
Fix prev commit, syncrepl_add_glue must start from sufix
[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}, dn, ndn;
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                 return NULL;
711         }
712
713         if ( rctrls ) {
714                 BerElementBuffer berbuf;
715                 BerElement      *ctrl_ber;
716
717                 rctrlp = *rctrls;
718                 ctrl_ber = (BerElement *)&berbuf;
719                 ber_init2( ctrl_ber, &rctrlp->ldctl_value, LBER_USE_DER );
720                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
721                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
722                         ber_scanf( ctrl_ber, "o}", syncCookie );
723                 }
724                 ldap_controls_free( rctrls );
725         } else {
726 #ifdef NEW_LOGGING
727                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
728                         " rctrls absent\n", 0, 0, 0 );
729 #else
730                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
731                         " rctrls absent\n", 0, 0, 0 );
732 #endif
733         }
734
735         rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
736
737         if ( rc != LDAP_SUCCESS ) {
738 #ifdef NEW_LOGGING
739                 LDAP_LOG( OPERATION, ERR,
740                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
741 #else
742                 Debug( LDAP_DEBUG_ANY,
743                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
744 #endif
745                 return NULL;
746         }
747
748         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
749         ber_dupbv( &op->o_req_dn, &dn );
750         ber_dupbv( &op->o_req_ndn, &ndn );
751         sl_free( ndn.bv_val, op->o_tmpmemctx );
752         sl_free( dn.bv_val, op->o_tmpmemctx );
753
754         if ( *syncstate == LDAP_SYNC_PRESENT || *syncstate == LDAP_SYNC_DELETE ) {
755                 return NULL;
756         }
757
758         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
759         e->e_name = op->o_req_dn;
760         e->e_nname = op->o_req_ndn;
761
762         while ( ber_remaining( ber ) ) {
763                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
764                         LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL )) break;
765
766                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
767
768                 mod->sml_op = LDAP_MOD_REPLACE;
769                 mod->sml_next = NULL;
770                 mod->sml_desc = NULL;
771                 mod->sml_type = tmp.sml_type;
772                 mod->sml_bvalues = tmp.sml_bvalues;
773                 mod->sml_nvalues = NULL;
774
775                 *modtail = mod;
776                 modtail = &mod->sml_next;
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 0;
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;
1113         } else {
1114                 /* suffix is "" */
1115                 suffrdns = 0;
1116         }
1117
1118         /* Start with BE suffix */
1119         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1120                 comma = strrchr(dn.bv_val, ',');
1121                 if ( ptr ) *ptr = ',';
1122                 if ( comma ) *comma = '\0';
1123                 ptr = comma;
1124         }
1125         if ( ptr ) {
1126                 *ptr++ = ',';
1127                 dn.bv_len -= ptr - dn.bv_val;
1128                 dn.bv_val = ptr;
1129         }
1130         /* the normalizedDNs are always the same length, no counting
1131          * required.
1132          */
1133         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1134                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1135                 ndn.bv_len = be->be_nsuffix[0].bv_len;
1136         }
1137
1138         while ( ndn.bv_val > e->e_nname.bv_val ) {
1139                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1140                 ber_dupbv( &glue->e_name, &dn );
1141                 ber_dupbv( &glue->e_nname, &ndn );
1142
1143                 a = ch_calloc( 1, sizeof( Attribute ));
1144                 a->a_desc = slap_schema.si_ad_objectClass;
1145
1146                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1147                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1148                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1149                 a->a_vals[2].bv_len = 0;
1150                 a->a_vals[2].bv_val = NULL;
1151
1152                 a->a_nvals = a->a_vals;
1153
1154                 a->a_next = glue->e_attrs;
1155                 glue->e_attrs = a;
1156
1157                 a = ch_calloc( 1, sizeof( Attribute ));
1158                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1159
1160                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1161                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1162                 a->a_vals[1].bv_len = 0;
1163                 a->a_vals[1].bv_val = NULL;
1164
1165                 a->a_nvals = a->a_vals;
1166
1167                 a->a_next = glue->e_attrs;
1168                 glue->e_attrs = a;
1169
1170                 op->o_req_dn = glue->e_name;
1171                 op->o_req_ndn = glue->e_nname;
1172                 op->ora_e = glue;
1173                 rc = be->be_add ( op, &rs );
1174                 if ( rc == LDAP_SUCCESS ) {
1175                         be_entry_release_w( op, glue );
1176                 } else {
1177                 /* incl. ALREADY EXIST */
1178                         entry_free( glue );
1179                 }
1180
1181                 /* Move to next child */
1182                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--);
1183                 if ( ptr == e->e_name.bv_val ) break;
1184                 dn.bv_val = ++ptr;
1185                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1186                 for (ptr = ndn.bv_val-2; ptr > e->e_nname.bv_val && *ptr != ','; ptr--);
1187                 ndn.bv_val = ++ptr;
1188                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1189         }
1190
1191         op->o_req_dn = e->e_name;
1192         op->o_req_ndn = e->e_nname;
1193         op->ora_e = e;
1194         rc = be->be_add ( op, &rs );
1195         if ( rc == LDAP_SUCCESS )
1196                 be_entry_release_w( op, e );
1197         else 
1198                 entry_free( e );
1199
1200         return;
1201 }
1202
1203 static struct berval ocbva[] = {
1204         BER_BVC("top"),
1205         BER_BVC("subentry"),
1206         BER_BVC("syncConsumerSubentry"),
1207         BER_BVNULL
1208 };
1209
1210 static struct berval cnbva[] = {
1211         BER_BVNULL,
1212         BER_BVNULL
1213 };
1214
1215 static struct berval ssbva[] = {
1216         BER_BVC("{}"),
1217         BER_BVNULL
1218 };
1219
1220 static struct berval scbva[] = {
1221         BER_BVNULL,
1222         BER_BVNULL
1223 };
1224
1225 void
1226 syncrepl_updateCookie(
1227         syncinfo_t *si,
1228         LDAP *ld,
1229         Operation *op,
1230         struct berval *pdn,
1231         struct berval *syncCookie
1232 )
1233 {
1234         Backend *be = op->o_bd;
1235         Modifications *ml;
1236         Modifications *mlnext;
1237         Modifications *mod;
1238         Modifications *modlist = NULL;
1239         Modifications **modtail = &modlist;
1240
1241         const char      *text;
1242         char txtbuf[SLAP_TEXT_BUFLEN];
1243         size_t textlen = sizeof txtbuf;
1244
1245         Entry* e = NULL;
1246         int rc;
1247
1248         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1249         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1250         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1251         
1252         slap_callback cb;
1253         SlapReply       rs = {REP_RESULT};
1254
1255         /* update in memory cookie */
1256         if ( si->syncCookie != NULL ) {
1257                 ber_bvfree( si->syncCookie );
1258         }
1259         si->syncCookie = ber_dupbv( NULL, syncCookie );
1260         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1261         mod->sml_op = LDAP_MOD_REPLACE;
1262         mod->sml_desc = slap_schema.si_ad_objectClass;
1263         mod->sml_type = mod->sml_desc->ad_cname;
1264         mod->sml_bvalues = ocbva;
1265         *modtail = mod;
1266         modtail = &mod->sml_next;
1267
1268         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1269         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1270                                                                 slap_syncrepl_bvc.bv_len,
1271                                                                 "syncrepl%d", si->id );
1272         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1273         mod->sml_op = LDAP_MOD_REPLACE;
1274         mod->sml_desc = slap_schema.si_ad_cn;
1275         mod->sml_type = mod->sml_desc->ad_cname;
1276         mod->sml_bvalues = cnbva;
1277         *modtail = mod;
1278         modtail = &mod->sml_next;
1279
1280         if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1281         ber_dupbv( &scbva[0], si->syncCookie );
1282         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1283         mod->sml_op = LDAP_MOD_REPLACE;
1284         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1285         mod->sml_type = mod->sml_desc->ad_cname;
1286         mod->sml_bvalues = scbva;
1287         *modtail = mod;
1288         modtail = &mod->sml_next;
1289
1290         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1291         mod->sml_op = LDAP_MOD_REPLACE;
1292         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1293         mod->sml_type = mod->sml_desc->ad_cname;
1294         mod->sml_bvalues = ssbva;
1295         *modtail = mod;
1296         modtail = &mod->sml_next;
1297
1298         mlnext = mod;
1299
1300         op->o_tag = LDAP_REQ_ADD;
1301         rc = slap_mods_opattrs( op, modlist, modtail,
1302                                                          &text,txtbuf, textlen );
1303
1304         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1305                 ml->sml_op = LDAP_MOD_REPLACE;
1306         }
1307
1308         if( rc != LDAP_SUCCESS ) {
1309 #ifdef NEW_LOGGING
1310                 LDAP_LOG( OPERATION, ERR,
1311                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1312 #else
1313                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1314                          text, 0, 0 );
1315 #endif
1316         }
1317
1318         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1319
1320         slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1321         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1322                                                                                 slap_syncrepl_cn_bvc.bv_len,
1323                                                                                 "cn=syncrepl%d", si->id );
1324
1325         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv, op->o_tmpmemctx );
1326         ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1327         ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1328
1329         if ( slap_syncrepl_dn_bv.bv_val )
1330                 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1331
1332         e->e_attrs = NULL;
1333
1334         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1335
1336         if( rc != LDAP_SUCCESS ) {
1337 #ifdef NEW_LOGGING
1338                 LDAP_LOG( OPERATION, ERR,
1339                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1340 #else
1341                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1342                          text, 0, 0 );
1343 #endif
1344         }
1345
1346         cb.sc_response = null_callback;
1347         cb.sc_private = si;
1348
1349         op->o_callback = &cb;
1350         op->o_req_dn = e->e_name;
1351         op->o_req_ndn = e->e_nname;
1352
1353         /* update persistent cookie */
1354 update_cookie_retry:
1355         op->o_tag = LDAP_REQ_MODIFY;
1356         op->orm_modlist = modlist;
1357         rc = be->be_modify( op, &rs );
1358
1359         if ( rc != LDAP_SUCCESS ) {
1360                 if ( rc == LDAP_REFERRAL ||
1361                          rc == LDAP_NO_SUCH_OBJECT ) {
1362                         op->o_tag = LDAP_REQ_ADD;
1363                         op->ora_e = e;
1364                         rc = be->be_add( op, &rs );
1365                         if ( rc != LDAP_SUCCESS ) {
1366                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1367                                         goto update_cookie_retry;
1368                                 } else if ( rc == LDAP_REFERRAL ||
1369                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1370 #ifdef NEW_LOGGING
1371                                         LDAP_LOG( OPERATION, ERR,
1372                                                 "cookie will be non-persistent\n",
1373                                                 0, 0, 0 );
1374 #else
1375                                         Debug( LDAP_DEBUG_ANY,
1376                                                 "cookie will be non-persistent\n",
1377                                                 0, 0, 0 );
1378 #endif
1379                                 } else {
1380 #ifdef NEW_LOGGING
1381                                         LDAP_LOG( OPERATION, ERR,
1382                                                 "be_add failed (%d)\n",
1383                                                 rc, 0, 0 );
1384 #else
1385                                         Debug( LDAP_DEBUG_ANY,
1386                                                 "be_add failed (%d)\n",
1387                                                 rc, 0, 0 );
1388 #endif
1389                                 }
1390                         } else {
1391                                 be_entry_release_w( op, e );
1392                                 goto done;
1393                         }
1394                 } else {
1395 #ifdef NEW_LOGGING
1396                         LDAP_LOG( OPERATION, ERR,
1397                                 "be_modify failed (%d)\n", rc, 0, 0 );
1398 #else
1399                         Debug( LDAP_DEBUG_ANY,
1400                                 "be_modify failed (%d)\n", rc, 0, 0 );
1401 #endif
1402                 }
1403         }
1404
1405         if ( e != NULL ) {
1406                 entry_free( e );
1407         }
1408
1409 done :
1410
1411         if ( cnbva[0].bv_val ) {
1412                 ch_free( cnbva[0].bv_val );
1413                 cnbva[0].bv_val = NULL;
1414         }
1415         if ( scbva[0].bv_val ) {
1416                 ch_free( scbva[0].bv_val );
1417                 scbva[0].bv_val = NULL;
1418         }
1419
1420         if ( mlnext->sml_next ) {
1421                 slap_mods_free( mlnext->sml_next );
1422                 mlnext->sml_next = NULL;
1423         }
1424
1425         for (ml = modlist ; ml != NULL; ml = mlnext ) {
1426                 mlnext = ml->sml_next;
1427                 free( ml );
1428         }
1429
1430         return;
1431 }
1432
1433 void
1434 avl_ber_bvfree( void *bv )
1435 {
1436         if( bv == NULL ) {
1437                 return;
1438         }
1439         if ( ((struct berval *)bv)->bv_val != NULL ) {
1440                 ch_free ( ((struct berval *)bv)->bv_val );
1441         }
1442         ch_free ( (char *) bv );
1443 }
1444
1445 static int
1446 dn_callback(
1447         Operation*      op,
1448         SlapReply*      rs
1449 )
1450 {
1451         syncinfo_t *si = op->o_callback->sc_private;
1452
1453         if ( rs->sr_type == REP_SEARCH ) {
1454                 if ( si->syncUUID_ndn != NULL ) {
1455 #ifdef NEW_LOGGING
1456                         LDAP_LOG( OPERATION, ERR,
1457                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1458 #else
1459                         Debug( LDAP_DEBUG_ANY,
1460                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1461 #endif
1462                 } else {
1463                         if ( rs->sr_entry == NULL ) {
1464                                 si->syncUUID_ndn = NULL;
1465                         } else {
1466                                 si->syncUUID_ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1467                         }
1468                 }
1469         }
1470
1471         return LDAP_SUCCESS;
1472 }
1473
1474 static int
1475 nonpresent_callback(
1476         Operation*      op,
1477         SlapReply*      rs
1478 )
1479 {
1480         syncinfo_t *si = op->o_callback->sc_private;
1481         Attribute *a;
1482         int count = 0;
1483         struct berval* present_uuid = NULL;
1484         struct nonpresent_entry *np_entry;
1485
1486         if ( rs->sr_type == REP_RESULT ) {
1487                 count = avl_free( si->presentlist, avl_ber_bvfree );
1488                 si->presentlist = NULL;
1489                 return LDAP_SUCCESS;
1490         } else if ( rs->sr_type == REP_SEARCH ) {
1491                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1492
1493                 if ( a == NULL )
1494                         return 0;
1495
1496                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1497
1498                 if ( present_uuid == NULL ) {
1499                         np_entry = (struct nonpresent_entry *)
1500                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1501                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1502                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1503                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1504                 } else {
1505                         avl_delete( &si->presentlist,
1506                                         &a->a_vals[0], syncuuid_cmp );
1507                         ch_free( present_uuid->bv_val );
1508                         ch_free( present_uuid );
1509                 }
1510                 return LDAP_SUCCESS;
1511         } else {
1512                 return LDAP_SUCCESS;
1513         }
1514
1515 }
1516
1517 static int
1518 null_callback(
1519         Operation*      op,
1520         SlapReply*      rs
1521 )
1522 {
1523         if ( rs->sr_err != LDAP_SUCCESS &&
1524                  rs->sr_err != LDAP_REFERRAL &&
1525                  rs->sr_err != LDAP_ALREADY_EXISTS &&
1526                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
1527 #ifdef NEW_LOGGING
1528                 LDAP_LOG( OPERATION, ERR,
1529                         "null_callback : error code 0x%x\n",
1530                         rs->sr_err, 0, 0 );
1531 #else
1532                 Debug( LDAP_DEBUG_ANY,
1533                         "null_callback : error code 0x%x\n",
1534                         rs->sr_err, 0, 0 );
1535 #endif
1536         }
1537         return LDAP_SUCCESS;
1538 }
1539
1540 Entry *
1541 slap_create_syncrepl_entry(
1542         Backend *be,
1543         struct berval *context_csn,
1544         struct berval *rdn,
1545         struct berval *cn
1546 )
1547 {
1548         Entry* e;
1549
1550         struct berval bv;
1551
1552         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1553
1554         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1555
1556         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
1557
1558         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1559
1560         if ( context_csn ) {
1561                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1562                         context_csn, NULL );
1563         }
1564
1565         bv.bv_val = "{}";
1566         bv.bv_len = sizeof("{}")-1;
1567         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1568
1569         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1570         ber_dupbv( &e->e_nname, &e->e_name );
1571
1572         return e;
1573 }