]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
ITS#2747, Reorganize syncrepl, fix some memleaks. More remain.
[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 * ) sl_calloc( 1, sizeof( Entry ), op->o_tmpmemctx);
727         dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
728
729         while ( ber_remaining( ber ) ) {
730                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
731                         LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL )) break;
732
733                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
734
735                 mod->sml_op = LDAP_MOD_REPLACE;
736                 mod->sml_next = NULL;
737                 mod->sml_desc = NULL;
738                 mod->sml_type = tmp.sml_type;
739                 mod->sml_bvalues = tmp.sml_bvalues;
740                 mod->sml_nvalues = NULL;
741
742                 *modtail = mod;
743                 modtail = &mod->sml_next;
744         }
745
746         if ( rctrls ) {
747                 BerElementBuffer berbuf;
748                 BerElement      *ctrl_ber;
749
750                 rctrlp = *rctrls;
751                 ctrl_ber = (BerElement *)&berbuf;
752                 ber_init2( ctrl_ber, &rctrlp->ldctl_value, LBER_USE_DER );
753                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
754                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
755                         ber_scanf( ctrl_ber, "o}", syncCookie );
756                 }
757                 ldap_controls_free( rctrls );
758         } else {
759 #ifdef NEW_LOGGING
760                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
761                         " rctrls absent\n", 0, 0, 0 );
762 #else
763                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
764                         " rctrls absent\n", 0, 0, 0 );
765 #endif
766         }
767
768         if ( *syncstate == LDAP_SYNC_PRESENT || *syncstate == LDAP_SYNC_DELETE ) {
769                 rc = 1;
770                 goto done;
771         }
772
773         if ( *modlist == NULL ) {
774 #ifdef NEW_LOGGING
775                 LDAP_LOG( OPERATION, ERR,
776                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
777 #else
778                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
779                                 0, 0, 0 );
780 #endif
781         }
782
783         rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
784
785         if ( rc != LDAP_SUCCESS ) {
786 #ifdef NEW_LOGGING
787                 LDAP_LOG( OPERATION, ERR,
788                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
789 #else
790                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
791                                 text, 0, 0 );
792 #endif
793                 goto done;
794         }
795         
796         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
797         if( rc != LDAP_SUCCESS ) {
798 #ifdef NEW_LOGGING
799                 LDAP_LOG( OPERATION, ERR,
800                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
801 #else
802                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
803                                 text, 0, 0 );
804 #endif
805         }
806
807 done:
808         ber_free ( ber, 0 );
809         if ( rc != LDAP_SUCCESS ) {
810                 entry_free( e );
811                 e = NULL;
812         }
813
814         return e;
815 }
816
817 int
818 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
819 {
820         const struct berval *uuid1 = v_uuid1;
821         const struct berval *uuid2 = v_uuid2;
822         int rc = uuid1->bv_len - uuid2->bv_len;
823         if ( rc ) return rc;
824         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
825 }
826
827 int
828 syncrepl_entry(
829         syncinfo_t* si,
830         LDAP *ld,
831         Operation *op,
832         Entry* e,
833         Modifications* modlist,
834         int syncstate,
835         struct berval* syncUUID,
836         struct berval* syncCookie,
837         int refresh
838 )
839 {
840         Backend *be = op->o_bd;
841         slap_callback   cb;
842         struct berval   *syncuuid_bv = NULL;
843
844         SlapReply       rs = {REP_RESULT};
845         int rc = LDAP_SUCCESS;
846
847         if ( refresh &&
848                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
849                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
850                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
851                                                 syncuuid_cmp, avl_dup_error );
852         }
853
854         if ( syncstate == LDAP_SYNC_PRESENT ) {
855                 if ( e ) {
856                         return 1;
857                 } else {
858                         return 0;
859                 }
860         }
861
862         op->ors_filterstr.bv_len = strlen("entryUUID=") + syncUUID->bv_len;
863         op->ors_filterstr.bv_val = (char *) sl_malloc( op->ors_filterstr.bv_len + 1,
864                                                                         op->o_tmpmemctx ); 
865         strcpy( op->ors_filterstr.bv_val, "entryUUID=" );
866         strcat( op->ors_filterstr.bv_val, syncUUID->bv_val );
867
868         si->e = e;
869         si->syncUUID_ndn = NULL;
870
871         op->ors_filter = str2filter_x( op, op->ors_filterstr.bv_val );
872         op->ors_scope = LDAP_SCOPE_SUBTREE;
873
874         /* get syncrepl cookie of shadow replica from subentry */
875         op->o_req_dn = si->base;
876         op->o_req_ndn = si->base;
877
878         /* set callback function */
879         op->o_callback = &cb;
880         cb.sc_response = dn_callback;
881         cb.sc_private = si;
882
883         si->syncUUID_ndn = NULL;
884
885         rc = be->be_search( op, &rs );
886
887         if ( op->ors_filter )
888                 filter_free_x( op, op->ors_filter );
889         if ( op->ors_filterstr.bv_val )
890                 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
891
892         cb.sc_response = null_callback;
893         cb.sc_private = si;
894
895         if ( rc == LDAP_SUCCESS && si->syncUUID_ndn && si->sync_mode != LDAP_SYNC_LOG_MODE ) {
896                 op->o_req_dn = *si->syncUUID_ndn;
897                 op->o_req_ndn = *si->syncUUID_ndn;
898                 op->o_tag = LDAP_REQ_DELETE;
899                 rc = be->be_delete( op, &rs );
900         }
901
902         if ( si->syncUUID_ndn ) {
903                 ber_bvfree( si->syncUUID_ndn );
904         }
905
906         switch ( syncstate ) {
907         case LDAP_SYNC_ADD :
908         case LDAP_SYNC_MODIFY :
909
910                 if ( rc == LDAP_SUCCESS ||
911                          rc == LDAP_REFERRAL ||
912                          rc == LDAP_NO_SUCH_OBJECT ) {
913
914                         attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
915                         attr_merge_normalize_one( e, slap_schema.si_ad_entryUUID, syncUUID, op->o_tmpmemctx );
916
917                         op->o_tag = LDAP_REQ_ADD;
918                         op->ora_e = e;
919                         op->o_req_dn = e->e_name;
920                         op->o_req_ndn = e->e_nname;
921                         rc = be->be_add( op, &rs );
922
923                         if ( rc != LDAP_SUCCESS ) {
924                                 if ( rc == LDAP_ALREADY_EXISTS ) {      
925                                         op->o_tag = LDAP_REQ_MODIFY;
926                                         op->orm_modlist = modlist;
927                                         op->o_req_dn = e->e_name;
928                                         op->o_req_ndn = e->e_nname;
929                                         rc = be->be_modify( op, &rs );
930                                         si->e = NULL;
931                                         if ( rc != LDAP_SUCCESS ) {
932 #ifdef NEW_LOGGING
933                                                 LDAP_LOG( OPERATION, ERR,
934                                                         "syncrepl_entry : be_modify failed (%d)\n",
935                                                         rc, 0, 0 );
936 #else
937                                                 Debug( LDAP_DEBUG_ANY,
938                                                         "syncrepl_entry : be_modify failed (%d)\n",
939                                                         rc, 0, 0 );
940 #endif
941                                         }
942                                         return 1;
943                                 } else if ( rc == LDAP_REFERRAL ||
944                                                         rc == LDAP_NO_SUCH_OBJECT ) {
945                                         syncrepl_add_glue( si, ld, op, e,
946                                                 modlist, syncstate,
947                                                 syncUUID, syncCookie);
948                                         si->e = NULL;
949                                         return 0;
950                                 } else {
951 #ifdef NEW_LOGGING
952                                         LDAP_LOG( OPERATION, ERR,
953                                                 "syncrepl_entry : be_add failed (%d)\n",
954                                                 rc, 0, 0 );
955 #else
956                                         Debug( LDAP_DEBUG_ANY,
957                                                 "syncrepl_entry : be_add failed (%d)\n",
958                                                 rc, 0, 0 );
959 #endif
960                                         si->e = NULL;
961                                         return 1;
962                                 }
963                         } else {
964                                 si->e = NULL;
965                                 be_entry_release_w( op, e );
966                                 return 0;
967                         }
968                 } else {
969 #ifdef NEW_LOGGING
970                         LDAP_LOG( OPERATION, ERR,
971                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
972 #else
973                         Debug( LDAP_DEBUG_ANY,
974                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
975 #endif
976                         si->e = NULL;
977                         return 1;
978                 }
979
980         case LDAP_SYNC_DELETE :
981                 if ( si->sync_mode == LDAP_SYNC_LOG_MODE ) {
982                         op->o_req_dn = *si->syncUUID_ndn;
983                         op->o_req_ndn = *si->syncUUID_ndn;
984                         op->o_tag = LDAP_REQ_DELETE;
985                         rc = be->be_delete( op, &rs );
986                 }
987                 /* Already deleted otherwise */
988                 return 1;
989
990         default :
991 #ifdef NEW_LOGGING
992                 LDAP_LOG( OPERATION, ERR,
993                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
994 #else
995                 Debug( LDAP_DEBUG_ANY,
996                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
997 #endif
998                 return 1;
999         }
1000 }
1001
1002 static void
1003 syncrepl_del_nonpresent(
1004         LDAP *ld,
1005         Operation *op,
1006         syncinfo_t *si
1007 )
1008 {
1009         Backend* be = op->o_bd;
1010         slap_callback   cb;
1011         SlapReply       rs = {REP_RESULT};
1012         struct nonpresent_entry *np_list, *np_prev;
1013
1014         op->o_req_dn = si->base;
1015         op->o_req_ndn = si->base;
1016
1017         cb.sc_response = nonpresent_callback;
1018         cb.sc_private = si;
1019
1020         op->o_callback = &cb;
1021         op->o_tag = LDAP_REQ_SEARCH;
1022         op->ors_scope = si->scope;
1023         op->ors_deref = LDAP_DEREF_NEVER;
1024         op->ors_slimit = 0;
1025         op->ors_tlimit = 0;
1026         op->ors_attrsonly = 0;
1027         op->ors_attrs = NULL;
1028         op->ors_filter = str2filter_x( op, si->filterstr.bv_val );
1029         op->ors_filterstr = si->filterstr;
1030
1031         op->o_nocaching = 1;
1032         be->be_search( op, &rs );
1033         op->o_nocaching = 0;
1034
1035         if ( op->ors_filter )
1036                 filter_free_x( op, op->ors_filter );
1037
1038         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1039                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1040                 while ( np_list != NULL ) {
1041                         LDAP_LIST_REMOVE( np_list, np_link );
1042                         np_prev = np_list;
1043                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1044                         op->o_tag = LDAP_REQ_DELETE;
1045                         op->o_callback = &cb;
1046                         cb.sc_response = null_callback;
1047                         cb.sc_private = si;
1048                         op->o_req_dn = *np_prev->dn;
1049                         op->o_req_ndn = *np_prev->ndn;
1050                         op->o_bd->be_delete( op, &rs );
1051                         ber_bvfree( np_prev->dn );
1052                         ber_bvfree( np_prev->ndn );
1053                         op->o_req_dn.bv_val = NULL;
1054                         op->o_req_ndn.bv_val = NULL;
1055                         ch_free( np_prev );
1056                 }
1057         }
1058
1059         return;
1060 }
1061
1062
1063 void
1064 syncrepl_add_glue(
1065         syncinfo_t *si,
1066         LDAP *ld,
1067         Operation* op,
1068         Entry *e,
1069         Modifications* modlist,
1070         int syncstate,
1071         struct berval* syncUUID,
1072         struct berval* syncCookie
1073 )
1074 {
1075         Backend *be = op->o_bd;
1076         slap_callback cb;
1077         Attribute       *a;
1078         int     rc;
1079         int levels = 0;
1080         int i, j, k;
1081         struct berval dn = {0, NULL};
1082         struct berval pdn = {0, NULL};
1083         struct berval ndn = {0, NULL};
1084         struct berval rdn = {0, NULL};
1085         Entry   *glue;
1086         SlapReply       rs = {REP_RESULT};
1087         Connection *conn = op->o_conn;
1088         char* ptr;
1089
1090         op->o_tag = LDAP_REQ_ADD;
1091         op->o_callback = &cb;
1092         cb.sc_response = null_callback;
1093         cb.sc_private = si;
1094
1095         ber_dupbv( &dn, &e->e_nname );
1096         ber_dupbv( &pdn, &e->e_nname );
1097
1098         ptr = dn.bv_val;
1099         while ( !be_issuffix ( be, &pdn )) {
1100                 dnParent( &dn, &pdn );
1101                 dn.bv_val = pdn.bv_val;
1102                 dn.bv_len = pdn.bv_len;
1103                 levels++;
1104         }
1105         ch_free( ptr );
1106
1107         for ( i = 0; i <= levels; i++ ) {
1108                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1109                 ber_dupbv( &dn, &e->e_nname );
1110                 j = levels - i;
1111
1112                 ptr = dn.bv_val;
1113                 for ( k = 0; k < j; k++ ) {
1114                         dnParent( &dn, &pdn );
1115                         dn.bv_val = pdn.bv_val;
1116                         dn.bv_len = pdn.bv_len;
1117                 }
1118
1119                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1120                 ber_dupbv( &glue->e_name, &pdn );
1121                 ber_dupbv( &glue->e_nname, &ndn );
1122                 ch_free( ptr );
1123                 ch_free( pdn.bv_val );
1124                 ch_free( ndn.bv_val );
1125
1126                 a = ch_calloc( 1, sizeof( Attribute ));
1127                 a->a_desc = slap_schema.si_ad_objectClass;
1128
1129                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1130                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1131                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1132                 a->a_vals[2].bv_len = 0;
1133                 a->a_vals[2].bv_val = NULL;
1134
1135                 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1136                 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1137                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1138                 a->a_nvals[2].bv_len = 0;
1139                 a->a_nvals[2].bv_val = NULL;
1140
1141                 a->a_next = glue->e_attrs;
1142                 glue->e_attrs = a;
1143
1144                 a = ch_calloc( 1, sizeof( Attribute ));
1145                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1146
1147                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1148                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1149                 a->a_vals[1].bv_len = 0;
1150                 a->a_vals[1].bv_val = NULL;
1151
1152                 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1153                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1154                 a->a_nvals[1].bv_len = 0;
1155                 a->a_nvals[1].bv_val = NULL;
1156
1157                 a->a_next = glue->e_attrs;
1158                 glue->e_attrs = a;
1159
1160                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1161                         op->o_req_dn = e->e_name;
1162                         op->o_req_ndn = e->e_nname;
1163                         op->ora_e = e;
1164                         rc = be->be_add ( op, &rs );
1165                         if ( rc == LDAP_SUCCESS )
1166                                 be_entry_release_w( op, e );
1167                         else 
1168                                 entry_free( e );
1169                         entry_free( glue );
1170                 } else {
1171                         op->o_req_dn = glue->e_name;
1172                         op->o_req_ndn = glue->e_nname;
1173                         op->ora_e = glue;
1174                         rc = be->be_add ( op, &rs );
1175                         if ( rc == LDAP_SUCCESS ) {
1176                                 be_entry_release_w( op, glue );
1177                         } else {
1178                         /* incl. ALREADY EXIST */
1179                                 entry_free( glue );
1180                         }
1181                 }
1182         }
1183
1184         return;
1185 }
1186
1187 static struct berval ocbva[] = {
1188         BER_BVC("top"),
1189         BER_BVC("subentry"),
1190         BER_BVC("syncConsumerSubentry"),
1191         BER_BVNULL
1192 };
1193
1194 static struct berval cnbva[] = {
1195         BER_BVNULL,
1196         BER_BVNULL
1197 };
1198
1199 static struct berval ssbva[] = {
1200         BER_BVC("{}"),
1201         BER_BVNULL
1202 };
1203
1204 static struct berval scbva[] = {
1205         BER_BVC("subentry"),
1206         BER_BVNULL
1207 };
1208
1209 void
1210 syncrepl_updateCookie(
1211         syncinfo_t *si,
1212         LDAP *ld,
1213         Operation *op,
1214         struct berval *pdn,
1215         struct berval *syncCookie
1216 )
1217 {
1218         Backend *be = op->o_bd;
1219         Modifications *ml;
1220         Modifications *mlnext;
1221         Modifications *mod;
1222         Modifications *modlist = NULL;
1223         Modifications **modtail = &modlist;
1224
1225         const char      *text;
1226         char txtbuf[SLAP_TEXT_BUFLEN];
1227         size_t textlen = sizeof txtbuf;
1228
1229         Entry* e = NULL;
1230         int rc;
1231
1232         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1233         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1234         
1235         slap_callback cb;
1236         SlapReply       rs = {REP_RESULT};
1237
1238         /* update in memory cookie */
1239         if ( si->syncCookie != NULL ) {
1240                 ber_bvfree( si->syncCookie );
1241         }
1242         si->syncCookie = ber_dupbv( NULL, syncCookie );
1243         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1244         mod->sml_op = LDAP_MOD_REPLACE;
1245         mod->sml_desc = slap_schema.si_ad_objectClass;
1246         mod->sml_type = mod->sml_desc->ad_cname;
1247         mod->sml_bvalues = ocbva;
1248         *modtail = mod;
1249         modtail = &mod->sml_next;
1250
1251         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1252         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1253                                                                 slap_syncrepl_bvc.bv_len,
1254                                                                 "syncrepl%d", si->id );
1255         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1256         mod->sml_op = LDAP_MOD_REPLACE;
1257         mod->sml_desc = slap_schema.si_ad_cn;
1258         mod->sml_type = mod->sml_desc->ad_cname;
1259         mod->sml_bvalues = cnbva;
1260         *modtail = mod;
1261         modtail = &mod->sml_next;
1262
1263         ber_dupbv( &scbva[0], si->syncCookie );
1264         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1265         mod->sml_op = LDAP_MOD_REPLACE;
1266         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1267         mod->sml_type = mod->sml_desc->ad_cname;
1268         mod->sml_bvalues = scbva;
1269         *modtail = mod;
1270         modtail = &mod->sml_next;
1271
1272         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1273         mod->sml_op = LDAP_MOD_REPLACE;
1274         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1275         mod->sml_type = mod->sml_desc->ad_cname;
1276         mod->sml_bvalues = ssbva;
1277         *modtail = mod;
1278         modtail = &mod->sml_next;
1279
1280 #if 0
1281         rc = slap_mods_check( modlist, 1, &text, txtbuf, textlen, NULL );
1282
1283         if ( rc != LDAP_SUCCESS ) {
1284 #ifdef NEW_LOGGING
1285                 LDAP_LOG( OPERATION, ERR,
1286                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1287 #else
1288                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1289                          text, 0, 0 );
1290 #endif
1291         }
1292 #endif
1293
1294         op->o_tag = LDAP_REQ_ADD;
1295         rc = slap_mods_opattrs( op, modlist, modtail,
1296                                                          &text,txtbuf, textlen );
1297
1298         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1299                 mlnext = ml->sml_next;
1300                 ml->sml_op = LDAP_MOD_REPLACE;
1301         }
1302
1303         if( rc != LDAP_SUCCESS ) {
1304 #ifdef NEW_LOGGING
1305                 LDAP_LOG( OPERATION, ERR,
1306                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1307 #else
1308                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1309                          text, 0, 0 );
1310 #endif
1311         }
1312
1313         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1314
1315         ber_dupbv( &slap_syncrepl_cn_bv, (struct berval *) &slap_syncrepl_cn_bvc );
1316         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1317                                                                                 slap_syncrepl_cn_bvc.bv_len,
1318                                                                                 "cn=syncrepl%d", si->id );
1319
1320         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv, NULL );
1321         dnPrettyNormal( NULL, &slap_syncrepl_dn_bv, &e->e_name, &e->e_nname, NULL );
1322
1323         if ( slap_syncrepl_cn_bv.bv_val )
1324                 ch_free( slap_syncrepl_cn_bv.bv_val );
1325         if ( slap_syncrepl_dn_bv.bv_val )
1326                 ch_free( slap_syncrepl_dn_bv.bv_val );
1327
1328         e->e_attrs = NULL;
1329
1330         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1331
1332         if( rc != LDAP_SUCCESS ) {
1333 #ifdef NEW_LOGGING
1334                 LDAP_LOG( OPERATION, ERR,
1335                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1336 #else
1337                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1338                          text, 0, 0 );
1339 #endif
1340         }
1341
1342         cb.sc_response = null_callback;
1343         cb.sc_private = si;
1344
1345         op->o_callback = &cb;
1346         op->o_req_dn = e->e_name;
1347         op->o_req_ndn = e->e_nname;
1348
1349         /* update persistent cookie */
1350 update_cookie_retry:
1351         op->o_tag = LDAP_REQ_MODIFY;
1352         op->orm_modlist = modlist;
1353         rc = be->be_modify( op, &rs );
1354
1355         if ( rc != LDAP_SUCCESS ) {
1356                 if ( rc == LDAP_REFERRAL ||
1357                          rc == LDAP_NO_SUCH_OBJECT ) {
1358                         op->o_tag = LDAP_REQ_ADD;
1359                         op->ora_e = e;
1360                         rc = be->be_add( op, &rs );
1361                         if ( rc != LDAP_SUCCESS ) {
1362                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1363                                         goto update_cookie_retry;
1364                                 } else if ( rc == LDAP_REFERRAL ||
1365                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1366 #ifdef NEW_LOGGING
1367                                         LDAP_LOG( OPERATION, ERR,
1368                                                 "cookie will be non-persistent\n",
1369                                                 0, 0, 0 );
1370 #else
1371                                         Debug( LDAP_DEBUG_ANY,
1372                                                 "cookie will be non-persistent\n",
1373                                                 0, 0, 0 );
1374 #endif
1375                                 } else {
1376 #ifdef NEW_LOGGING
1377                                         LDAP_LOG( OPERATION, ERR,
1378                                                 "be_add failed (%d)\n",
1379                                                 rc, 0, 0 );
1380 #else
1381                                         Debug( LDAP_DEBUG_ANY,
1382                                                 "be_add failed (%d)\n",
1383                                                 rc, 0, 0 );
1384 #endif
1385                                 }
1386                         } else {
1387                                 be_entry_release_w( op, e );
1388                                 goto done;
1389                         }
1390                 } else {
1391 #ifdef NEW_LOGGING
1392                         LDAP_LOG( OPERATION, ERR,
1393                                 "be_modify failed (%d)\n", rc, 0, 0 );
1394 #else
1395                         Debug( LDAP_DEBUG_ANY,
1396                                 "be_modify failed (%d)\n", rc, 0, 0 );
1397 #endif
1398                 }
1399         }
1400
1401         if ( e != NULL ) {
1402                 entry_free( e );
1403         }
1404
1405 done :
1406
1407         if ( cnbva[0].bv_val )
1408                 ch_free( cnbva[0].bv_val );
1409
1410         for ( ; ml != NULL; ml = mlnext ) {
1411                 mlnext = ml->sml_next;
1412                 free( ml );
1413         }
1414
1415         return;
1416 }
1417
1418 void
1419 avl_ber_bvfree( void *bv )
1420 {
1421         if( bv == NULL ) {
1422                 return;
1423         }
1424         if ( ((struct berval *)bv)->bv_val != NULL ) {
1425                 ch_free ( ((struct berval *)bv)->bv_val );
1426         }
1427         ch_free ( (char *) bv );
1428 }
1429
1430 static int
1431 dn_callback(
1432         Operation*      op,
1433         SlapReply*      rs
1434 )
1435 {
1436         syncinfo_t *si = op->o_callback->sc_private;
1437
1438         if ( rs->sr_type == REP_SEARCH ) {
1439                 if ( si->syncUUID_ndn != NULL ) {
1440 #ifdef NEW_LOGGING
1441                         LDAP_LOG( OPERATION, ERR,
1442                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1443 #else
1444                         Debug( LDAP_DEBUG_ANY,
1445                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1446 #endif
1447                 } else {
1448                         if ( rs->sr_entry == NULL ) {
1449                                 si->syncUUID_ndn = NULL;
1450                         } else {
1451                                 si->syncUUID_ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1452                         }
1453                 }
1454         }
1455
1456         return LDAP_SUCCESS;
1457 }
1458
1459 static int
1460 nonpresent_callback(
1461         Operation*      op,
1462         SlapReply*      rs
1463 )
1464 {
1465         syncinfo_t *si = op->o_callback->sc_private;
1466         Attribute *a;
1467         int count = 0;
1468         struct berval* present_uuid = NULL;
1469         struct nonpresent_entry *np_entry;
1470
1471         if ( rs->sr_type == REP_RESULT ) {
1472                 count = avl_free( si->presentlist, avl_ber_bvfree );
1473                 si->presentlist = NULL;
1474                 return LDAP_SUCCESS;
1475         } else if ( rs->sr_type == REP_SEARCH ) {
1476                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1477
1478                 if ( a == NULL )
1479                         return 0;
1480
1481                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1482
1483                 if ( present_uuid == NULL ) {
1484                         np_entry = (struct nonpresent_entry *)
1485                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1486                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1487                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1488                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1489                 } else {
1490                         avl_delete( &si->presentlist,
1491                                         &a->a_vals[0], syncuuid_cmp );
1492                         ch_free( present_uuid->bv_val );
1493                         ch_free( present_uuid );
1494                 }
1495                 return LDAP_SUCCESS;
1496         } else {
1497                 return LDAP_SUCCESS;
1498         }
1499
1500 }
1501
1502 static int
1503 null_callback(
1504         Operation*      op,
1505         SlapReply*      rs
1506 )
1507 {
1508         if ( rs->sr_err != LDAP_SUCCESS &&
1509                  rs->sr_err != LDAP_REFERRAL &&
1510                  rs->sr_err != LDAP_ALREADY_EXISTS &&
1511                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
1512 #ifdef NEW_LOGGING
1513                 LDAP_LOG( OPERATION, ERR,
1514                         "null_callback : error code 0x%x\n",
1515                         rs->sr_err, 0, 0 );
1516 #else
1517                 Debug( LDAP_DEBUG_ANY,
1518                         "null_callback : error code 0x%x\n",
1519                         rs->sr_err, 0, 0 );
1520 #endif
1521         }
1522         return LDAP_SUCCESS;
1523 }
1524
1525 Entry *
1526 slap_create_syncrepl_entry(
1527         Backend *be,
1528         struct berval *context_csn,
1529         struct berval *rdn,
1530         struct berval *cn
1531 )
1532 {
1533         Entry* e;
1534
1535         struct berval bv;
1536
1537         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1538
1539         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1540
1541         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass, &ocbva[1], NULL );
1542
1543         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1544
1545         if ( context_csn ) {
1546                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1547                         context_csn, NULL );
1548         }
1549
1550         bv.bv_val = "{}";
1551         bv.bv_len = sizeof("{}")-1;
1552         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1553
1554         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1555         ber_dupbv( &e->e_nname, &e->e_name );
1556
1557         return e;
1558 }