]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
691c47b00e131283196ae3b3fb315bf2163150bd
[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                 if ( colon[2] == ' ' ) {
1186                         bv.bv_val = colon + 3;
1187                         bv.bv_len = vals[i].bv_len - ( bv.bv_val - vals[i].bv_val );
1188                         ber_dupbv( &bv2, &bv );
1189                         ber_bvarray_add( &mod->sml_values, &bv2 );
1190                 }
1191         }
1192         return modlist;
1193 }
1194
1195 static Modifications *
1196 syncrepl_changelog_mods(
1197         syncinfo_t *si,
1198         struct berval *vals
1199 )
1200 {
1201         return NULL;    /* FIXME */
1202 }
1203
1204 static int
1205 syncrepl_message_to_op(
1206         syncinfo_t      *si,
1207         Operation       *op,
1208         LDAPMessage     *msg
1209 )
1210 {
1211         BerElement      *ber = NULL;
1212         Modifications   *modlist = NULL;
1213         logschema *ls;
1214         SlapReply rs = { REP_RESULT };
1215         slap_callback cb = { NULL, null_callback, NULL, NULL };
1216
1217         const char      *text;
1218         char txtbuf[SLAP_TEXT_BUFLEN];
1219         size_t textlen = sizeof txtbuf;
1220
1221         struct berval   bdn, dn = BER_BVNULL, ndn;
1222         struct berval   bv, *bvals = NULL;
1223         struct berval   rdn = BER_BVNULL, sup = BER_BVNULL,
1224                 prdn = BER_BVNULL, nrdn = BER_BVNULL,
1225                 psup = BER_BVNULL, nsup = BER_BVNULL;
1226         int             rc, deleteOldRdn = 0;
1227
1228         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1229                 Debug( LDAP_DEBUG_ANY,
1230                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1231                 return -1;
1232         }
1233
1234         if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1235                 ls = &accesslog_sc;
1236         else
1237                 ls = &changelog_sc;
1238
1239         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1240
1241         if ( rc != LDAP_SUCCESS ) {
1242                 Debug( LDAP_DEBUG_ANY,
1243                         "syncrepl_message_to_op : dn get failed (%d)", rc, 0, 0 );
1244                 return rc;
1245         }
1246
1247         op->o_tag = LBER_DEFAULT;
1248
1249         while (( rc = ldap_get_attribute_ber( si->si_ld, msg, ber, &bv, &bvals ))
1250                 == LDAP_SUCCESS ) {
1251                 if ( bv.bv_val == NULL )
1252                         break;
1253
1254                 if ( !ber_bvstrcasecmp( &bv, &ls->ls_dn )) {
1255                         bdn = bvals[0];
1256                         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1257                         ber_dupbv( &op->o_req_dn, &dn );
1258                         ber_dupbv( &op->o_req_ndn, &ndn );
1259                         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1260                         slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1261                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_req )) {
1262                         int i = verb_to_mask( bvals[0].bv_val, modops );
1263                         if ( i < 0 ) {
1264                                 Debug( LDAP_DEBUG_ANY,
1265                                         "syncrepl_message_to_op : unknown op %s",
1266                                         bvals[0].bv_val, 0, 0 );
1267                                 ch_free( bvals );
1268                                 rc = -1;
1269                                 goto done;
1270                         }
1271                         op->o_tag = modops[i].mask;
1272                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_mod )) {
1273                         /* Parse attribute into modlist */
1274                         if ( si->si_syncdata == SYNCDATA_ACCESSLOG )
1275                                 modlist = syncrepl_accesslog_mods( si, bvals );
1276                         else
1277                                 modlist = syncrepl_changelog_mods( si, bvals );
1278                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newRdn )) {
1279                         rdn = bvals[0];
1280                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_delRdn )) {
1281                         if ( !ber_bvstrcasecmp( &slap_true_bv, bvals ))
1282                                 deleteOldRdn = 1;
1283                 } else if ( !ber_bvstrcasecmp( &bv, &ls->ls_newSup )) {
1284                         sup = bvals[0];
1285                 } else if ( !ber_bvstrcasecmp( &bv,
1286                         &slap_schema.si_ad_entryCSN->ad_cname )) {
1287                         slap_queue_csn( op, bvals );
1288                 }
1289                 ch_free( bvals );
1290         }
1291
1292         /* If we didn't get a mod type or a target DN, bail out */
1293         if ( op->o_tag == LBER_DEFAULT || BER_BVISNULL( &dn )) {
1294                 rc = -1;
1295                 goto done;
1296         }
1297
1298         op->o_callback = &cb;
1299
1300         switch( op->o_tag ) {
1301         case LDAP_REQ_ADD:
1302         case LDAP_REQ_MODIFY:
1303                 /* If we didn't get required data, bail */
1304                 if ( !modlist ) goto done;
1305
1306                 rc = slap_mods_check( modlist, &text, txtbuf, textlen, NULL );
1307
1308                 if ( rc != LDAP_SUCCESS ) {
1309                         Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods check (%s)\n",
1310                                 text, 0, 0 );
1311                         goto done;
1312                 }
1313
1314                 if ( op->o_tag == LDAP_REQ_ADD ) {
1315                         op->ora_e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1316                         op->ora_e->e_name = op->o_req_dn;
1317                         op->ora_e->e_nname = op->o_req_ndn;
1318                         rc = slap_mods2entry( modlist, &op->ora_e, 1, 0, &text, txtbuf, textlen);
1319                         if( rc != LDAP_SUCCESS ) {
1320                                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_op: mods2entry (%s)\n",
1321                                         text, 0, 0 );
1322                         } else {
1323                                 rc = op->o_bd->be_add( op, &rs );
1324                         }
1325                         be_entry_release_w( op, op->ora_e );
1326                 } else {
1327                         op->orm_modlist = modlist;
1328                         rc = op->o_bd->be_modify( op, &rs );
1329                 }
1330                 break;
1331         case LDAP_REQ_MODRDN:
1332                 if ( BER_BVISNULL( &rdn )) goto done;
1333
1334                 if ( rdnPretty( NULL, &rdn, &prdn, NULL ))
1335                         goto done;
1336                 if ( rdnNormalize( 0, NULL, NULL, &rdn, &nrdn, NULL ))
1337                         goto done;
1338                 if ( !BER_BVISNULL( &sup )) {
1339                         if ( dnPrettyNormal( NULL, &sup, &psup, &nsup, NULL ))
1340                                 goto done;
1341                         op->orr_newSup = &psup;
1342                         op->orr_nnewSup = &nsup;
1343                 }
1344                 op->orr_newrdn = prdn;
1345                 op->orr_nnewrdn = nrdn;
1346                 op->orr_deleteoldrdn = deleteOldRdn;
1347                 rc = op->o_bd->be_modrdn( op, &rs );
1348                 break;
1349         case LDAP_REQ_DELETE:
1350                 rc = op->o_bd->be_delete( op, &rs );
1351                 break;
1352         }
1353 done:
1354         slap_graduate_commit_csn( op );
1355         if ( modlist )
1356                 slap_mods_free( modlist, op->o_tag != LDAP_REQ_ADD );
1357         if ( !BER_BVISNULL( &rdn )) {
1358                 if ( !BER_BVISNULL( &nsup ))
1359                         ch_free( nsup.bv_val );
1360                 if ( !BER_BVISNULL( &psup ))
1361                         ch_free( psup.bv_val );
1362                 if ( !BER_BVISNULL( &nrdn ))
1363                         ch_free( nrdn.bv_val );
1364                 if ( !BER_BVISNULL( &prdn ))
1365                         ch_free( prdn.bv_val );
1366         }
1367         ber_free ( ber, 0 );
1368         return rc;
1369 }
1370
1371 static int
1372 syncrepl_message_to_entry(
1373         syncinfo_t      *si,
1374         Operation       *op,
1375         LDAPMessage     *msg,
1376         Modifications   **modlist,
1377         Entry                   **entry,
1378         int             syncstate
1379 )
1380 {
1381         Entry           *e = NULL;
1382         BerElement      *ber = NULL;
1383         Modifications   tmp;
1384         Modifications   *mod;
1385         Modifications   **modtail = modlist;
1386
1387         const char      *text;
1388         char txtbuf[SLAP_TEXT_BUFLEN];
1389         size_t textlen = sizeof txtbuf;
1390
1391         struct berval   bdn = {0, NULL}, dn, ndn;
1392         int             rc;
1393
1394         *modlist = NULL;
1395
1396         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
1397                 Debug( LDAP_DEBUG_ANY,
1398                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
1399                 return -1;
1400         }
1401
1402         op->o_tag = LDAP_REQ_ADD;
1403
1404         rc = ldap_get_dn_ber( si->si_ld, msg, &ber, &bdn );
1405
1406         if ( rc != LDAP_SUCCESS ) {
1407                 Debug( LDAP_DEBUG_ANY,
1408                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
1409                 return rc;
1410         }
1411
1412         dnPrettyNormal( NULL, &bdn, &dn, &ndn, op->o_tmpmemctx );
1413         ber_dupbv( &op->o_req_dn, &dn );
1414         ber_dupbv( &op->o_req_ndn, &ndn );
1415         slap_sl_free( ndn.bv_val, op->o_tmpmemctx );
1416         slap_sl_free( dn.bv_val, op->o_tmpmemctx );
1417
1418         if ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_DELETE ) {
1419                 if ( entry )
1420                         *entry = NULL;
1421                 return LDAP_SUCCESS;
1422         }
1423
1424         if ( entry == NULL ) {
1425                 return -1;
1426         }
1427
1428         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ) );
1429         *entry = e;
1430         e->e_name = op->o_req_dn;
1431         e->e_nname = op->o_req_ndn;
1432
1433         while ( ber_remaining( ber ) ) {
1434                 if ( (ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values ) ==
1435                         LBER_ERROR ) || BER_BVISNULL( &tmp.sml_type ) )
1436                 {
1437                         break;
1438                 }
1439
1440                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
1441
1442                 mod->sml_op = LDAP_MOD_REPLACE;
1443                 mod->sml_flags = 0;
1444                 mod->sml_next = NULL;
1445                 mod->sml_desc = NULL;
1446                 mod->sml_type = tmp.sml_type;
1447                 mod->sml_values = tmp.sml_values;
1448                 mod->sml_nvalues = NULL;
1449
1450                 *modtail = mod;
1451                 modtail = &mod->sml_next;
1452         }
1453
1454         if ( *modlist == NULL ) {
1455                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
1456                         0, 0, 0 );
1457                 rc = -1;
1458                 goto done;
1459         }
1460
1461         rc = slap_mods_check( *modlist, &text, txtbuf, textlen, NULL );
1462
1463         if ( rc != LDAP_SUCCESS ) {
1464                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
1465                         text, 0, 0 );
1466                 goto done;
1467         }
1468
1469         /* Strip out dynamically generated attrs */
1470         for ( modtail = modlist; *modtail ; ) {
1471                 mod = *modtail;
1472                 if ( mod->sml_desc->ad_type->sat_flags & SLAP_AT_DYNAMIC ) {
1473                         *modtail = mod->sml_next;
1474                         slap_mod_free( &mod->sml_mod, 0 );
1475                         ch_free( mod );
1476                 } else {
1477                         modtail = &mod->sml_next;
1478                 }
1479         }
1480
1481         /* Strip out attrs in exattrs list */
1482         for ( modtail = modlist; *modtail ; ) {
1483                 mod = *modtail;
1484                 if ( ldap_charray_inlist( si->si_exattrs,
1485                                         mod->sml_desc->ad_type->sat_cname.bv_val )) {
1486                         *modtail = mod->sml_next;
1487                         slap_mod_free( &mod->sml_mod, 0 );
1488                         ch_free( mod );
1489                 } else {
1490                         modtail = &mod->sml_next;
1491                 }
1492         }
1493         
1494         rc = slap_mods2entry( *modlist, &e, 1, 1, &text, txtbuf, textlen);
1495         if( rc != LDAP_SUCCESS ) {
1496                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
1497                         text, 0, 0 );
1498         }
1499
1500 done:
1501         ber_free ( ber, 0 );
1502         if ( rc != LDAP_SUCCESS ) {
1503                 if ( e ) {
1504                         entry_free( e );
1505                         *entry = e = NULL;
1506                 }
1507         }
1508
1509         return rc;
1510 }
1511
1512 static struct berval generic_filterstr = BER_BVC("(objectclass=*)");
1513
1514 /* During a refresh, we may get an LDAP_SYNC_ADD for an already existing
1515  * entry if a previous refresh was interrupted before sending us a new
1516  * context state. We try to compare the new entry to the existing entry
1517  * and ignore the new entry if they are the same.
1518  *
1519  * Also, we may get an update where the entryDN has changed, due to
1520  * a ModDn on the provider. We detect this as well, so we can issue
1521  * the corresponding operation locally.
1522  *
1523  * In the case of a modify, we get a list of all the attributes
1524  * in the original entry. Rather than deleting the entry and re-adding it,
1525  * we issue a Modify request that deletes all the attributes and adds all
1526  * the new ones. This avoids the issue of trying to delete/add a non-leaf
1527  * entry.
1528  *
1529  * We don't try to otherwise distinguish ModDN from Modify; in the case of
1530  * a ModDN we will issue both operations on the local database.
1531  */
1532 typedef struct dninfo {
1533         Entry *new_entry;
1534         struct berval dn;
1535         struct berval ndn;
1536         int renamed;    /* Was an existing entry renamed? */
1537         int wasChanged; /* are the attributes changed? */
1538         int attrs;              /* how many attribute types are in the ads list */
1539         AttributeDescription **ads;
1540 } dninfo;
1541
1542 static int
1543 syncrepl_entry(
1544         syncinfo_t* si,
1545         Operation *op,
1546         Entry* entry,
1547         Modifications** modlist,
1548         int syncstate,
1549         struct berval* syncUUID,
1550         struct sync_cookie* syncCookie_req,
1551         struct berval* syncCSN )
1552 {
1553         Backend *be = op->o_bd;
1554         slap_callback   cb = { NULL, NULL, NULL, NULL };
1555         struct berval   *syncuuid_bv = NULL;
1556         struct berval   syncUUID_strrep = BER_BVNULL;
1557         struct berval   uuid_bv = BER_BVNULL;
1558
1559         SlapReply       rs_search = {REP_RESULT};
1560         SlapReply       rs_delete = {REP_RESULT};
1561         SlapReply       rs_add = {REP_RESULT};
1562         SlapReply       rs_modify = {REP_RESULT};
1563         Filter f = {0};
1564 #ifdef LDAP_COMP_MATCH
1565         AttributeAssertion ava = { NULL, BER_BVNULL, NULL };
1566 #else
1567         AttributeAssertion ava = { NULL, BER_BVNULL };
1568 #endif
1569         int rc = LDAP_SUCCESS;
1570         int ret = LDAP_SUCCESS;
1571
1572         struct berval pdn = BER_BVNULL;
1573         dninfo dni = {0};
1574         int     retry = 1;
1575
1576         switch( syncstate ) {
1577         case LDAP_SYNC_PRESENT:
1578                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1579                                         "syncrepl_entry",
1580                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_PRESENT)", 0 );
1581                 break;
1582         case LDAP_SYNC_ADD:
1583                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1584                                         "syncrepl_entry",
1585                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_ADD)", 0 );
1586                 break;
1587         case LDAP_SYNC_DELETE:
1588                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1589                                         "syncrepl_entry",
1590                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_DELETE)", 0 );
1591                 break;
1592         case LDAP_SYNC_MODIFY:
1593                 Debug( LDAP_DEBUG_SYNC, "%s: %s\n",
1594                                         "syncrepl_entry",
1595                                         "LDAP_RES_SEARCH_ENTRY(LDAP_SYNC_MODIFY)", 0 );
1596                 break;
1597         default:
1598                 Debug( LDAP_DEBUG_ANY, "%s: %s\n",
1599                                         "syncrepl_entry",
1600                                         "LDAP_RES_SEARCH_ENTRY(UNKNOWN syncstate)", 0 );
1601         }
1602
1603         if (( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
1604                 if ( !si->si_refreshPresent ) {
1605                         syncuuid_bv = ber_dupbv( NULL, syncUUID );
1606                         avl_insert( &si->si_presentlist, (caddr_t) syncuuid_bv,
1607                                 syncuuid_cmp, avl_dup_error );
1608                 }
1609         }
1610
1611         if ( syncstate == LDAP_SYNC_PRESENT ) {
1612                 return 0;
1613         } else if ( syncstate != LDAP_SYNC_DELETE ) {
1614                 if ( entry == NULL ) {
1615                         return 0;
1616                 }
1617         }
1618
1619         f.f_choice = LDAP_FILTER_EQUALITY;
1620         f.f_ava = &ava;
1621         ava.aa_desc = slap_schema.si_ad_entryUUID;
1622         (void)slap_uuidstr_from_normalized( &syncUUID_strrep, syncUUID, op->o_tmpmemctx );
1623         ava.aa_value = *syncUUID;
1624         op->ors_filter = &f;
1625
1626         op->ors_filterstr.bv_len = STRLENOF( "(entryUUID=)" ) + syncUUID->bv_len;
1627         op->ors_filterstr.bv_val = (char *) slap_sl_malloc(
1628                 op->ors_filterstr.bv_len + 1, op->o_tmpmemctx ); 
1629         AC_MEMCPY( op->ors_filterstr.bv_val, "(entryUUID=", STRLENOF( "(entryUUID=" ) );
1630         AC_MEMCPY( &op->ors_filterstr.bv_val[STRLENOF( "(entryUUID=" )],
1631                 syncUUID->bv_val, syncUUID->bv_len );
1632         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len - 1] = ')';
1633         op->ors_filterstr.bv_val[op->ors_filterstr.bv_len] = '\0';
1634
1635         op->o_tag = LDAP_REQ_SEARCH;
1636         op->ors_scope = LDAP_SCOPE_SUBTREE;
1637         op->ors_deref = LDAP_DEREF_NEVER;
1638
1639         /* get the entry for this UUID */
1640         op->o_req_dn = si->si_base;
1641         op->o_req_ndn = si->si_base;
1642
1643         op->o_time = slap_get_time();
1644         op->ors_tlimit = SLAP_NO_LIMIT;
1645         op->ors_slimit = 1;
1646
1647         op->ors_attrs = slap_anlist_all_attributes;
1648         op->ors_attrsonly = 0;
1649
1650         /* set callback function */
1651         op->o_callback = &cb;
1652         cb.sc_response = dn_callback;
1653         cb.sc_private = &dni;
1654         dni.new_entry = entry;
1655
1656         if ( limits_check( op, &rs_search ) == 0 ) {
1657                 rc = be->be_search( op, &rs_search );
1658                 Debug( LDAP_DEBUG_SYNC,
1659                                 "syncrepl_entry: %s (%d)\n", 
1660                                 "be_search", rc, 0 );
1661         }
1662
1663         if ( !BER_BVISNULL( &op->ors_filterstr ) ) {
1664                 slap_sl_free( op->ors_filterstr.bv_val, op->o_tmpmemctx );
1665         }
1666
1667         cb.sc_response = null_callback;
1668         cb.sc_private = si;
1669
1670         if ( entry && !BER_BVISNULL( &entry->e_name ) ) {
1671                 Debug( LDAP_DEBUG_SYNC,
1672                                 "syncrepl_entry: %s\n",
1673                                 entry->e_name.bv_val, 0, 0 );
1674         } else {
1675                 Debug( LDAP_DEBUG_SYNC,
1676                                 "syncrepl_entry: %s\n",
1677                                 dni.dn.bv_val ? dni.dn.bv_val : "(null)", 0, 0 );
1678         }
1679
1680         if ( syncstate != LDAP_SYNC_DELETE ) {
1681                 Attribute       *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryUUID );
1682
1683                 if ( a == NULL ) {
1684                         /* add if missing */
1685                         attr_merge_one( entry, slap_schema.si_ad_entryUUID,
1686                                 &syncUUID_strrep, syncUUID );
1687
1688                 } else if ( !bvmatch( &a->a_nvals[0], syncUUID ) ) {
1689                         /* replace only if necessary */
1690                         if ( a->a_nvals != a->a_vals ) {
1691                                 ber_memfree( a->a_nvals[0].bv_val );
1692                                 ber_dupbv( &a->a_nvals[0], syncUUID );
1693                         }
1694                         ber_memfree( a->a_vals[0].bv_val );
1695                         ber_dupbv( &a->a_vals[0], &syncUUID_strrep );
1696                 }
1697         }
1698
1699         switch ( syncstate ) {
1700         case LDAP_SYNC_ADD:
1701         case LDAP_SYNC_MODIFY:
1702                 {
1703                         Attribute *a = attr_find( entry->e_attrs, slap_schema.si_ad_entryCSN );
1704                         if ( a )
1705                                 op->o_csn = a->a_vals[0];
1706                 }
1707 retry_add:;
1708                 if ( BER_BVISNULL( &dni.dn )) {
1709
1710                         op->o_req_dn = entry->e_name;
1711                         op->o_req_ndn = entry->e_nname;
1712                         op->o_tag = LDAP_REQ_ADD;
1713                         op->ora_e = entry;
1714
1715                         rc = be->be_add( op, &rs_add );
1716                         Debug( LDAP_DEBUG_SYNC,
1717                                         "syncrepl_entry: %s (%d)\n", 
1718                                         "be_add", rc, 0 );
1719                         switch ( rs_add.sr_err ) {
1720                         case LDAP_SUCCESS:
1721                                 be_entry_release_w( op, entry );
1722                                 ret = 0;
1723                                 break;
1724
1725                         case LDAP_REFERRAL:
1726                         /* we assume that LDAP_NO_SUCH_OBJECT is returned 
1727                          * only if the suffix entry is not present */
1728                         case LDAP_NO_SUCH_OBJECT:
1729                                 syncrepl_add_glue( op, entry );
1730                                 ret = 0;
1731                                 break;
1732
1733                         /* if an entry was added via syncrepl_add_glue(),
1734                          * it likely has no entryUUID, so the previous
1735                          * be_search() doesn't find it.  In this case,
1736                          * give syncrepl a chance to modify it. Also
1737                          * allow for entries that were recreated with the
1738                          * same DN but a different entryUUID.
1739                          */
1740                         case LDAP_ALREADY_EXISTS:
1741                                 if ( retry ) {
1742                                         Operation       op2 = *op;
1743                                         SlapReply       rs2 = { 0 };
1744                                         slap_callback   cb2 = { 0 };
1745
1746                                         op2.o_tag = LDAP_REQ_SEARCH;
1747                                         op2.o_req_dn = entry->e_name;
1748                                         op2.o_req_ndn = entry->e_nname;
1749                                         op2.ors_scope = LDAP_SCOPE_BASE;
1750                                         op2.ors_deref = LDAP_DEREF_NEVER;
1751                                         op2.ors_attrs = slap_anlist_all_attributes;
1752                                         op2.ors_attrsonly = 0;
1753                                         op2.ors_limit = NULL;
1754                                         op2.ors_slimit = 1;
1755                                         op2.ors_tlimit = SLAP_NO_LIMIT;
1756
1757                                         f.f_choice = LDAP_FILTER_PRESENT;
1758                                         f.f_desc = slap_schema.si_ad_objectClass;
1759                                         op2.ors_filter = &f;
1760                                         op2.ors_filterstr = generic_filterstr;
1761
1762                                         op2.o_callback = &cb2;
1763                                         cb2.sc_response = dn_callback;
1764                                         cb2.sc_private = &dni;
1765
1766                                         be->be_search( &op2, &rs2 );
1767
1768                                         retry = 0;
1769                                         goto retry_add;
1770                                 }
1771                                 /* FALLTHRU */
1772
1773                         default:
1774                                 Debug( LDAP_DEBUG_ANY,
1775                                         "syncrepl_entry : be_add failed (%d)\n",
1776                                         rs_add.sr_err, 0, 0 );
1777                                 ret = 1;
1778                                 break;
1779                         }
1780                         goto done;
1781                 }
1782                 /* FALLTHRU */
1783                 op->o_req_dn = dni.dn;
1784                 op->o_req_ndn = dni.ndn;
1785                 if ( dni.renamed ) {
1786                         struct berval noldp, newp, nnewp;
1787
1788                         op->o_tag = LDAP_REQ_MODRDN;
1789                         dnRdn( &entry->e_name, &op->orr_newrdn );
1790                         dnRdn( &entry->e_nname, &op->orr_nnewrdn );
1791
1792                         dnParent( &dni.ndn, &noldp );
1793                         dnParent( &entry->e_nname, &nnewp );
1794                         if ( !dn_match( &noldp, &newp )) {
1795                                 dnParent( &entry->e_name, &newp );
1796                                 op->orr_newSup = &newp;
1797                                 op->orr_nnewSup = &nnewp;
1798                         }
1799                         op->orr_deleteoldrdn = 0;
1800                         rc = be->be_modrdn( op, &rs_modify );
1801                         Debug( LDAP_DEBUG_SYNC,
1802                                         "syncrepl_entry: %s (%d)\n", 
1803                                         "be_modrdn", rc, 0 );
1804                         if ( rs_modify.sr_err == LDAP_SUCCESS ) {
1805                                 op->o_req_dn = entry->e_name;
1806                                 op->o_req_ndn = entry->e_nname;
1807                         } else {
1808                                 ret = 1;
1809                                 goto done;
1810                         }
1811                 }
1812                 if ( dni.wasChanged ) {
1813                         Modifications *mod, *modhead = NULL;
1814                         Modifications *modtail = NULL;
1815                         int i;
1816
1817                         op->o_tag = LDAP_REQ_MODIFY;
1818
1819                         assert( *modlist != NULL );
1820
1821                         /* Delete all the old attrs */
1822                         for ( i = 0; i < dni.attrs; i++ ) {
1823                                 mod = ch_malloc( sizeof( Modifications ) );
1824                                 mod->sml_op = LDAP_MOD_DELETE;
1825                                 mod->sml_flags = 0;
1826                                 mod->sml_desc = dni.ads[i];
1827                                 mod->sml_type = mod->sml_desc->ad_cname;
1828                                 mod->sml_values = NULL;
1829                                 mod->sml_nvalues = NULL;
1830                                 if ( !modhead ) modhead = mod;
1831                                 if ( modtail ) {
1832                                         modtail->sml_next = mod;
1833                                 }
1834                                 modtail = mod;
1835                         }
1836
1837                         /* Append passed in list to ours */
1838                         if ( modtail ) {
1839                                 modtail->sml_next = *modlist;
1840                                 *modlist = modhead;
1841                         } else {
1842                                 mod = *modlist;
1843                         }
1844
1845                         /* Find end of this list */
1846                         for ( ; mod != NULL; mod = mod->sml_next ) {
1847                                 modtail = mod;
1848                         }
1849
1850                         mod = (Modifications *)ch_calloc(1, sizeof(Modifications));
1851                         mod->sml_op = LDAP_MOD_REPLACE;
1852                         mod->sml_flags = 0;
1853                         mod->sml_desc = slap_schema.si_ad_entryUUID;
1854                         mod->sml_type = mod->sml_desc->ad_cname;
1855                         ber_dupbv( &uuid_bv, &syncUUID_strrep );
1856                         ber_bvarray_add( &mod->sml_values, &uuid_bv );
1857                         ber_dupbv( &uuid_bv, syncUUID );
1858                         ber_bvarray_add( &mod->sml_nvalues, &uuid_bv );
1859                         modtail->sml_next = mod;
1860                                         
1861                         op->o_tag = LDAP_REQ_MODIFY;
1862                         op->orm_modlist = *modlist;
1863
1864                         rc = be->be_modify( op, &rs_modify );
1865                         Debug( LDAP_DEBUG_SYNC,
1866                                         "syncrepl_entry: %s (%d)\n", 
1867                                         "be_modify", rc, 0 );
1868                         if ( rs_modify.sr_err != LDAP_SUCCESS ) {
1869                                 Debug( LDAP_DEBUG_ANY,
1870                                         "syncrepl_entry : be_modify failed (%d)\n",
1871                                         rs_modify.sr_err, 0, 0 );
1872                         }
1873                 }
1874                 ret = 1;
1875                 goto done;
1876         case LDAP_SYNC_DELETE :
1877                 if ( !BER_BVISNULL( &dni.dn )) {
1878                         op->o_req_dn = dni.dn;
1879                         op->o_req_ndn = dni.ndn;
1880                         op->o_tag = LDAP_REQ_DELETE;
1881                         rc = be->be_delete( op, &rs_delete );
1882                         Debug( LDAP_DEBUG_SYNC,
1883                                         "syncrepl_entry: %s (%d)\n", 
1884                                         "be_delete", rc, 0 );
1885
1886                         while ( rs_delete.sr_err == LDAP_SUCCESS
1887                                 && op->o_delete_glue_parent ) {
1888                                 op->o_delete_glue_parent = 0;
1889                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
1890                                         slap_callback cb = { NULL };
1891                                         cb.sc_response = slap_null_cb;
1892                                         dnParent( &op->o_req_ndn, &pdn );
1893                                         op->o_req_dn = pdn;
1894                                         op->o_req_ndn = pdn;
1895                                         op->o_callback = &cb;
1896                                         op->o_bd->be_delete( op, &rs_delete );
1897                                 } else {
1898                                         break;
1899                                 }
1900                         }
1901                 }
1902                 ret = 0;
1903                 goto done;
1904
1905         default :
1906                 Debug( LDAP_DEBUG_ANY,
1907                         "syncrepl_entry : unknown syncstate\n", 0, 0, 0 );
1908                 ret = 1;
1909                 goto done;
1910         }
1911
1912 done :
1913         if ( !BER_BVISNULL( &syncUUID_strrep ) ) {
1914                 slap_sl_free( syncUUID_strrep.bv_val, op->o_tmpmemctx );
1915                 BER_BVZERO( &syncUUID_strrep );
1916         }
1917         if ( dni.ads ) {
1918                 op->o_tmpfree( dni.ads, op->o_tmpmemctx );
1919         }
1920         if ( !BER_BVISNULL( &dni.ndn ) ) {
1921                 op->o_tmpfree( dni.ndn.bv_val, op->o_tmpmemctx );
1922         }
1923         if ( !BER_BVISNULL( &dni.dn ) ) {
1924                 op->o_tmpfree( dni.dn.bv_val, op->o_tmpmemctx );
1925         }
1926         BER_BVZERO( &op->o_csn );
1927         return ret;
1928 }
1929
1930 static struct berval gcbva[] = {
1931         BER_BVC("top"),
1932         BER_BVC("glue"),
1933         BER_BVNULL
1934 };
1935
1936 #define NP_DELETE_ONE   2
1937
1938 static void
1939 syncrepl_del_nonpresent(
1940         Operation *op,
1941         syncinfo_t *si,
1942         BerVarray uuids )
1943 {
1944         Backend* be = op->o_bd;
1945         slap_callback   cb = { NULL };
1946         SlapReply       rs_search = {REP_RESULT};
1947         SlapReply       rs_delete = {REP_RESULT};
1948         SlapReply       rs_modify = {REP_RESULT};
1949         struct nonpresent_entry *np_list, *np_prev;
1950         int rc;
1951         AttributeName   an[2];
1952
1953         struct berval pdn = BER_BVNULL;
1954
1955         op->o_req_dn = si->si_base;
1956         op->o_req_ndn = si->si_base;
1957
1958         cb.sc_response = nonpresent_callback;
1959         cb.sc_private = si;
1960
1961         op->o_callback = &cb;
1962         op->o_tag = LDAP_REQ_SEARCH;
1963         op->ors_scope = si->si_scope;
1964         op->ors_deref = LDAP_DEREF_NEVER;
1965         op->o_time = slap_get_time();
1966         op->ors_tlimit = SLAP_NO_LIMIT;
1967
1968
1969         if ( uuids ) {
1970                 Filter uf;
1971 #ifdef LDAP_COMP_MATCH
1972                 AttributeAssertion eq = { NULL, BER_BVNULL, NULL };
1973 #else
1974                 AttributeAssertion eq = { NULL, BER_BVNULL };
1975 #endif
1976                 int i;
1977
1978                 op->ors_attrsonly = 1;
1979                 op->ors_attrs = slap_anlist_no_attrs;
1980                 op->ors_limit = NULL;
1981                 op->ors_filter = &uf;
1982
1983                 uf.f_ava = &eq;
1984                 uf.f_av_desc = slap_schema.si_ad_entryUUID;
1985                 uf.f_next = NULL;
1986                 uf.f_choice = LDAP_FILTER_EQUALITY;
1987                 si->si_refreshDelete |= NP_DELETE_ONE;
1988
1989                 for (i=0; uuids[i].bv_val; i++) {
1990                         op->ors_slimit = 1;
1991                         uf.f_av_value = uuids[i];
1992                         rc = be->be_search( op, &rs_search );
1993                 }
1994                 si->si_refreshDelete ^= NP_DELETE_ONE;
1995         } else {
1996                 memset( &an[0], 0, 2 * sizeof( AttributeName ) );
1997                 an[0].an_name = slap_schema.si_ad_entryUUID->ad_cname;
1998                 an[0].an_desc = slap_schema.si_ad_entryUUID;
1999                 op->ors_attrs = an;
2000                 op->ors_slimit = SLAP_NO_LIMIT;
2001                 op->ors_attrsonly = 0;
2002                 op->ors_filter = str2filter_x( op, si->si_filterstr.bv_val );
2003                 op->ors_filterstr = si->si_filterstr;
2004                 op->o_nocaching = 1;
2005
2006                 if ( limits_check( op, &rs_search ) == 0 ) {
2007                         rc = be->be_search( op, &rs_search );
2008                 }
2009                 if ( op->ors_filter ) filter_free_x( op, op->ors_filter );
2010         }
2011
2012         op->o_nocaching = 0;
2013
2014         if ( !LDAP_LIST_EMPTY( &si->si_nonpresentlist ) ) {
2015
2016                 slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2017
2018                 np_list = LDAP_LIST_FIRST( &si->si_nonpresentlist );
2019                 while ( np_list != NULL ) {
2020                         LDAP_LIST_REMOVE( np_list, npe_link );
2021                         np_prev = np_list;
2022                         np_list = LDAP_LIST_NEXT( np_list, npe_link );
2023                         op->o_tag = LDAP_REQ_DELETE;
2024                         op->o_callback = &cb;
2025                         cb.sc_response = null_callback;
2026                         cb.sc_private = si;
2027                         op->o_req_dn = *np_prev->npe_name;
2028                         op->o_req_ndn = *np_prev->npe_nname;
2029                         rc = op->o_bd->be_delete( op, &rs_delete );
2030
2031                         if ( rs_delete.sr_err == LDAP_NOT_ALLOWED_ON_NONLEAF ) {
2032                                 Modifications mod1, mod2;
2033                                 mod1.sml_op = LDAP_MOD_REPLACE;
2034                                 mod1.sml_flags = 0;
2035                                 mod1.sml_desc = slap_schema.si_ad_objectClass;
2036                                 mod1.sml_type = mod1.sml_desc->ad_cname;
2037                                 mod1.sml_values = &gcbva[0];
2038                                 mod1.sml_nvalues = NULL;
2039                                 mod1.sml_next = &mod2;
2040
2041                                 mod2.sml_op = LDAP_MOD_REPLACE;
2042                                 mod2.sml_flags = 0;
2043                                 mod2.sml_desc = slap_schema.si_ad_structuralObjectClass;
2044                                 mod2.sml_type = mod2.sml_desc->ad_cname;
2045                                 mod2.sml_values = &gcbva[1];
2046                                 mod2.sml_nvalues = NULL;
2047                                 mod2.sml_next = NULL;
2048
2049                                 op->o_tag = LDAP_REQ_MODIFY;
2050                                 op->orm_modlist = &mod1;
2051
2052                                 rc = be->be_modify( op, &rs_modify );
2053                         }
2054
2055                         while ( rs_delete.sr_err == LDAP_SUCCESS &&
2056                                         op->o_delete_glue_parent ) {
2057                                 op->o_delete_glue_parent = 0;
2058                                 if ( !be_issuffix( op->o_bd, &op->o_req_ndn )) {
2059                                         slap_callback cb = { NULL };
2060                                         cb.sc_response = slap_null_cb;
2061                                         dnParent( &op->o_req_ndn, &pdn );
2062                                         op->o_req_dn = pdn;
2063                                         op->o_req_ndn = pdn;
2064                                         op->o_callback = &cb;
2065                                         /* give it a root privil ? */
2066                                         op->o_bd->be_delete( op, &rs_delete );
2067                                 } else {
2068                                         break;
2069                             }
2070                         }
2071
2072                         op->o_delete_glue_parent = 0;
2073
2074                         ber_bvfree( np_prev->npe_name );
2075                         ber_bvfree( np_prev->npe_nname );
2076                         ch_free( np_prev );
2077                 }
2078
2079                 slap_graduate_commit_csn( op );
2080         }
2081
2082         return;
2083 }
2084
2085 void
2086 syncrepl_add_glue(
2087         Operation* op,
2088         Entry *e )
2089 {
2090         Backend *be = op->o_bd;
2091         slap_callback cb = { NULL };
2092         Attribute       *a;
2093         int     rc;
2094         int suffrdns;
2095         int i;
2096         struct berval dn = {0, NULL};
2097         struct berval ndn = {0, NULL};
2098         Entry   *glue;
2099         SlapReply       rs_add = {REP_RESULT};
2100         char    *ptr, *comma;
2101
2102         op->o_tag = LDAP_REQ_ADD;
2103         op->o_callback = &cb;
2104         cb.sc_response = null_callback;
2105         cb.sc_private = NULL;
2106
2107         dn = e->e_name;
2108         ndn = e->e_nname;
2109
2110         /* count RDNs in suffix */
2111         if ( !BER_BVISEMPTY( &be->be_nsuffix[0] ) ) {
2112                 for ( i = 0, ptr = be->be_nsuffix[0].bv_val; ptr; ptr = strchr( ptr, ',' ) ) {
2113                         ptr++;
2114                         i++;
2115                 }
2116                 suffrdns = i;
2117         } else {
2118                 /* suffix is "" */
2119                 suffrdns = 0;
2120         }
2121
2122         /* Start with BE suffix */
2123         for ( i = 0, ptr = NULL; i < suffrdns; i++ ) {
2124                 comma = strrchr( dn.bv_val, ',' );
2125                 if ( ptr ) *ptr = ',';
2126                 if ( comma ) *comma = '\0';
2127                 ptr = comma;
2128         }
2129         if ( ptr ) {
2130                 *ptr++ = ',';
2131                 dn.bv_len -= ptr - dn.bv_val;
2132                 dn.bv_val = ptr;
2133         }
2134         /* the normalizedDNs are always the same length, no counting
2135          * required.
2136          */
2137         if ( ndn.bv_len > be->be_nsuffix[0].bv_len ) {
2138                 ndn.bv_val += ndn.bv_len - be->be_nsuffix[0].bv_len;
2139                 ndn.bv_len = be->be_nsuffix[0].bv_len;
2140         }
2141
2142         while ( ndn.bv_val > e->e_nname.bv_val ) {
2143                 glue = (Entry *) ch_calloc( 1, sizeof(Entry) );
2144                 ber_dupbv( &glue->e_name, &dn );
2145                 ber_dupbv( &glue->e_nname, &ndn );
2146
2147                 a = ch_calloc( 1, sizeof( Attribute ));
2148                 a->a_desc = slap_schema.si_ad_objectClass;
2149
2150                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
2151                 ber_dupbv( &a->a_vals[0], &gcbva[0] );
2152                 ber_dupbv( &a->a_vals[1], &gcbva[1] );
2153                 ber_dupbv( &a->a_vals[2], &gcbva[2] );
2154
2155                 a->a_nvals = a->a_vals;
2156
2157                 a->a_next = glue->e_attrs;
2158                 glue->e_attrs = a;
2159
2160                 a = ch_calloc( 1, sizeof( Attribute ));
2161                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
2162
2163                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
2164                 ber_dupbv( &a->a_vals[0], &gcbva[1] );
2165                 ber_dupbv( &a->a_vals[1], &gcbva[2] );
2166
2167                 a->a_nvals = a->a_vals;
2168
2169                 a->a_next = glue->e_attrs;
2170                 glue->e_attrs = a;
2171
2172                 op->o_req_dn = glue->e_name;
2173                 op->o_req_ndn = glue->e_nname;
2174                 op->ora_e = glue;
2175                 rc = be->be_add ( op, &rs_add );
2176                 if ( rs_add.sr_err == LDAP_SUCCESS ) {
2177                         be_entry_release_w( op, glue );
2178                 } else {
2179                 /* incl. ALREADY EXIST */
2180                         entry_free( glue );
2181                 }
2182
2183                 /* Move to next child */
2184                 for (ptr = dn.bv_val-2; ptr > e->e_name.bv_val && *ptr != ','; ptr--) {
2185                         /* empty */
2186                 }
2187                 if ( ptr == e->e_name.bv_val ) break;
2188                 dn.bv_val = ++ptr;
2189                 dn.bv_len = e->e_name.bv_len - (ptr-e->e_name.bv_val);
2190                 for( ptr = ndn.bv_val-2;
2191                         ptr > e->e_nname.bv_val && *ptr != ',';
2192                         ptr--)
2193                 {
2194                         /* empty */
2195                 }
2196                 ndn.bv_val = ++ptr;
2197                 ndn.bv_len = e->e_nname.bv_len - (ptr-e->e_nname.bv_val);
2198         }
2199
2200         op->o_req_dn = e->e_name;
2201         op->o_req_ndn = e->e_nname;
2202         op->ora_e = e;
2203         rc = be->be_add ( op, &rs_add );
2204         if ( rs_add.sr_err == LDAP_SUCCESS ) {
2205                 be_entry_release_w( op, e );
2206         } else {
2207                 entry_free( e );
2208         }
2209
2210         return;
2211 }
2212
2213 static void
2214 syncrepl_updateCookie(
2215         syncinfo_t *si,
2216         Operation *op,
2217         struct berval *pdn,
2218         struct sync_cookie *syncCookie )
2219 {
2220         Backend *be = op->o_bd;
2221         Modifications mod = { { 0 } };
2222         struct berval vals[ 2 ];
2223
2224         int rc;
2225
2226         slap_callback cb = { NULL };
2227         SlapReply       rs_modify = {REP_RESULT};
2228
2229         slap_sync_cookie_free( &si->si_syncCookie, 0 );
2230         slap_dup_sync_cookie( &si->si_syncCookie, syncCookie );
2231
2232         mod.sml_op = LDAP_MOD_REPLACE;
2233         mod.sml_desc = slap_schema.si_ad_contextCSN;
2234         mod.sml_type = mod.sml_desc->ad_cname;
2235         mod.sml_values = vals;
2236         vals[0] = si->si_syncCookie.ctxcsn;
2237         vals[1].bv_val = NULL;
2238         vals[1].bv_len = 0;
2239
2240         slap_queue_csn( op, &si->si_syncCookie.ctxcsn );
2241
2242         op->o_tag = LDAP_REQ_MODIFY;
2243
2244         assert( si->si_rid < 1000 );
2245
2246         cb.sc_response = null_callback;
2247         cb.sc_private = si;
2248
2249         op->o_callback = &cb;
2250         op->o_req_dn = op->o_bd->be_suffix[0];
2251         op->o_req_ndn = op->o_bd->be_nsuffix[0];
2252
2253         /* update contextCSN */
2254         op->o_msgid = SLAP_SYNC_UPDATE_MSGID;
2255         op->orm_modlist = &mod;
2256         rc = be->be_modify( op, &rs_modify );
2257         op->o_msgid = 0;
2258
2259         if ( rs_modify.sr_err != LDAP_SUCCESS ) {
2260                 Debug( LDAP_DEBUG_ANY,
2261                         "be_modify failed (%d)\n", rs_modify.sr_err, 0, 0 );
2262         }
2263
2264         slap_graduate_commit_csn( op );
2265
2266         return;
2267 }
2268
2269 static int
2270 dn_callback(
2271         Operation*      op,
2272         SlapReply*      rs )
2273 {
2274         dninfo *dni = op->o_callback->sc_private;
2275
2276         if ( rs->sr_type == REP_SEARCH ) {
2277                 if ( !BER_BVISNULL( &dni->dn ) ) {
2278                         Debug( LDAP_DEBUG_ANY,
2279                                 "dn_callback : consistency error - "
2280                                 "entryUUID is not unique\n", 0, 0, 0 );
2281                 } else {
2282                         ber_dupbv_x( &dni->dn, &rs->sr_entry->e_name, op->o_tmpmemctx );
2283                         ber_dupbv_x( &dni->ndn, &rs->sr_entry->e_nname, op->o_tmpmemctx );
2284                         /* If there is a new entry, see if it differs from the old.
2285                          * We compare the non-normalized values so that cosmetic changes
2286                          * in the provider are always propagated.
2287                          */
2288                         if ( dni->new_entry ) {
2289                                 Attribute *old, *new;
2290                                 int i;
2291
2292                                 /* Did the DN change? Note that we don't explicitly try to
2293                                  * discover if the deleteOldRdn argument applies here. It
2294                                  * would save an unnecessary Modify if we detected it, but
2295                                  * that's a fair amount of trouble to compare the two attr
2296                                  * lists in detail. (Just test normalized DN; we ignore
2297                                  * insignificant changes here.)
2298                                  */
2299                                 if ( !dn_match( &rs->sr_entry->e_nname,
2300                                                 &dni->new_entry->e_nname ) )
2301                                 {
2302                                         dni->renamed = 1;
2303                                 }
2304
2305                                 for ( i = 0, old = rs->sr_entry->e_attrs;
2306                                                 old;
2307                                                 i++, old = old->a_next )
2308                                         ;
2309
2310                                 dni->attrs = i;
2311
2312                                 /* We assume that attributes are saved in the same order
2313                                  * in the remote and local databases. So if we walk through
2314                                  * the attributeDescriptions one by one they should match in
2315                                  * lock step. If not, we signal a change. Otherwise we test
2316                                  * all the values...
2317                                  */
2318                                 for ( old = rs->sr_entry->e_attrs, new = dni->new_entry->e_attrs;
2319                                                 old && new;
2320                                                 old = old->a_next, new = new->a_next )
2321                                 {
2322                                         if ( old->a_desc != new->a_desc ) {
2323                                                 dni->wasChanged = 1;
2324                                                 break;
2325                                         }
2326                                         for ( i = 0; ; i++ ) {
2327                                                 int nold, nnew;
2328                                                 nold = BER_BVISNULL( &old->a_vals[i] );
2329                                                 nnew = BER_BVISNULL( &new->a_vals[i] );
2330                                                 /* If both are empty, stop looking */
2331                                                 if ( nold && nnew ) {
2332                                                         break;
2333                                                 }
2334                                                 /* If they are different, stop looking */
2335                                                 if ( nold != nnew ) {
2336                                                         dni->wasChanged = 1;
2337                                                         break;
2338                                                 }
2339                                                 if ( ber_bvcmp( &old->a_vals[i], &new->a_vals[i] )) {
2340                                                         dni->wasChanged = 1;
2341                                                         break;
2342                                                 }
2343                                         }
2344                                         if ( dni->wasChanged ) break;
2345                                 }
2346                                 if ( dni->wasChanged ) {
2347                                         dni->ads = op->o_tmpalloc( dni->attrs *
2348                                                 sizeof(AttributeDescription *), op->o_tmpmemctx );
2349                                         i = 0;
2350                                         for ( old = rs->sr_entry->e_attrs; old; old = old->a_next ) {
2351                                                 dni->ads[i] = old->a_desc;
2352                                                 i++;
2353                                         }
2354                                 }
2355                         }
2356                 }
2357         } else if ( rs->sr_type == REP_RESULT ) {
2358                 if ( rs->sr_err == LDAP_SIZELIMIT_EXCEEDED ) {
2359                         Debug( LDAP_DEBUG_ANY,
2360                                 "dn_callback : consistency error - "
2361                                 "entryUUID is not unique\n", 0, 0, 0 );
2362                 }
2363         }
2364
2365         return LDAP_SUCCESS;
2366 }
2367
2368 static int
2369 nonpresent_callback(
2370         Operation*      op,
2371         SlapReply*      rs )
2372 {
2373         syncinfo_t *si = op->o_callback->sc_private;
2374         Attribute *a;
2375         int count = 0;
2376         struct berval* present_uuid = NULL;
2377         struct nonpresent_entry *np_entry;
2378
2379         if ( rs->sr_type == REP_RESULT ) {
2380                 count = avl_free( si->si_presentlist, avl_ber_bvfree );
2381                 si->si_presentlist = NULL;
2382
2383         } else if ( rs->sr_type == REP_SEARCH ) {
2384                 if ( !(si->si_refreshDelete & NP_DELETE_ONE )) {
2385                         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2386
2387                         if ( a == NULL ) return 0;
2388
2389                         present_uuid = avl_find( si->si_presentlist, &a->a_nvals[0],
2390                                 syncuuid_cmp );
2391                 }
2392
2393                 if ( present_uuid == NULL ) {
2394                         np_entry = (struct nonpresent_entry *)
2395                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2396                         np_entry->npe_name = ber_dupbv( NULL, &rs->sr_entry->e_name );
2397                         np_entry->npe_nname = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2398                         LDAP_LIST_INSERT_HEAD( &si->si_nonpresentlist, np_entry, npe_link );
2399
2400                 } else {
2401                         avl_delete( &si->si_presentlist,
2402                                         &a->a_nvals[0], syncuuid_cmp );
2403                         ch_free( present_uuid->bv_val );
2404                         ch_free( present_uuid );
2405                 }
2406         }
2407         return LDAP_SUCCESS;
2408 }
2409
2410 static int
2411 null_callback(
2412         Operation*      op,
2413         SlapReply*      rs )
2414 {
2415         if ( rs->sr_err != LDAP_SUCCESS &&
2416                 rs->sr_err != LDAP_REFERRAL &&
2417                 rs->sr_err != LDAP_ALREADY_EXISTS &&
2418                 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2419                 rs->sr_err != LDAP_NOT_ALLOWED_ON_NONLEAF )
2420         {
2421                 Debug( LDAP_DEBUG_ANY,
2422                         "null_callback : error code 0x%x\n",
2423                         rs->sr_err, 0, 0 );
2424         }
2425         return LDAP_SUCCESS;
2426 }
2427
2428 static struct berval *
2429 slap_uuidstr_from_normalized(
2430         struct berval* uuidstr,
2431         struct berval* normalized,
2432         void *ctx )
2433 {
2434         struct berval *new;
2435         unsigned char nibble;
2436         int i, d = 0;
2437
2438         if ( normalized == NULL ) return NULL;
2439         if ( normalized->bv_len != 16 ) return NULL;
2440
2441         if ( uuidstr ) {
2442                 new = uuidstr;
2443         } else {
2444                 new = (struct berval *)slap_sl_malloc( sizeof(struct berval), ctx );
2445                 if ( new == NULL ) {
2446                         return NULL;
2447                 }
2448         }
2449
2450         new->bv_len = 36;
2451
2452         if ( ( new->bv_val = slap_sl_malloc( new->bv_len + 1, ctx ) ) == NULL ) {
2453                 if ( new != uuidstr ) {
2454                         slap_sl_free( new, ctx );
2455                 }
2456                 return NULL;
2457         }
2458
2459         for ( i = 0; i < 16; i++ ) {
2460                 if ( i == 4 || i == 6 || i == 8 || i == 10 ) {
2461                         new->bv_val[(i<<1)+d] = '-';
2462                         d += 1;
2463                 }
2464
2465                 nibble = (normalized->bv_val[i] >> 4) & 0xF;
2466                 if ( nibble < 10 ) {
2467                         new->bv_val[(i<<1)+d] = nibble + '0';
2468                 } else {
2469                         new->bv_val[(i<<1)+d] = nibble - 10 + 'a';
2470                 }
2471
2472                 nibble = (normalized->bv_val[i]) & 0xF;
2473                 if ( nibble < 10 ) {
2474                         new->bv_val[(i<<1)+d+1] = nibble + '0';
2475                 } else {
2476                         new->bv_val[(i<<1)+d+1] = nibble - 10 + 'a';
2477                 }
2478         }
2479
2480         new->bv_val[new->bv_len] = '\0';
2481         return new;
2482 }
2483
2484 static int
2485 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
2486 {
2487         const struct berval *uuid1 = v_uuid1;
2488         const struct berval *uuid2 = v_uuid2;
2489         int rc = uuid1->bv_len - uuid2->bv_len;
2490         if ( rc ) return rc;
2491         return ( memcmp( uuid1->bv_val, uuid2->bv_val, uuid1->bv_len ) );
2492 }
2493
2494 static void
2495 avl_ber_bvfree( void *v_bv )
2496 {
2497         struct berval   *bv = (struct berval *)v_bv;
2498         
2499         if( v_bv == NULL ) return;
2500         if ( !BER_BVISNULL( bv ) ) {
2501                 ch_free( bv->bv_val );
2502         }
2503         ch_free( (char *) bv );
2504 }
2505
2506 void
2507 syncinfo_free( syncinfo_t *sie )
2508 {
2509         ldap_pvt_thread_mutex_destroy( &sie->si_mutex );
2510         if ( !BER_BVISNULL( &sie->si_provideruri ) ) {
2511                 ch_free( sie->si_provideruri.bv_val );
2512         }
2513
2514         bindconf_free( &sie->si_bindconf );
2515
2516         if ( sie->si_filterstr.bv_val ) {
2517                 ch_free( sie->si_filterstr.bv_val );
2518         }
2519         if ( sie->si_base.bv_val ) {
2520                 ch_free( sie->si_base.bv_val );
2521         }
2522         if ( sie->si_attrs ) {
2523                 int i = 0;
2524                 while ( sie->si_attrs[i] != NULL ) {
2525                         ch_free( sie->si_attrs[i] );
2526                         i++;
2527                 }
2528                 ch_free( sie->si_attrs );
2529         }
2530         if ( sie->si_exattrs ) {
2531                 int i = 0;
2532                 while ( sie->si_exattrs[i] != NULL ) {
2533                         ch_free( sie->si_exattrs[i] );
2534                         i++;
2535                 }
2536                 ch_free( sie->si_exattrs );
2537         }
2538         if ( sie->si_anlist ) {
2539                 int i = 0;
2540                 while ( sie->si_anlist[i].an_name.bv_val != NULL ) {
2541                         ch_free( sie->si_anlist[i].an_name.bv_val );
2542                         i++;
2543                 }
2544                 ch_free( sie->si_anlist );
2545         }
2546         if ( sie->si_exanlist ) {
2547                 int i = 0;
2548                 while ( sie->si_exanlist[i].an_name.bv_val != NULL ) {
2549                         ch_free( sie->si_exanlist[i].an_name.bv_val );
2550                         i++;
2551                 }
2552                 ch_free( sie->si_exanlist );
2553         }
2554         if ( sie->si_retryinterval ) {
2555                 ch_free( sie->si_retryinterval );
2556         }
2557         if ( sie->si_retrynum ) {
2558                 ch_free( sie->si_retrynum );
2559         }
2560         if ( sie->si_retrynum_init ) {
2561                 ch_free( sie->si_retrynum_init );
2562         }
2563         slap_sync_cookie_free( &sie->si_syncCookie, 0 );
2564         if ( sie->si_presentlist ) {
2565             avl_free( sie->si_presentlist, avl_ber_bvfree );
2566         }
2567         if ( sie->si_ld ) {
2568                 ldap_unbind_ext( sie->si_ld, NULL, NULL );
2569         }
2570         while ( !LDAP_LIST_EMPTY( &sie->si_nonpresentlist )) {
2571                 struct nonpresent_entry* npe;
2572                 npe = LDAP_LIST_FIRST( &sie->si_nonpresentlist );
2573                 LDAP_LIST_REMOVE( npe, npe_link );
2574                 if ( npe->npe_name ) {
2575                         if ( npe->npe_name->bv_val ) {
2576                                 ch_free( npe->npe_name->bv_val );
2577                         }
2578                         ch_free( npe->npe_name );
2579                 }
2580                 if ( npe->npe_nname ) {
2581                         if ( npe->npe_nname->bv_val ) {
2582                                 ch_free( npe->npe_nname->bv_val );
2583                         }
2584                         ch_free( npe->npe_nname );
2585                 }
2586                 ch_free( npe );
2587         }
2588         ch_free( sie );
2589 }
2590
2591
2592
2593 /* NOTE: used & documented in slapd.conf(5) */
2594 #define IDSTR                   "rid"
2595 #define PROVIDERSTR             "provider"
2596 #define SCHEMASTR               "schemachecking"
2597 #define FILTERSTR               "filter"
2598 #define SEARCHBASESTR           "searchbase"
2599 #define SCOPESTR                "scope"
2600 #define ATTRSONLYSTR            "attrsonly"
2601 #define ATTRSSTR                "attrs"
2602 #define TYPESTR                 "type"
2603 #define INTERVALSTR             "interval"
2604 #define RETRYSTR                "retry"
2605 #define SLIMITSTR               "sizelimit"
2606 #define TLIMITSTR               "timelimit"
2607 #define SYNCDATASTR             "syncdata"
2608
2609 /* FIXME: undocumented */
2610 #define LOGBASESTR      "logbase"
2611 #define LOGFILTERSTR    "logfilter"
2612 #define OLDAUTHCSTR             "bindprincipal"
2613 #define EXATTRSSTR              "exattrs"
2614 #define MANAGEDSAITSTR          "manageDSAit"
2615
2616 /* FIXME: unused */
2617 #define LASTMODSTR              "lastmod"
2618 #define LMGENSTR                "gen"
2619 #define LMNOSTR                 "no"
2620 #define LMREQSTR                "req"
2621 #define SRVTABSTR               "srvtab"
2622 #define SUFFIXSTR               "suffix"
2623
2624 /* mandatory */
2625 #define GOT_ID                  0x0001
2626 #define GOT_PROVIDER            0x0002
2627
2628 /* check */
2629 #define GOT_ALL                 (GOT_ID|GOT_PROVIDER)
2630
2631 static struct {
2632         struct berval key;
2633         int val;
2634 } scopes[] = {
2635         { BER_BVC("base"), LDAP_SCOPE_BASE },
2636         { BER_BVC("one"), LDAP_SCOPE_ONELEVEL },
2637         { BER_BVC("onelevel"), LDAP_SCOPE_ONELEVEL },   /* OpenLDAP extension */
2638 #ifdef LDAP_SCOPE_SUBORDINATE
2639         { BER_BVC("children"), LDAP_SCOPE_SUBORDINATE },
2640         { BER_BVC("subordinate"), LDAP_SCOPE_SUBORDINATE },
2641 #endif
2642         { BER_BVC("sub"), LDAP_SCOPE_SUBTREE },
2643         { BER_BVC("subtree"), LDAP_SCOPE_SUBTREE },     /* OpenLDAP extension */
2644         { BER_BVNULL, 0 }
2645 };
2646
2647 static slap_verbmasks datamodes[] = {
2648         { BER_BVC("default"), SYNCDATA_DEFAULT },
2649         { BER_BVC("accesslog"), SYNCDATA_ACCESSLOG },
2650         { BER_BVC("changelog"), SYNCDATA_CHANGELOG },
2651         { BER_BVNULL, 0 }
2652 };
2653
2654 static int
2655 parse_syncrepl_line(
2656         char            **cargv,
2657         int             cargc,
2658         syncinfo_t      *si
2659 )
2660 {
2661         int     gots = 0;
2662         int     i;
2663         char    *val;
2664
2665         for ( i = 1; i < cargc; i++ ) {
2666                 if ( !strncasecmp( cargv[ i ], IDSTR "=",
2667                                         STRLENOF( IDSTR "=" ) ) )
2668                 {
2669                         int tmp;
2670                         /* '\0' string terminator accounts for '=' */
2671                         val = cargv[ i ] + STRLENOF( IDSTR "=" );
2672                         tmp= atoi( val );
2673                         if ( tmp >= 1000 || tmp < 0 ) {
2674                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2675                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2676                                 return -1;
2677                         }
2678                         si->si_rid = tmp;
2679                         gots |= GOT_ID;
2680                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR "=",
2681                                         STRLENOF( PROVIDERSTR "=" ) ) )
2682                 {
2683                         val = cargv[ i ] + STRLENOF( PROVIDERSTR "=" );
2684                         ber_str2bv( val, 0, 1, &si->si_provideruri );
2685                         gots |= GOT_PROVIDER;
2686                 } else if ( !strncasecmp( cargv[ i ], SCHEMASTR "=",
2687                                         STRLENOF( SCHEMASTR "=" ) ) )
2688                 {
2689                         val = cargv[ i ] + STRLENOF( SCHEMASTR "=" );
2690                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2691                                 si->si_schemachecking = 1;
2692                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2693                                 si->si_schemachecking = 0;
2694                         } else {
2695                                 si->si_schemachecking = 1;
2696                         }
2697                 } else if ( !strncasecmp( cargv[ i ], FILTERSTR "=",
2698                                         STRLENOF( FILTERSTR "=" ) ) )
2699                 {
2700                         val = cargv[ i ] + STRLENOF( FILTERSTR "=" );
2701                         if ( si->si_filterstr.bv_val )
2702                                 ch_free( si->si_filterstr.bv_val );
2703                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2704                 } else if ( !strncasecmp( cargv[ i ], LOGFILTERSTR "=",
2705                                         STRLENOF( LOGFILTERSTR "=" ) ) )
2706                 {
2707                         val = cargv[ i ] + STRLENOF( LOGFILTERSTR "=" );
2708                         if ( si->si_logfilterstr.bv_val )
2709                                 ch_free( si->si_logfilterstr.bv_val );
2710                         ber_str2bv( val, 0, 1, &si->si_logfilterstr );
2711                 } else if ( !strncasecmp( cargv[ i ], SEARCHBASESTR "=",
2712                                         STRLENOF( SEARCHBASESTR "=" ) ) )
2713                 {
2714                         struct berval   bv;
2715                         int             rc;
2716
2717                         val = cargv[ i ] + STRLENOF( SEARCHBASESTR "=" );
2718                         if ( si->si_base.bv_val ) {
2719                                 ch_free( si->si_base.bv_val );
2720                         }
2721                         ber_str2bv( val, 0, 0, &bv );
2722                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL );
2723                         if ( rc != LDAP_SUCCESS ) {
2724                                 fprintf( stderr, "Invalid base DN \"%s\": %d (%s)\n",
2725                                         val, rc, ldap_err2string( rc ) );
2726                                 return -1;
2727                         }
2728                 } else if ( !strncasecmp( cargv[ i ], LOGBASESTR "=",
2729                                         STRLENOF( LOGBASESTR "=" ) ) )
2730                 {
2731                         struct berval   bv;
2732                         int             rc;
2733
2734                         val = cargv[ i ] + STRLENOF( LOGBASESTR "=" );
2735                         if ( si->si_logbase.bv_val ) {
2736                                 ch_free( si->si_logbase.bv_val );
2737                         }
2738                         ber_str2bv( val, 0, 0, &bv );
2739                         rc = dnNormalize( 0, NULL, NULL, &bv, &si->si_logbase, NULL );
2740                         if ( rc != LDAP_SUCCESS ) {
2741                                 fprintf( stderr, "Invalid logbase DN \"%s\": %d (%s)\n",
2742                                         val, rc, ldap_err2string( rc ) );
2743                                 return -1;
2744                         }
2745                 } else if ( !strncasecmp( cargv[ i ], SCOPESTR "=",
2746                                         STRLENOF( SCOPESTR "=" ) ) )
2747                 {
2748                         int j;
2749                         val = cargv[ i ] + STRLENOF( SCOPESTR "=" );
2750                         for ( j=0; !BER_BVISNULL(&scopes[j].key); j++ ) {
2751                                 if (!strcasecmp( val, scopes[j].key.bv_val )) {
2752                                         si->si_scope = scopes[j].val;
2753                                         break;
2754                                 }
2755                         }
2756                         if ( BER_BVISNULL(&scopes[j].key) ) {
2757                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2758                                         "unknown scope \"%s\"\n", val);
2759                                 return -1;
2760                         }
2761                 } else if ( !strncasecmp( cargv[ i ], ATTRSONLYSTR "=",
2762                                         STRLENOF( ATTRSONLYSTR "=" ) ) )
2763                 {
2764                         si->si_attrsonly = 1;
2765                 } else if ( !strncasecmp( cargv[ i ], ATTRSSTR "=",
2766                                         STRLENOF( ATTRSSTR "=" ) ) )
2767                 {
2768                         val = cargv[ i ] + STRLENOF( ATTRSSTR "=" );
2769                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") ) ) {
2770                                 char *attr_fname;
2771                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2772                                 si->si_anlist = file2anlist( si->si_anlist, attr_fname, " ,\t" );
2773                                 if ( si->si_anlist == NULL ) {
2774                                         ch_free( attr_fname );
2775                                         return -1;
2776                                 }
2777                                 si->si_anfile = attr_fname;
2778                         } else {
2779                                 char *str, *s, *next;
2780                                 char delimstr[] = " ,\t";
2781                                 str = ch_strdup( val );
2782                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2783                                                 s != NULL;
2784                                                 s = ldap_pvt_strtok( NULL, delimstr, &next ) )
2785                                 {
2786                                         if ( strlen(s) == 1 && *s == '*' ) {
2787                                                 si->si_allattrs = 1;
2788                                                 *(val + ( s - str )) = delimstr[0];
2789                                         }
2790                                         if ( strlen(s) == 1 && *s == '+' ) {
2791                                                 si->si_allopattrs = 1;
2792                                                 *(val + ( s - str )) = delimstr[0];
2793                                         }
2794                                 }
2795                                 ch_free( str );
2796                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2797                                 if ( si->si_anlist == NULL ) {
2798                                         return -1;
2799                                 }
2800                         }
2801                 } else if ( !strncasecmp( cargv[ i ], EXATTRSSTR "=",
2802                                         STRLENOF( EXATTRSSTR "=" ) ) )
2803                 {
2804                         val = cargv[ i ] + STRLENOF( EXATTRSSTR "=" );
2805                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2806                                 char *attr_fname;
2807                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2808                                 si->si_exanlist = file2anlist(
2809                                                                         si->si_exanlist, attr_fname, " ,\t" );
2810                                 if ( si->si_exanlist == NULL ) {
2811                                         ch_free( attr_fname );
2812                                         return -1;
2813                                 }
2814                                 ch_free( attr_fname );
2815                         } else {
2816                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2817                                 if ( si->si_exanlist == NULL ) {
2818                                         return -1;
2819                                 }
2820                         }
2821                 } else if ( !strncasecmp( cargv[ i ], TYPESTR "=",
2822                                         STRLENOF( TYPESTR "=" ) ) )
2823                 {
2824                         val = cargv[ i ] + STRLENOF( TYPESTR "=" );
2825                         if ( !strncasecmp( val, "refreshOnly",
2826                                                 STRLENOF("refreshOnly") ))
2827                         {
2828                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2829                         } else if ( !strncasecmp( val, "refreshAndPersist",
2830                                                 STRLENOF("refreshAndPersist") ))
2831                         {
2832                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2833                                 si->si_interval = 60;
2834                         } else {
2835                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2836                                         "unknown sync type \"%s\"\n", val);
2837                                 return -1;
2838                         }
2839                 } else if ( !strncasecmp( cargv[ i ], INTERVALSTR "=",
2840                                         STRLENOF( INTERVALSTR "=" ) ) )
2841                 {
2842                         val = cargv[ i ] + STRLENOF( INTERVALSTR "=" );
2843                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2844                                 si->si_interval = 0;
2845                         } else {
2846                                 char *hstr;
2847                                 char *mstr;
2848                                 char *dstr;
2849                                 char *sstr;
2850                                 int dd, hh, mm, ss;
2851                                 dstr = val;
2852                                 hstr = strchr( dstr, ':' );
2853                                 if ( hstr == NULL ) {
2854                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2855                                                 "invalid interval \"%s\"\n", val );
2856                                         return -1;
2857                                 }
2858                                 *hstr++ = '\0';
2859                                 mstr = strchr( hstr, ':' );
2860                                 if ( mstr == NULL ) {
2861                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2862                                                 "invalid interval \"%s\"\n", val );
2863                                         return -1;
2864                                 }
2865                                 *mstr++ = '\0';
2866                                 sstr = strchr( mstr, ':' );
2867                                 if ( sstr == NULL ) {
2868                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2869                                                 "invalid interval \"%s\"\n", val );
2870                                         return -1;
2871                                 }
2872                                 *sstr++ = '\0';
2873
2874                                 dd = atoi( dstr );
2875                                 hh = atoi( hstr );
2876                                 mm = atoi( mstr );
2877                                 ss = atoi( sstr );
2878                                 if (( hh > 24 ) || ( hh < 0 ) ||
2879                                         ( mm > 60 ) || ( mm < 0 ) ||
2880                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2881                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2882                                                 "invalid interval \"%s\"\n", val );
2883                                         return -1;
2884                                 }
2885                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2886                         }
2887                         if ( si->si_interval < 0 ) {
2888                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2889                                         "invalid interval \"%ld\"\n",
2890                                         (long) si->si_interval);
2891                                 return -1;
2892                         }
2893                 } else if ( !strncasecmp( cargv[ i ], RETRYSTR "=",
2894                                         STRLENOF( RETRYSTR "=" ) ) )
2895                 {
2896                         char **retry_list;
2897                         int j, k, n;
2898
2899                         val = cargv[ i ] + STRLENOF( RETRYSTR "=" );
2900                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2901                         retry_list[0] = NULL;
2902
2903                         slap_str2clist( &retry_list, val, " ,\t" );
2904
2905                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2906                         n = k / 2;
2907                         if ( k % 2 ) {
2908                                 fprintf( stderr,
2909                                                 "Error: incomplete syncrepl retry list\n" );
2910                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2911                                         ch_free( retry_list[k] );
2912                                 }
2913                                 ch_free( retry_list );
2914                                 exit( EXIT_FAILURE );
2915                         }
2916                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2917                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2918                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2919                         for ( j = 0; j < n; j++ ) {
2920                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2921                                 if ( *retry_list[j*2+1] == '+' ) {
2922                                         si->si_retrynum_init[j] = -1;
2923                                         si->si_retrynum[j] = -1;
2924                                         j++;
2925                                         break;
2926                                 } else {
2927                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2928                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2929                                 }
2930                         }
2931                         si->si_retrynum_init[j] = -2;
2932                         si->si_retrynum[j] = -2;
2933                         si->si_retryinterval[j] = 0;
2934                         
2935                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2936                                 ch_free( retry_list[k] );
2937                         }
2938                         ch_free( retry_list );
2939                 } else if ( !strncasecmp( cargv[ i ], MANAGEDSAITSTR "=",
2940                                         STRLENOF( MANAGEDSAITSTR "=" ) ) )
2941                 {
2942                         val = cargv[ i ] + STRLENOF( MANAGEDSAITSTR "=" );
2943                         si->si_manageDSAit = atoi( val );
2944                 } else if ( !strncasecmp( cargv[ i ], SLIMITSTR "=",
2945                                         STRLENOF( SLIMITSTR "=") ) )
2946                 {
2947                         val = cargv[ i ] + STRLENOF( SLIMITSTR "=" );
2948                         si->si_slimit = atoi( val );
2949                 } else if ( !strncasecmp( cargv[ i ], TLIMITSTR "=",
2950                                         STRLENOF( TLIMITSTR "=" ) ) )
2951                 {
2952                         val = cargv[ i ] + STRLENOF( TLIMITSTR "=" );
2953                         si->si_tlimit = atoi( val );
2954                 } else if ( !strncasecmp( cargv[ i ], SYNCDATASTR "=",
2955                                         STRLENOF( SYNCDATASTR "=" ) ) )
2956                 {
2957                         val = cargv[ i ] + STRLENOF( SYNCDATASTR "=" );
2958                         si->si_syncdata = verb_to_mask( val, datamodes );
2959                 } else if ( bindconf_parse( cargv[i], &si->si_bindconf )) {
2960                         fprintf( stderr, "Error: parse_syncrepl_line: "
2961                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2962                         return -1;
2963                 }
2964         }
2965
2966         if ( gots != GOT_ALL ) {
2967                 fprintf( stderr,
2968                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2969                 return -1;
2970         }
2971
2972         return 0;
2973 }
2974
2975 static int
2976 add_syncrepl(
2977         Backend *be,
2978         char    **cargv,
2979         int     cargc
2980 )
2981 {
2982         syncinfo_t *si;
2983         int     rc = 0;
2984
2985         if ( !( be->be_search && be->be_add && be->be_modify && be->be_delete )) {
2986                 Debug( LDAP_DEBUG_ANY, "database %s does not support operations "
2987                         "required for syncrepl\n", be->be_type, 0, 0 );
2988                 return 1;
2989         }
2990         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2991
2992         if ( si == NULL ) {
2993                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2994                 return 1;
2995         }
2996
2997         si->si_bindconf.sb_tls = SB_TLS_OFF;
2998         si->si_bindconf.sb_method = LDAP_AUTH_SIMPLE;
2999         si->si_schemachecking = 0;
3000         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
3001                 &si->si_filterstr );
3002         si->si_base.bv_val = NULL;
3003         si->si_scope = LDAP_SCOPE_SUBTREE;
3004         si->si_attrsonly = 0;
3005         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3006         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
3007         si->si_attrs = NULL;
3008         si->si_allattrs = 0;
3009         si->si_allopattrs = 0;
3010         si->si_exattrs = NULL;
3011         si->si_type = LDAP_SYNC_REFRESH_ONLY;
3012         si->si_interval = 86400;
3013         si->si_retryinterval = NULL;
3014         si->si_retrynum_init = NULL;
3015         si->si_retrynum = NULL;
3016         si->si_manageDSAit = 0;
3017         si->si_tlimit = 0;
3018         si->si_slimit = 0;
3019
3020         si->si_presentlist = NULL;
3021         LDAP_LIST_INIT( &si->si_nonpresentlist );
3022         ldap_pvt_thread_mutex_init( &si->si_mutex );
3023
3024         rc = parse_syncrepl_line( cargv, cargc, si );
3025
3026         if ( rc == 0 ) {
3027                 si->si_be = be;
3028                 init_syncrepl( si );
3029                 si->si_re = ldap_pvt_runqueue_insert( &slapd_rq, si->si_interval,
3030                         do_syncrepl, si, "do_syncrepl", be->be_suffix[0].bv_val );
3031                 if ( !si->si_re )
3032                         rc = -1;
3033         }
3034         if ( rc < 0 ) {
3035                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
3036                 syncinfo_free( si );    
3037                 return 1;
3038         } else {
3039                 Debug( LDAP_DEBUG_CONFIG,
3040                         "Config: ** successfully added syncrepl \"%s\"\n",
3041                         BER_BVISNULL( &si->si_provideruri ) ?
3042                         "(null)" : si->si_provideruri.bv_val, 0, 0 );
3043                 if ( !si->si_schemachecking ) {
3044                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
3045                 }
3046                 be->be_syncinfo = si;
3047                 return 0;
3048         }
3049 }
3050
3051 static void
3052 syncrepl_unparse( syncinfo_t *si, struct berval *bv )
3053 {
3054         struct berval bc;
3055         char buf[BUFSIZ*2], *ptr;
3056         int i;
3057
3058         bindconf_unparse( &si->si_bindconf, &bc );
3059         ptr = buf;
3060         ptr += sprintf( ptr, IDSTR "=%03ld " PROVIDERSTR "=%s",
3061                 si->si_rid, si->si_provideruri.bv_val );
3062         if ( !BER_BVISNULL( &bc )) {
3063                 ptr = lutil_strcopy( ptr, bc.bv_val );
3064                 free( bc.bv_val );
3065         }
3066         if ( !BER_BVISEMPTY( &si->si_filterstr )) {
3067                 ptr = lutil_strcopy( ptr, " " FILTERSTR "=\"" );
3068                 ptr = lutil_strcopy( ptr, si->si_filterstr.bv_val );
3069                 *ptr++ = '"';
3070         }
3071         if ( !BER_BVISNULL( &si->si_base )) {
3072                 ptr = lutil_strcopy( ptr, " " SEARCHBASESTR "=\"" );
3073                 ptr = lutil_strcopy( ptr, si->si_base.bv_val );
3074                 *ptr++ = '"';
3075         }
3076         if ( !BER_BVISEMPTY( &si->si_logfilterstr )) {
3077                 ptr = lutil_strcopy( ptr, " " LOGFILTERSTR "=\"" );
3078                 ptr = lutil_strcopy( ptr, si->si_logfilterstr.bv_val );
3079                 *ptr++ = '"';
3080         }
3081         if ( !BER_BVISNULL( &si->si_logbase )) {
3082                 ptr = lutil_strcopy( ptr, " " LOGBASESTR "=\"" );
3083                 ptr = lutil_strcopy( ptr, si->si_logbase.bv_val );
3084                 *ptr++ = '"';
3085         }
3086         for (i=0; !BER_BVISNULL(&scopes[i].key);i++) {
3087                 if ( si->si_scope == scopes[i].val ) {
3088                         ptr = lutil_strcopy( ptr, " " SCOPESTR "=" );
3089                         ptr = lutil_strcopy( ptr, scopes[i].key.bv_val );
3090                         break;
3091                 }
3092         }
3093         if ( si->si_attrsonly ) {
3094                 ptr = lutil_strcopy( ptr, " " ATTRSONLYSTR "=yes" );
3095         }
3096         if ( si->si_anfile ) {
3097                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=:include:" );
3098                 ptr = lutil_strcopy( ptr, si->si_anfile );
3099         } else if ( si->si_allattrs || si->si_allopattrs ||
3100                 ( si->si_anlist && !BER_BVISNULL(&si->si_anlist[0].an_name) )) {
3101                 char *old;
3102                 ptr = lutil_strcopy( ptr, " " ATTRSSTR "=\"" );
3103                 old = ptr;
3104                 ptr = anlist_unparse( si->si_anlist, ptr );
3105                 if ( si->si_allattrs ) {
3106                         if ( old != ptr ) *ptr++ = ',';
3107                         *ptr++ = '*';
3108                 }
3109                 if ( si->si_allopattrs ) {
3110                         if ( old != ptr ) *ptr++ = ',';
3111                         *ptr++ = '+';
3112                 }
3113                 *ptr++ = '"';
3114         }
3115         if ( si->si_exanlist && !BER_BVISNULL(&si->si_exanlist[0].an_name) ) {
3116                 ptr = lutil_strcopy( ptr, " " EXATTRSSTR "=" );
3117                 ptr = anlist_unparse( si->si_exanlist, ptr );
3118         }
3119         ptr = lutil_strcopy( ptr, " " SCHEMASTR "=" );
3120         ptr = lutil_strcopy( ptr, si->si_schemachecking ? "on" : "off" );
3121         
3122         ptr = lutil_strcopy( ptr, " " TYPESTR "=" );
3123         ptr = lutil_strcopy( ptr, si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ?
3124                 "refreshAndPersist" : "refreshOnly" );
3125
3126         if ( si->si_type == LDAP_SYNC_REFRESH_ONLY ) {
3127                 int dd, hh, mm, ss;
3128
3129                 dd = si->si_interval;
3130                 ss = dd % 60;
3131                 dd /= 60;
3132                 mm = dd % 60;
3133                 dd /= 60;
3134                 hh = dd % 24;
3135                 dd /= 24;
3136                 ptr = lutil_strcopy( ptr, " " INTERVALSTR "=" );
3137                 ptr += sprintf( ptr, "%02d:%02d:%02d:%02d", dd, hh, mm, ss );
3138         } else if ( si->si_retryinterval ) {
3139                 int space=0;
3140                 ptr = lutil_strcopy( ptr, " " RETRYSTR "=\"" );
3141                 for (i=0; si->si_retryinterval[i]; i++) {
3142                         if ( space ) *ptr++ = ' ';
3143                         space = 1;
3144                         ptr += sprintf( ptr, "%ld ", (long) si->si_retryinterval[i] );
3145                         if ( si->si_retrynum_init[i] == -1 )
3146                                 *ptr++ = '+';
3147                         else
3148                                 ptr += sprintf( ptr, "%d", si->si_retrynum_init[i] );
3149                 }
3150                 *ptr++ = '"';
3151         }
3152
3153         if ( si->si_slimit ) {
3154                 ptr = lutil_strcopy( ptr, " " SLIMITSTR "=" );
3155                 ptr += sprintf( ptr, "%d", si->si_slimit );
3156         }
3157
3158         if ( si->si_tlimit ) {
3159                 ptr = lutil_strcopy( ptr, " " TLIMITSTR "=" );
3160                 ptr += sprintf( ptr, "%d", si->si_tlimit );
3161         }
3162
3163         if ( si->si_syncdata ) {
3164                 if ( enum_to_verb( datamodes, si->si_syncdata, &bc ) >= 0 ) {
3165                         ptr = lutil_strcopy( ptr, " " SYNCDATASTR "=" );
3166                         ptr = lutil_strcopy( ptr, bc.bv_val );
3167                 }
3168         }
3169         bc.bv_len = ptr - buf;
3170         bc.bv_val = buf;
3171         ber_dupbv( bv, &bc );
3172 }
3173
3174 int
3175 syncrepl_config(ConfigArgs *c) {
3176         if (c->op == SLAP_CONFIG_EMIT) {
3177                 if ( c->be->be_syncinfo ) {
3178                         struct berval bv;
3179                         syncrepl_unparse( c->be->be_syncinfo, &bv ); 
3180                         ber_bvarray_add( &c->rvalue_vals, &bv );
3181                         return 0;
3182                 }
3183                 return 1;
3184         } else if ( c->op == LDAP_MOD_DELETE ) {
3185                 struct re_s *re;
3186
3187                 if ( c->be->be_syncinfo ) {
3188                         re = c->be->be_syncinfo->si_re;
3189                         if ( re ) {
3190                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
3191                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
3192                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
3193                         }
3194                         syncinfo_free( c->be->be_syncinfo );
3195                         c->be->be_syncinfo = NULL;
3196                 }
3197                 SLAP_DBFLAGS(c->be) &= ~(SLAP_DBFLAG_SHADOW|SLAP_DBFLAG_SYNC_SHADOW);
3198                 return 0;
3199         }
3200         if(SLAP_SHADOW(c->be)) {
3201                 Debug(LDAP_DEBUG_ANY, "%s: "
3202                         "syncrepl: database already shadowed.\n",
3203                         c->log, 0, 0);
3204                 return(1);
3205         } else if(add_syncrepl(c->be, c->argv, c->argc)) {
3206                 return(1);
3207         }
3208         SLAP_DBFLAGS(c->be) |= (SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW);
3209         return(0);
3210 }