]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
Suck in changes from HEAD (CHANGES needs ITS #s)
[openldap] / servers / slapd / syncrepl.c
1 /* syncrepl.c -- Replication Engine which uses the LDAP Sync protocol */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2003-2004 The OpenLDAP Foundation.
6  * Portions Copyright 2003 by IBM Corporation.
7  * Portions Copyright 2003 by Howard Chu, Symas Corporation.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted only as authorized by the OpenLDAP
12  * Public License.
13  *
14  * A copy of this license is available in the file LICENSE in the
15  * top-level directory of the distribution or, alternatively, at
16  * <http://www.OpenLDAP.org/license.html>.
17  */
18
19 #include "portable.h"
20
21 #include <stdio.h>
22
23 #include <ac/string.h>
24 #include <ac/socket.h>
25
26 #include "ldap_pvt.h"
27 #include "lutil.h"
28 #include "slap.h"
29 #include "lutil_ldap.h"
30
31 #include "ldap_rq.h"
32
33 #define SYNCREPL_STR    "syncreplxxx"
34 #define CN_STR  "cn="
35
36 static const struct berval slap_syncrepl_bvc = BER_BVC(SYNCREPL_STR);
37 static const struct berval slap_syncrepl_cn_bvc = BER_BVC(CN_STR SYNCREPL_STR);
38
39 static int syncuuid_cmp( const void *, const void * );
40 static void avl_ber_bvfree( void * );
41 static void syncrepl_del_nonpresent( Operation *, syncinfo_t * );
42
43 /* callback functions */
44 static int dn_callback( struct slap_op *, struct slap_rep * );
45 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
46 static int null_callback( struct slap_op *, struct slap_rep * );
47
48 static AttributeDescription *sync_descs[4];
49
50 struct runqueue_s syncrepl_rq;
51
52 void
53 init_syncrepl(syncinfo_t *si)
54 {
55         int i, j, k, n;
56         char **tmp;
57
58         if ( !sync_descs[0] ) {
59                 sync_descs[0] = slap_schema.si_ad_objectClass;
60                 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
61                 sync_descs[2] = slap_schema.si_ad_entryCSN;
62                 sync_descs[3] = NULL;
63         }
64
65         for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
66
67         if ( n ) {
68                 /* Delete Attributes */
69                 for ( i = 0; sync_descs[i] != NULL; i++ ) {
70                         for ( j = 0; si->si_attrs[j] != NULL; j++ ) {
71                                 if ( strcmp( si->si_attrs[j], sync_descs[i]->ad_cname.bv_val )
72                                         == 0 )
73                                 {
74                                         ch_free( si->si_attrs[j] );
75                                         for ( k = j; si->si_attrs[k] != NULL; k++ ) {
76                                                 si->si_attrs[k] = si->si_attrs[k+1];
77                                         }
78                                 }
79                         }
80                 }
81                 for ( n = 0; si->si_attrs[ n ] != NULL; n++ ) /* empty */;
82                 tmp = ( char ** ) ch_realloc( si->si_attrs, (n + 4)*sizeof( char * ));
83                 if ( tmp == NULL ) {
84 #ifdef NEW_LOGGING
85                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
86 #else
87                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
88 #endif
89                 }
90         } else {
91                 tmp = ( char ** ) ch_realloc( si->si_attrs, 5 * sizeof( char * ));
92                 if ( tmp == NULL ) {
93 #ifdef NEW_LOGGING
94                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
95 #else
96                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
97 #endif
98                 }
99                 tmp[ n++ ] = ch_strdup( "*" );
100         }
101         
102         si->si_attrs = tmp;
103
104         /* Add Attributes */
105
106         for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
107                 si->si_attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
108                 si->si_attrs[ n ] = NULL;
109         }
110 }
111
112 static int
113 ldap_sync_search(
114         syncinfo_t *si,
115         void *ctx
116 )
117 {
118         BerElementBuffer berbuf;
119         BerElement *ber = (BerElement *)&berbuf;
120         LDAPControl c[2], *ctrls[3];
121         struct timeval timeout;
122         ber_int_t       msgid;
123         int rc;
124
125         /* setup LDAP SYNC control */
126         ber_init2( ber, NULL, LBER_USE_DER );
127         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
128
129         if ( si->si_syncCookie.octet_str &&
130                  si->si_syncCookie.octet_str[0].bv_val ) {
131                 ber_printf( ber, "{eO}", abs(si->si_type),
132                                         &si->si_syncCookie.octet_str[0] );
133         } else {
134                 ber_printf( ber, "{e}", abs(si->si_type) );
135         }
136
137         if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
138                 ber_free_buf( ber );
139                 return rc;
140         }
141
142         c[0].ldctl_oid = LDAP_CONTROL_SYNC;
143         c[0].ldctl_iscritical = si->si_type < 0;
144         ctrls[0] = &c[0];
145
146         if ( si->si_authzId ) {
147                 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
148                 ber_str2bv( si->si_authzId, 0, 0, &c[1].ldctl_value );
149                 c[1].ldctl_iscritical = 1;
150                 ctrls[1] = &c[1];
151                 ctrls[2] = NULL;
152         } else {
153                 ctrls[1] = NULL;
154         }
155
156         timeout.tv_sec = si->si_tlimit > 0 ? si->si_tlimit : 1;
157         timeout.tv_usec = 0;
158
159         rc = ldap_search_ext( si->si_ld, si->si_base.bv_val, si->si_scope,
160                 si->si_filterstr.bv_val, si->si_attrs, si->si_attrsonly,
161                 ctrls, NULL, si->si_tlimit < 0 ? NULL : &timeout,
162                 si->si_slimit, &msgid );
163         ber_free_buf( ber );
164
165         return rc;
166 }
167
168 static int
169 do_syncrep1(
170         Operation *op,
171         syncinfo_t *si )
172 {
173         int     rc;
174         int cmdline_cookie_found = 0;
175
176         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
177         struct berval syncrepl_cn_bv;
178         struct sync_cookie      *sc = NULL;
179         struct sync_cookie      syncCookie = { NULL, -1, NULL };
180         struct berval   *psub;
181
182         psub = &si->si_be->be_nsuffix[0];
183
184         /* Init connection to master */
185
186         rc = ldap_initialize( &si->si_ld, si->si_provideruri );
187         if ( rc != LDAP_SUCCESS ) {
188 #ifdef NEW_LOGGING
189                 LDAP_LOG( OPERATION, ERR,
190                         "do_syncrep1: ldap_initialize failed (%s)\n",
191                         si->si_provideruri, 0, 0 );
192 #else
193                 Debug( LDAP_DEBUG_ANY,
194                         "do_syncrep1: ldap_initialize failed (%s)\n",
195                         si->si_provideruri, 0, 0 );
196 #endif
197                 return rc;
198         }
199
200         op->o_protocol = LDAP_VERSION3;
201         ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
202
203         /* Bind to master */
204
205         if ( si->si_tls ) {
206                 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
207                 if( rc != LDAP_SUCCESS ) {
208 #ifdef NEW_LOGGING
209                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
210                                 "%s: ldap_start_tls failed (%d)\n",
211                                 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
212                                 rc, 0 );
213 #else
214                         Debug( LDAP_DEBUG_ANY,
215                                 "%s: ldap_start_tls failed (%d)\n",
216                                 si->si_tls == SYNCINFO_TLS_CRITICAL ? "Error" : "Warning",
217                                 rc, 0 );
218 #endif
219                         if( si->si_tls == SYNCINFO_TLS_CRITICAL ) goto done;
220                 }
221         }
222
223         if ( si->si_bindmethod == LDAP_AUTH_SASL ) {
224 #ifdef HAVE_CYRUS_SASL
225                 void *defaults;
226
227                 if ( si->si_secprops != NULL ) {
228                         rc = ldap_set_option( si->si_ld,
229                                 LDAP_OPT_X_SASL_SECPROPS, si->si_secprops);
230
231                         if( rc != LDAP_OPT_SUCCESS ) {
232 #ifdef NEW_LOGGING
233                                 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
234                                         "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
235                                         si->si_provideruri, si->si_secprops, 0 );
236 #else
237                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
238                                         "(%s,SECPROPS,\"%s\") failed!\n",
239                                         si->si_provideruri, si->si_secprops, 0 );
240 #endif
241                                 goto done;
242                         }
243                 }
244
245                 defaults = lutil_sasl_defaults( si->si_ld,
246                         si->si_saslmech, si->si_realm,
247                         si->si_authcId, si->si_passwd, si->si_authzId );
248
249                 rc = ldap_sasl_interactive_bind_s( si->si_ld,
250                                 si->si_binddn,
251                                 si->si_saslmech,
252                                 NULL, NULL,
253                                 LDAP_SASL_QUIET,
254                                 lutil_sasl_interact,
255                                 defaults );
256
257                 lutil_sasl_freedefs( defaults );
258
259                 /* FIXME: different error behaviors according to
260                  *      1) return code
261                  *      2) on err policy : exit, retry, backoff ...
262                  */
263                 if ( rc != LDAP_SUCCESS ) {
264 #ifdef NEW_LOGGING
265                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
266                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
267                                 rc, 0, 0 );
268 #else
269                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
270                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
271                                 rc, 0, 0 );
272 #endif
273
274                         /* FIXME (see above comment) */
275                         /* if Kerberos credentials cache is not active, retry */
276                         if ( strcmp( si->si_saslmech, "GSSAPI" ) == 0 &&
277                                 rc == LDAP_LOCAL_ERROR )
278                         {
279                                 rc = LDAP_SERVER_DOWN;
280                         }
281
282                         goto done;
283                 }
284 #else /* HAVE_CYRUS_SASL */
285                 /* Should never get here, we trapped this at config time */
286                 fprintf( stderr, "not compiled with SASL support\n" );
287                 rc = LDAP_OTHER;
288                 goto done;
289 #endif
290         } else {
291                 rc = ldap_bind_s( si->si_ld,
292                         si->si_binddn, si->si_passwd, si->si_bindmethod );
293                 if ( rc != LDAP_SUCCESS ) {
294 #ifdef NEW_LOGGING
295                         LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
296                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
297 #else
298                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
299                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
300 #endif
301                         goto done;
302                 }
303         }
304
305         /* get syncrepl cookie of shadow replica from subentry */
306
307         assert( si->si_rid < 1000 );
308         syncrepl_cn_bv.bv_val = syncrepl_cbuf;
309         syncrepl_cn_bv.bv_len = snprintf(syncrepl_cbuf, sizeof(syncrepl_cbuf),
310                 CN_STR "syncrepl%ld", si->si_rid );
311         build_new_dn( &op->o_req_ndn, psub, &syncrepl_cn_bv, op->o_tmpmemctx );
312         op->o_req_dn = op->o_req_ndn;
313
314         LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
315                 if ( si->si_rid == sc->rid ) {
316                         cmdline_cookie_found = 1;
317                         break;
318                 }
319         }
320
321         if ( cmdline_cookie_found ) {
322                 /* cookie is supplied in the command line */
323                 BerVarray cookie = NULL;
324                 struct berval cookie_bv;
325
326                 LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
327                 slap_sync_cookie_free( &si->si_syncCookie, 0 );
328
329                 /* read stored cookie if it exists */
330                 backend_attribute( op, NULL, &op->o_req_ndn,
331                         slap_schema.si_ad_syncreplCookie, &cookie );
332
333                 if ( !cookie ) {
334                         /* no stored cookie */
335                         if ( sc->ctxcsn == NULL ||
336                                  sc->ctxcsn->bv_val == NULL ) {
337                                 /* if cmdline cookie does not have ctxcsn */
338                                 /* component, set it to an initial value */
339                                 slap_init_sync_cookie_ctxcsn( sc );
340                         }
341                         slap_dup_sync_cookie( &si->si_syncCookie, sc );
342                         slap_sync_cookie_free( sc, 1 );
343                         sc = NULL;
344                 } else {
345                         /* stored cookie */
346                         struct berval newcookie = { 0, NULL };
347                         ber_dupbv( &cookie_bv, &cookie[0] );
348                         ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
349                         slap_parse_sync_cookie( &si->si_syncCookie );
350                         ber_bvarray_free( si->si_syncCookie.octet_str );
351                         si->si_syncCookie.octet_str = NULL;
352                         ber_bvarray_free_x( cookie, op->o_tmpmemctx );
353                         if ( sc->sid != -1 ) {
354                                 /* command line cookie wins */
355                                 si->si_syncCookie.sid = sc->sid;
356                         }
357                         if ( sc->ctxcsn != NULL ) {
358                                 /* command line cookie wins */
359                                 if ( si->si_syncCookie.ctxcsn ) {
360                                         ber_bvarray_free( si->si_syncCookie.ctxcsn );
361                                         si->si_syncCookie.ctxcsn = NULL;
362                                 }
363                                 ber_dupbv( &cookie_bv, &sc->ctxcsn[0] );
364                                 ber_bvarray_add( &si->si_syncCookie.ctxcsn, &cookie_bv );
365                         }
366                         if ( sc->rid != -1 ) {
367                                 /* command line cookie wins */
368                                 si->si_syncCookie.rid = sc->rid;
369                         }
370                         slap_sync_cookie_free( sc, 1 );
371                         sc = NULL;
372                         slap_compose_sync_cookie( NULL, &newcookie,
373                                         &si->si_syncCookie.ctxcsn[0],
374                                         si->si_syncCookie.sid, si->si_syncCookie.rid );
375                         ber_bvarray_add( &si->si_syncCookie.octet_str, &newcookie );
376                 }
377         } else {
378                 /* no command line cookie is specified */
379                 if ( si->si_syncCookie.octet_str == NULL ) {
380                         BerVarray cookie = NULL;
381                         struct berval cookie_bv;
382                         /* try to read stored cookie */
383                         backend_attribute( op, NULL, &op->o_req_ndn,
384                                 slap_schema.si_ad_syncreplCookie, &cookie );
385                         if ( cookie ) {
386                                 ber_dupbv( &cookie_bv, &cookie[0] );
387                                 ber_bvarray_add( &si->si_syncCookie.octet_str, &cookie_bv );
388                                 slap_parse_sync_cookie( &si->si_syncCookie );
389                                 ber_bvarray_free_x( cookie, op->o_tmpmemctx );
390                         }
391                 }
392         }
393
394         rc = ldap_sync_search( si, op->o_tmpmemctx );
395
396         if( rc != LDAP_SUCCESS ) {
397 #ifdef NEW_LOGGING
398                 LDAP_LOG ( OPERATION, ERR, "do_syncrep1: "
399                         "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
400 #else
401                 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
402                         "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
403 #endif
404         }
405
406 done:
407         if ( rc ) {
408                 if ( si->si_ld ) {
409                         ldap_unbind( si->si_ld );
410                         si->si_ld = NULL;
411                 }
412         }
413
414         return rc;
415 }
416
417 static int
418 do_syncrep2(
419         Operation *op,
420         syncinfo_t *si )
421 {
422         LDAPControl     **rctrls = NULL;
423         LDAPControl     *rctrlp;
424
425         BerElementBuffer berbuf;
426         BerElement      *ber = (BerElement *)&berbuf;
427
428         LDAPMessage     *res = NULL;
429         LDAPMessage     *msg = NULL;
430
431         char            *retoid = NULL;
432         struct berval   *retdata = NULL;
433
434         Entry           *entry = NULL;
435
436         int             syncstate;
437         struct berval   syncUUID = { 0, NULL };
438         struct sync_cookie      syncCookie = { NULL, -1, NULL };
439         struct sync_cookie      syncCookie_req = { NULL, -1, NULL };
440         struct berval           cookie = { 0, NULL };
441
442         int     rc, err, i;
443         ber_len_t       len;
444
445         int rc_efree = 1;
446
447         struct berval   *psub;
448         Modifications   *modlist = NULL;
449
450         const char              *text;
451         int                             match;
452
453         struct timeval *tout_p = NULL;
454         struct timeval tout = { 0, 0 };
455
456         int             refreshDeletes = 0;
457         int             refreshDone = 1;
458         BerVarray syncUUIDs = NULL;
459         ber_tag_t si_tag;
460
461         if ( slapd_shutdown ) {
462                 rc = -2;
463                 goto done;
464         }
465
466         ber_init2( ber, NULL, LBER_USE_DER );
467         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
468
469 #ifdef NEW_LOGGING
470         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrep2\n", 0, 0, 0 );
471 #else
472         Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
473 #endif
474
475         psub = &si->si_be->be_nsuffix[0];
476
477         slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
478
479         if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ){
480                 tout_p = &tout;
481         } else {
482                 tout_p = NULL;
483         }
484
485         while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE, tout_p, &res ))
486                 > 0 )
487         {
488                 if ( slapd_shutdown ) {
489                         rc = -2;
490                         goto done;
491                 }
492                 for( msg = ldap_first_message( si->si_ld, res );
493                   msg != NULL;
494                   msg = ldap_next_message( si->si_ld, msg ) )
495                 {
496                         switch( ldap_msgtype( msg ) ) {
497                         case LDAP_RES_SEARCH_ENTRY:
498                                 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
499                                 /* we can't work without the control */
500                                 if ( !rctrls ) {
501                                         rc = -1;
502                                         goto done;
503                                 }
504                                 rctrlp = *rctrls;
505                                 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
506                                 ber_scanf( ber, "{em", &syncstate, &syncUUID );
507                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
508                                         ber_scanf( ber, "m}", &cookie );
509                                         if ( cookie.bv_val ) {
510                                                 struct berval tmp_bv;
511                                                 ber_dupbv( &tmp_bv, &cookie );
512                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv );
513                                         }
514                                         if ( syncCookie.octet_str &&
515                                                         syncCookie.octet_str[0].bv_val )
516                                                 slap_parse_sync_cookie( &syncCookie );
517                                 }
518                                 if ( syncrepl_message_to_entry( si, op, msg,
519                                         &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
520                                         rc_efree = syncrepl_entry( si, op, entry, modlist,
521                                                                 syncstate, &syncUUID, &syncCookie_req );
522                                         if ( syncCookie.octet_str &&
523                                                  syncCookie.octet_str[0].bv_val ) {
524                                                 syncrepl_updateCookie( si, op, psub, &syncCookie );
525                                         }
526                                 }
527                                 ldap_controls_free( rctrls );
528                                 if ( modlist ) {
529                                         slap_mods_free( modlist );
530                                 }
531                                 if ( rc_efree && entry ) {
532                                         entry_free( entry );
533                                         entry = NULL;
534                                 }
535                                 break;
536
537                         case LDAP_RES_SEARCH_REFERENCE:
538 #ifdef NEW_LOGGING
539                                 LDAP_LOG( OPERATION, ERR,
540                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
541 #else
542                                 Debug( LDAP_DEBUG_ANY,
543                                         "do_syncrep2 : reference received\n", 0, 0, 0 );
544 #endif
545                                 break;
546
547                         case LDAP_RES_SEARCH_RESULT:
548                                 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
549                                         &rctrls, 0 );
550                                 if ( rctrls ) {
551                                         rctrlp = *rctrls;
552                                         ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
553
554                                         ber_scanf( ber, "{" /*"}"*/);
555                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
556                                         {
557                                                 ber_scanf( ber, "m", &cookie );
558                                                 if ( cookie.bv_val ) {
559                                                         struct berval tmp_bv;
560                                                         ber_dupbv( &tmp_bv, &cookie );
561                                                         ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
562                                                 }
563                                                 if ( syncCookie.octet_str &&
564                                                                  syncCookie.octet_str[0].bv_val )
565                                                         slap_parse_sync_cookie( &syncCookie );
566                                         }
567                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
568                                         {
569                                                 ber_scanf( ber, "b", &refreshDeletes );
570                                         }
571                                         ber_scanf( ber, "}" );
572                                 }
573                                 if ( syncCookie_req.ctxcsn == NULL ) {
574                                         match = -1;
575                                 } else if ( syncCookie.ctxcsn == NULL ) {
576                                         match = 1;
577                                 } else {
578                                         value_match( &match, slap_schema.si_ad_entryCSN,
579                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
580                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
581                                                 &syncCookie_req.ctxcsn[0], &syncCookie.ctxcsn[0], &text );
582                                 }
583                                 if ( syncCookie.octet_str && syncCookie.octet_str->bv_val
584                                          && match < 0 && err == LDAP_SUCCESS ) {
585                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
586                                 }
587                                 if ( rctrls ) {
588                                         ldap_controls_free( rctrls );
589                                 }
590                                 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
591                                         /* FIXME : different error behaviors according to
592                                          *      1) err code : LDAP_BUSY ...
593                                          *      2) on err policy : stop service, stop sync, retry
594                                          */
595                                         if ( refreshDeletes == 0 && match < 0 &&
596                                                  err == LDAP_SUCCESS ) {
597                                                 syncrepl_del_nonpresent( op, si );
598                                         } else {
599                                                 avl_free( si->si_presentlist, avl_ber_bvfree );
600                                                 si->si_presentlist = NULL;
601                                         }
602                                 }
603                                 rc = -2;
604                                 goto done;
605                                 break;
606
607                         case LDAP_RES_INTERMEDIATE:
608                                 rc = ldap_parse_intermediate( si->si_ld, msg,
609                                         &retoid, &retdata, NULL, 0 );
610                                 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
611                                         int             si_refreshDelete = 0;
612                                         int             si_refreshPresent = 0;
613                                         ber_init2( ber, retdata, LBER_USE_DER );
614
615                                         switch ( si_tag = ber_peek_tag( ber, &len )) {
616                                         ber_tag_t tag;
617                                         case LDAP_TAG_SYNC_NEW_COOKIE:
618                                                 ber_scanf( ber, "tm", &tag, &cookie );
619                                                 break;
620                                         case LDAP_TAG_SYNC_REFRESH_DELETE:
621                                                 si_refreshDelete = 1;
622                                         case LDAP_TAG_SYNC_REFRESH_PRESENT:
623                                                 si_refreshPresent = 1;
624                                                 ber_scanf( ber, "t{", &tag );
625                                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
626                                                 {
627                                                         ber_scanf( ber, "m", &cookie );
628                                                         if ( cookie.bv_val ) {
629                                                                 struct berval tmp_bv;
630                                                                 ber_dupbv( &tmp_bv, &cookie );
631                                                                 ber_bvarray_add( &syncCookie.octet_str, &tmp_bv);
632                                                         }
633                                                         if ( syncCookie.octet_str &&
634                                                                          syncCookie.octet_str[0].bv_val )
635                                                                 slap_parse_sync_cookie( &syncCookie );
636                                                 }
637                                                 if ( ber_peek_tag( ber, &len ) ==
638                                                                         LDAP_TAG_REFRESHDONE )
639                                                 {
640                                                         ber_scanf( ber, "b", &refreshDone );
641                                                 }
642                                                 ber_scanf( ber, "}" );
643                                                 break;
644                                         case LDAP_TAG_SYNC_ID_SET:
645                                                 ber_scanf( ber, "t{", &tag );
646                                                 if ( ber_peek_tag( ber, &len ) ==
647                                                                 LDAP_TAG_SYNC_COOKIE ) {
648                                                         ber_scanf( ber, "m", &cookie );
649                                                         if ( cookie.bv_val ) {
650                                                                 struct berval tmp_bv;
651                                                                 ber_dupbv( &tmp_bv, &cookie );
652                                                                 ber_bvarray_add( &syncCookie.octet_str,
653                                                                                                  &tmp_bv );
654                                                         }
655                                                         if ( syncCookie.octet_str &&
656                                                                          syncCookie.octet_str[0].bv_val )
657                                                                 slap_parse_sync_cookie( &syncCookie );
658                                                 }
659                                                 if ( ber_peek_tag( ber, &len ) ==
660                                                                         LDAP_TAG_REFRESHDELETES )
661                                                 {
662                                                         ber_scanf( ber, "b", &refreshDeletes );
663                                                 }
664                                                 ber_scanf( ber, "[W]", &syncUUIDs );
665                                                 ber_scanf( ber, "}" );
666                                                 for ( i = 0; syncUUIDs[i].bv_val; i++ ) {
667                                                         struct berval *syncuuid_bv;
668                                                         syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
669                                                         avl_insert( &si->si_presentlist,
670                                                                         (caddr_t) syncuuid_bv,
671                                                                         syncuuid_cmp, avl_dup_error );
672                                                 }
673                                                 ber_memfree_x( syncUUIDs, op->o_tmpmemctx );
674                                                 break;
675                                         default:
676 #ifdef NEW_LOGGING
677                                         LDAP_LOG( OPERATION, ERR,
678                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
679                                                 (long) si_tag, 0, 0 );
680 #else
681                                         Debug( LDAP_DEBUG_ANY,
682                                                 "do_syncrep2 : unknown syncinfo tag (%ld)\n",
683                                                 (long) si_tag, 0, 0 );
684 #endif
685                                                 ldap_memfree( retoid );
686                                                 ber_bvfree( retdata );
687                                                 continue;
688                                         }
689
690                                         if ( syncCookie_req.ctxcsn == NULL ) {
691                                                 match = -1;
692                                         } else if ( syncCookie.ctxcsn == NULL ) {
693                                                 match = 1;
694                                         } else {
695                                                 value_match( &match, slap_schema.si_ad_entryCSN,
696                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
697                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
698                                                         &syncCookie_req.ctxcsn[0],
699                                                         &syncCookie.ctxcsn[0], &text );
700                                         }
701
702                                         if ( syncCookie.ctxcsn && syncCookie.ctxcsn[0].bv_val
703                                                  && match < 0 ) {
704                                                 syncrepl_updateCookie( si, op, psub, &syncCookie);
705                                         }
706
707                                         if ( si_refreshPresent == 1 ) {
708                                                 if ( match < 0 ) {
709                                                         syncrepl_del_nonpresent( op, si );
710                                                 }
711                                         } 
712
713                                         ldap_memfree( retoid );
714                                         ber_bvfree( retdata );
715                                         break;
716                                 } else {
717 #ifdef NEW_LOGGING
718                                         LDAP_LOG( OPERATION, ERR,"do_syncrep2 :"
719                                                 " unknown intermediate "
720                                                 "response\n", 0, 0, 0 );
721 #else
722                                         Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
723                                                 "unknown intermediate response (%d)\n",
724                                                 rc, 0, 0 );
725 #endif
726                                         ldap_memfree( retoid );
727                                         ber_bvfree( retdata );
728                                         break;
729                                 }
730                                 break;
731                         default:
732 #ifdef NEW_LOGGING
733                                 LDAP_LOG( OPERATION, ERR, "do_syncrep2 : "
734                                         "unknown message\n", 0, 0, 0 );
735 #else
736                                 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
737                                         "unknown message\n", 0, 0, 0 );
738 #endif
739                                 break;
740
741                         }
742                         if ( syncCookie.octet_str ) {
743                                 slap_sync_cookie_free( &syncCookie_req, 0 );
744                                 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
745                                 slap_sync_cookie_free( &syncCookie, 0 );
746                         }
747                 }
748                 ldap_msgfree( res );
749                 res = NULL;
750         }
751
752         if ( rc == -1 ) {
753                 const char *errstr;
754
755                 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
756                 errstr = ldap_err2string( rc );
757                 
758 #ifdef NEW_LOGGING
759                 LDAP_LOG( OPERATION, ERR,
760                         "do_syncrep2 : %s\n", errstr, 0, 0 );
761 #else
762                 Debug( LDAP_DEBUG_ANY,
763                         "do_syncrep2 : %s\n", errstr, 0, 0 );
764 #endif
765         }
766
767 done:
768         slap_sync_cookie_free( &syncCookie, 0 );
769         slap_sync_cookie_free( &syncCookie_req, 0 );
770
771         if ( res ) ldap_msgfree( res );
772
773         if ( rc && si->si_ld ) {
774                 ldap_unbind( si->si_ld );
775                 si->si_ld = NULL;
776         }
777
778         return rc;
779 }
780
781 void *
782 do_syncrepl(
783         void    *ctx,
784         void    *arg )
785 {
786         struct re_s* rtask = arg;
787         syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
788         Connection conn = {0};
789         Operation op = {0};
790         int rc = LDAP_SUCCESS;
791         int first = 0;
792         int dostop = 0;
793         ber_socket_t s;
794
795 #ifdef NEW_LOGGING
796         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
797 #else
798         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
799 #endif
800
801         if ( si == NULL )
802                 return NULL;
803
804         switch( abs( si->si_type )) {
805         case LDAP_SYNC_REFRESH_ONLY:
806         case LDAP_SYNC_REFRESH_AND_PERSIST:
807                 break;
808         default:
809                 return NULL;
810         }
811
812         if ( slapd_shutdown && si->si_ld ) {
813                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
814                 connection_client_stop( s );
815                 ldap_unbind( si->si_ld );
816                 si->si_ld = NULL;
817                 return NULL;
818         }
819
820         connection_fake_init( &conn, &op, ctx );
821
822         op.o_dn = si->si_updatedn;
823         op.o_ndn = si->si_updatedn;
824         op.o_managedsait = 1;
825         op.o_bd = si->si_be;
826
827         op.o_sync_state.ctxcsn = NULL;
828         op.o_sync_state.sid = -1;
829         op.o_sync_state.octet_str = NULL;
830         op.o_sync_slog_size = -1;
831         LDAP_STAILQ_FIRST( &op.o_sync_slog_list ) = NULL;
832         op.o_sync_slog_list.stqh_last = &LDAP_STAILQ_FIRST(&op.o_sync_slog_list);
833
834         /* Establish session, do search */
835         if ( !si->si_ld ) {
836                 first = 1;
837                 rc = do_syncrep1( &op, si );
838         }
839
840         /* Process results */
841         if ( rc == LDAP_SUCCESS ) {
842                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
843
844                 rc = do_syncrep2( &op, si );
845
846                 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
847                         /* If we succeeded, enable the connection for further listening.
848                          * If we failed, tear down the connection and reschedule.
849                          */
850                         if ( rc == LDAP_SUCCESS ) {
851                                 if ( first ) {
852                                         rc = connection_client_setup( s, do_syncrepl,
853                                                 arg );
854                                 } else {
855                                         connection_client_enable( s );
856                                 }
857                         } else if ( !first ) {
858                                 dostop = 1;
859                         }
860                 } else {
861                         if ( rc == -2 ) rc = 0;
862                 }
863         }
864
865         /* At this point, we have 4 cases:
866          * 1) for any hard failure, give up and remove this task
867          * 2) for ServerDown, reschedule this task to run
868          * 3) for Refresh and Success, reschedule to run
869          * 4) for Persist and Success, reschedule to defer
870          */
871         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
872         if ( ldap_pvt_runqueue_isrunning( &syncrepl_rq, rtask )) {
873                 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
874         }
875
876         if ( dostop ) {
877                 connection_client_stop( s );
878         }
879
880         if ( rc && rc != LDAP_SERVER_DOWN ) {
881                 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
882         } else {
883                 if ( rc == LDAP_SERVER_DOWN ||
884                         si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
885                         rc = 0;
886                 } else {
887                         rc = 1;
888                 }
889                 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask, rc );
890         }
891         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
892
893         return NULL;
894 }
895
896 int
897 syncrepl_message_to_entry(
898         syncinfo_t      *si,
899         Operation       *op,
900         LDAPMessage     *msg,
901         Modifications   **modlist,
902         Entry                   **entry,
903         int             syncstate
904 )
905 {
906         Entry           *e = NULL;
907         BerElement      *ber = NULL;
908         Modifications   tmp;
909         Modifications   *mod;
910         Modifications   **modtail = modlist;
911
912         const char      *text;
913         char txtbuf[SLAP_TEXT_BUFLEN];
914         size_t textlen = sizeof txtbuf;
915
916         struct berval   bdn = {0, NULL}, dn, ndn;
917         int             rc;
918
919         *modlist = NULL;
920
921         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
922 #ifdef NEW_LOGGING
923                 LDAP_LOG( OPERATION, ERR,
924                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
925 #else
926                 Debug( LDAP_DEBUG_ANY,
927                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
928 #endif
929                 return -1;
930         }
931
932         op->o_tag = LDAP_REQ_ADD;
933
934         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
935
936         if ( rc != LDAP_SUCCESS ) {
937 #ifdef NEW_LOGGING
938                 LDAP_LOG( OPERATION, ERR,
939                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
940 #else
941                 Debug( LDAP_DEBUG_ANY,
942                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
943 #endif
944                 return rc;
945         }
946
947         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
948         ber_dupbv( &op->o_req_dn, &dn );
949         ber_dupbv( &op->o_req_ndn, &ndn );
950         sl_free( ndn.bv_val, op->o_tmpmemctx );
951         sl_free( dn.bv_val, op->o_tmpmemctx );
952
953         if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE )
954         {
955                 return LDAP_SUCCESS;
956         }
957
958         if ( entry == NULL ) {
959                 return -1;
960         }
961
962         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
963         *entry = e;
964         e->e_name = op->o_req_dn;
965         e->e_nname = op->o_req_ndn;
966
967         while ( ber_remaining( ber ) ) {
968                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
969                         LBER_ERROR ) || ( tmp.sml_type.bv_val == NULL ))
970                 {
971                         break;
972                 }
973
974                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
975
976                 mod->sml_op = LDAP_MOD_REPLACE;
977                 mod->sml_next = NULL;
978                 mod->sml_desc = NULL;
979                 mod->sml_type = tmp.sml_type;
980                 mod->sml_bvalues = tmp.sml_bvalues;
981                 mod->sml_nvalues = NULL;
982
983                 *modtail = mod;
984                 modtail = &mod->sml_next;
985         }
986
987         if ( *modlist == NULL ) {
988 #ifdef NEW_LOGGING
989                 LDAP_LOG( OPERATION, ERR,
990                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
991 #else
992                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
993                                 0, 0, 0 );
994 #endif
995         }
996
997         rc = slap_mods_check( *modlist, 1, &text, txtbuf, textlen, NULL );
998
999         if ( rc != LDAP_SUCCESS ) {
1000 #ifdef NEW_LOGGING
1001                 LDAP_LOG( OPERATION, ERR,
1002                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
1003 #else
1004                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1005                                 text, 0, 0 );
1006 #endif
1007                 goto done;
1008         }
1009         
1010         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1011         if( rc != LDAP_SUCCESS ) {
1012 #ifdef NEW_LOGGING
1013                 LDAP_LOG( OPERATION, ERR,
1014                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
1015 #else
1016                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1017                                 text, 0, 0 );
1018 #endif
1019         }
1020
1021 done:
1022         ber_free ( ber, 0 );
1023         if ( rc != LDAP_SUCCESS ) {
1024                 entry_free( e );
1025                 e = NULL;
1026         }
1027
1028         return rc;
1029 }
1030
1031 int
1032 syncrepl_entry(
1033         syncinfo_t* si,
1034         Operation *op,
1035         Entry* e,
1036         Modifications* modlist,
1037         int syncstate,
1038         struct berval* syncUUID,
1039         struct sync_cookie* syncCookie_req
1040 )
1041 {
1042         Backend *be = op->o_bd;
1043         slap_callback   cb = { NULL };
1044         struct berval   *syncuuid_bv = NULL;
1045         struct berval   syncUUID_strrep = { 0, NULL };
1046         struct berval   uuid_bv = { 0, NULL };
1047
1048         SlapReply       rs = {REP_RESULT};
1049         Filter f = {0};
1050         AttributeAssertion ava = {0};
1051         int rc = LDAP_SUCCESS;
1052         int ret = LDAP_SUCCESS;
1053         const char *text;
1054
1055         struct berval pdn = { 0, NULL };
1056         struct berval org_req_dn = { 0, NULL };
1057         struct berval org_req_ndn = { 0, NULL };
1058         struct berval org_dn = { 0, NULL };
1059         struct berval org_ndn = { 0, NULL };
1060         int     org_managedsait;
1061
1062         if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD ))
1063         {
1064                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
1065                 avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1066                         syncuuid_cmp, avl_dup_error );
1067         }
1068
1069         if ( syncstate == LDAP_SYNC_PRESENT ) {
1070                 return e ? 1 : 0;
1071         }
1072
1073         f.f_choice = LDAP_FILTER_EQUALITY;
1074         f.f_ava = &ava;
1075         ava.aa_desc = slap_schema.si_ad_entryUUID;
1076         slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1077         ava.aa_value = *syncUUID;
1078         op->ors_filter = &f;
1079
1080         op->ors_filterstr.bv_len = (sizeof("entryUUID=")-1) + syncUUID->bv_len;
1081         op->ors_filterstr.bv_val = (char *) sl_malloc(
1082                 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx ); 
1083         AC_MEMCPY( op->ors_filterstr.bv_val, "entryUUID=", sizeof("entryUUID=")-1 );
1084         AC_MEMCPY( &op->ors_filterstr.bv_val[sizeof("entryUUID=")-1],
1085                 syncUUID->bv_val, syncUUID->bv_len );
1086         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1087
1088         op->o_tag = LDAP_REQ_SEARCH;
1089         op->ors_scope = LDAP_SCOPE_SUBTREE;
1090
1091         /* get syncrepl cookie of shadow replica from subentry */
1092         op->o_req_dn = si->si_base;
1093         op->o_req_ndn = si->si_base;
1094
1095         /* set callback function */
1096         op->o_callback = &cb;
1097         cb.sc_response = dn_callback;
1098         cb.sc_private = si;
1099
1100         si->si_syncUUID_ndn.bv_val = NULL;
1101
1102         if ( limits_check( op, &rs ) == 0 ) {
1103                 rc = be->be_search( op, &rs );
1104         }
1105
1106         if ( op->ors_filterstr.bv_val ) {
1107                 sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1108         }
1109
1110         cb.sc_response = null_callback;
1111         cb.sc_private = si;
1112
1113         if ( rc == LDAP_SUCCESS && si->si_syncUUID_ndn.bv_val )
1114         {
1115                 char *subseq_ptr;
1116
1117                 if ( syncstate != LDAP_SYNC_DELETE ) {
1118                         op->o_no_psearch = 1;
1119                 }
1120
1121                 ber_dupbv( &op->o_sync_csn, syncCookie_req->ctxcsn );
1122                 if ( op->o_sync_csn.bv_val ) {
1123                         subseq_ptr = strstr( op->o_sync_csn.bv_val, "#0000" );
1124                         subseq_ptr += 4;
1125                         *subseq_ptr = '1';
1126                 }
1127                 
1128                 op->o_req_dn = si->si_syncUUID_ndn;
1129                 op->o_req_ndn = si->si_syncUUID_ndn;
1130                 op->o_tag = LDAP_REQ_DELETE;
1131                 rc = be->be_delete( op, &rs );
1132
1133                 org_req_dn = op->o_req_dn;
1134                 org_req_ndn = op->o_req_ndn;
1135                 org_dn = op->o_dn;
1136                 org_ndn = op->o_ndn;
1137                 org_managedsait = get_manageDSAit( op );
1138                 op->o_dn = op->o_bd->be_rootdn;
1139                 op->o_ndn = op->o_bd->be_rootndn;
1140                 op->o_managedsait = 1;
1141
1142                 while ( rs.sr_err == LDAP_SUCCESS &&
1143                                 op->o_delete_glue_parent ) {
1144                         op->o_delete_glue_parent = 0;
1145                         if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1146                                 slap_callback cb = { NULL };
1147                                 cb.sc_response = slap_null_cb;
1148                                 dnParent( &op->o_req_ndn, &pdn );
1149                                 op->o_req_dn = pdn;
1150                                 op->o_req_ndn = pdn;
1151                                 op->o_callback = &cb;
1152                                 op->o_bd->be_delete( op, &rs );
1153                         } else {
1154                                 break;
1155                     }
1156                 }
1157
1158                 op->o_managedsait = org_managedsait;
1159                 op->o_dn = org_dn;
1160                 op->o_ndn = org_ndn;
1161                 op->o_req_dn = org_req_dn;
1162                 op->o_req_ndn = org_req_ndn;
1163                 op->o_delete_glue_parent = 0;
1164
1165                 op->o_no_psearch = 0;
1166         }
1167
1168         switch ( syncstate ) {
1169         case LDAP_SYNC_ADD:
1170         case LDAP_SYNC_MODIFY:
1171                 if ( rc == LDAP_SUCCESS ||
1172                          rc == LDAP_REFERRAL ||
1173                          rc == LDAP_NO_SUCH_OBJECT ||
1174                          rc == LDAP_NOT_ALLOWED_ON_NONLEAF )
1175                 {
1176                         attr_delete( &e->e_attrs, slap_schema.si_ad_entryUUID );
1177                         attr_merge_one( e, slap_schema.si_ad_entryUUID,
1178                                 syncUUID, &ava.aa_value );
1179
1180                         op->o_tag = LDAP_REQ_ADD;
1181                         op->ora_e = e;
1182                         op->o_req_dn = e->e_name;
1183                         op->o_req_ndn = e->e_nname;
1184                         rc = be->be_add( op, &rs );
1185
1186                         if ( rc != LDAP_SUCCESS ) {
1187                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1188                                         Modifications *mod;
1189                                         Modifications *modtail;
1190
1191                                         for ( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1192                                                 modtail = mod;
1193                                         }
1194
1195                                         mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1196                                         ber_dupbv( &uuid_bv, syncUUID );
1197                                         mod->sml_op = LDAP_MOD_REPLACE;
1198                                         mod->sml_desc = slap_schema.si_ad_entryUUID;
1199                                         mod->sml_type = mod->sml_desc->ad_cname;
1200                                         ber_bvarray_add( &mod->sml_bvalues, &uuid_bv );
1201                                         modtail->sml_next = mod;
1202                                         
1203                                         op->o_tag = LDAP_REQ_MODIFY;
1204                                         op->orm_modlist = modlist;
1205                                         op->o_req_dn = e->e_name;
1206                                         op->o_req_ndn = e->e_nname;
1207
1208                                         rc = be->be_modify( op, &rs );
1209                                         if ( rc != LDAP_SUCCESS ) {
1210 #ifdef NEW_LOGGING
1211                                                 LDAP_LOG( OPERATION, ERR,
1212                                                         "syncrepl_entry : be_modify failed (%d)\n",
1213                                                         rc, 0, 0 );
1214 #else
1215                                                 Debug( LDAP_DEBUG_ANY,
1216                                                         "syncrepl_entry : be_modify failed (%d)\n",
1217                                                         rc, 0, 0 );
1218 #endif
1219                                         }
1220                                         ret = 1;
1221                                         goto done;
1222                                 } else if ( rc == LDAP_REFERRAL || rc == LDAP_NO_SUCH_OBJECT ) {
1223                                         syncrepl_add_glue( op, e );
1224                                         ret = 0;
1225                                         goto done;
1226                                 } else {
1227 #ifdef NEW_LOGGING
1228                                         LDAP_LOG( OPERATION, ERR,
1229                                                 "syncrepl_entry : be_add failed (%d)\n",
1230                                                 rc, 0, 0 );
1231 #else
1232                                         Debug( LDAP_DEBUG_ANY,
1233                                                 "syncrepl_entry : be_add failed (%d)\n",
1234                                                 rc, 0, 0 );
1235 #endif
1236                                         ret = 1;
1237                                         goto done;
1238                                 }
1239                         } else {
1240                                 be_entry_release_w( op, e );
1241                                 ret = 0;
1242                                 goto done;
1243                         }
1244                 } else {
1245 #ifdef NEW_LOGGING
1246                         LDAP_LOG( OPERATION, ERR,
1247                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1248 #else
1249                         Debug( LDAP_DEBUG_ANY,
1250                                 "syncrepl_entry : be_search failed (%d)\n", rc, 0, 0 );
1251 #endif
1252                         ret = 1;
1253                         goto done;
1254                 }
1255
1256         case LDAP_SYNC_DELETE :
1257                 /* Already deleted */
1258                 ret = 0;
1259                 goto done;
1260
1261         default :
1262 #ifdef NEW_LOGGING
1263                 LDAP_LOG( OPERATION, ERR,
1264                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1265 #else
1266                 Debug( LDAP_DEBUG_ANY,
1267                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1268 #endif
1269                 ret = 1;
1270                 goto done;
1271         }
1272
1273 done :
1274
1275         if ( syncUUID_strrep.bv_val ) {
1276                 ber_memfree_x( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1277         }
1278         if ( si->si_syncUUID_ndn.bv_val ) {
1279                 ber_memfree_x( si->si_syncUUID_ndn.bv_val, op->o_tmpmemctx );
1280         }
1281         return ret;
1282 }
1283
1284 static struct berval gcbva[] = {
1285         BER_BVC("top"),
1286         BER_BVC("glue"),
1287         BER_BVNULL
1288 };
1289
1290 static void
1291 syncrepl_del_nonpresent(
1292         Operation *op,
1293         syncinfo_t *si
1294 )
1295 {
1296         Backend* be = op->o_bd;
1297         slap_callback   cb = { NULL };
1298         SlapReply       rs = {REP_RESULT};
1299         struct nonpresent_entry *np_list, *np_prev;
1300         int rc;
1301         Modifications *ml;
1302         Modifications *mlnext;
1303         Modifications *mod;
1304         Modifications *modlist = NULL;
1305         Modifications **modtail = &modlist;
1306         Attribute       *attr;
1307
1308         struct berval pdn = { 0, NULL };
1309         struct berval org_req_dn = { 0, NULL };
1310         struct berval org_req_ndn = { 0, NULL };
1311         struct berval org_dn = { 0, NULL };
1312         struct berval org_ndn = { 0, NULL };
1313         int     org_managedsait;
1314
1315         op->o_req_dn = si->si_base;
1316         op->o_req_ndn = si->si_base;
1317
1318         cb.sc_response = nonpresent_callback;
1319         cb.sc_private = si;
1320
1321         op->o_callback = &cb;
1322         op->o_tag = LDAP_REQ_SEARCH;
1323         op->ors_scope = si->si_scope;
1324         op->ors_deref = LDAP_DEREF_NEVER;
1325         op->ors_slimit = 0;
1326         op->ors_tlimit = 0;
1327         op->ors_attrsonly = 0;
1328         op->ors_attrs = NULL;
1329         op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1330         op->ors_filterstr = si->si_filterstr;
1331
1332         op->o_nocaching = 1;
1333         op->o_managedsait = 0;
1334
1335         if ( limits_check( op, &rs ) == 0 ) {
1336                 be->be_search( op, &rs );
1337         }
1338
1339         op->o_managedsait = 1;
1340         op->o_nocaching = 0;
1341
1342         if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
1343
1344         if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
1345                 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
1346                 while ( np_list != NULL ) {
1347                         LDAP_LIST_REMOVE( np_list, npe_link );
1348                         np_prev = np_list;
1349                         np_list = LDAP_LIST_NEXT( np_list, npe_link );
1350                         op->o_tag = LDAP_REQ_DELETE;
1351                         op->o_callback = &cb;
1352                         cb.sc_response = null_callback;
1353                         cb.sc_private = si;
1354                         op->o_req_dn = *np_prev->npe_name;
1355                         op->o_req_ndn = *np_prev->npe_nname;
1356                         rc = op->o_bd->be_delete( op, &rs );
1357
1358                         if ( rc == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
1359                                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1360                                 mod->sml_op = LDAP_MOD_REPLACE;
1361                                 mod->sml_desc = slap_schema.si_ad_objectClass;
1362                                 mod->sml_type = mod->sml_desc->ad_cname;
1363                                 mod->sml_bvalues = &gcbva[0];
1364                                 *modtail = mod;
1365                                 modtail = &mod->sml_next;
1366
1367                                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1368                                 mod->sml_op = LDAP_MOD_REPLACE;
1369                                 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1370                                 mod->sml_type = mod->sml_desc->ad_cname;
1371                                 mod->sml_bvalues = &gcbva[1];
1372                                 *modtail = mod;
1373                                 modtail = &mod->sml_next;
1374
1375                                 op->o_tag = LDAP_REQ_MODIFY;
1376                                 op->orm_modlist = modlist;
1377
1378                                 rc = be->be_modify( op, &rs );
1379
1380                                 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1381                                         mlnext = ml->sml_next;
1382                                         free( ml );
1383                                 }
1384                         }
1385
1386                         org_req_dn = op->o_req_dn;
1387                         org_req_ndn = op->o_req_ndn;
1388                         org_dn = op->o_dn;
1389                         org_ndn = op->o_ndn;
1390                         org_managedsait = get_manageDSAit( op );
1391                         op->o_dn = op->o_bd->be_rootdn;
1392                         op->o_ndn = op->o_bd->be_rootndn;
1393                         op->o_managedsait = 1;
1394
1395                         while ( rs.sr_err == LDAP_SUCCESS &&
1396                                         op->o_delete_glue_parent ) {
1397                                 op->o_delete_glue_parent = 0;
1398                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1399                                         slap_callback cb = { NULL };
1400                                         cb.sc_response = slap_null_cb;
1401                                         dnParent( &op->o_req_ndn, &pdn );
1402                                         op->o_req_dn = pdn;
1403                                         op->o_req_ndn = pdn;
1404                                         op->o_callback = &cb;
1405                                         /* give it a root privil ? */
1406                                         op->o_bd->be_delete( op, &rs );
1407                                 } else {
1408                                         break;
1409                             }
1410                         }
1411
1412                         op->o_managedsait = org_managedsait;
1413                         op->o_dn = org_dn;
1414                         op->o_ndn = org_ndn;
1415                         op->o_req_dn = org_req_dn;
1416                         op->o_req_ndn = org_req_ndn;
1417                         op->o_delete_glue_parent = 0;
1418
1419                         ber_bvfree( np_prev->npe_name );
1420                         ber_bvfree( np_prev->npe_nname );
1421                         op->o_req_dn.bv_val = NULL;
1422                         op->o_req_ndn.bv_val = NULL;
1423                         ch_free( np_prev );
1424                 }
1425         }
1426
1427         return;
1428 }
1429
1430
1431 void
1432 syncrepl_add_glue(
1433         Operation* op,
1434         Entry *e
1435 )
1436 {
1437         Backend *be = op->o_bd;
1438         slap_callback cb = { NULL };
1439         Attribute       *a;
1440         int     rc;
1441         int suffrdns;
1442         int i;
1443         struct berval dn = {0, NULL};
1444         struct berval ndn = {0, NULL};
1445         Entry   *glue;
1446         SlapReply       rs = {REP_RESULT};
1447         char    *ptr, *comma;
1448
1449         op->o_tag = LDAP_REQ_ADD;
1450         op->o_callback = &cb;
1451         cb.sc_response = null_callback;
1452         cb.sc_private = NULL;
1453
1454         dn = e->e_name;
1455         ndn = e->e_nname;
1456
1457         /* count RDNs in suffix */
1458         if ( be->be_nsuffix[0].bv_len ) {
1459                 for (i=0, ptr=be->be_nsuffix[0].bv_val; ptr; ptr=strchr( ptr, ',' )) {
1460                         ptr++;
1461                         i++;
1462                 }
1463                 suffrdns = i;
1464         } else {
1465                 /* suffix is "" */
1466                 suffrdns = 0;
1467         }
1468
1469         /* Start with BE suffix */
1470         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
1471                 comma = strrchr(dn.bv_val, ',');
1472                 if ( ptr ) *ptr = ',';
1473                 if ( comma ) *comma = '\0';
1474                 ptr = comma;
1475         }
1476         if ( ptr ) {
1477                 *ptr++ = ',';
1478                 dn.bv_len -= ptr - dn.bv_val;
1479                 dn.bv_val = ptr;
1480         }
1481         /* the normalizedDNs are always the same length, no counting
1482          * required.
1483          */
1484         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
1485                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
1486                 ndn.bv_len = be->be_nsuffix[0].bv_len;
1487         }
1488
1489         while ( ndn.bv_val > e->e_nname.bv_val ) {
1490                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
1491                 ber_dupbv( &glue->e_name, &dn );
1492                 ber_dupbv( &glue->e_nname, &ndn );
1493
1494                 a = ch_calloc( 1, sizeof( Attribute ));
1495                 a->a_desc = slap_schema.si_ad_objectClass;
1496
1497                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1498                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
1499                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
1500                 ber_dupbv( &a->a_vals[2], &gcbva[2] );
1501
1502                 a->a_nvals = a->a_vals;
1503
1504                 a->a_next = glue->e_attrs;
1505                 glue->e_attrs = a;
1506
1507                 a = ch_calloc( 1, sizeof( Attribute ));
1508                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1509
1510                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1511                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
1512                 ber_dupbv( &a->a_vals[1], &gcbva[2] );
1513
1514                 a->a_nvals = a->a_vals;
1515
1516                 a->a_next = glue->e_attrs;
1517                 glue->e_attrs = a;
1518
1519                 op->o_req_dn = glue->e_name;
1520                 op->o_req_ndn = glue->e_nname;
1521                 op->ora_e = glue;
1522                 rc = be->be_add ( op, &rs );
1523                 if ( rc == LDAP_SUCCESS ) {
1524                         be_entry_release_w( op, glue );
1525                 } else {
1526                 /* incl. ALREADY EXIST */
1527                         entry_free( glue );
1528                 }
1529
1530                 /* Move to next child */
1531                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
1532                         /* empty */
1533                 }
1534                 if ( ptr == e->e_name.bv_val ) break;
1535                 dn.bv_val = ++ptr;
1536                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
1537                 for( ptr = ndn.bv_val-2;
1538                         ptr > e->e_nname.bv_val && *ptr != ',';
1539                         ptr--)
1540                 {
1541                         /* empty */
1542                 }
1543                 ndn.bv_val = ++ptr;
1544                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
1545         }
1546
1547         op->o_req_dn = e->e_name;
1548         op->o_req_ndn = e->e_nname;
1549         op->ora_e = e;
1550         rc = be->be_add ( op, &rs );
1551         if ( rc == LDAP_SUCCESS ) {
1552                 be_entry_release_w( op, e );
1553         } else {
1554                 entry_free( e );
1555         }
1556
1557         return;
1558 }
1559
1560 static struct berval ocbva[] = {
1561         BER_BVC("top"),
1562         BER_BVC("subentry"),
1563         BER_BVC("syncConsumerSubentry"),
1564         BER_BVNULL
1565 };
1566
1567 static struct berval cnbva[] = {
1568         BER_BVNULL,
1569         BER_BVNULL
1570 };
1571
1572 static struct berval ssbva[] = {
1573         BER_BVC("{}"),
1574         BER_BVNULL
1575 };
1576
1577 static struct berval scbva[] = {
1578         BER_BVNULL,
1579         BER_BVNULL
1580 };
1581
1582 void
1583 syncrepl_updateCookie(
1584         syncinfo_t *si,
1585         Operation *op,
1586         struct berval *pdn,
1587         struct sync_cookie *syncCookie
1588 )
1589 {
1590         Backend *be = op->o_bd;
1591         Modifications *ml;
1592         Modifications *mlnext;
1593         Modifications *mod;
1594         Modifications *modlist = NULL;
1595         Modifications **modtail = &modlist;
1596
1597         const char      *text;
1598         char txtbuf[SLAP_TEXT_BUFLEN];
1599         size_t textlen = sizeof txtbuf;
1600
1601         Entry* e = NULL;
1602         int rc;
1603
1604         char syncrepl_cbuf[sizeof(CN_STR SYNCREPL_STR)];
1605         struct berval slap_syncrepl_dn_bv = BER_BVNULL;
1606         struct berval slap_syncrepl_cn_bv = BER_BVNULL;
1607         
1608         slap_callback cb = { NULL };
1609         SlapReply       rs = {REP_RESULT};
1610
1611         slap_sync_cookie_free( &si->si_syncCookie, 0 );
1612         slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
1613
1614         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1615         mod->sml_op = LDAP_MOD_REPLACE;
1616         mod->sml_desc = slap_schema.si_ad_objectClass;
1617         mod->sml_type = mod->sml_desc->ad_cname;
1618         mod->sml_bvalues = ocbva;
1619         *modtail = mod;
1620         modtail = &mod->sml_next;
1621
1622         ber_dupbv( &cnbva[0], (struct berval *) &slap_syncrepl_bvc );
1623         assert( si->si_rid < 1000 );
1624         cnbva[0].bv_len = snprintf( cnbva[0].bv_val,
1625                 slap_syncrepl_bvc.bv_len + 1,
1626                 "syncrepl%ld", si->si_rid );
1627         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1628         mod->sml_op = LDAP_MOD_REPLACE;
1629         mod->sml_desc = slap_schema.si_ad_cn;
1630         mod->sml_type = mod->sml_desc->ad_cname;
1631         mod->sml_bvalues = cnbva;
1632         *modtail = mod;
1633         modtail = &mod->sml_next;
1634
1635         if ( scbva[0].bv_val ) ch_free( scbva[0].bv_val );
1636         ber_dupbv( &scbva[0], &si->si_syncCookie.octet_str[0] );
1637         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1638         mod->sml_op = LDAP_MOD_REPLACE;
1639         mod->sml_desc = slap_schema.si_ad_syncreplCookie;
1640         mod->sml_type = mod->sml_desc->ad_cname;
1641         mod->sml_bvalues = scbva;
1642         *modtail = mod;
1643         modtail = &mod->sml_next;
1644
1645         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ));
1646         mod->sml_op = LDAP_MOD_REPLACE;
1647         mod->sml_desc = slap_schema.si_ad_subtreeSpecification;
1648         mod->sml_type = mod->sml_desc->ad_cname;
1649         mod->sml_bvalues = ssbva;
1650         *modtail = mod;
1651         modtail = &mod->sml_next;
1652
1653         mlnext = mod;
1654
1655         op->o_tag = LDAP_REQ_ADD;
1656         rc = slap_mods_opattrs( op, modlist, modtail,
1657                                                          &text,txtbuf, textlen );
1658
1659         for ( ml = modlist; ml != NULL; ml = ml->sml_next ) {
1660                 ml->sml_op = LDAP_MOD_REPLACE;
1661         }
1662
1663         if( rc != LDAP_SUCCESS ) {
1664 #ifdef NEW_LOGGING
1665                 LDAP_LOG( OPERATION, ERR,
1666                         "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1667 #else
1668                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1669                          text, 0, 0 );
1670 #endif
1671         }
1672
1673         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1674
1675         slap_syncrepl_cn_bv.bv_val = syncrepl_cbuf;
1676         assert( si->si_rid < 1000 );
1677         slap_syncrepl_cn_bv.bv_len = snprintf( slap_syncrepl_cn_bv.bv_val,
1678                 slap_syncrepl_cn_bvc.bv_len + 1,
1679                 "cn=syncrepl%ld", si->si_rid );
1680
1681         build_new_dn( &slap_syncrepl_dn_bv, pdn, &slap_syncrepl_cn_bv,
1682                 op->o_tmpmemctx );
1683         ber_dupbv( &e->e_name, &slap_syncrepl_dn_bv );
1684         ber_dupbv( &e->e_nname, &slap_syncrepl_dn_bv );
1685
1686         if ( slap_syncrepl_dn_bv.bv_val ) {
1687                 sl_free( slap_syncrepl_dn_bv.bv_val, op->o_tmpmemctx );
1688         }
1689
1690         e->e_attrs = NULL;
1691
1692         rc = slap_mods2entry( modlist, &e, 1, 1, &text, txtbuf, textlen );
1693
1694         if( rc != LDAP_SUCCESS ) {
1695 #ifdef NEW_LOGGING
1696                 LDAP_LOG( OPERATION, ERR,
1697                         "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1698 #else
1699                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1700                          text, 0, 0 );
1701 #endif
1702         }
1703
1704         cb.sc_response = null_callback;
1705         cb.sc_private = si;
1706
1707         op->o_callback = &cb;
1708         op->o_req_dn = e->e_name;
1709         op->o_req_ndn = e->e_nname;
1710
1711         /* update persistent cookie */
1712 update_cookie_retry:
1713         op->o_tag = LDAP_REQ_MODIFY;
1714         op->orm_modlist = modlist;
1715         rc = be->be_modify( op, &rs );
1716
1717         if ( rc != LDAP_SUCCESS ) {
1718                 if ( rc == LDAP_REFERRAL ||
1719                          rc == LDAP_NO_SUCH_OBJECT ) {
1720                         op->o_tag = LDAP_REQ_ADD;
1721                         op->ora_e = e;
1722                         rc = be->be_add( op, &rs );
1723                         if ( rc != LDAP_SUCCESS ) {
1724                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1725                                         goto update_cookie_retry;
1726                                 } else if ( rc == LDAP_REFERRAL ||
1727                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1728 #ifdef NEW_LOGGING
1729                                         LDAP_LOG( OPERATION, ERR,
1730                                                 "cookie will be non-persistent\n",
1731                                                 0, 0, 0 );
1732 #else
1733                                         Debug( LDAP_DEBUG_ANY,
1734                                                 "cookie will be non-persistent\n",
1735                                                 0, 0, 0 );
1736 #endif
1737                                 } else {
1738 #ifdef NEW_LOGGING
1739                                         LDAP_LOG( OPERATION, ERR,
1740                                                 "be_add failed (%d)\n",
1741                                                 rc, 0, 0 );
1742 #else
1743                                         Debug( LDAP_DEBUG_ANY,
1744                                                 "be_add failed (%d)\n",
1745                                                 rc, 0, 0 );
1746 #endif
1747                                 }
1748                         } else {
1749                                 be_entry_release_w( op, e );
1750                                 goto done;
1751                         }
1752                 } else {
1753 #ifdef NEW_LOGGING
1754                         LDAP_LOG( OPERATION, ERR,
1755                                 "be_modify failed (%d)\n", rc, 0, 0 );
1756 #else
1757                         Debug( LDAP_DEBUG_ANY,
1758                                 "be_modify failed (%d)\n", rc, 0, 0 );
1759 #endif
1760                 }
1761         }
1762
1763         if ( e != NULL ) {
1764                 entry_free( e );
1765         }
1766
1767 done :
1768
1769         if ( cnbva[0].bv_val ) {
1770                 ch_free( cnbva[0].bv_val );
1771                 cnbva[0].bv_val = NULL;
1772         }
1773         if ( scbva[0].bv_val ) {
1774                 ch_free( scbva[0].bv_val );
1775                 scbva[0].bv_val = NULL;
1776         }
1777
1778         if ( mlnext->sml_next ) {
1779                 slap_mods_free( mlnext->sml_next );
1780                 mlnext->sml_next = NULL;
1781         }
1782
1783         for (ml = modlist ; ml != NULL; ml = mlnext ) {
1784                 mlnext = ml->sml_next;
1785                 free( ml );
1786         }
1787
1788         return;
1789 }
1790
1791 static int
1792 dn_callback(
1793         Operation*      op,
1794         SlapReply*      rs
1795 )
1796 {
1797         syncinfo_t *si = op->o_callback->sc_private;
1798
1799         if ( rs->sr_type == REP_SEARCH ) {
1800                 if ( si->si_syncUUID_ndn.bv_val != NULL ) {
1801 #ifdef NEW_LOGGING
1802                         LDAP_LOG( OPERATION, ERR,
1803                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1804 #else
1805                         Debug( LDAP_DEBUG_ANY,
1806                                 "dn_callback : multiple entries match dn\n", 0, 0, 0 );
1807 #endif
1808                 } else {
1809                         ber_dupbv_x( &si->si_syncUUID_ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
1810                 }
1811         }
1812
1813         return LDAP_SUCCESS;
1814 }
1815
1816 static int
1817 nonpresent_callback(
1818         Operation*      op,
1819         SlapReply*      rs
1820 )
1821 {
1822         syncinfo_t *si = op->o_callback->sc_private;
1823         Attribute *a;
1824         int count = 0;
1825         struct berval* present_uuid = NULL;
1826         struct nonpresent_entry *np_entry;
1827
1828         if ( rs->sr_type == REP_RESULT ) {
1829                 count = avl_free( si->si_presentlist, avl_ber_bvfree );
1830                 si->si_presentlist = NULL;
1831
1832         } else if ( rs->sr_type == REP_SEARCH ) {
1833                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1834
1835                 if ( a == NULL ) return 0;
1836
1837                 present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
1838                         syncuuid_cmp );
1839
1840                 if ( present_uuid == NULL ) {
1841                         np_entry = (struct nonpresent_entry *)
1842                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1843                         np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
1844                         np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1845                         LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
1846
1847                 } else {
1848                         avl_delete( &si->si_presentlist,
1849                                         &a->a_nvals[0], syncuuid_cmp );
1850                         ch_free( present_uuid->bv_val );
1851                         ch_free( present_uuid );
1852                 }
1853         }
1854         return LDAP_SUCCESS;
1855 }
1856
1857 static int
1858 null_callback(
1859         Operation*      op,
1860         SlapReply*      rs )
1861 {
1862         if ( rs->sr_err != LDAP_SUCCESS &&
1863                 rs->sr_err != LDAP_REFERRAL &&
1864                 rs->sr_err != LDAP_ALREADY_EXISTS &&
1865                 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1866                 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
1867         {
1868 #ifdef NEW_LOGGING
1869                 LDAP_LOG( OPERATION, ERR,
1870                         "null_callback : error code 0x%x\n",
1871                         rs->sr_err, 0, 0 );
1872 #else
1873                 Debug( LDAP_DEBUG_ANY,
1874                         "null_callback : error code 0x%x\n",
1875                         rs->sr_err, 0, 0 );
1876 #endif
1877         }
1878         return LDAP_SUCCESS;
1879 }
1880
1881 Entry *
1882 slap_create_syncrepl_entry(
1883         Backend *be,
1884         struct berval *context_csn,
1885         struct berval *rdn,
1886         struct berval *cn )
1887 {
1888         Entry* e;
1889
1890         struct berval bv;
1891
1892         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1893
1894         attr_merge( e, slap_schema.si_ad_objectClass, ocbva, NULL );
1895
1896         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1897                 &ocbva[1], NULL );
1898
1899         attr_merge_one( e, slap_schema.si_ad_cn, cn, NULL );
1900
1901         if ( context_csn ) {
1902                 attr_merge_one( e, slap_schema.si_ad_syncreplCookie,
1903                         context_csn, NULL );
1904         }
1905
1906         bv.bv_val = "{}";
1907         bv.bv_len = sizeof("{}")-1;
1908         attr_merge_one( e, slap_schema.si_ad_subtreeSpecification, &bv, NULL );
1909
1910         build_new_dn( &e->e_name, &be->be_nsuffix[0], rdn, NULL );
1911         ber_dupbv( &e->e_nname, &e->e_name );
1912
1913         return e;
1914 }
1915
1916 struct berval *
1917 slap_uuidstr_from_normalized(
1918         struct berval* uuidstr,
1919         struct berval* normalized,
1920         void *ctx )
1921 {
1922         struct berval *new;
1923         unsigned char nibble;
1924         int i, d = 0;
1925
1926         if ( normalized == NULL )
1927                 return NULL;
1928
1929         if ( normalized->bv_len != 16 ) {
1930                 return NULL;
1931         }
1932
1933         if ( uuidstr ) {
1934                 new = uuidstr;
1935         } else {
1936                 new = (struct berval *)sl_malloc( sizeof(struct berval), ctx );
1937         }
1938
1939         new->bv_len = 36;
1940
1941         if (( new->bv_val = sl_malloc( new->bv_len + 1, ctx )) == NULL) {
1942                 if ( !uuidstr )
1943                         sl_free( new, ctx );
1944                 return NULL;
1945         }
1946
1947         for ( i = 0; i < 16; i++ ) {
1948                 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
1949                         new->bv_val[(i<<1)+d] = '-';
1950                         d += 1;
1951                 }
1952
1953                 nibble = (normalized->bv_val[i] >> 4) & 0xF;
1954                 if ( nibble < 10 ) {
1955                         new->bv_val[(i<<1)+d] = nibble + '0';
1956                 } else {
1957                         new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
1958                 }
1959
1960                 nibble = (normalized->bv_val[i]) & 0xF;
1961                 if ( nibble < 10 ) {
1962                         new->bv_val[(i<<1)+d+1] = nibble + '0';
1963                 } else {
1964                         new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
1965                 }
1966         }
1967
1968         new->bv_val[new->bv_len] = '\0';
1969
1970         return new;
1971 }
1972
1973 static int
1974 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
1975 {
1976         const struct berval *uuid1 = v_uuid1;
1977         const struct berval *uuid2 = v_uuid2;
1978         int rc = uuid1->bv_len - uuid2->bv_len;
1979         if ( rc ) return rc;
1980         return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
1981 }
1982
1983 static void
1984 avl_ber_bvfree( void *bv )
1985 {
1986         if( bv == NULL ) {
1987                 return;
1988         }
1989         if ( ((struct berval *)bv)->bv_val != NULL ) {
1990                 ch_free ( ((struct berval *)bv)->bv_val );
1991         }
1992         ch_free ( (char *) bv );
1993 }
1994