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