]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
The rest of the reloadHint support...
[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-2005 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 "lutil.h"
27 #include "slap.h"
28 #include "lutil_ldap.h"
29
30 #include "config.h"
31
32 #include "ldap_rq.h"
33
34 struct nonpresent_entry {
35         struct berval *npe_name;
36         struct berval *npe_nname;
37         LDAP_LIST_ENTRY(nonpresent_entry) npe_link;
38 };
39
40 #define SYNCDATA_DEFAULT        0       /* entries are plain LDAP entries */
41 #define SYNCDATA_ACCESSLOG      1       /* entries are accesslog format */
42 #define SYNCDATA_CHANGELOG      2       /* entries are changelog format */
43
44 #define SYNCLOG_LOGGING         0       /* doing a log-based update */
45 #define SYNCLOG_FALLBACK        1       /* doing a full refresh */
46
47 typedef struct syncinfo_s {
48         struct slap_backend_db *si_be;
49         struct re_s                     *si_re;
50         long                            si_rid;
51         struct berval           si_provideruri;
52         slap_bindconf           si_bindconf;
53         struct berval           si_base;
54         struct berval           si_logbase;
55         struct berval           si_filterstr;
56         struct berval           si_logfilterstr;
57         int                                     si_scope;
58         int                                     si_attrsonly;
59         char                            *si_anfile;
60         AttributeName           *si_anlist;
61         AttributeName           *si_exanlist;
62         char                            **si_attrs;
63         char                            **si_exattrs;
64         int                                     si_allattrs;
65         int                                     si_allopattrs;
66         int                                     si_schemachecking;
67         int                                     si_type;
68         time_t                          si_interval;
69         time_t                          *si_retryinterval;
70         int                                     *si_retrynum_init;
71         int                                     *si_retrynum;
72         struct sync_cookie      si_syncCookie;
73         int                                     si_manageDSAit;
74         int                                     si_slimit;
75         int                                     si_tlimit;
76         int                                     si_refreshDelete;
77         int                                     si_refreshPresent;
78         int                                     si_syncdata;
79         int                                     si_logstate;
80         Avlnode                         *si_presentlist;
81         LDAP                            *si_ld;
82         LDAP_LIST_HEAD(np, nonpresent_entry) si_nonpresentlist;
83         ldap_pvt_thread_mutex_t si_mutex;
84 } syncinfo_t;
85
86 static int syncuuid_cmp( const void *, const void * );
87 static void avl_ber_bvfree( void * );
88 static void syncrepl_del_nonpresent( Operation *, syncinfo_t *, BerVarray );
89 static int syncrepl_message_to_op(
90                                         syncinfo_t *, Operation *, LDAPMessage * );
91 static int syncrepl_message_to_entry(
92                                         syncinfo_t *, Operation *, LDAPMessage *,
93                                         Modifications **, Entry **, int );
94 static int syncrepl_entry(
95                                         syncinfo_t *, Operation*, Entry*,
96                                         Modifications**,int, struct berval*,
97                                         struct sync_cookie *,
98                                         struct berval * );
99 static void syncrepl_updateCookie(
100                                         syncinfo_t *, Operation *, struct berval *,
101                                         struct sync_cookie * );
102 static struct berval * slap_uuidstr_from_normalized(
103                                         struct berval *, struct berval *, void * );
104
105 /* callback functions */
106 static int dn_callback( struct slap_op *, struct slap_rep * );
107 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
108 static int null_callback( struct slap_op *, struct slap_rep * );
109
110 static AttributeDescription *sync_descs[4];
111
112 static void
113 init_syncrepl(syncinfo_t *si)
114 {
115         int i, j, k, l, n;
116         char **attrs, **exattrs;
117
118         if ( !sync_descs[0] ) {
119                 sync_descs[0] = slap_schema.si_ad_objectClass;
120                 sync_descs[1] = slap_schema.si_ad_structuralObjectClass;
121                 sync_descs[2] = slap_schema.si_ad_entryCSN;
122                 sync_descs[3] = NULL;
123         }
124
125         if ( si->si_allattrs && si->si_allopattrs )
126                 attrs = NULL;
127         else
128                 attrs = anlist2attrs( si->si_anlist );
129
130         if ( attrs ) {
131                 if ( si->si_allattrs ) {
132                         i = 0;
133                         while ( attrs[i] ) {
134                                 if ( !is_at_operational( at_find( attrs[i] ))) {
135                                         for ( j = i; attrs[j] != NULL; j++ ) {
136                                                 if ( j == i )
137                                                         ch_free( attrs[i] );
138                                                 attrs[j] = attrs[j+1];
139                                         }
140                                 } else {
141                                         i++;
142                                 }
143                         }
144                         attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
145                         attrs[i] = ch_strdup("*");
146                         attrs[i + 1] = NULL;
147
148                 } else if ( si->si_allopattrs ) {
149                         i = 0;
150                         while ( attrs[i] ) {
151                                 if ( is_at_operational( at_find( attrs[i] ))) {
152                                         for ( j = i; attrs[j] != NULL; j++ ) {
153                                                 if ( j == i )
154                                                         ch_free( attrs[i] );
155                                                 attrs[j] = attrs[j+1];
156                                         }
157                                 } else {
158                                         i++;
159                                 }
160                         }
161                         attrs = ( char ** ) ch_realloc( attrs, (i + 2)*sizeof( char * ) );
162                         attrs[i] = ch_strdup("+");
163                         attrs[i + 1] = NULL;
164                 }
165
166                 for ( i = 0; sync_descs[i] != NULL; i++ ) {
167                         j = 0;
168                         while ( attrs[j] ) {
169                                 if ( !strcmp( attrs[j], sync_descs[i]->ad_cname.bv_val )) {
170                                         for ( k = j; attrs[k] != NULL; k++ ) {
171                                                 if ( k == j )
172                                                         ch_free( attrs[k] );
173                                                 attrs[k] = attrs[k+1];
174                                         }
175                                 } else {
176                                         j++;
177                                 }
178                         }
179                 }
180
181                 for ( n = 0; attrs[ n ] != NULL; n++ ) /* empty */;
182
183                 if ( si->si_allopattrs ) {
184                         attrs = ( char ** ) ch_realloc( attrs, (n + 2)*sizeof( char * ));
185                 } else {
186                         attrs = ( char ** ) ch_realloc( attrs, (n + 4)*sizeof( char * ));
187                 }
188
189                 if ( attrs == NULL ) {
190                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
191                 }
192
193                 /* Add Attributes */
194                 if ( si->si_allopattrs ) {
195                         attrs[n++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
196                 } else {
197                         for ( i = 0; sync_descs[ i ] != NULL; i++ ) {
198                                 attrs[ n++ ] = ch_strdup ( sync_descs[i]->ad_cname.bv_val );
199                         }
200                 }
201                 attrs[ n ] = NULL;
202
203         } else {
204
205                 i = 0;
206                 if ( si->si_allattrs == si->si_allopattrs ) {
207                         attrs = (char**) ch_malloc( 3 * sizeof(char*) );
208                         attrs[i++] = ch_strdup( "*" );
209                         attrs[i++] = ch_strdup( "+" );
210                 } else if ( si->si_allattrs && !si->si_allopattrs ) {
211                         for ( n = 0; sync_descs[ n ] != NULL; n++ ) ;
212                         attrs = (char**) ch_malloc( (n+1)* sizeof(char*) );
213                         attrs[i++] = ch_strdup( "*" );
214                         for ( j = 1; sync_descs[ j ] != NULL; j++ ) {
215                                 attrs[i++] = ch_strdup ( sync_descs[j]->ad_cname.bv_val );
216                         }
217                 } else if ( !si->si_allattrs && si->si_allopattrs ) {
218                         attrs = (char**) ch_malloc( 3 * sizeof(char*) );
219                         attrs[i++] = ch_strdup( "+" );
220                         attrs[i++] = ch_strdup( sync_descs[0]->ad_cname.bv_val );
221                 }
222                 attrs[i] = NULL;
223         }
224         
225         si->si_attrs = attrs;
226
227         exattrs = anlist2attrs( si->si_exanlist );
228
229         if ( exattrs ) {
230                 for ( n = 0; exattrs[n] != NULL; n++ ) ;
231
232                 for ( i = 0; sync_descs[i] != NULL; i++ ) {
233                         j = 0;
234                         while ( exattrs[j] != NULL ) {
235                                 if ( !strcmp( exattrs[j], sync_descs[i]->ad_cname.bv_val )) {
236                                         ch_free( exattrs[j] );
237                                         for ( k = j; exattrs[k] != NULL; k++ ) {
238                                                 exattrs[k] = exattrs[k+1];
239                                         }
240                                 } else {
241                                         j++;
242                                 }
243                         }
244                 }
245
246                 for ( i = 0; exattrs[i] != NULL; i++ ) {
247                         for ( j = 0; si->si_anlist[j].an_name.bv_val; j++ ) {
248                                 ObjectClass     *oc;
249                                 if ( ( oc = si->si_anlist[j].an_oc ) ) {
250                                         k = 0;
251                                         while ( oc->soc_required[k] ) {
252                                                 if ( !strcmp( exattrs[i],
253                                                          oc->soc_required[k]->sat_cname.bv_val )) {
254                                                         ch_free( exattrs[i] );
255                                                         for ( l = i; exattrs[l]; l++ ) {
256                                                                 exattrs[l] = exattrs[l+1];
257                                                         }
258                                                 } else {
259                                                         k++;
260                                                 }
261                                         }
262                                 }
263                         }
264                 }
265
266                 for ( i = 0; exattrs[i] != NULL; i++ ) ;
267
268                 if ( i != n )
269                         exattrs = (char **) ch_realloc( exattrs, (i + 1)*sizeof(char *));
270         }
271
272         si->si_exattrs = exattrs;       
273 }
274
275 static char *allattrs[] = {"*", "+", NULL};
276
277 static int
278 ldap_sync_search(
279         syncinfo_t *si,
280         void *ctx )
281 {
282         BerElementBuffer berbuf;
283         BerElement *ber = (BerElement *)&berbuf;
284         LDAPControl c[2], *ctrls[3];
285         struct timeval timeout;
286         ber_int_t       msgid;
287         int rc;
288         int rhint;
289         char *base;
290         char **attrs;
291         char *filter;
292         int attrsonly;
293         int stype;
294         int scope;
295
296         /* setup LDAP SYNC control */
297         ber_init2( ber, NULL, LBER_USE_DER );
298         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &ctx );
299
300         /* If we're using a log but we have no state, then fallback to
301          * normal mode for a full refresh.
302          */
303         if ( si->si_syncdata && BER_BVISEMPTY( &si->si_syncCookie.ctxcsn ))
304                 si->si_logstate = SYNCLOG_FALLBACK;
305
306         /* Use the log parameters if we're in log mode */
307         if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
308                 rhint = 0;
309                 base = si->si_logbase.bv_val;
310                 filter = si->si_logfilterstr.bv_val;
311                 attrs = allattrs;
312                 attrsonly = 0;
313                 scope = LDAP_SCOPE_ONELEVEL;
314         } else {
315                 rhint = 1;
316                 base = si->si_base.bv_val;
317                 filter = si->si_filterstr.bv_val;
318                 attrs = si->si_attrs;
319                 attrsonly = si->si_attrsonly;
320                 scope = si->si_scope;
321         }
322         if ( si->si_syncdata && si->si_logstate == SYNCLOG_FALLBACK ) {
323                 stype = LDAP_SYNC_REFRESH_ONLY;
324         } else {
325                 stype = si->si_type;
326         }
327
328         if ( !BER_BVISNULL( &si->si_syncCookie.octet_str ) )
329         {
330                 ber_printf( ber, "{eOb}",
331                         abs(stype), &si->si_syncCookie.octet_str, rhint );
332         } else {
333                 ber_printf( ber, "{eb}",
334                         abs(stype), rhint );
335         }
336
337         if ( (rc = ber_flatten2( ber, &c[0].ldctl_value, 0 )) == LBER_ERROR ) {
338                 ber_free_buf( ber );
339                 return rc;
340         }
341
342         c[0].ldctl_oid = LDAP_CONTROL_SYNC;
343         c[0].ldctl_iscritical = si->si_type < 0;
344         ctrls[0] = &c[0];
345
346         if ( !BER_BVISNULL( &si->si_bindconf.sb_authzId ) ) {
347                 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
348                 c[1].ldctl_value = si->si_bindconf.sb_authzId;
349                 c[1].ldctl_iscritical = 1;
350                 ctrls[1] = &c[1];
351                 ctrls[2] = NULL;
352         } else {
353                 ctrls[1] = NULL;
354         }
355
356         timeout.tv_sec = si->si_tlimit;
357         timeout.tv_usec = 0;
358
359         rc = ldap_search_ext( si->si_ld, base, scope, filter, attrs, attrsonly,
360                 ctrls, NULL, si->si_tlimit > 0 ? &timeout : NULL,
361                 si->si_slimit, &msgid );
362         ber_free_buf( ber );
363         return rc;
364 }
365
366 static int
367 do_syncrep1(
368         Operation *op,
369         syncinfo_t *si )
370 {
371         int     rc;
372         int cmdline_cookie_found = 0;
373
374         struct sync_cookie      *sc = NULL;
375         struct berval   *psub;
376 #ifdef HAVE_TLS
377         void    *ssl;
378 #endif
379
380         psub = &si->si_be->be_nsuffix[0];
381
382         /* Init connection to master */
383         rc = ldap_initialize( &si->si_ld, si->si_provideruri.bv_val );
384         if ( rc != LDAP_SUCCESS ) {
385                 Debug( LDAP_DEBUG_ANY,
386                         "do_syncrep1: ldap_initialize failed (%s)\n",
387                         si->si_provideruri.bv_val, 0, 0 );
388                 return rc;
389         }
390
391         op->o_protocol = LDAP_VERSION3;
392         ldap_set_option( si->si_ld, LDAP_OPT_PROTOCOL_VERSION, &op->o_protocol );
393
394         /* Bind to master */
395
396         if ( si->si_bindconf.sb_tls ) {
397                 rc = ldap_start_tls_s( si->si_ld, NULL, NULL );
398                 if( rc != LDAP_SUCCESS ) {
399                         Debug( LDAP_DEBUG_ANY,
400                                 "%s: ldap_start_tls failed (%d)\n",
401                                 si->si_bindconf.sb_tls == SB_TLS_CRITICAL ? "Error" : "Warning",
402                                 rc, 0 );
403                         if( si->si_bindconf.sb_tls == SB_TLS_CRITICAL ) goto done;
404                 }
405         }
406
407         if ( si->si_bindconf.sb_method == LDAP_AUTH_SASL ) {
408 #ifdef HAVE_CYRUS_SASL
409                 void *defaults;
410
411                 if ( si->si_bindconf.sb_secprops != NULL ) {
412                         rc = ldap_set_option( si->si_ld,
413                                 LDAP_OPT_X_SASL_SECPROPS, si->si_bindconf.sb_secprops);
414
415                         if( rc != LDAP_OPT_SUCCESS ) {
416                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
417                                         "(%s,SECPROPS,\"%s\") failed!\n",
418                                         si->si_provideruri.bv_val, si->si_bindconf.sb_secprops, 0 );
419                                 goto done;
420                         }
421                 }
422
423                 defaults = lutil_sasl_defaults( si->si_ld,
424                         si->si_bindconf.sb_saslmech.bv_val,
425                         si->si_bindconf.sb_realm.bv_val,
426                         si->si_bindconf.sb_authcId.bv_val,
427                         si->si_bindconf.sb_cred.bv_val,
428                         si->si_bindconf.sb_authzId.bv_val );
429
430                 rc = ldap_sasl_interactive_bind_s( si->si_ld,
431                                 si->si_bindconf.sb_binddn.bv_val,
432                                 si->si_bindconf.sb_saslmech.bv_val,
433                                 NULL, NULL,
434                                 LDAP_SASL_QUIET,
435                                 lutil_sasl_interact,
436                                 defaults );
437
438                 lutil_sasl_freedefs( defaults );
439
440                 /* FIXME: different error behaviors according to
441                  *      1) return code
442                  *      2) on err policy : exit, retry, backoff ...
443                  */
444                 if ( rc != LDAP_SUCCESS ) {
445                         static struct berval bv_GSSAPI = BER_BVC( "GSSAPI" );
446
447                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
448                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
449                                 rc, 0, 0 );
450
451                         /* FIXME (see above comment) */
452                         /* if Kerberos credentials cache is not active, retry */
453                         if ( ber_bvcmp( &si->si_bindconf.sb_saslmech, &bv_GSSAPI ) == 0 &&
454                                 rc == LDAP_LOCAL_ERROR )
455                         {
456                                 rc = LDAP_SERVER_DOWN;
457                         }
458
459                         goto done;
460                 }
461 #else /* HAVE_CYRUS_SASL */
462                 /* Should never get here, we trapped this at config time */
463                 assert(0);
464                 fprintf( stderr, "not compiled with SASL support\n" );
465                 rc = LDAP_OTHER;
466                 goto done;
467 #endif
468
469         } else if ( si->si_bindconf.sb_method == LDAP_AUTH_SIMPLE ) {
470                 rc = ldap_sasl_bind_s( si->si_ld,
471                         si->si_bindconf.sb_binddn.bv_val, LDAP_SASL_SIMPLE,
472                         &si->si_bindconf.sb_cred, NULL, NULL, NULL );
473                 if ( rc != LDAP_SUCCESS ) {
474                         Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
475                                 "ldap_sasl_bind_s failed (%d)\n", rc, 0, 0 );
476                         goto done;
477                 }
478         }
479
480         /* Set SSF to strongest of TLS, SASL SSFs */
481         op->o_sasl_ssf = 0;
482         op->o_tls_ssf = 0;
483         op->o_transport_ssf = 0;
484 #ifdef HAVE_TLS
485         if ( ldap_get_option( si->si_ld, LDAP_OPT_X_TLS_SSL_CTX, &ssl )
486                 == LDAP_SUCCESS && ssl != NULL )
487         {
488                 op->o_tls_ssf = ldap_pvt_tls_get_strength( ssl );
489         }
490 #endif /* HAVE_TLS */
491         ldap_get_option( si->si_ld, LDAP_OPT_X_SASL_SSF, &op->o_sasl_ssf );
492         op->o_ssf = ( op->o_sasl_ssf > op->o_tls_ssf )
493                 ?  op->o_sasl_ssf : op->o_tls_ssf;
494
495
496         if ( BER_BVISNULL( &si->si_syncCookie.octet_str )) {
497                 /* get contextCSN shadow replica from database */
498                 BerVarray csn = NULL;
499
500                 assert( si->si_rid < 1000 );
501                 op->o_req_ndn = op->o_bd->be_nsuffix[0];
502                 op->o_req_dn = op->o_req_ndn;
503
504                 /* try to read stored contextCSN */
505                 backend_attribute( op, NULL, &op->o_req_ndn,
506                         slap_schema.si_ad_contextCSN, &csn, ACL_READ );
507                 if ( csn ) {
508                         ch_free( si->si_syncCookie.ctxcsn.bv_val );
509                         ber_dupbv( &si->si_syncCookie.ctxcsn, csn );
510                         ber_bvarray_free_x( csn, op->o_tmpmemctx );
511                 }
512
513                 si->si_syncCookie.rid = si->si_rid;
514
515                 LDAP_STAILQ_FOREACH( sc, &slap_sync_cookie, sc_next ) {
516                         if ( si->si_rid == sc->rid ) {
517                                 cmdline_cookie_found = 1;
518                                 break;
519                         }
520                 }
521
522                 if ( cmdline_cookie_found ) {
523                         /* cookie is supplied in the command line */
524
525                         LDAP_STAILQ_REMOVE( &slap_sync_cookie, sc, sync_cookie, sc_next );
526
527                         /* ctxcsn wasn't parsed yet, do it now */
528                         slap_parse_sync_cookie( sc, op->o_tmpmemctx );
529                         if ( BER_BVISNULL( &sc->ctxcsn ) ) {
530                                 /* if cmdline cookie does not have ctxcsn */
531                                 /* component, set it to an initial value */
532                                 slap_init_sync_cookie_ctxcsn( sc );
533                         }
534                         slap_sync_cookie_free( &si->si_syncCookie, 0 );
535                         slap_dup_sync_cookie( &si->si_syncCookie, sc );
536                         slap_sync_cookie_free( sc, 1 );
537                 }
538
539                 slap_compose_sync_cookie( NULL, &si->si_syncCookie.octet_str,
540                         &si->si_syncCookie.ctxcsn, si->si_syncCookie.rid );
541         }
542
543         rc = ldap_sync_search( si, op->o_tmpmemctx );
544
545         if( rc != LDAP_SUCCESS ) {
546                 Debug( LDAP_DEBUG_ANY, "do_syncrep1: "
547                         "ldap_search_ext: %s (%d)\n", ldap_err2string( rc ), rc, 0 );
548         }
549
550 done:
551         if ( rc ) {
552                 if ( si->si_ld ) {
553                         ldap_unbind_ext( si->si_ld, NULL, NULL );
554                         si->si_ld = NULL;
555                 }
556         }
557
558         return rc;
559 }
560
561 static int
562 do_syncrep2(
563         Operation *op,
564         syncinfo_t *si )
565 {
566         LDAPControl     **rctrls = NULL;
567         LDAPControl     *rctrlp;
568
569         BerElementBuffer berbuf;
570         BerElement      *ber = (BerElement *)&berbuf;
571
572         LDAPMessage     *res = NULL;
573         LDAPMessage     *msg = NULL;
574
575         char            *retoid = NULL;
576         struct berval   *retdata = NULL;
577
578         Entry           *entry = NULL;
579
580         int             syncstate;
581         struct berval   syncUUID = BER_BVNULL;
582         struct sync_cookie      syncCookie = { BER_BVNULL };
583         struct sync_cookie      syncCookie_req = { BER_BVNULL };
584         struct berval           cookie = BER_BVNULL;
585
586         int     rc, err, i;
587         ber_len_t       len;
588
589         int rc_efree = 1;
590
591         struct berval   *psub;
592         Modifications   *modlist = NULL;
593
594         const char              *text;
595         int                             match;
596
597         struct timeval *tout_p = NULL;
598         struct timeval tout = { 0, 0 };
599
600         int             refreshDeletes = 0;
601         int             refreshDone = 1;
602         BerVarray syncUUIDs = NULL;
603         ber_tag_t si_tag;
604
605         if ( slapd_shutdown ) {
606                 rc = -2;
607                 goto done;
608         }
609
610         ber_init2( ber, NULL, LBER_USE_DER );
611         ber_set_option( ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
612
613         Debug( LDAP_DEBUG_TRACE, "=>do_syncrep2\n", 0, 0, 0 );
614
615         psub = &si->si_be->be_nsuffix[0];
616
617         slap_dup_sync_cookie( &syncCookie_req, &si->si_syncCookie );
618
619         if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
620                 tout_p = &tout;
621         } else {
622                 tout_p = NULL;
623         }
624
625         while (( rc = ldap_result( si->si_ld, LDAP_RES_ANY, LDAP_MSG_ONE,
626                 tout_p, &res )) > 0 )
627         {
628                 if ( slapd_shutdown ) {
629                         rc = -2;
630                         goto done;
631                 }
632                 for( msg = ldap_first_message( si->si_ld, res );
633                         msg != NULL;
634                         msg = ldap_next_message( si->si_ld, msg ) )
635                 {
636                         if ( slapd_shutdown ) {
637                                 rc = -2;
638                                 goto done;
639                         }
640                         switch( ldap_msgtype( msg ) ) {
641                         case LDAP_RES_SEARCH_ENTRY:
642                                 ldap_get_entry_controls( si->si_ld, msg, &rctrls );
643                                 /* we can't work without the control */
644                                 if ( !rctrls ) {
645                                         Debug( LDAP_DEBUG_ANY, "do_syncrep2: "
646                                                 "got search entry without "
647                                                 "control\n", 0, 0, 0 );
648                                         rc = -1;
649                                         goto done;
650                                 }
651                                 rctrlp = *rctrls;
652                                 ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
653                                 ber_scanf( ber, "{em" /*"}"*/, &syncstate, &syncUUID );
654                                 /* FIXME: what if syncUUID is NULL or empty? */
655                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
656                                         ber_scanf( ber, /*"{"*/ "m}", &cookie );
657                                         if ( !BER_BVISNULL( &cookie ) ) {
658                                                 ch_free( syncCookie.octet_str.bv_val );
659                                                 ber_dupbv( &syncCookie.octet_str, &cookie );
660                                         }
661                                         if ( !BER_BVISNULL( &syncCookie.octet_str ) )
662                                         {
663                                                 slap_parse_sync_cookie( &syncCookie, NULL );
664                                         }
665                                 }
666                                 if ( si->si_syncdata && si->si_logstate == SYNCLOG_LOGGING ) {
667                                         entry = NULL;
668                                         modlist = NULL;
669                                         if ( syncrepl_message_to_op( si, op, msg ) == LDAP_SUCCESS &&
670                                                 !BER_BVISNULL( &syncCookie.ctxcsn ) ) {
671                                                 syncrepl_updateCookie( si, op, psub, &syncCookie );
672                                         }
673                                 } else if ( syncrepl_message_to_entry( si, op, msg,
674                                         &modlist, &entry, syncstate ) == LDAP_SUCCESS ) {
675                                         rc_efree = syncrepl_entry( si, op, entry, &modlist,
676                                                 syncstate, &syncUUID, &syncCookie_req, &syncCookie.ctxcsn );
677                                         if ( !BER_BVISNULL( &syncCookie.ctxcsn ) )
678                                         {
679                                                 syncrepl_updateCookie( si, op, psub, &syncCookie );
680                                         }
681                                 }
682                                 ldap_controls_free( rctrls );
683                                 if ( modlist ) {
684                                         slap_mods_free( modlist, 1 );
685                                 }
686                                 if ( rc_efree && entry ) {
687                                         entry_free( entry );
688                                 }
689                                 entry = NULL;
690                                 break;
691
692                         case LDAP_RES_SEARCH_REFERENCE:
693                                 Debug( LDAP_DEBUG_ANY,
694                                         "do_syncrep2: reference received error\n", 0, 0, 0 );
695                                 break;
696
697                         case LDAP_RES_SEARCH_RESULT:
698                                 Debug( LDAP_DEBUG_SYNC,
699                                         "do_syncrep2: LDAP_RES_SEARCH_RESULT\n", 0, 0, 0 );
700                                 ldap_parse_result( si->si_ld, msg, &err, NULL, NULL, NULL,
701                                         &rctrls, 0 );
702                                 if ( err == LDAP_SYNC_REFRESH_REQUIRED ) {
703                                         if ( si->si_logstate == SYNCLOG_LOGGING ) {
704                                                 si->si_logstate = SYNCLOG_FALLBACK;
705                                         }
706                                         rc = err;
707                                         break;
708                                 }
709                                 if ( rctrls ) {
710                                         rctrlp = *rctrls;
711                                         ber_init2( ber, &rctrlp->ldctl_value, LBER_USE_DER );
712
713                                         ber_scanf( ber, "{" /*"}"*/);
714                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE ) {
715                                                 ber_scanf( ber, "m", &cookie );
716                                                 if ( !BER_BVISNULL( &cookie ) ) {
717                                                         ch_free( syncCookie.octet_str.bv_val );
718                                                         ber_dupbv( &syncCookie.octet_str, &cookie);
719                                                 }
720                                                 if ( !BER_BVISNULL( &syncCookie.octet_str ) )
721                                                 {
722                                                         slap_parse_sync_cookie( &syncCookie, NULL );
723                                                 }
724                                         }
725                                         if ( ber_peek_tag( ber, &len ) == LDAP_TAG_REFRESHDELETES )
726                                         {
727                                                 ber_scanf( ber, "b", &refreshDeletes );
728                                         }
729                                         ber_scanf( ber, /*"{"*/ "}" );
730                                 }
731                                 if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
732                                         match = -1;
733                                 } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
734                                         match = 1;
735                                 } else {
736                                         value_match( &match, slap_schema.si_ad_entryCSN,
737                                                 slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
738                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
739                                                 &syncCookie_req.ctxcsn, &syncCookie.ctxcsn,
740                                                 &text );
741                                 }
742                                 if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
743                                         match < 0 && err == LDAP_SUCCESS )
744                                 {
745                                         syncrepl_updateCookie( si, op, psub, &syncCookie );
746                                 }
747                                 if ( rctrls ) {
748                                         ldap_controls_free( rctrls );
749                                 }
750                                 if (si->si_type != LDAP_SYNC_REFRESH_AND_PERSIST) {
751                                         /* FIXME : different error behaviors according to
752                                          *      1) err code : LDAP_BUSY ...
753                                          *      2) on err policy : stop service, stop sync, retry
754                                          */
755                                         if ( refreshDeletes == 0 && match < 0 &&
756                                                 err == LDAP_SUCCESS )
757                                         {
758                                                 syncrepl_del_nonpresent( op, si, NULL );
759                                         } else {
760                                                 avl_free( si->si_presentlist, avl_ber_bvfree );
761                                                 si->si_presentlist = NULL;
762                                         }
763                                 }
764                                 if ( err == LDAP_SUCCESS
765                                         && si->si_logstate == SYNCLOG_FALLBACK ) {
766                                         si->si_logstate = SYNCLOG_LOGGING;
767                                         rc = LDAP_SYNC_REFRESH_REQUIRED;
768                                 } else {
769                                         rc = -2;
770                                 }
771                                 goto done;
772                                 break;
773
774                         case LDAP_RES_INTERMEDIATE:
775                                 rc = ldap_parse_intermediate( si->si_ld, msg,
776                                         &retoid, &retdata, NULL, 0 );
777                                 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
778                                         ber_init2( ber, retdata, LBER_USE_DER );
779
780                                         switch ( si_tag = ber_peek_tag( ber, &len )) {
781                                         ber_tag_t tag;
782                                         case LDAP_TAG_SYNC_NEW_COOKIE:
783                                                 Debug( LDAP_DEBUG_SYNC,
784                                                         "do_syncrep2: %s - %s%s\n", 
785                                                         "LDAP_RES_INTERMEDIATE", 
786                                                         "NEW_COOKIE", "\n" );
787                                                 ber_scanf( ber, "tm", &tag, &cookie );
788                                                 break;
789                                         case LDAP_TAG_SYNC_REFRESH_DELETE:
790                                         case LDAP_TAG_SYNC_REFRESH_PRESENT:
791                                                 Debug( LDAP_DEBUG_SYNC,
792                                                         "do_syncrep2: %s - %s%s\n", 
793                                                         "LDAP_RES_INTERMEDIATE", 
794                                                         si_tag == LDAP_TAG_SYNC_REFRESH_PRESENT ?
795                                                         "REFRESH_PRESENT" : "REFRESH_DELETE",
796                                                         "\n" );
797                                                 if ( si_tag == LDAP_TAG_SYNC_REFRESH_DELETE ) {
798                                                         si->si_refreshDelete = 1;
799                                                 } else {
800                                                         si->si_refreshPresent = 1;
801                                                 }
802                                                 ber_scanf( ber, "t{" /*"}"*/, &tag );
803                                                 if ( ber_peek_tag( ber, &len ) == LDAP_TAG_SYNC_COOKIE )
804                                                 {
805                                                         ber_scanf( ber, "m", &cookie );
806                                                         if ( !BER_BVISNULL( &cookie ) ) {
807                                                                 ch_free( syncCookie.octet_str.bv_val );
808                                                                 ber_dupbv( &syncCookie.octet_str, &cookie );
809                                                         }
810                                                         if ( !BER_BVISNULL( &syncCookie.octet_str ) )
811                                                         {
812                                                                 slap_parse_sync_cookie( &syncCookie, NULL );
813                                                         }
814                                                 }
815                                                 if ( ber_peek_tag( ber, &len ) ==
816                                                         LDAP_TAG_REFRESHDONE )
817                                                 {
818                                                         ber_scanf( ber, "b", &refreshDone );
819                                                 }
820                                                 ber_scanf( ber, /*"{"*/ "}" );
821                                                 break;
822                                         case LDAP_TAG_SYNC_ID_SET:
823                                                 Debug( LDAP_DEBUG_SYNC,
824                                                         "do_syncrep2: %s - %s%s\n", 
825                                                         "LDAP_RES_INTERMEDIATE", 
826                                                         "SYNC_ID_SET",
827                                                         "\n" );
828                                                 ber_scanf( ber, "t{" /*"}"*/, &tag );
829                                                 if ( ber_peek_tag( ber, &len ) ==
830                                                         LDAP_TAG_SYNC_COOKIE )
831                                                 {
832                                                         ber_scanf( ber, "m", &cookie );
833                                                         if ( !BER_BVISNULL( &cookie ) ) {
834                                                                 ch_free( syncCookie.octet_str.bv_val );
835                                                                 ber_dupbv( &syncCookie.octet_str, &cookie );
836                                                         }
837                                                         if ( !BER_BVISNULL( &syncCookie.octet_str ) )
838                                                         {
839                                                                 slap_parse_sync_cookie( &syncCookie, NULL );
840                                                         }
841                                                 }
842                                                 if ( ber_peek_tag( ber, &len ) ==
843                                                         LDAP_TAG_REFRESHDELETES )
844                                                 {
845                                                         ber_scanf( ber, "b", &refreshDeletes );
846                                                 }
847                                                 ber_scanf( ber, "[W]", &syncUUIDs );
848                                                 ber_scanf( ber, /*"{"*/ "}" );
849                                                 if ( refreshDeletes ) {
850                                                         syncrepl_del_nonpresent( op, si, syncUUIDs );
851                                                         ber_bvarray_free_x( syncUUIDs, op->o_tmpmemctx );
852                                                 } else {
853                                                         for ( i = 0; !BER_BVISNULL( &syncUUIDs[i] ); i++ ) {
854                                                                 struct berval *syncuuid_bv;
855                                                                 syncuuid_bv = ber_dupbv( NULL, &syncUUIDs[i] );
856                                                                 slap_sl_free( syncUUIDs[i].bv_val,op->o_tmpmemctx );
857                                                                 avl_insert( &si->si_presentlist,
858                                                                         (caddr_t) syncuuid_bv,
859                                                                         syncuuid_cmp, avl_dup_error );
860                                                         }
861                                                         slap_sl_free( syncUUIDs, op->o_tmpmemctx );
862                                                 }
863                                                 break;
864                                         default:
865                                                 Debug( LDAP_DEBUG_ANY,
866                                                         "do_syncrep2 : unknown syncinfo tag (%ld)\n",
867                                                 (long) si_tag, 0, 0 );
868                                                 ldap_memfree( retoid );
869                                                 ber_bvfree( retdata );
870                                                 continue;
871                                         }
872
873                                         if ( BER_BVISNULL( &syncCookie_req.ctxcsn )) {
874                                                 match = -1;
875                                         } else if ( BER_BVISNULL( &syncCookie.ctxcsn )) {
876                                                 match = 1;
877                                         } else {
878                                                 value_match( &match, slap_schema.si_ad_entryCSN,
879                                                         slap_schema.si_ad_entryCSN->ad_type->sat_ordering,
880                                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
881                                                         &syncCookie_req.ctxcsn,
882                                                         &syncCookie.ctxcsn, &text );
883                                         }
884
885                                         if ( !BER_BVISNULL( &syncCookie.ctxcsn ) &&
886                                                 match < 0 )
887                                         {
888                                                 syncrepl_updateCookie( si, op, psub, &syncCookie);
889                                         }
890
891                                         if ( si->si_refreshPresent == 1 ) {
892                                                 if ( match < 0 ) {
893                                                         syncrepl_del_nonpresent( op, si, NULL );
894                                                 }
895                                         } 
896
897                                         ldap_memfree( retoid );
898                                         ber_bvfree( retdata );
899                                         break;
900
901                                 } else {
902                                         Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
903                                                 "unknown intermediate response (%d)\n",
904                                                 rc, 0, 0 );
905                                         ldap_memfree( retoid );
906                                         ber_bvfree( retdata );
907                                         break;
908                                 }
909                                 break;
910
911                         default:
912                                 Debug( LDAP_DEBUG_ANY, "do_syncrep2 : "
913                                         "unknown message\n", 0, 0, 0 );
914                                 break;
915
916                         }
917                         if ( !BER_BVISNULL( &syncCookie.octet_str )) {
918                                 slap_sync_cookie_free( &syncCookie_req, 0 );
919                                 slap_dup_sync_cookie( &syncCookie_req, &syncCookie );
920                                 slap_sync_cookie_free( &syncCookie, 0 );
921                         }
922                 }
923                 ldap_msgfree( res );
924                 res = NULL;
925         }
926
927         if ( rc == -1 ) {
928                 const char *errstr;
929
930                 ldap_get_option( si->si_ld, LDAP_OPT_ERROR_NUMBER, &rc );
931                 errstr = ldap_err2string( rc );
932                 
933                 Debug( LDAP_DEBUG_ANY,
934                         "do_syncrep2 : %s\n", errstr, 0, 0 );
935         }
936
937 done:
938         slap_sync_cookie_free( &syncCookie, 0 );
939         slap_sync_cookie_free( &syncCookie_req, 0 );
940
941         if ( res ) ldap_msgfree( res );
942
943         if ( rc && rc != LDAP_SYNC_REFRESH_REQUIRED && si->si_ld ) {
944                 ldap_unbind_ext( si->si_ld, NULL, NULL );
945                 si->si_ld = NULL;
946         }
947
948         return rc;
949 }
950
951 static void *
952 do_syncrepl(
953         void    *ctx,
954         void    *arg )
955 {
956         struct re_s* rtask = arg;
957         syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
958         Connection conn = {0};
959         OperationBuffer opbuf;
960         Operation *op;
961         int rc = LDAP_SUCCESS;
962         int first = 0;
963         int dostop = 0;
964         ber_socket_t s;
965         int i, defer = 1;
966         Backend *be;
967
968         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
969
970         if ( si == NULL )
971                 return NULL;
972
973         ldap_pvt_thread_mutex_lock( &si->si_mutex );
974
975         switch( abs( si->si_type )) {
976         case LDAP_SYNC_REFRESH_ONLY:
977         case LDAP_SYNC_REFRESH_AND_PERSIST:
978                 break;
979         default:
980                 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
981                 return NULL;
982         }
983
984         if ( slapd_shutdown ) {
985                 if ( si->si_ld ) {
986                         ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
987                         connection_client_stop( s );
988                         ldap_unbind_ext( si->si_ld, NULL, NULL );
989                         si->si_ld = NULL;
990                 }
991                 ldap_pvt_thread_mutex_unlock( &si->si_mutex );
992                 return NULL;
993         }
994
995         op = (Operation *) &opbuf;
996         connection_fake_init( &conn, op, ctx );
997
998         /* use global malloc for now */
999         op->o_tmpmemctx = NULL;
1000         op->o_tmpmfuncs = &ch_mfuncs;
1001
1002         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
1003         op->o_bd = be = si->si_be;
1004         op->o_dn = op->o_bd->be_rootdn;
1005         op->o_ndn = op->o_bd->be_rootndn;
1006
1007         /* Establish session, do search */
1008         if ( !si->si_ld ) {
1009 reload:
1010                 first = 1;
1011                 si->si_refreshDelete = 0;
1012                 si->si_refreshPresent = 0;
1013                 rc = do_syncrep1( op, si );
1014         }
1015
1016         /* Process results */
1017         if ( rc == LDAP_SUCCESS ) {
1018                 ldap_get_option( si->si_ld, LDAP_OPT_DESC, &s );
1019
1020                 rc = do_syncrep2( op, si );
1021                 if ( rc == LDAP_SYNC_REFRESH_REQUIRED ) 
1022                         goto reload;
1023
1024                 if ( abs(si->si_type) == LDAP_SYNC_REFRESH_AND_PERSIST ) {
1025                         /* If we succeeded, enable the connection for further listening.
1026                          * If we failed, tear down the connection and reschedule.
1027                          */
1028                         if ( rc == LDAP_SUCCESS ) {
1029                                 if ( first ) {
1030                                         rc = connection_client_setup( s, do_syncrepl, arg );
1031                                 } else {
1032                                         connection_client_enable( s );
1033                                 } 
1034                         } else if ( !first ) {
1035                                 dostop = 1;
1036                         }
1037                 } else {
1038                         if ( rc == -2 ) rc = 0;
1039                 }
1040         }
1041
1042         /* At this point, we have 4 cases:
1043          * 1) for any hard failure, give up and remove this task
1044          * 2) for ServerDown, reschedule this task to run
1045          * 3) for Refresh and Success, reschedule to run
1046          * 4) for Persist and Success, reschedule to defer
1047          */
1048         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1049
1050         if ( ldap_pvt_runqueue_isrunning( &slapd_rq, rtask )) {
1051                 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1052         }
1053
1054         if ( dostop ) {
1055                 connection_client_stop( s );
1056         }
1057
1058         if ( rc == LDAP_SUCCESS ) {
1059                 if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
1060                         defer = 0;
1061                 }
1062                 rtask->interval.tv_sec = si->si_interval;
1063                 ldap_pvt_runqueue_resched( &slapd_rq, rtask, defer );
1064                 if ( si->si_retrynum ) {
1065                         for ( i = 0; si->si_retrynum_init[i] != -2; i++ ) {
1066                                 si->si_retrynum[i] = si->si_retrynum_init[i];
1067                         }
1068                         si->si_retrynum[i] = -2;
1069                 }
1070         } else {
1071                 for ( i = 0; si->si_retrynum && si->si_retrynum[i] <= 0; i++ ) {
1072                         if ( si->si_retrynum[i] == -1  || si->si_retrynum[i] == -2 )
1073                                 break;
1074                 }
1075
1076                 if ( !si->si_retrynum || si->si_retrynum[i] == -2 ) {
1077                         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1078                 } else if ( si->si_retrynum[i] >= -1 ) {
1079                         if ( si->si_retrynum[i] > 0 )
1080                                 si->si_retrynum[i]--;
1081                         rtask->interval.tv_sec = si->si_retryinterval[i];
1082                         ldap_pvt_runqueue_resched( &slapd_rq, rtask, 0 );
1083                         slap_wake_listener();
1084                 }
1085         }
1086         
1087         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1088         ldap_pvt_thread_mutex_unlock( &si->si_mutex );
1089
1090         return NULL;
1091 }
1092
1093 typedef struct logschema {
1094         struct berval ls_dn;
1095         struct berval ls_req;
1096         struct berval ls_mod;
1097         struct berval ls_newRdn;
1098         struct berval ls_delRdn;
1099         struct berval ls_newSup;
1100 } logschema;
1101
1102 static logschema changelog_sc = {
1103         BER_BVC("targetDN"),
1104         BER_BVC("changeType"),
1105         BER_BVC("changes"),
1106         BER_BVC("newRDN"),
1107         BER_BVC("deleteOldRDN"),
1108         BER_BVC("newSuperior")
1109 };
1110
1111 static logschema accesslog_sc = {
1112         BER_BVC("reqDN"),
1113         BER_BVC("reqType"),
1114         BER_BVC("reqMod"),
1115         BER_BVC("reqNewRDN"),
1116         BER_BVC("reqDeleteOldRDN"),
1117         BER_BVC("reqNewSuperior")
1118 };
1119
1120 static slap_verbmasks modops[] = {
1121         { BER_BVC("add"), LDAP_REQ_ADD },
1122         { BER_BVC("delete"), LDAP_REQ_DELETE },
1123         { BER_BVC("modify"), LDAP_REQ_MODIFY },
1124         { BER_BVC("modrdn"), LDAP_REQ_MODRDN},
1125         { BER_BVNULL, 0 }
1126 };
1127
1128 static Modifications *
1129 syncrepl_accesslog_mods(
1130         syncinfo_t *si,
1131         struct berval *vals
1132 )
1133 {
1134         char *colon;
1135         const char *text;
1136         AttributeDescription *ad;
1137         struct berval bv, bv2;
1138         short op;
1139         Modifications *mod = NULL, *modlist = NULL, **modtail;
1140         int i;
1141
1142         modtail = &modlist;
1143
1144         for (i=0; !BER_BVISNULL( &vals[i] ); i++) {
1145                 ad = NULL;
1146                 bv = vals[i];
1147
1148                 colon = strchr( bv.bv_val, ':' );
1149                 if ( !colon )
1150                         continue;       /* invalid */
1151                 bv.bv_len = colon - bv.bv_val;
1152                 if ( slap_bv2ad( &bv, &ad, &text )) {
1153                         /* Invalid */
1154                         continue;
1155                 }
1156                 /* Ignore dynamically generated attrs */
1157                 if ( ad->ad_type->sat_flags & SLAP_AT_DYNAMIC )
1158                         continue;
1159                 /* Ignore excluded attrs */
1160                 if ( ldap_charray_inlist( si->si_exattrs,
1161                         ad->ad_type->sat_cname.bv_val ))
1162                         continue;
1163
1164                 switch(colon[1]) {
1165                 case '+':       op = LDAP_MOD_ADD; break;
1166                 case '-':       op = LDAP_MOD_DELETE; break;
1167                 case '=':       op = LDAP_MOD_REPLACE; break;
1168                 case '#':       op = LDAP_MOD_INCREMENT; break;
1169                 default:        continue;
1170                 }
1171
1172                 if ( !mod || ad != mod->sml_desc || op != mod->sml_op ) {
1173                         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1174                         mod->sml_flags = 0;
1175                         mod->sml_op = op;
1176                         mod->sml_next = NULL;
1177                         mod->sml_desc = ad;
1178                         mod->sml_type = ad->ad_cname;
1179                         mod->sml_values = NULL;
1180                         mod->sml_nvalues = NULL;
1181
1182                         *modtail = mod;
1183                         modtail = &mod->sml_next;
1184                 }
1185                 bv.bv_val = colon + 3;
1186                 bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1187                 ber_dupbv( &bv2, &bv );
1188                 ber_bvarray_add( &mod->sml_values, &bv2 );
1189         }
1190         return modlist;
1191 }
1192
1193 static Modifications *
1194 syncrepl_changelog_mods(
1195         syncinfo_t *si,
1196         struct berval *vals
1197 )
1198 {
1199         return NULL;    /* FIXME */
1200 }
1201
1202 static int
1203 syncrepl_message_to_op(
1204         syncinfo_t      *si,
1205         Operation       *op,
1206         LDAPMessage     *msg
1207 )
1208 {
1209         BerElement      *ber = NULL;
1210         Modifications   *modlist = NULL;
1211         logschema *ls;
1212         SlapReply rs = { REP_RESULT };
1213         slap_callback cb = { NULL, null_callback, NULL, NULL };
1214
1215         const char      *text;
1216         char txtbuf[SLAP_TEXT_BUFLEN];
1217         size_t textlen = sizeof txtbuf;
1218
1219         struct berval   bdn, dn = BER_BVNULL, ndn;
1220         struct berval   bv, *bvals = NULL;
1221         struct berval   rdn = BER_BVNULL, sup = BER_BVNULL,
1222                 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1223                 psup = BER_BVNULL, nsup = BER_BVNULL;
1224         int             rc, deleteOldRdn = 0;
1225
1226         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1227                 Debug( LDAP_DEBUG_ANY,
1228                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1229                 return -1;
1230         }
1231
1232         if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1233                 ls = &accesslog_sc;
1234         else
1235                 ls = &changelog_sc;
1236
1237         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1238
1239         if ( rc != LDAP_SUCCESS ) {
1240                 Debug( LDAP_DEBUG_ANY,
1241                         "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1242                 return rc;
1243         }
1244
1245         op->o_tag = LBER_DEFAULT;
1246
1247         while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1248                 == LDAP_SUCCESS ) {
1249                 if ( bv.bv_val == NULL )
1250                         break;
1251
1252                 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1253                         bdn = bvals[0];
1254                         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1255                         ber_dupbv( &op->o_req_dn, &dn );
1256                         ber_dupbv( &op->o_req_ndn, &ndn );
1257                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1258                         slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1259                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1260                         int i = verb_to_mask( bvals[0].bv_val, modops );
1261                         if ( i < 0 ) {
1262                                 Debug( LDAP_DEBUG_ANY,
1263                                         "syncrepl_message_to_op : unknown op %s",
1264                                         bvals[0].bv_val, 0, 0 );
1265                                 ch_free( bvals );
1266                                 rc = -1;
1267                                 goto done;
1268                         }
1269                         op->o_tag = modops[i].mask;
1270                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1271                         /* Parse attribute into modlist */
1272                         if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1273                                 modlist = syncrepl_accesslog_mods( si, bvals );
1274                         else
1275                                 modlist = syncrepl_changelog_mods( si, bvals );
1276                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1277                         rdn = bvals[0];
1278                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1279                         if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1280                                 deleteOldRdn = 1;
1281                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1282                         sup = bvals[0];
1283                 } else if ( !ber_bvstrcasecmp( &bv,
1284                         &slap_schema.si_ad_entryCSN->ad_cname )) {
1285                         slap_queue_csn( op, bvals );
1286                 }
1287                 ch_free( bvals );
1288         }
1289
1290         /* If we didn't get a mod type or a target DN, bail out */
1291         if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1292                 rc = -1;
1293                 goto done;
1294         }
1295
1296         op->o_callback = &cb;
1297
1298         switch( op->o_tag ) {
1299         case LDAP_REQ_ADD:
1300         case LDAP_REQ_MODIFY:
1301                 /* If we didn't get required data, bail */
1302                 if ( !modlist ) goto done;
1303
1304                 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1305
1306                 if ( rc != LDAP_SUCCESS ) {
1307                         Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1308                                 text, 0, 0 );
1309                         goto done;
1310                 }
1311
1312                 if ( op->o_tag == LDAP_REQ_ADD ) {
1313                         op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1314                         op->ora_e->e_name = op->o_req_dn;
1315                         op->ora_e->e_nname = op->o_req_ndn;
1316                         rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1317                         if( rc != LDAP_SUCCESS ) {
1318                                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1319                                         text, 0, 0 );
1320                         } else {
1321                                 rc = op->o_bd->be_add( op, &rs );
1322                         }
1323                         be_entry_release_w( op, op->ora_e );
1324                 } else {
1325                         op->orm_modlist = modlist;
1326                         rc = op->o_bd->be_modify( op, &rs );
1327                 }
1328                 break;
1329         case LDAP_REQ_MODRDN:
1330                 if ( BER_BVISNULL( &rdn )) goto done;
1331
1332                 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1333                         goto done;
1334                 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1335                         goto done;
1336                 if ( !BER_BVISNULL( &sup )) {
1337                         if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1338                                 goto done;
1339                         op->orr_newSup = &psup;
1340                         op->orr_nnewSup = &nsup;
1341                 }
1342                 op->orr_newrdn = prdn;
1343                 op->orr_nnewrdn = nrdn;
1344                 op->orr_deleteoldrdn = deleteOldRdn;
1345                 rc = op->o_bd->be_modrdn( op, &rs );
1346                 break;
1347         }
1348 done:
1349         slap_graduate_commit_csn( op );
1350         if ( modlist )
1351                 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1352         if ( !BER_BVISNULL( &rdn )) {
1353                 if ( !BER_BVISNULL( &nsup ))
1354                         ch_free( nsup.bv_val );
1355                 if ( !BER_BVISNULL( &psup ))
1356                         ch_free( psup.bv_val );
1357                 if ( !BER_BVISNULL( &nrdn ))
1358                         ch_free( nrdn.bv_val );
1359                 if ( !BER_BVISNULL( &prdn ))
1360                         ch_free( prdn.bv_val );
1361         }
1362         ber_free ( ber, 0 );
1363         return rc;
1364 }
1365
1366 static int
1367 syncrepl_message_to_entry(
1368         syncinfo_t      *si,
1369         Operation       *op,
1370         LDAPMessage     *msg,
1371         Modifications   **modlist,
1372         Entry                   **entry,
1373         int             syncstate
1374 )
1375 {
1376         Entry           *e = NULL;
1377         BerElement      *ber = NULL;
1378         Modifications   tmp;
1379         Modifications   *mod;
1380         Modifications   **modtail = modlist;
1381
1382         const char      *text;
1383         char txtbuf[SLAP_TEXT_BUFLEN];
1384         size_t textlen = sizeof txtbuf;
1385
1386         struct berval   bdn = {0, NULL}, dn, ndn;
1387         int             rc;
1388
1389         *modlist = NULL;
1390
1391         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1392                 Debug( LDAP_DEBUG_ANY,
1393                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1394                 return -1;
1395         }
1396
1397         op->o_tag = LDAP_REQ_ADD;
1398
1399         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1400
1401         if ( rc != LDAP_SUCCESS ) {
1402                 Debug( LDAP_DEBUG_ANY,
1403                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1404                 return rc;
1405         }
1406
1407         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1408         ber_dupbv( &op->o_req_dn, &dn );
1409         ber_dupbv( &op->o_req_ndn, &ndn );
1410         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1411         slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1412
1413         if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1414                 if ( entry )
1415                         *entry = NULL;
1416                 return LDAP_SUCCESS;
1417         }
1418
1419         if ( entry == NULL ) {
1420                 return -1;
1421         }
1422
1423         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1424         *entry = e;
1425         e->e_name = op->o_req_dn;
1426         e->e_nname = op->o_req_ndn;
1427
1428         while ( ber_remaining( ber ) ) {
1429                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1430                         LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1431                 {
1432                         break;
1433                 }
1434
1435                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
1436
1437                 mod->sml_op = LDAP_MOD_REPLACE;
1438                 mod->sml_flags = 0;
1439                 mod->sml_next = NULL;
1440                 mod->sml_desc = NULL;
1441                 mod->sml_type = tmp.sml_type;
1442                 mod->sml_values = tmp.sml_values;
1443                 mod->sml_nvalues = NULL;
1444
1445                 *modtail = mod;
1446                 modtail = &mod->sml_next;
1447         }
1448
1449         if ( *modlist == NULL ) {
1450                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1451                         0, 0, 0 );
1452                 rc = -1;
1453                 goto done;
1454         }
1455
1456         rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1457
1458         if ( rc != LDAP_SUCCESS ) {
1459                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1460                         text, 0, 0 );
1461                 goto done;
1462         }
1463
1464         /* Strip out dynamically generated attrs */
1465         for ( modtail = modlist; *modtail ; ) {
1466                 mod = *modtail;
1467                 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1468                         *modtail = mod->sml_next;
1469                         slap_mod_free( &mod->sml_mod, 0 );
1470                         ch_free( mod );
1471                 } else {
1472                         modtail = &mod->sml_next;
1473                 }
1474         }
1475
1476         /* Strip out attrs in exattrs list */
1477         for ( modtail = modlist; *modtail ; ) {
1478                 mod = *modtail;
1479                 if ( ldap_charray_inlist( si->si_exattrs,
1480                                         mod->sml_desc->ad_type->sat_cname.bv_val )) {
1481                         *modtail = mod->sml_next;
1482                         slap_mod_free( &mod->sml_mod, 0 );
1483                         ch_free( mod );
1484                 } else {
1485                         modtail = &mod->sml_next;
1486                 }
1487         }
1488         
1489         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1490         if( rc != LDAP_SUCCESS ) {
1491                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1492                         text, 0, 0 );
1493         }
1494
1495 done:
1496         ber_free ( ber, 0 );
1497         if ( rc != LDAP_SUCCESS ) {
1498                 if ( e ) {
1499                         entry_free( e );
1500                         *entry = e = NULL;
1501                 }
1502         }
1503
1504         return rc;
1505 }
1506
1507 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1508
1509 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1510  * entry if a previous refresh was interrupted before sending us a new
1511  * context state. We try to compare the new entry to the existing entry
1512  * and ignore the new entry if they are the same.
1513  *
1514  * Also, we may get an update where the entryDN has changed, due to
1515  * a ModDn on the provider. We detect this as well, so we can issue
1516  * the corresponding operation locally.
1517  *
1518  * In the case of a modify, we get a list of all the attributes
1519  * in the original entry. Rather than deleting the entry and re-adding it,
1520  * we issue a Modify request that deletes all the attributes and adds all
1521  * the new ones. This avoids the issue of trying to delete/add a non-leaf
1522  * entry.
1523  *
1524  * We don't try to otherwise distinguish ModDN from Modify; in the case of
1525  * a ModDN we will issue both operations on the local database.
1526  */
1527 typedef struct dninfo {
1528         Entry *new_entry;
1529         struct berval dn;
1530         struct berval ndn;
1531         int renamed;    /* Was an existing entry renamed? */
1532         int wasChanged; /* are the attributes changed? */
1533         int attrs;              /* how many attribute types are in the ads list */
1534         AttributeDescription **ads;
1535 } dninfo;
1536
1537 static int
1538 syncrepl_entry(
1539         syncinfo_t* si,
1540         Operation *op,
1541         Entry* entry,
1542         Modifications** modlist,
1543         int syncstate,
1544         struct berval* syncUUID,
1545         struct sync_cookie* syncCookie_req,
1546         struct berval* syncCSN )
1547 {
1548         Backend *be = op->o_bd;
1549         slap_callback   cb = { NULL, NULL, NULL, NULL };
1550         struct berval   *syncuuid_bv = NULL;
1551         struct berval   syncUUID_strrep = BER_BVNULL;
1552         struct berval   uuid_bv = BER_BVNULL;
1553
1554         SlapReply       rs_search = {REP_RESULT};
1555         SlapReply       rs_delete = {REP_RESULT};
1556         SlapReply       rs_add = {REP_RESULT};
1557         SlapReply       rs_modify = {REP_RESULT};
1558         Filter f = {0};
1559 #ifdef LDAP_COMP_MATCH
1560         AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1561 #else
1562         AttributeAssertion ava = { NULL, BER_BVNULL };
1563 #endif
1564         int rc = LDAP_SUCCESS;
1565         int ret = LDAP_SUCCESS;
1566
1567         struct berval pdn = BER_BVNULL;
1568         dninfo dni = {0};
1569         int     retry = 1;
1570
1571         switch( syncstate ) {
1572         case LDAP_SYNC_PRESENT:
1573                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1574                                         "syncrepl_entry",
1575                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1576                 break;
1577         case LDAP_SYNC_ADD:
1578                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1579                                         "syncrepl_entry",
1580                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1581                 break;
1582         case LDAP_SYNC_DELETE:
1583                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1584                                         "syncrepl_entry",
1585                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1586                 break;
1587         case LDAP_SYNC_MODIFY:
1588                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1589                                         "syncrepl_entry",
1590                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1591                 break;
1592         default:
1593                 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1594                                         "syncrepl_entry",
1595                                         "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1596         }
1597
1598         if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1599                 if ( !si->si_refreshPresent ) {
1600                         syncuuid_bv = ber_dupbv( NULL, syncUUID );
1601                         avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1602                                 syncuuid_cmp, avl_dup_error );
1603                 }
1604         }
1605
1606         if ( syncstate == LDAP_SYNC_PRESENT ) {
1607                 return 0;
1608         } else if ( syncstate != LDAP_SYNC_DELETE ) {
1609                 if ( entry == NULL ) {
1610                         return 0;
1611                 }
1612         }
1613
1614         f.f_choice = LDAP_FILTER_EQUALITY;
1615         f.f_ava = &ava;
1616         ava.aa_desc = slap_schema.si_ad_entryUUID;
1617         (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1618         ava.aa_value = *syncUUID;
1619         op->ors_filter = &f;
1620
1621         op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1622         op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1623                 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx ); 
1624         AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1625         AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1626                 syncUUID->bv_val, syncUUID->bv_len );
1627         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1628         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1629
1630         op->o_tag = LDAP_REQ_SEARCH;
1631         op->ors_scope = LDAP_SCOPE_SUBTREE;
1632         op->ors_deref = LDAP_DEREF_NEVER;
1633
1634         /* get the entry for this UUID */
1635         op->o_req_dn = si->si_base;
1636         op->o_req_ndn = si->si_base;
1637
1638         op->o_time = slap_get_time();
1639         op->ors_tlimit = SLAP_NO_LIMIT;
1640         op->ors_slimit = 1;
1641
1642         op->ors_attrs = slap_anlist_all_attributes;
1643         op->ors_attrsonly = 0;
1644
1645         /* set callback function */
1646         op->o_callback = &cb;
1647         cb.sc_response = dn_callback;
1648         cb.sc_private = &dni;
1649         dni.new_entry = entry;
1650
1651         if ( limits_check( op, &rs_search ) == 0 ) {
1652                 rc = be->be_search( op, &rs_search );
1653                 Debug( LDAP_DEBUG_SYNC,
1654                                 "syncrepl_entry: %s (%d)\n", 
1655                                 "be_search", rc, 0 );
1656         }
1657
1658         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1659                 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1660         }
1661
1662         cb.sc_response = null_callback;
1663         cb.sc_private = si;
1664
1665         if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1666                 Debug( LDAP_DEBUG_SYNC,
1667                                 "syncrepl_entry: %s\n",
1668                                 entry->e_name.bv_val, 0, 0 );
1669         } else {
1670                 Debug( LDAP_DEBUG_SYNC,
1671                                 "syncrepl_entry: %s\n",
1672                                 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1673         }
1674
1675         if ( syncstate != LDAP_SYNC_DELETE ) {
1676                 Attribute       *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1677
1678                 if ( a == NULL ) {
1679                         /* add if missing */
1680                         attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1681                                 &syncUUID_strrep, syncUUID );
1682
1683                 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1684                         /* replace only if necessary */
1685                         if ( a->a_nvals != a->a_vals ) {
1686                                 ber_memfree( a->a_nvals[0].bv_val );
1687                                 ber_dupbv( &a->a_nvals[0], syncUUID );
1688                         }
1689                         ber_memfree( a->a_vals[0].bv_val );
1690                         ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1691                 }
1692         }
1693
1694         switch ( syncstate ) {
1695         case LDAP_SYNC_ADD:
1696         case LDAP_SYNC_MODIFY:
1697                 {
1698                         Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1699                         if ( a )
1700                                 op->o_csn = a->a_vals[0];
1701                 }
1702 retry_add:;
1703                 if ( BER_BVISNULL( &dni.dn )) {
1704
1705                         op->o_req_dn = entry->e_name;
1706                         op->o_req_ndn = entry->e_nname;
1707                         op->o_tag = LDAP_REQ_ADD;
1708                         op->ora_e = entry;
1709
1710                         rc = be->be_add( op, &rs_add );
1711                         Debug( LDAP_DEBUG_SYNC,
1712                                         "syncrepl_entry: %s (%d)\n", 
1713                                         "be_add", rc, 0 );
1714                         switch ( rs_add.sr_err ) {
1715                         case LDAP_SUCCESS:
1716                                 be_entry_release_w( op, entry );
1717                                 ret = 0;
1718                                 break;
1719
1720                         case LDAP_REFERRAL:
1721                         /* we assume that LDAP_NO_SUCH_OBJECT is returned 
1722                          * only if the suffix entry is not present */
1723                         case LDAP_NO_SUCH_OBJECT:
1724                                 syncrepl_add_glue( op, entry );
1725                                 ret = 0;
1726                                 break;
1727
1728                         /* if an entry was added via syncrepl_add_glue(),
1729                          * it likely has no entryUUID, so the previous
1730                          * be_search() doesn't find it.  In this case,
1731                          * give syncrepl a chance to modify it. Also
1732                          * allow for entries that were recreated with the
1733                          * same DN but a different entryUUID.
1734                          */
1735                         case LDAP_ALREADY_EXISTS:
1736                                 if ( retry ) {
1737                                         Operation       op2 = *op;
1738                                         SlapReply       rs2 = { 0 };
1739                                         slap_callback   cb2 = { 0 };
1740
1741                                         op2.o_tag = LDAP_REQ_SEARCH;
1742                                         op2.o_req_dn = entry->e_name;
1743                                         op2.o_req_ndn = entry->e_nname;
1744                                         op2.ors_scope = LDAP_SCOPE_BASE;
1745                                         op2.ors_deref = LDAP_DEREF_NEVER;
1746                                         op2.ors_attrs = slap_anlist_all_attributes;
1747                                         op2.ors_attrsonly = 0;
1748                                         op2.ors_limit = NULL;
1749                                         op2.ors_slimit = 1;
1750                                         op2.ors_tlimit = SLAP_NO_LIMIT;
1751
1752                                         f.f_choice = LDAP_FILTER_PRESENT;
1753                                         f.f_desc = slap_schema.si_ad_objectClass;
1754                                         op2.ors_filter = &f;
1755                                         op2.ors_filterstr = generic_filterstr;
1756
1757                                         op2.o_callback = &cb2;
1758                                         cb2.sc_response = dn_callback;
1759                                         cb2.sc_private = &dni;
1760
1761                                         be->be_search( &op2, &rs2 );
1762
1763                                         retry = 0;
1764                                         goto retry_add;
1765                                 }
1766                                 /* FALLTHRU */
1767
1768                         default:
1769                                 Debug( LDAP_DEBUG_ANY,
1770                                         "syncrepl_entry : be_add failed (%d)\n",
1771                                         rs_add.sr_err, 0, 0 );
1772                                 ret = 1;
1773                                 break;
1774                         }
1775                         goto done;
1776                 }
1777                 /* FALLTHRU */
1778                 op->o_req_dn = dni.dn;
1779                 op->o_req_ndn = dni.ndn;
1780                 if ( dni.renamed ) {
1781                         struct berval noldp, newp, nnewp;
1782
1783                         op->o_tag = LDAP_REQ_MODRDN;
1784                         dnRdn( &entry->e_name, &op->orr_newrdn );
1785                         dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1786
1787                         dnParent( &dni.ndn, &noldp );
1788                         dnParent( &entry->e_nname, &nnewp );
1789                         if ( !dn_match( &noldp, &newp )) {
1790                                 dnParent( &entry->e_name, &newp );
1791                                 op->orr_newSup = &newp;
1792                                 op->orr_nnewSup = &nnewp;
1793                         }
1794                         op->orr_deleteoldrdn = 0;
1795                         rc = be->be_modrdn( op, &rs_modify );
1796                         Debug( LDAP_DEBUG_SYNC,
1797                                         "syncrepl_entry: %s (%d)\n", 
1798                                         "be_modrdn", rc, 0 );
1799                         if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1800                                 op->o_req_dn = entry->e_name;
1801                                 op->o_req_ndn = entry->e_nname;
1802                         } else {
1803                                 ret = 1;
1804                                 goto done;
1805                         }
1806                 }
1807                 if ( dni.wasChanged ) {
1808                         Modifications *mod, *modhead = NULL;
1809                         Modifications *modtail = NULL;
1810                         int i;
1811
1812                         op->o_tag = LDAP_REQ_MODIFY;
1813
1814                         assert( *modlist != NULL );
1815
1816                         /* Delete all the old attrs */
1817                         for ( i = 0; i < dni.attrs; i++ ) {
1818                                 mod = ch_malloc( sizeof( Modifications ) );
1819                                 mod->sml_op = LDAP_MOD_DELETE;
1820                                 mod->sml_flags = 0;
1821                                 mod->sml_desc = dni.ads[i];
1822                                 mod->sml_type = mod->sml_desc->ad_cname;
1823                                 mod->sml_values = NULL;
1824                                 mod->sml_nvalues = NULL;
1825                                 if ( !modhead ) modhead = mod;
1826                                 if ( modtail ) {
1827                                         modtail->sml_next = mod;
1828                                 }
1829                                 modtail = mod;
1830                         }
1831
1832                         /* Append passed in list to ours */
1833                         if ( modtail ) {
1834                                 modtail->sml_next = *modlist;
1835                                 *modlist = modhead;
1836                         } else {
1837                                 mod = *modlist;
1838                         }
1839
1840                         /* Find end of this list */
1841                         for ( ; mod != NULL; mod = mod->sml_next ) {
1842                                 modtail = mod;
1843                         }
1844
1845                         mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1846                         mod->sml_op = LDAP_MOD_REPLACE;
1847                         mod->sml_flags = 0;
1848                         mod->sml_desc = slap_schema.si_ad_entryUUID;
1849                         mod->sml_type = mod->sml_desc->ad_cname;
1850                         ber_dupbv( &uuid_bv, &syncUUID_strrep );
1851                         ber_bvarray_add( &mod->sml_values, &uuid_bv );
1852                         ber_dupbv( &uuid_bv, syncUUID );
1853                         ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1854                         modtail->sml_next = mod;
1855                                         
1856                         op->o_tag = LDAP_REQ_MODIFY;
1857                         op->orm_modlist = *modlist;
1858
1859                         rc = be->be_modify( op, &rs_modify );
1860                         Debug( LDAP_DEBUG_SYNC,
1861                                         "syncrepl_entry: %s (%d)\n", 
1862                                         "be_modify", rc, 0 );
1863                         if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1864                                 Debug( LDAP_DEBUG_ANY,
1865                                         "syncrepl_entry : be_modify failed (%d)\n",
1866                                         rs_modify.sr_err, 0, 0 );
1867                         }
1868                 }
1869                 ret = 1;
1870                 goto done;
1871         case LDAP_SYNC_DELETE :
1872                 if ( !BER_BVISNULL( &dni.dn )) {
1873                         op->o_req_dn = dni.dn;
1874                         op->o_req_ndn = dni.ndn;
1875                         op->o_tag = LDAP_REQ_DELETE;
1876                         rc = be->be_delete( op, &rs_delete );
1877                         Debug( LDAP_DEBUG_SYNC,
1878                                         "syncrepl_entry: %s (%d)\n", 
1879                                         "be_delete", rc, 0 );
1880
1881                         while ( rs_delete.sr_err == LDAP_SUCCESS
1882                                 && op->o_delete_glue_parent ) {
1883                                 op->o_delete_glue_parent = 0;
1884                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1885                                         slap_callback cb = { NULL };
1886                                         cb.sc_response = slap_null_cb;
1887                                         dnParent( &op->o_req_ndn, &pdn );
1888                                         op->o_req_dn = pdn;
1889                                         op->o_req_ndn = pdn;
1890                                         op->o_callback = &cb;
1891                                         op->o_bd->be_delete( op, &rs_delete );
1892                                 } else {
1893                                         break;
1894                                 }
1895                         }
1896                 }
1897                 ret = 0;
1898                 goto done;
1899
1900         default :
1901                 Debug( LDAP_DEBUG_ANY,
1902                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1903                 ret = 1;
1904                 goto done;
1905         }
1906
1907 done :
1908         if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1909                 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1910                 BER_BVZERO( &syncUUID_strrep );
1911         }
1912         if ( dni.ads ) {
1913                 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1914         }
1915         if ( !BER_BVISNULL( &dni.ndn ) ) {
1916                 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1917         }
1918         if ( !BER_BVISNULL( &dni.dn ) ) {
1919                 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1920         }
1921         BER_BVZERO( &op->o_csn );
1922         return ret;
1923 }
1924
1925 static struct berval gcbva[] = {
1926         BER_BVC("top"),
1927         BER_BVC("glue"),
1928         BER_BVNULL
1929 };
1930
1931 #define NP_DELETE_ONE   2
1932
1933 static void
1934 syncrepl_del_nonpresent(
1935         Operation *op,
1936         syncinfo_t *si,
1937         BerVarray uuids )
1938 {
1939         Backend* be = op->o_bd;
1940         slap_callback   cb = { NULL };
1941         SlapReply       rs_search = {REP_RESULT};
1942         SlapReply       rs_delete = {REP_RESULT};
1943         SlapReply       rs_modify = {REP_RESULT};
1944         struct nonpresent_entry *np_list, *np_prev;
1945         int rc;
1946         AttributeName   an[2];
1947
1948         struct berval pdn = BER_BVNULL;
1949
1950         op->o_req_dn = si->si_base;
1951         op->o_req_ndn = si->si_base;
1952
1953         cb.sc_response = nonpresent_callback;
1954         cb.sc_private = si;
1955
1956         op->o_callback = &cb;
1957         op->o_tag = LDAP_REQ_SEARCH;
1958         op->ors_scope = si->si_scope;
1959         op->ors_deref = LDAP_DEREF_NEVER;
1960         op->o_time = slap_get_time();
1961         op->ors_tlimit = SLAP_NO_LIMIT;
1962
1963
1964         if ( uuids ) {
1965                 Filter uf;
1966 #ifdef LDAP_COMP_MATCH
1967                 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1968 #else
1969                 AttributeAssertion eq = { NULL, BER_BVNULL };
1970 #endif
1971                 int i;
1972
1973                 op->ors_attrsonly = 1;
1974                 op->ors_attrs = slap_anlist_no_attrs;
1975                 op->ors_limit = NULL;
1976                 op->ors_filter = &uf;
1977
1978                 uf.f_ava = &eq;
1979                 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1980                 uf.f_next = NULL;
1981                 uf.f_choice = LDAP_FILTER_EQUALITY;
1982                 si->si_refreshDelete |= NP_DELETE_ONE;
1983
1984                 for (i=0; uuids[i].bv_val; i++) {
1985                         op->ors_slimit = 1;
1986                         uf.f_av_value = uuids[i];
1987                         rc = be->be_search( op, &rs_search );
1988                 }
1989                 si->si_refreshDelete ^= NP_DELETE_ONE;
1990         } else {
1991                 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1992                 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1993                 an[0].an_desc = slap_schema.si_ad_entryUUID;
1994                 op->ors_attrs = an;
1995                 op->ors_slimit = SLAP_NO_LIMIT;
1996                 op->ors_attrsonly = 0;
1997                 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
1998                 op->ors_filterstr = si->si_filterstr;
1999                 op->o_nocaching = 1;
2000
2001                 if ( limits_check( op, &rs_search ) == 0 ) {
2002                         rc = be->be_search( op, &rs_search );
2003                 }
2004                 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2005         }
2006
2007         op->o_nocaching = 0;
2008
2009         if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2010
2011                 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2012
2013                 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2014                 while ( np_list != NULL ) {
2015                         LDAP_LIST_REMOVE( np_list, npe_link );
2016                         np_prev = np_list;
2017                         np_list = LDAP_LIST_NEXT( np_list, npe_link );
2018                         op->o_tag = LDAP_REQ_DELETE;
2019                         op->o_callback = &cb;
2020                         cb.sc_response = null_callback;
2021                         cb.sc_private = si;
2022                         op->o_req_dn = *np_prev->npe_name;
2023                         op->o_req_ndn = *np_prev->npe_nname;
2024                         rc = op->o_bd->be_delete( op, &rs_delete );
2025
2026                         if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2027                                 Modifications mod1, mod2;
2028                                 mod1.sml_op = LDAP_MOD_REPLACE;
2029                                 mod1.sml_flags = 0;
2030                                 mod1.sml_desc = slap_schema.si_ad_objectClass;
2031                                 mod1.sml_type = mod1.sml_desc->ad_cname;
2032                                 mod1.sml_values = &gcbva[0];
2033                                 mod1.sml_nvalues = NULL;
2034                                 mod1.sml_next = &mod2;
2035
2036                                 mod2.sml_op = LDAP_MOD_REPLACE;
2037                                 mod2.sml_flags = 0;
2038                                 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2039                                 mod2.sml_type = mod2.sml_desc->ad_cname;
2040                                 mod2.sml_values = &gcbva[1];
2041                                 mod2.sml_nvalues = NULL;
2042                                 mod2.sml_next = NULL;
2043
2044                                 op->o_tag = LDAP_REQ_MODIFY;
2045                                 op->orm_modlist = &mod1;
2046
2047                                 rc = be->be_modify( op, &rs_modify );
2048                         }
2049
2050                         while ( rs_delete.sr_err == LDAP_SUCCESS &&
2051                                         op->o_delete_glue_parent ) {
2052                                 op->o_delete_glue_parent = 0;
2053                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2054                                         slap_callback cb = { NULL };
2055                                         cb.sc_response = slap_null_cb;
2056                                         dnParent( &op->o_req_ndn, &pdn );
2057                                         op->o_req_dn = pdn;
2058                                         op->o_req_ndn = pdn;
2059                                         op->o_callback = &cb;
2060                                         /* give it a root privil ? */
2061                                         op->o_bd->be_delete( op, &rs_delete );
2062                                 } else {
2063                                         break;
2064                             }
2065                         }
2066
2067                         op->o_delete_glue_parent = 0;
2068
2069                         ber_bvfree( np_prev->npe_name );
2070                         ber_bvfree( np_prev->npe_nname );
2071                         ch_free( np_prev );
2072                 }
2073
2074                 slap_graduate_commit_csn( op );
2075         }
2076
2077         return;
2078 }
2079
2080 void
2081 syncrepl_add_glue(
2082         Operation* op,
2083         Entry *e )
2084 {
2085         Backend *be = op->o_bd;
2086         slap_callback cb = { NULL };
2087         Attribute       *a;
2088         int     rc;
2089         int suffrdns;
2090         int i;
2091         struct berval dn = {0, NULL};
2092         struct berval ndn = {0, NULL};
2093         Entry   *glue;
2094         SlapReply       rs_add = {REP_RESULT};
2095         char    *ptr, *comma;
2096
2097         op->o_tag = LDAP_REQ_ADD;
2098         op->o_callback = &cb;
2099         cb.sc_response = null_callback;
2100         cb.sc_private = NULL;
2101
2102         dn = e->e_name;
2103         ndn = e->e_nname;
2104
2105         /* count RDNs in suffix */
2106         if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2107                 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2108                         ptr++;
2109                         i++;
2110                 }
2111                 suffrdns = i;
2112         } else {
2113                 /* suffix is "" */
2114                 suffrdns = 0;
2115         }
2116
2117         /* Start with BE suffix */
2118         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2119                 comma = strrchr( dn.bv_val, ',' );
2120                 if ( ptr ) *ptr = ',';
2121                 if ( comma ) *comma = '\0';
2122                 ptr = comma;
2123         }
2124         if ( ptr ) {
2125                 *ptr++ = ',';
2126                 dn.bv_len -= ptr - dn.bv_val;
2127                 dn.bv_val = ptr;
2128         }
2129         /* the normalizedDNs are always the same length, no counting
2130          * required.
2131          */
2132         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2133                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2134                 ndn.bv_len = be->be_nsuffix[0].bv_len;
2135         }
2136
2137         while ( ndn.bv_val > e->e_nname.bv_val ) {
2138                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2139                 ber_dupbv( &glue->e_name, &dn );
2140                 ber_dupbv( &glue->e_nname, &ndn );
2141
2142                 a = ch_calloc( 1, sizeof( Attribute ));
2143                 a->a_desc = slap_schema.si_ad_objectClass;
2144
2145                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2146                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2147                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2148                 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2149
2150                 a->a_nvals = a->a_vals;
2151
2152                 a->a_next = glue->e_attrs;
2153                 glue->e_attrs = a;
2154
2155                 a = ch_calloc( 1, sizeof( Attribute ));
2156                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2157
2158                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2159                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2160                 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2161
2162                 a->a_nvals = a->a_vals;
2163
2164                 a->a_next = glue->e_attrs;
2165                 glue->e_attrs = a;
2166
2167                 op->o_req_dn = glue->e_name;
2168                 op->o_req_ndn = glue->e_nname;
2169                 op->ora_e = glue;
2170                 rc = be->be_add ( op, &rs_add );
2171                 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2172                         be_entry_release_w( op, glue );
2173                 } else {
2174                 /* incl. ALREADY EXIST */
2175                         entry_free( glue );
2176                 }
2177
2178                 /* Move to next child */
2179                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2180                         /* empty */
2181                 }
2182                 if ( ptr == e->e_name.bv_val ) break;
2183                 dn.bv_val = ++ptr;
2184                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2185                 for( ptr = ndn.bv_val-2;
2186                         ptr > e->e_nname.bv_val && *ptr != ',';
2187                         ptr--)
2188                 {
2189                         /* empty */
2190                 }
2191                 ndn.bv_val = ++ptr;
2192                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2193         }
2194
2195         op->o_req_dn = e->e_name;
2196         op->o_req_ndn = e->e_nname;
2197         op->ora_e = e;
2198         rc = be->be_add ( op, &rs_add );
2199         if ( rs_add.sr_err == LDAP_SUCCESS ) {
2200                 be_entry_release_w( op, e );
2201         } else {
2202                 entry_free( e );
2203         }
2204
2205         return;
2206 }
2207
2208 static void
2209 syncrepl_updateCookie(
2210         syncinfo_t *si,
2211         Operation *op,
2212         struct berval *pdn,
2213         struct sync_cookie *syncCookie )
2214 {
2215         Backend *be = op->o_bd;
2216         Modifications mod = { { 0 } };
2217         struct berval vals[ 2 ];
2218
2219         int rc;
2220
2221         slap_callback cb = { NULL };
2222         SlapReply       rs_modify = {REP_RESULT};
2223
2224         slap_sync_cookie_free( &si->si_syncCookie, 0 );
2225         slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2226
2227         mod.sml_op = LDAP_MOD_REPLACE;
2228         mod.sml_desc = slap_schema.si_ad_contextCSN;
2229         mod.sml_type = mod.sml_desc->ad_cname;
2230         mod.sml_values = vals;
2231         vals[0] = si->si_syncCookie.ctxcsn;
2232         vals[1].bv_val = NULL;
2233         vals[1].bv_len = 0;
2234
2235         slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2236
2237         op->o_tag = LDAP_REQ_MODIFY;
2238
2239         assert( si->si_rid < 1000 );
2240
2241         cb.sc_response = null_callback;
2242         cb.sc_private = si;
2243
2244         op->o_callback = &cb;
2245         op->o_req_dn = op->o_bd->be_suffix[0];
2246         op->o_req_ndn = op->o_bd->be_nsuffix[0];
2247
2248         /* update contextCSN */
2249         op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2250         op->orm_modlist = &mod;
2251         rc = be->be_modify( op, &rs_modify );
2252         op->o_msgid = 0;
2253
2254         if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2255                 Debug( LDAP_DEBUG_ANY,
2256                         "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2257         }
2258
2259         slap_graduate_commit_csn( op );
2260
2261         return;
2262 }
2263
2264 static int
2265 dn_callback(
2266         Operation*      op,
2267         SlapReply*      rs )
2268 {
2269         dninfo *dni = op->o_callback->sc_private;
2270
2271         if ( rs->sr_type == REP_SEARCH ) {
2272                 if ( !BER_BVISNULL( &dni->dn ) ) {
2273                         Debug( LDAP_DEBUG_ANY,
2274                                 "dn_callback : consistency error - "
2275                                 "entryUUID is not unique\n", 0, 0, 0 );
2276                 } else {
2277                         ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2278                         ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2279                         /* If there is a new entry, see if it differs from the old.
2280                          * We compare the non-normalized values so that cosmetic changes
2281                          * in the provider are always propagated.
2282                          */
2283                         if ( dni->new_entry ) {
2284                                 Attribute *old, *new;
2285                                 int i;
2286
2287                                 /* Did the DN change? Note that we don't explicitly try to
2288                                  * discover if the deleteOldRdn argument applies here. It
2289                                  * would save an unnecessary Modify if we detected it, but
2290                                  * that's a fair amount of trouble to compare the two attr
2291                                  * lists in detail. (Just test normalized DN; we ignore
2292                                  * insignificant changes here.)
2293                                  */
2294                                 if ( !dn_match( &rs->sr_entry->e_nname,
2295                                                 &dni->new_entry->e_nname ) )
2296                                 {
2297                                         dni->renamed = 1;
2298                                 }
2299
2300                                 for ( i = 0, old = rs->sr_entry->e_attrs;
2301                                                 old;
2302                                                 i++, old = old->a_next )
2303                                         ;
2304
2305                                 dni->attrs = i;
2306
2307                                 /* We assume that attributes are saved in the same order
2308                                  * in the remote and local databases. So if we walk through
2309                                  * the attributeDescriptions one by one they should match in
2310                                  * lock step. If not, we signal a change. Otherwise we test
2311                                  * all the values...
2312                                  */
2313                                 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2314                                                 old && new;
2315                                                 old = old->a_next, new = new->a_next )
2316                                 {
2317                                         if ( old->a_desc != new->a_desc ) {
2318                                                 dni->wasChanged = 1;
2319                                                 break;
2320                                         }
2321                                         for ( i = 0; ; i++ ) {
2322                                                 int nold, nnew;
2323                                                 nold = BER_BVISNULL( &old->a_vals[i] );
2324                                                 nnew = BER_BVISNULL( &new->a_vals[i] );
2325                                                 /* If both are empty, stop looking */
2326                                                 if ( nold && nnew ) {
2327                                                         break;
2328                                                 }
2329                                                 /* If they are different, stop looking */
2330                                                 if ( nold != nnew ) {
2331                                                         dni->wasChanged = 1;
2332                                                         break;
2333                                                 }
2334                                                 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2335                                                         dni->wasChanged = 1;
2336                                                         break;
2337                                                 }
2338                                         }
2339                                         if ( dni->wasChanged ) break;
2340                                 }
2341                                 if ( dni->wasChanged ) {
2342                                         dni->ads = op->o_tmpalloc( dni->attrs *
2343                                                 sizeof(AttributeDescription *), op->o_tmpmemctx );
2344                                         i = 0;
2345                                         for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2346                                                 dni->ads[i] = old->a_desc;
2347                                                 i++;
2348                                         }
2349                                 }
2350                         }
2351                 }
2352         } else if ( rs->sr_type == REP_RESULT ) {
2353                 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2354                         Debug( LDAP_DEBUG_ANY,
2355                                 "dn_callback : consistency error - "
2356                                 "entryUUID is not unique\n", 0, 0, 0 );
2357                 }
2358         }
2359
2360         return LDAP_SUCCESS;
2361 }
2362
2363 static int
2364 nonpresent_callback(
2365         Operation*      op,
2366         SlapReply*      rs )
2367 {
2368         syncinfo_t *si = op->o_callback->sc_private;
2369         Attribute *a;
2370         int count = 0;
2371         struct berval* present_uuid = NULL;
2372         struct nonpresent_entry *np_entry;
2373
2374         if ( rs->sr_type == REP_RESULT ) {
2375                 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2376                 si->si_presentlist = NULL;
2377
2378         } else if ( rs->sr_type == REP_SEARCH ) {
2379                 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2380                         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2381
2382                         if ( a == NULL ) return 0;
2383
2384                         present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2385                                 syncuuid_cmp );
2386                 }
2387
2388                 if ( present_uuid == NULL ) {
2389                         np_entry = (struct nonpresent_entry *)
2390                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2391                         np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2392                         np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2393                         LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2394
2395                 } else {
2396                         avl_delete( &si->si_presentlist,
2397                                         &a->a_nvals[0], syncuuid_cmp );
2398                         ch_free( present_uuid->bv_val );
2399                         ch_free( present_uuid );
2400                 }
2401         }
2402         return LDAP_SUCCESS;
2403 }
2404
2405 static int
2406 null_callback(
2407         Operation*      op,
2408         SlapReply*      rs )
2409 {
2410         if ( rs->sr_err != LDAP_SUCCESS &&
2411                 rs->sr_err != LDAP_REFERRAL &&
2412                 rs->sr_err != LDAP_ALREADY_EXISTS &&
2413                 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2414                 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2415         {
2416                 Debug( LDAP_DEBUG_ANY,
2417                         "null_callback : error code 0x%x\n",
2418                         rs->sr_err, 0, 0 );
2419         }
2420         return LDAP_SUCCESS;
2421 }
2422
2423 static struct berval *
2424 slap_uuidstr_from_normalized(
2425         struct berval* uuidstr,
2426         struct berval* normalized,
2427         void *ctx )
2428 {
2429         struct berval *new;
2430         unsigned char nibble;
2431         int i, d = 0;
2432
2433         if ( normalized == NULL ) return NULL;
2434         if ( normalized->bv_len != 16 ) return NULL;
2435
2436         if ( uuidstr ) {
2437                 new = uuidstr;
2438         } else {
2439                 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2440                 if ( new == NULL ) {
2441                         return NULL;
2442                 }
2443         }
2444
2445         new->bv_len = 36;
2446
2447         if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2448                 if ( new != uuidstr ) {
2449                         slap_sl_free( new, ctx );
2450                 }
2451                 return NULL;
2452         }
2453
2454         for ( i = 0; i < 16; i++ ) {
2455                 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2456                         new->bv_val[(i<<1)+d] = '-';
2457                         d += 1;
2458                 }
2459
2460                 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2461                 if ( nibble < 10 ) {
2462                         new->bv_val[(i<<1)+d] = nibble + '0';
2463                 } else {
2464                         new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2465                 }
2466
2467                 nibble = (normalized->bv_val[i]) & 0xF;
2468                 if ( nibble < 10 ) {
2469                         new->bv_val[(i<<1)+d+1] = nibble + '0';
2470                 } else {
2471                         new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2472                 }
2473         }
2474
2475         new->bv_val[new->bv_len] = '\0';
2476         return new;
2477 }
2478
2479 static int
2480 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2481 {
2482         const struct berval *uuid1 = v_uuid1;
2483         const struct berval *uuid2 = v_uuid2;
2484         int rc = uuid1->bv_len - uuid2->bv_len;
2485         if ( rc ) return rc;
2486         return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2487 }
2488
2489 static void
2490 avl_ber_bvfree( void *v_bv )
2491 {
2492         struct berval   *bv = (struct berval *)v_bv;
2493         
2494         if( v_bv == NULL ) return;
2495         if ( !BER_BVISNULL( bv ) ) {
2496                 ch_free( bv->bv_val );
2497         }
2498         ch_free( (char *) bv );
2499 }
2500
2501 void
2502 syncinfo_free( syncinfo_t *sie )
2503 {
2504         ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2505         if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2506                 ch_free( sie->si_provideruri.bv_val );
2507         }
2508
2509         bindconf_free( &sie->si_bindconf );
2510
2511         if ( sie->si_filterstr.bv_val ) {
2512                 ch_free( sie->si_filterstr.bv_val );
2513         }
2514         if ( sie->si_base.bv_val ) {
2515                 ch_free( sie->si_base.bv_val );
2516         }
2517         if ( sie->si_attrs ) {
2518                 int i = 0;
2519                 while ( sie->si_attrs[i] != NULL ) {
2520                         ch_free( sie->si_attrs[i] );
2521                         i++;
2522                 }
2523                 ch_free( sie->si_attrs );
2524         }
2525         if ( sie->si_exattrs ) {
2526                 int i = 0;
2527                 while ( sie->si_exattrs[i] != NULL ) {
2528                         ch_free( sie->si_exattrs[i] );
2529                         i++;
2530                 }
2531                 ch_free( sie->si_exattrs );
2532         }
2533         if ( sie->si_anlist ) {
2534                 int i = 0;
2535                 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2536                         ch_free( sie->si_anlist[i].an_name.bv_val );
2537                         i++;
2538                 }
2539                 ch_free( sie->si_anlist );
2540         }
2541         if ( sie->si_exanlist ) {
2542                 int i = 0;
2543                 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2544                         ch_free( sie->si_exanlist[i].an_name.bv_val );
2545                         i++;
2546                 }
2547                 ch_free( sie->si_exanlist );
2548         }
2549         if ( sie->si_retryinterval ) {
2550                 ch_free( sie->si_retryinterval );
2551         }
2552         if ( sie->si_retrynum ) {
2553                 ch_free( sie->si_retrynum );
2554         }
2555         if ( sie->si_retrynum_init ) {
2556                 ch_free( sie->si_retrynum_init );
2557         }
2558         slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2559         if ( sie->si_presentlist ) {
2560             avl_free( sie->si_presentlist, avl_ber_bvfree );
2561         }
2562         if ( sie->si_ld ) {
2563                 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2564         }
2565         while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2566                 struct nonpresent_entry* npe;
2567                 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2568                 LDAP_LIST_REMOVE( npe, npe_link );
2569                 if ( npe->npe_name ) {
2570                         if ( npe->npe_name->bv_val ) {
2571                                 ch_free( npe->npe_name->bv_val );
2572                         }
2573                         ch_free( npe->npe_name );
2574                 }
2575                 if ( npe->npe_nname ) {
2576                         if ( npe->npe_nname->bv_val ) {
2577                                 ch_free( npe->npe_nname->bv_val );
2578                         }
2579                         ch_free( npe->npe_nname );
2580                 }
2581                 ch_free( npe );
2582         }
2583         ch_free( sie );
2584 }
2585
2586
2587
2588 /* NOTE: used & documented in slapd.conf(5) */
2589 #define IDSTR                   "rid"
2590 #define PROVIDERSTR             "provider"
2591 #define SCHEMASTR               "schemachecking"
2592 #define FILTERSTR               "filter"
2593 #define SEARCHBASESTR           "searchbase"
2594 #define SCOPESTR                "scope"
2595 #define ATTRSONLYSTR            "attrsonly"
2596 #define ATTRSSTR                "attrs"
2597 #define TYPESTR                 "type"
2598 #define INTERVALSTR             "interval"
2599 #define RETRYSTR                "retry"
2600 #define SLIMITSTR               "sizelimit"
2601 #define TLIMITSTR               "timelimit"
2602 #define SYNCDATASTR             "syncdata"
2603
2604 /* FIXME: undocumented */
2605 #define LOGBASESTR      "logbase"
2606 #define LOGFILTERSTR    "logfilter"
2607 #define OLDAUTHCSTR             "bindprincipal"
2608 #define EXATTRSSTR              "exattrs"
2609 #define MANAGEDSAITSTR          "manageDSAit"
2610
2611 /* FIXME: unused */
2612 #define LASTMODSTR              "lastmod"
2613 #define LMGENSTR                "gen"
2614 #define LMNOSTR                 "no"
2615 #define LMREQSTR                "req"
2616 #define SRVTABSTR               "srvtab"
2617 #define SUFFIXSTR               "suffix"
2618
2619 /* mandatory */
2620 #define GOT_ID                  0x0001
2621 #define GOT_PROVIDER            0x0002
2622
2623 /* check */
2624 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2625
2626 static struct {
2627         struct berval key;
2628         int val;
2629 } scopes[] = {
2630         { BER_BVC("base"), LDAP_SCOPE_BASE },
2631         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2632         { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL },   /* OpenLDAP extension */
2633 #ifdef LDAP_SCOPE_SUBORDINATE
2634         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2635         { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2636 #endif
2637         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2638         { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE },     /* OpenLDAP extension */
2639         { BER_BVNULL, 0 }
2640 };
2641
2642 static slap_verbmasks datamodes[] = {
2643         { BER_BVC("default"), SYNCDATA_DEFAULT },
2644         { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2645         { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2646         { BER_BVNULL, 0 }
2647 };
2648
2649 static int
2650 parse_syncrepl_line(
2651         char            **cargv,
2652         int             cargc,
2653         syncinfo_t      *si
2654 )
2655 {
2656         int     gots = 0;
2657         int     i;
2658         char    *val;
2659
2660         for ( i = 1; i < cargc; i++ ) {
2661                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2662                                         STRLENOF( IDSTR "=" ) ) )
2663                 {
2664                         int tmp;
2665                         /* '\0' string terminator accounts for '=' */
2666                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2667                         tmp= atoi( val );
2668                         if ( tmp >= 1000 || tmp < 0 ) {
2669                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2670                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2671                                 return -1;
2672                         }
2673                         si->si_rid = tmp;
2674                         gots |= GOT_ID;
2675                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2676                                         STRLENOF( PROVIDERSTR "=" ) ) )
2677                 {
2678                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2679                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2680                         gots |= GOT_PROVIDER;
2681                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2682                                         STRLENOF( SCHEMASTR "=" ) ) )
2683                 {
2684                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2685                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2686                                 si->si_schemachecking = 1;
2687                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2688                                 si->si_schemachecking = 0;
2689                         } else {
2690                                 si->si_schemachecking = 1;
2691                         }
2692                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2693                                         STRLENOF( FILTERSTR "=" ) ) )
2694                 {
2695                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2696                         if ( si->si_filterstr.bv_val )
2697                                 ch_free( si->si_filterstr.bv_val );
2698                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2699                 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2700                                         STRLENOF( LOGFILTERSTR "=" ) ) )
2701                 {
2702                         val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2703                         if ( si->si_logfilterstr.bv_val )
2704                                 ch_free( si->si_logfilterstr.bv_val );
2705                         ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2706                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2707                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2708                 {
2709                         struct berval   bv;
2710                         int             rc;
2711
2712                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2713                         if ( si->si_base.bv_val ) {
2714                                 ch_free( si->si_base.bv_val );
2715                         }
2716                         ber_str2bv( val, 0, 0, &bv );
2717                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2718                         if ( rc != LDAP_SUCCESS ) {
2719                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2720                                         val, rc, ldap_err2string( rc ) );
2721                                 return -1;
2722                         }
2723                 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2724                                         STRLENOF( LOGBASESTR "=" ) ) )
2725                 {
2726                         struct berval   bv;
2727                         int             rc;
2728
2729                         val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2730                         if ( si->si_logbase.bv_val ) {
2731                                 ch_free( si->si_logbase.bv_val );
2732                         }
2733                         ber_str2bv( val, 0, 0, &bv );
2734                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2735                         if ( rc != LDAP_SUCCESS ) {
2736                                 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2737                                         val, rc, ldap_err2string( rc ) );
2738                                 return -1;
2739                         }
2740                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2741                                         STRLENOF( SCOPESTR "=" ) ) )
2742                 {
2743                         int j;
2744                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2745                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2746                                 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2747                                         si->si_scope = scopes[j].val;
2748                                         break;
2749                                 }
2750                         }
2751                         if ( BER_BVISNULL(&scopes[j].key) ) {
2752                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2753                                         "unknown scope \"%s\"\n", val);
2754                                 return -1;
2755                         }
2756                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2757                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2758                 {
2759                         si->si_attrsonly = 1;
2760                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2761                                         STRLENOF( ATTRSSTR "=" ) ) )
2762                 {
2763                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2764                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2765                                 char *attr_fname;
2766                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2767                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2768                                 if ( si->si_anlist == NULL ) {
2769                                         ch_free( attr_fname );
2770                                         return -1;
2771                                 }
2772                                 si->si_anfile = attr_fname;
2773                         } else {
2774                                 char *str, *s, *next;
2775                                 char delimstr[] = " ,\t";
2776                                 str = ch_strdup( val );
2777                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2778                                                 s != NULL;
2779                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2780                                 {
2781                                         if ( strlen(s) == 1 && *s == '*' ) {
2782                                                 si->si_allattrs = 1;
2783                                                 *(val + ( s - str )) = delimstr[0];
2784                                         }
2785                                         if ( strlen(s) == 1 && *s == '+' ) {
2786                                                 si->si_allopattrs = 1;
2787                                                 *(val + ( s - str )) = delimstr[0];
2788                                         }
2789                                 }
2790                                 ch_free( str );
2791                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2792                                 if ( si->si_anlist == NULL ) {
2793                                         return -1;
2794                                 }
2795                         }
2796                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2797                                         STRLENOF( EXATTRSSTR "=" ) ) )
2798                 {
2799                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2800                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2801                                 char *attr_fname;
2802                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2803                                 si->si_exanlist = file2anlist(
2804                                                                         si->si_exanlist, attr_fname, " ,\t" );
2805                                 if ( si->si_exanlist == NULL ) {
2806                                         ch_free( attr_fname );
2807                                         return -1;
2808                                 }
2809                                 ch_free( attr_fname );
2810                         } else {
2811                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2812                                 if ( si->si_exanlist == NULL ) {
2813                                         return -1;
2814                                 }
2815                         }
2816                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2817                                         STRLENOF( TYPESTR "=" ) ) )
2818                 {
2819                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2820                         if ( !strncasecmp( val, "refreshOnly",
2821                                                 STRLENOF("refreshOnly") ))
2822                         {
2823                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2824                         } else if ( !strncasecmp( val, "refreshAndPersist",
2825                                                 STRLENOF("refreshAndPersist") ))
2826                         {
2827                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2828                                 si->si_interval = 60;
2829                         } else {
2830                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2831                                         "unknown sync type \"%s\"\n", val);
2832                                 return -1;
2833                         }
2834                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2835                                         STRLENOF( INTERVALSTR "=" ) ) )
2836                 {
2837                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2838                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2839                                 si->si_interval = 0;
2840                         } else {
2841                                 char *hstr;
2842                                 char *mstr;
2843                                 char *dstr;
2844                                 char *sstr;
2845                                 int dd, hh, mm, ss;
2846                                 dstr = val;
2847                                 hstr = strchr( dstr, ':' );
2848                                 if ( hstr == NULL ) {
2849                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2850                                                 "invalid interval \"%s\"\n", val );
2851                                         return -1;
2852                                 }
2853                                 *hstr++ = '\0';
2854                                 mstr = strchr( hstr, ':' );
2855                                 if ( mstr == NULL ) {
2856                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2857                                                 "invalid interval \"%s\"\n", val );
2858                                         return -1;
2859                                 }
2860                                 *mstr++ = '\0';
2861                                 sstr = strchr( mstr, ':' );
2862                                 if ( sstr == NULL ) {
2863                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2864                                                 "invalid interval \"%s\"\n", val );
2865                                         return -1;
2866                                 }
2867                                 *sstr++ = '\0';
2868
2869                                 dd = atoi( dstr );
2870                                 hh = atoi( hstr );
2871                                 mm = atoi( mstr );
2872                                 ss = atoi( sstr );
2873                                 if (( hh > 24 ) || ( hh < 0 ) ||
2874                                         ( mm > 60 ) || ( mm < 0 ) ||
2875                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2876                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2877                                                 "invalid interval \"%s\"\n", val );
2878                                         return -1;
2879                                 }
2880                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2881                         }
2882                         if ( si->si_interval < 0 ) {
2883                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2884                                         "invalid interval \"%ld\"\n",
2885                                         (long) si->si_interval);
2886                                 return -1;
2887                         }
2888                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2889                                         STRLENOF( RETRYSTR "=" ) ) )
2890                 {
2891                         char **retry_list;
2892                         int j, k, n;
2893
2894                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2895                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2896                         retry_list[0] = NULL;
2897
2898                         slap_str2clist( &retry_list, val, " ,\t" );
2899
2900                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2901                         n = k / 2;
2902                         if ( k % 2 ) {
2903                                 fprintf( stderr,
2904                                                 "Error: incomplete syncrepl retry list\n" );
2905                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2906                                         ch_free( retry_list[k] );
2907                                 }
2908                                 ch_free( retry_list );
2909                                 exit( EXIT_FAILURE );
2910                         }
2911                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2912                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2913                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2914                         for ( j = 0; j < n; j++ ) {
2915                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2916                                 if ( *retry_list[j*2+1] == '+' ) {
2917                                         si->si_retrynum_init[j] = -1;
2918                                         si->si_retrynum[j] = -1;
2919                                         j++;
2920                                         break;
2921                                 } else {
2922                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2923                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2924                                 }
2925                         }
2926                         si->si_retrynum_init[j] = -2;
2927                         si->si_retrynum[j] = -2;
2928                         si->si_retryinterval[j] = 0;
2929                         
2930                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2931                                 ch_free( retry_list[k] );
2932                         }
2933                         ch_free( retry_list );
2934                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2935                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2936                 {
2937                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2938                         si->si_manageDSAit = atoi( val );
2939                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2940                                         STRLENOF( SLIMITSTR "=") ) )
2941                 {
2942                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2943                         si->si_slimit = atoi( val );
2944                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2945                                         STRLENOF( TLIMITSTR "=" ) ) )
2946                 {
2947                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2948                         si->si_tlimit = atoi( val );
2949                 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2950                                         STRLENOF( SYNCDATASTR "=" ) ) )
2951                 {
2952                         val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2953                         si->si_syncdata = verb_to_mask( val, datamodes );
2954                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2955                         fprintf( stderr, "Error: parse_syncrepl_line: "
2956                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2957                         return -1;
2958                 }
2959         }
2960
2961         if ( gots != GOT_ALL ) {
2962                 fprintf( stderr,
2963                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2964                 return -1;
2965         }
2966
2967         return 0;
2968 }
2969
2970 static int
2971 add_syncrepl(
2972         Backend *be,
2973         char    **cargv,
2974         int     cargc
2975 )
2976 {
2977         syncinfo_t *si;
2978         int     rc = 0;
2979
2980         if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
2981                 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
2982                         "required for syncrepl\n", be->be_type, 0, 0 );
2983                 return 1;
2984         }
2985         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2986
2987         if ( si == NULL ) {
2988                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2989                 return 1;
2990         }
2991
2992         si->si_bindconf.sb_tls = SB_TLS_OFF;
2993         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2994         si->si_schemachecking = 0;
2995         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2996                 &si->si_filterstr );
2997         si->si_base.bv_val = NULL;
2998         si->si_scope = LDAP_SCOPE_SUBTREE;
2999         si->si_attrsonly = 0;
3000         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3001         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3002         si->si_attrs = NULL;
3003         si->si_allattrs = 0;
3004         si->si_allopattrs = 0;
3005         si->si_exattrs = NULL;
3006         si->si_type = LDAP_SYNC_REFRESH_ONLY;
3007         si->si_interval = 86400;
3008         si->si_retryinterval = NULL;
3009         si->si_retrynum_init = NULL;
3010         si->si_retrynum = NULL;
3011         si->si_manageDSAit = 0;
3012         si->si_tlimit = 0;
3013         si->si_slimit = 0;
3014
3015         si->si_presentlist = NULL;
3016         LDAP_LIST_INIT( &si->si_nonpresentlist );
3017         ldap_pvt_thread_mutex_init( &si->si_mutex );
3018
3019         rc = parse_syncrepl_line( cargv, cargc, si );
3020
3021         if ( rc == 0 ) {
3022                 si->si_be = be;
3023                 init_syncrepl( si );
3024                 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3025                         do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3026                 if ( !si->si_re )
3027                         rc = -1;
3028         }
3029         if ( rc < 0 ) {
3030                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3031                 syncinfo_free( si );    
3032                 return 1;
3033         } else {
3034                 Debug( LDAP_DEBUG_CONFIG,
3035                         "Config: ** successfully added syncrepl \"%s\"\n",
3036                         BER_BVISNULL( &si->si_provideruri ) ?
3037                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
3038                 if ( !si->si_schemachecking ) {
3039                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3040                 }
3041                 be->be_syncinfo = si;
3042                 return 0;
3043         }
3044 }
3045
3046 static void
3047 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3048 {
3049         struct berval bc;
3050         char buf[BUFSIZ*2], *ptr;
3051         int i;
3052
3053         bindconf_unparse( &si->si_bindconf, &bc );
3054         ptr = buf;
3055         ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3056                 si->si_rid, si->si_provideruri.bv_val );
3057         if ( !BER_BVISNULL( &bc )) {
3058                 ptr = lutil_strcopy( ptr, bc.bv_val );
3059                 free( bc.bv_val );
3060         }
3061         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3062                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3063                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3064                 *ptr++ = '"';
3065         }
3066         if ( !BER_BVISNULL( &si->si_base )) {
3067                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3068                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3069                 *ptr++ = '"';
3070         }
3071         if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3072                 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3073                 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3074                 *ptr++ = '"';
3075         }
3076         if ( !BER_BVISNULL( &si->si_logbase )) {
3077                 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3078                 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3079                 *ptr++ = '"';
3080         }
3081         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3082                 if ( si->si_scope == scopes[i].val ) {
3083                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3084                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3085                         break;
3086                 }
3087         }
3088         if ( si->si_attrsonly ) {
3089                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3090         }
3091         if ( si->si_anfile ) {
3092                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3093                 ptr = lutil_strcopy( ptr, si->si_anfile );
3094         } else if ( si->si_allattrs || si->si_allopattrs ||
3095                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3096                 char *old;
3097                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3098                 old = ptr;
3099                 ptr = anlist_unparse( si->si_anlist, ptr );
3100                 if ( si->si_allattrs ) {
3101                         if ( old != ptr ) *ptr++ = ',';
3102                         *ptr++ = '*';
3103                 }
3104                 if ( si->si_allopattrs ) {
3105                         if ( old != ptr ) *ptr++ = ',';
3106                         *ptr++ = '+';
3107                 }
3108                 *ptr++ = '"';
3109         }
3110         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3111                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3112                 ptr = anlist_unparse( si->si_exanlist, ptr );
3113         }
3114         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3115         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3116         
3117         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3118         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3119                 "refreshAndPersist" : "refreshOnly" );
3120
3121         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3122                 int dd, hh, mm, ss;
3123
3124                 dd = si->si_interval;
3125                 ss = dd % 60;
3126                 dd /= 60;
3127                 mm = dd % 60;
3128                 dd /= 60;
3129                 hh = dd % 24;
3130                 dd /= 24;
3131                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3132                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3133         } else if ( si->si_retryinterval ) {
3134                 int space=0;
3135                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3136                 for (i=0; si->si_retryinterval[i]; i++) {
3137                         if ( space ) *ptr++ = ' ';
3138                         space = 1;
3139                         ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3140                         if ( si->si_retrynum_init[i] == -1 )
3141                                 *ptr++ = '+';
3142                         else
3143                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3144                 }
3145                 *ptr++ = '"';
3146         }
3147
3148         if ( si->si_slimit ) {
3149                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3150                 ptr += sprintf( ptr, "%d", si->si_slimit );
3151         }
3152
3153         if ( si->si_tlimit ) {
3154                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3155                 ptr += sprintf( ptr, "%d", si->si_tlimit );
3156         }
3157
3158         if ( si->si_syncdata ) {
3159                 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3160                         ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3161                         ptr = lutil_strcopy( ptr, bc.bv_val );
3162                 }
3163         }
3164         bc.bv_len = ptr - buf;
3165         bc.bv_val = buf;
3166         ber_dupbv( bv, &bc );
3167 }
3168
3169 int
3170 syncrepl_config(ConfigArgs *c) {
3171         if (c->op == SLAP_CONFIG_EMIT) {
3172                 if ( c->be->be_syncinfo ) {
3173                         struct berval bv;
3174                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
3175                         ber_bvarray_add( &c->rvalue_vals, &bv );
3176                         return 0;
3177                 }
3178                 return 1;
3179         } else if ( c->op == LDAP_MOD_DELETE ) {
3180                 struct re_s *re;
3181
3182                 if ( c->be->be_syncinfo ) {
3183                         re = c->be->be_syncinfo->si_re;
3184                         if ( re ) {
3185                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3186                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3187                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
3188                         }
3189                         syncinfo_free( c->be->be_syncinfo );
3190                         c->be->be_syncinfo = NULL;
3191                 }
3192                 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3193                 return 0;
3194         }
3195         if(SLAP_SHADOW(c->be)) {
3196                 Debug(LDAP_DEBUG_ANY, "%s: "
3197                         "syncrepl: database already shadowed.\n",
3198                         c->log, 0, 0);
3199                 return(1);
3200         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3201                 return(1);
3202         }
3203         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
3204         return(0);
3205 }