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