]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
Fix debug output (print int as %d, not %s).
[openldap] / servers / slapd / syncrepl.c
1 /* $OpenLDAP$ */
2 /*
3  * Replication Engine which uses the LDAP Sync protocol
4  */
5 /* Copyright (c) 2003 by International Business Machines, Inc.
6  *
7  * International Business Machines, Inc. (hereinafter called IBM) grants
8  * permission under its copyrights to use, copy, modify, and distribute this
9  * Software with or without fee, provided that the above copyright notice and
10  * all paragraphs of this notice appear in all copies, and that the name of IBM
11  * not be used in connection with the marketing of any product incorporating
12  * the Software or modifications thereof, without specific, written prior
13  * permission.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
16  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
18  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
19  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
20  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26
27 #include <ac/string.h>
28 #include <ac/socket.h>
29 #include <db.h>
30
31 #include "ldap_pvt.h"
32 #include "lutil.h"
33 #include "slap.h"
34 #include "lutil_ldap.h"
35
36 #ifdef LDAP_SYNCREPL
37
38 #include "ldap_rq.h"
39
40 static Entry*
41 syncrepl_message_to_entry ( LDAP *, Operation *, LDAPMessage *,
42                 Modifications **, int*, struct berval *, struct berval * );
43
44 static int
45 syncrepl_entry( LDAP *, Operation*, Entry*, Modifications*,
46                 int, struct berval*, struct berval*, int );
47
48 static int
49 syncrepl_del_nonpresent( LDAP *, Operation * );
50
51 static void
52 syncrepl_add_glue( LDAP *, Operation*, Entry*, Modifications*, int,
53                    struct berval*, struct berval* );
54
55 static void
56 syncrepl_updateCookie( LDAP *, Operation *, struct berval *, struct berval * );
57
58 static int
59 slap_mods_check_syncrepl( Operation *, Modifications **,
60                           const char **, char *, size_t, void *ctx );
61
62 static int
63 slap_mods_opattrs_syncrepl( Operation *, Modifications *, Modifications **,
64                                 const char **, char *, size_t );
65
66 static int
67 slap_mods2entry_syncrepl( Modifications *, Entry **, int,
68                           const char **, char *, size_t );
69
70 /* callback functions */
71 static int cookie_callback( struct slap_op *, struct slap_rep * );
72 static int dn_callback( struct slap_op *, struct slap_rep * );
73 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
74 static int null_callback( struct slap_op *, struct slap_rep * );
75
76 static AttributeDescription **add_descs;
77 static AttributeDescription **add_descs_lastmod;
78 static AttributeDescription **del_descs;
79 static AttributeDescription **del_descs_lastmod;
80
81 struct runqueue_s syncrepl_rq;
82
83 void
84 init_syncrepl()
85 {
86         add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
87         add_descs[0] = slap_schema.si_ad_objectClass;
88         add_descs[1] = NULL;
89
90         add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
91         add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
92         add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
93         add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
94         add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
95         add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
96         add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
97         add_descs_lastmod[6] = NULL;
98
99         del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
100         del_descs[0] = slap_schema.si_ad_structuralObjectClass;
101         del_descs[1] = slap_schema.si_ad_subschemaSubentry;
102         del_descs[2] = slap_schema.si_ad_hasSubordinates;
103         del_descs[3] = slap_schema.si_ad_creatorsName;
104         del_descs[4] = slap_schema.si_ad_modifiersName;
105         del_descs[5] = slap_schema.si_ad_createTimestamp;
106         del_descs[6] = slap_schema.si_ad_modifyTimestamp;
107         del_descs[7] = slap_schema.si_ad_entryCSN;
108         del_descs[8] = NULL;
109
110         del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
111         del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
112         del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
113         del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
114         del_descs_lastmod[3] = NULL;
115 }
116
117 void *
118 do_syncrepl(
119         void    *ctx,
120         void    *arg )
121 {
122         struct re_s* rtask = arg;
123         Backend *be = rtask->arg;
124         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
125
126         SlapReply       rs = {REP_RESULT};
127
128         LDAPControl     c[2];
129         LDAPControl     **sctrls = NULL;
130         LDAPControl     **rctrls = NULL;
131         LDAPControl     *rctrlp = NULL;
132         BerElement      *sync_ber = NULL;
133         struct berval   *sync_bvalp = NULL;
134
135         BerElement      *ctrl_ber = NULL;
136         BerElement      *res_ber = NULL;
137
138         LDAP    *ld = NULL;
139         LDAPMessage     *res = NULL;
140         LDAPMessage     *msg = NULL;
141
142         ber_int_t       msgid;
143
144         int             nresponses, nreferences, nextended, npartial;
145         int             nresponses_psearch;
146
147         int             cancel_msgid = -1;
148         char            *retoid = NULL;
149         struct berval   *retdata = NULL;
150
151         int             sync_info_arrived = 0;
152         Entry           *entry = NULL;
153
154         int             syncstate;
155         struct berval   syncUUID = { 0, NULL };
156         struct berval   syncCookie = { 0, NULL };
157
158         int     rc;
159         int     err;
160         ber_len_t       len;
161         int     syncinfo_arrived = 0;
162         int     cancel_response = 0;
163
164         char **tmp = NULL;
165         AttributeDescription** descs = NULL;
166
167         Connection conn;
168         Operation op = {0};
169         slap_callback   cb;
170
171         void *memctx = NULL;
172         ber_len_t memsiz;
173         
174         int i, j, k, n;
175         int rc_efree;
176
177         struct berval base_bv = { 0, NULL };
178         struct berval pbase = { 0, NULL };
179         struct berval nbase = { 0, NULL };
180         struct berval sub_bv = { 0, NULL };
181         struct berval psubrdn = { 0, NULL };
182         struct berval nsubrdn = { 0, NULL };
183         struct berval psub = { 0, NULL };
184         struct berval nsub = { 0, NULL };
185         char substr[64];
186         Modifications   *modlist = NULL;
187         Modifications   *ml, *mlnext;
188         char *def_filter_str = NULL;
189
190 #ifdef NEW_LOGGING
191         LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
192 #else
193         Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
194 #endif
195
196         if ( si == NULL )
197                 return NULL;
198
199         if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
200              abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
201                 return NULL;
202         }
203
204         /* Init connection to master */
205
206         if ( ldap_is_ldap_url( si->masteruri )) {
207                 rc = ldap_initialize( &ld, si->masteruri );
208                 if ( rc != LDAP_SUCCESS ) {
209 #ifdef NEW_LOGGING
210                         LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
211                                 "ldap_initialize failed (%s)\n",
212                                 si->masteruri, 0, 0 );
213 #else
214                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
215                                 "ldap_initialize failed (%s)\n",
216                                 si->masteruri, 0, 0 );
217 #endif
218                 }
219         } else {
220                 ld = ldap_init( si->mastername, si->masterport );
221                 if ( ld == NULL ) {
222 #ifdef NEW_LOGGING
223                         LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
224                                 "ldap_init failed (%s:%d)\n",
225                                 si->mastername, si->masterport, 0 );
226 #else
227                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
228                                 "ldap_init failed (%s:%d)\n",
229                                 si->mastername, si->masterport, 0 );
230 #endif
231                 }
232         }
233
234         op.o_protocol = LDAP_VERSION3;
235         ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
236
237         /* Bind to master */
238
239         if ( si->tls ) {
240                 rc = ldap_start_tls_s( ld, NULL, NULL );
241                 if( rc != LDAP_SUCCESS ) {
242 #ifdef NEW_LOGGING
243                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
244                                 "%s: ldap_start_tls failed (%d)\n",
245                                 si->tls == TLS_CRITICAL ? "Error" : "Warning",
246                                 rc, 0 );
247 #else
248                         Debug( LDAP_DEBUG_ANY,
249                                 "%s: ldap_start_tls failed (%d)\n",
250                                 si->tls == TLS_CRITICAL ? "Error" : "Warning",
251                                 rc, 0 );
252 #endif
253                         if( si->tls == TLS_CRITICAL )
254                                 return NULL;
255                 }
256         }
257
258         if ( si->bindmethod == LDAP_AUTH_SASL ) {
259 #ifdef HAVE_CYRUS_SASL
260                 void *defaults;
261
262                 if ( si->secprops != NULL ) {
263                         int err = ldap_set_option( ld,
264                                         LDAP_OPT_X_SASL_SECPROPS, si->secprops);
265
266                         if( err != LDAP_OPT_SUCCESS ) {
267 #ifdef NEW_LOGGING
268                                 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
269                                         "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
270                                         si->mastername, si->secprops, 0 );
271 #else
272                                 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
273                                         "(%s,SECPROPS,\"%s\") failed!\n",
274                                         si->mastername, si->secprops, NULL );
275 #endif
276                                 return NULL;
277                         }
278                 }
279
280                 defaults = lutil_sasl_defaults( ld,
281                                 si->saslmech,
282                                 si->realm,
283                                 si->authcId,
284                                 si->passwd,
285                                 si->authzId );
286
287                 rc = ldap_sasl_interactive_bind_s( ld,
288                                 si->binddn,
289                                 si->saslmech,
290                                 NULL, NULL,
291                                 LDAP_SASL_AUTOMATIC,
292                                 lutil_sasl_interact,
293                                 defaults );
294
295                 if ( rc != LDAP_SUCCESS ) {
296 #ifdef NEW_LOGGING
297                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
298                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
299                                 rc, 0, 0 );
300 #else
301                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
302                                 "ldap_sasl_interactive_bind_s failed (%d)\n",
303                                 rc, 0, 0 );
304 #endif
305                         return NULL;
306                 }
307 #else /* HAVE_CYRUS_SASL */
308                 fprintf( stderr, "not compiled with SASL support\n" );
309                 return NULL;
310 #endif
311         } else {
312                 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
313                 if ( rc != LDAP_SUCCESS ) {
314 #ifdef NEW_LOGGING
315                         LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
316                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
317 #else
318                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
319                                 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
320 #endif
321                         return NULL;
322                 }
323         }
324
325         si->ctx = ctx;
326
327         op.o_tmpmemctx = NULL; /* FIXME : to use per-thread mem context */
328         op.o_tag = LDAP_REQ_SEARCH;
329         op.o_dn = si->updatedn;
330         op.o_ndn = si->updatedn;
331         op.o_callback = &cb;
332         op.o_time = slap_get_time();
333         op.o_managedsait = 1;
334         op.o_threadctx = si->ctx;
335         op.o_bd = be;
336         op.o_conn = &conn;
337         op.o_connid = op.o_conn->c_connid;
338         op.ors_scope = LDAP_SCOPE_BASE;
339         op.ors_deref = LDAP_DEREF_NEVER;
340         op.ors_slimit = -1;
341         op.ors_tlimit = -1;
342         op.ors_attrsonly = 0;
343         op.ors_attrs = NULL;
344         op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
345         ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
346                                 &op.ors_filterstr );
347
348         si->conn = &conn;
349         conn.c_send_ldap_result = slap_send_ldap_result;
350         conn.c_send_search_entry = slap_send_search_entry;
351         conn.c_send_search_reference = slap_send_search_reference;
352
353         /* get syncrepl cookie of shadow replica from subentry */
354         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
355         dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
356
357         sprintf( substr, "cn=syncrepl%d", si->id );
358         ber_str2bv( substr, strlen(substr), 1, &sub_bv );
359         dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
360
361         build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
362         build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
363
364         ch_free( base_bv.bv_val );
365         ch_free( pbase.bv_val );
366         ch_free( nbase.bv_val );
367         ch_free( sub_bv.bv_val );
368         ch_free( psubrdn.bv_val );
369         ch_free( nsubrdn.bv_val );
370
371         /* set callback function */
372         cb.sc_response = cookie_callback;
373         cb.sc_private = si;
374
375         /* search subentry to retrieve cookie */
376         si->syncCookie = NULL;
377         be->be_search( &op, &rs );
378
379         ch_free( op.o_req_dn.bv_val );
380         ch_free( op.o_req_ndn.bv_val );
381         filter_free( op.ors_filter );
382         ch_free( op.ors_filterstr.bv_val );
383
384         psub = be->be_nsuffix[0];
385
386         /* setup LDAP SYNC control */
387         sync_ber = ber_alloc_t( LBER_USE_DER );
388         ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
389
390         if ( si->syncCookie ) {
391                 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
392         } else {
393                 ber_printf( sync_ber, "{e}", abs(si->type) );
394         }
395
396         if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
397                 ber_free( sync_ber, 1 );
398                 return NULL;
399         }
400         ber_free( sync_ber, 1 );
401
402         sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
403
404         c[0].ldctl_oid = LDAP_CONTROL_SYNC;
405         c[0].ldctl_value = (*sync_bvalp);
406         c[0].ldctl_iscritical = si->type < 0;
407         sctrls[0] = &c[0];
408
409         if ( si->authzId ) {
410                 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
411                 c[1].ldctl_value.bv_val = si->authzId;
412                 c[1].ldctl_value.bv_len = strlen( si->authzId );
413                 c[1].ldctl_iscritical = 1;
414                 sctrls[1] = &c[1];
415         } else {
416                 sctrls[1] = NULL;
417         }
418
419         sctrls[2] = NULL;
420
421         err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
422
423         ber_bvfree( sync_bvalp );
424         ch_free( sctrls );
425
426         if ( err != LDAP_OPT_SUCCESS )
427                 fprintf( stderr, "Could not set controls : %d\n", err );
428
429         /* Delete Attributes */
430         if ( si->lastmod == LASTMOD_REQ ) {
431                 descs = del_descs_lastmod;
432         } else {
433                 descs = del_descs;
434         }
435
436         for ( i = 0; descs[i] != NULL; i++ ) {
437                 for ( j = 0; si->attrs[j] != NULL; j++ ) {
438                         if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
439                                 ch_free( si->attrs[j] );
440                                 for ( k = j; si->attrs[k] != NULL; k++ ) {
441                                         si->attrs[k] = si->attrs[k+1];
442                                 }
443                         }
444                 }
445         }
446
447         /* Add Attributes */
448
449         for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
450         
451         if ( si->lastmod == LASTMOD_REQ ) {
452                 descs = add_descs_lastmod;
453         } else {
454                 descs = add_descs;
455         }
456
457         for ( i = 0; descs[i] != NULL; i++ ) {
458                 tmp = ( char ** ) ch_realloc( si->attrs,
459                                 ( n + 2 ) * sizeof( char * ));
460                 if ( tmp == NULL ) {
461 #ifdef NEW_LOGGING
462                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
463 #else
464                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
465 #endif
466                 }
467                 si->attrs = tmp;
468                 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
469                 si->attrs[ n ] = NULL;
470         }
471
472         /* Send LDAP SYNC search */
473
474         rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
475                                 si->attrs, si->attrsonly, NULL, NULL,
476                                 NULL, -1, &msgid );
477
478         if( rc != LDAP_SUCCESS ) {
479                 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
480                                         ldap_err2string( rc ), rc );
481                 return NULL;
482         }
483
484         while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
485
486                 for ( msg = ldap_first_message( ld, res );
487                       msg != NULL;
488                       msg = ldap_next_message( ld, msg ) )
489                 {
490                         switch( ldap_msgtype( msg ) ) {
491                         case LDAP_RES_SEARCH_ENTRY:
492                                 entry = syncrepl_message_to_entry( ld, &op, msg,
493                                         &modlist, &syncstate, &syncUUID, &syncCookie );
494                                 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
495                                                 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
496                                 if ( syncCookie.bv_len ) {
497                                         syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
498                                 }
499                                 if ( rc_efree )
500                                         entry_free( entry );
501                                 for ( ml = modlist; ml != NULL; ml = mlnext ) {
502                                         mlnext = ml->sml_next;
503                                         ber_memfree( ml );
504                                 }
505                                 break;
506
507                         case LDAP_RES_SEARCH_REFERENCE:
508 #ifdef NEW_LOGGING
509                                 LDAP_LOG( OPERATION, ERR,
510                                         "do_syncrepl : reference received\n", 0, 0, 0 );
511 #else
512                                 Debug( LDAP_DEBUG_ANY,
513                                         "do_syncrepl : reference received\n", 0, 0, 0 );
514 #endif
515                                 break;
516
517                         case LDAP_RES_SEARCH_RESULT:
518                                 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
519                                 if ( rctrls ) {
520                                         rctrlp = *rctrls;
521                                         ctrl_ber = ber_alloc_t( LBER_USE_DER );
522                                         ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
523                                         ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
524                                         ber_reset( ctrl_ber, 1 );
525
526                                         ber_scanf( ctrl_ber, "{" );
527                                         if ( ber_peek_tag( ctrl_ber, &len )
528                                                 == LDAP_SYNC_TAG_COOKIE ) {
529                                                 ber_scanf( ctrl_ber, "o", &syncCookie );
530                                         }
531                                 }
532                                 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
533                                         if ( cancel_response ) {
534                                                 if ( syncCookie.bv_len ) {
535                                                         ber_bvfree( si->syncCookie );
536                                                         si->syncCookie = ber_dupbv( NULL, &syncCookie );
537                                                 }
538                                                 if ( ctrl_ber )
539                                                         ber_free( ctrl_ber, 1 );
540                                                 goto done;
541                                         }
542                                         else {
543                                                 if ( ctrl_ber )
544                                                         ber_free( ctrl_ber, 1 );
545                                                 break;
546                                         }
547                                 } else {
548                                         if ( syncCookie.bv_len ) {
549                                                 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
550                                         }
551                                         syncrepl_del_nonpresent( ld, &op );
552                                         if ( ctrl_ber )
553                                                 ber_free( ctrl_ber, 1 );
554                                         goto done;
555                                 }
556                                 break;
557
558                         case LDAP_RES_INTERMEDIATE_RESP:
559                                 ldap_parse_intermediate_resp_result( ld, msg,
560                                                 &retoid, &retdata, 0 );
561                                 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
562                                         sync_info_arrived = 1;
563                                         res_ber = ber_init( retdata );
564                                         ber_scanf( res_ber, "{e", &syncstate );
565
566                                         if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
567                                                 syncrepl_del_nonpresent( ld, &op );
568                                         } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
569 #ifdef NEW_LOGGING
570                                                 LDAP_LOG( OPERATION, ERR,
571                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
572 #else
573                                                 Debug( LDAP_DEBUG_ANY,
574                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
575 #endif
576                                         }
577
578                                         if ( ber_peek_tag( res_ber, &len )
579                                                                 == LDAP_SYNC_TAG_COOKIE ) {
580                                                 ber_scanf( res_ber, "o}", &syncCookie );
581                                                 if ( syncCookie.bv_len ) {
582                                                         ber_bvfree( si->syncCookie );
583                                                         si->syncCookie = ber_dupbv( NULL, &syncCookie );
584                                                 }
585                                         } else {
586                                                 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
587 #ifdef NEW_LOGGING
588                                                         LDAP_LOG( OPERATION, ERR,
589                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
590 #else
591                                                         Debug( LDAP_DEBUG_ANY,
592                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
593 #endif
594                                                 }
595                                         }
596
597                                         ldap_memfree( retoid );
598                                         ber_bvfree( retdata );
599                                         ber_free( res_ber, 1 );
600                                         break;
601                                 } else {
602 #ifdef NEW_LOGGING
603                                         LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
604                                                 " unknown intermediate "
605                                                 "response\n", 0, 0, 0 );
606 #else
607                                         Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
608                                                 "unknown intermediate "
609                                                 "response\n", 0, 0, 0 );
610 #endif
611                                         ldap_memfree( retoid );
612                                         ber_bvfree( retdata );
613                                         break;
614                                 }
615                                 break;
616                         default:
617 #ifdef NEW_LOGGING
618                                 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
619                                         "unknown message\n", 0, 0, 0 );
620 #else
621                                 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
622                                         "unknown message\n", 0, 0, 0 );
623 #endif
624                                 break;
625
626                         }
627                 }
628                 ldap_msgfree( res );
629         }
630
631         if ( rc == -1 ) {
632 #ifdef NEW_LOGGING
633                 LDAP_LOG( OPERATION, ERR,
634                         "do_syncrepl : unknown result\n", 0, 0, 0 );
635 #else
636                 Debug( LDAP_DEBUG_ANY,
637                         "do_syncrepl : unknown result\n", 0, 0, 0 );
638 #endif
639         }
640
641 done:
642         if ( syncCookie.bv_val )
643                 ch_free( syncCookie.bv_val );
644         if ( syncUUID.bv_val )
645                 ch_free( syncUUID.bv_val );
646
647         if ( res )
648                 ldap_msgfree( res );
649         ldap_unbind( ld );
650
651         ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
652         ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
653         if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
654                 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
655         } else {
656                 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
657         }
658         ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
659
660         return NULL;
661 }
662
663 static Entry*
664 syncrepl_message_to_entry(
665         LDAP            *ld,
666         Operation       *op,
667         LDAPMessage     *msg,
668         Modifications   **modlist,
669         int             *syncstate,
670         struct berval   *syncUUID,
671         struct berval   *syncCookie
672 )
673 {
674         Entry           *e;
675         BerElement      *ber = NULL;
676         BerElement      *tmpber;
677         struct berval   bv = {0, NULL};
678         Modifications   tmp;
679         Modifications   *mod;
680         Modifications   **modtail = modlist;
681         Backend         *be = op->o_bd;
682
683         const char      *text;
684         char txtbuf[SLAP_TEXT_BUFLEN];
685         size_t textlen = sizeof txtbuf;
686
687         struct berval   **bvals = NULL;
688         char            *dn;
689         struct berval   bdn = {0, NULL};
690         Attribute       *attr;
691         struct berval   empty_bv = { 0, NULL };
692         int             rc;
693         char            *a;
694
695         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
696
697         ber_len_t       len;
698         LDAPControl*    rctrlp;
699         LDAPControl**   rctrls = NULL;
700         BerElement*     ctrl_ber;
701
702         ber_tag_t       tag;
703
704         *modlist = NULL;
705
706         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
707 #ifdef NEW_LOGGING
708                 LDAP_LOG( OPERATION, ERR,
709                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
710 #else
711                 Debug( LDAP_DEBUG_ANY,
712                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
713 #endif
714                 return NULL;
715         }
716
717         op->o_tag = LDAP_REQ_ADD;
718
719         rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
720
721         if ( rc != LDAP_SUCCESS ) {
722 #ifdef NEW_LOGGING
723                 LDAP_LOG( OPERATION, ERR,
724                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
725 #else
726                 Debug( LDAP_DEBUG_ANY,
727                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
728 #endif
729                 return NULL;
730         }
731
732         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
733         dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
734
735         e->e_attrs = NULL;
736
737         while ( ber_remaining( ber ) ) {
738                 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
739
740                 if ( tag == LBER_ERROR ) break;
741                 if ( tmp.sml_type.bv_val == NULL ) break;
742
743                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
744
745                 mod->sml_op = LDAP_MOD_REPLACE;
746                 mod->sml_next = NULL;
747                 mod->sml_desc = NULL;
748                 mod->sml_type = tmp.sml_type;
749                 mod->sml_bvalues = tmp.sml_bvalues;
750                 mod->sml_nvalues = tmp.sml_bvalues;
751
752                 *modtail = mod;
753                 modtail = &mod->sml_next;
754         }
755
756         if ( ber_scanf( ber, "}") == LBER_ERROR ) {
757 #ifdef NEW_LOGGING
758                 LDAP_LOG( OPERATION, ERR,
759                                 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
760 #else
761                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
762                                 0, 0, 0 );
763 #endif
764                 return NULL;
765         }
766
767         ber_free( ber, 0 );
768         tmpber = ldap_get_message_ber( msg );
769         ber = ber_dup( tmpber );
770
771         ber_scanf( ber, "{xx" );
772
773         rc = ldap_int_get_controls( ber, &rctrls );
774
775         if ( rc != LDAP_SUCCESS ) {
776 #ifdef NEW_LOGGING
777                 LDAP_LOG( OPERATION, ERR,
778                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
779 #else
780                 Debug( LDAP_DEBUG_ANY,
781                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
782 #endif
783                 return NULL;
784         }
785
786         if ( rctrls ) {
787                 rctrlp = *rctrls;
788                 ctrl_ber = ber_alloc_t( LBER_USE_DER );
789                 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
790                 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
791                 ber_reset( ctrl_ber, 1 );
792                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
793                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
794                         ber_scanf( ctrl_ber, "o}", syncCookie );
795                 }
796                 ber_free( ctrl_ber, 1 );
797         } else {
798 #ifdef NEW_LOGGING
799                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
800                         " rctrls absent\n", 0, 0, 0 );
801 #else
802                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
803                         " rctrls absent\n", 0, 0, 0 );
804 #endif
805         }
806
807         if ( *syncstate == LDAP_SYNC_PRESENT ) {
808                 e = NULL;
809                 goto done;
810         }
811
812         if ( *modlist == NULL ) {
813 #ifdef NEW_LOGGING
814                 LDAP_LOG( OPERATION, ERR,
815                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
816 #else
817                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
818                                 0, 0, 0 );
819 #endif
820         }
821
822         rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
823
824         if ( rc != LDAP_SUCCESS ) {
825 #ifdef NEW_LOGGING
826                 LDAP_LOG( OPERATION, ERR,
827                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
828 #else
829                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
830                                 text, 0, 0 );
831 #endif
832                 return NULL;
833         }
834         
835         rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
836                                          &text,txtbuf, textlen );
837         
838         if( rc != LDAP_SUCCESS ) {
839 #ifdef NEW_LOGGING
840                 LDAP_LOG( OPERATION, ERR,
841                                 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
842 #else
843                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
844                                 text, 0, 0 );
845 #endif
846                 return NULL;
847         }
848
849         rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
850         if( rc != LDAP_SUCCESS ) {
851 #ifdef NEW_LOGGING
852                 LDAP_LOG( OPERATION, ERR,
853                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
854 #else
855                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
856                                 text, 0, 0 );
857 #endif
858         }
859
860 done:
861
862         ber_free ( ber, 0 );
863
864         return e;
865 }
866
867 int
868 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
869 {
870         const struct berval *uuid1 = v_uuid1;
871         const struct berval *uuid2 = v_uuid2;
872         int rc = uuid1->bv_len - uuid2->bv_len;
873         if ( rc ) return rc;
874         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
875 }
876
877 static int
878 syncrepl_entry(
879         LDAP *ld,
880         Operation *op,
881         Entry* e,
882         Modifications* modlist,
883         int syncstate,
884         struct berval* syncUUID,
885         struct berval* syncCookie,
886         int refresh
887 )
888 {
889         Backend *be = op->o_bd;
890         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
891         slap_callback   cb;
892         struct berval   csn_bv = {0, NULL};
893         struct berval   *syncuuid_bv = NULL;
894         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
895
896         SlapReply       rs = {REP_RESULT};
897         int rc = LDAP_SUCCESS;
898
899         struct berval base_bv = {0, NULL};
900
901         char *filterstr;
902         Filter *filter;
903
904         Attribute *a;
905
906         if ( refresh &&
907                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
908                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
909                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
910                                                 syncuuid_cmp, avl_dup_error );
911         }
912
913         if ( syncstate == LDAP_SYNC_PRESENT ) {
914                 if ( e )
915                         return 1;
916                 else
917                         return 0;
918         }
919
920         if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
921                 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
922         }
923
924         filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1, op->o_tmpmemctx ); 
925         strcpy( filterstr, "entryUUID=" );
926         strcat( filterstr, syncUUID->bv_val );
927
928         si->e = e;
929         si->syncUUID = syncUUID;
930         si->syncUUID_ndn = NULL;
931
932         filter = str2filter( filterstr );
933         ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
934         ch_free( filterstr );
935         op->ors_filter = filter;
936         op->ors_scope = LDAP_SCOPE_SUBTREE;
937
938         /* get syncrepl cookie of shadow replica from subentry */
939         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
940         dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
941         ch_free( base_bv.bv_val );
942
943         /* set callback function */
944         op->o_callback = &cb;
945         cb.sc_response = dn_callback;
946         cb.sc_private = si;
947
948         be->be_search( op, &rs );
949
950         ch_free( op->o_req_dn.bv_val );
951         ch_free( op->o_req_ndn.bv_val );
952         filter_free( op->ors_filter );
953         ch_free( op->ors_filterstr.bv_val );
954
955         cb.sc_response = null_callback;
956
957         if ( si->syncUUID_ndn )
958                 printf("syncUUID_ndn = %s\n", si->syncUUID_ndn );
959
960         switch ( syncstate ) {
961         case LDAP_SYNC_ADD :
962         case LDAP_SYNC_MODIFY :
963
964                 rc = LDAP_SUCCESS;
965
966                 if ( si->syncUUID_ndn ) {
967                         op->o_req_dn = *si->syncUUID_ndn;
968                         op->o_req_ndn = *si->syncUUID_ndn;
969                         op->o_tag = LDAP_REQ_DELETE;
970                         rc = be->be_delete( op, &rs );
971                 }
972
973                 if ( rc == LDAP_SUCCESS ||
974                          rc == LDAP_REFERRAL ||
975                          rc == LDAP_NO_SUCH_OBJECT ||
976                          rc == DB_NOTFOUND ) {
977                         op->o_tag = LDAP_REQ_ADD;
978                         op->ora_e = e;
979                         op->o_req_dn = e->e_name;
980                         op->o_req_ndn = e->e_nname;
981                         rc = be->be_add( op, &rs );
982                         if ( rc != LDAP_SUCCESS ) {
983                                 if ( rc == LDAP_ALREADY_EXISTS ) {
984 #ifdef NEW_LOGGING
985                                         LDAP_LOG( OPERATION, ERR,
986                                                 "be_add failed : already exists (%d)\n",
987                                                 rc, 0, 0 );
988 #else
989                                         Debug( LDAP_DEBUG_ANY,
990                                                 "be_add failed : already exists (%d)\n",
991                                                 rc, 0, 0 );
992 #endif
993                                 } else if ( rc == LDAP_REFERRAL ||
994                                                         rc == LDAP_NO_SUCH_OBJECT ||
995                                                         rc == DB_NOTFOUND ) {
996                                         syncrepl_add_glue(ld, op, e,
997                                                 modlist, syncstate,
998                                                 syncUUID, syncCookie);
999                                 } else {
1000 #ifdef NEW_LOGGING
1001                                         LDAP_LOG( OPERATION, ERR,
1002                                                 "be_add failed (%d)\n",
1003                                                 rc, 0, 0 );
1004 #else
1005                                         Debug( LDAP_DEBUG_ANY,
1006                                                 "be_add failed (%d)\n",
1007                                                 rc, 0, 0 );
1008 #endif
1009                                 }
1010                         } else {
1011                                 return 0;
1012                         }
1013                 } else {
1014 #ifdef NEW_LOGGING
1015                         LDAP_LOG( OPERATION, ERR,
1016                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1017 #else
1018                         Debug( LDAP_DEBUG_ANY,
1019                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1020 #endif
1021                 }
1022
1023                 si->e = NULL;
1024                 return 1;
1025         case LDAP_SYNC_DELETE :
1026                 op->o_tag = LDAP_REQ_DELETE;
1027                 be->be_delete( op, &rs );
1028                 si->e = NULL;
1029                 return 1;
1030         default :
1031 #ifdef NEW_LOGGING
1032                 LDAP_LOG( OPERATION, ERR,
1033                         "unknown syncstate\n", 0, 0, 0 );
1034 #else
1035                 Debug( LDAP_DEBUG_ANY,
1036                         "unknown syncstate\n", 0, 0, 0 );
1037 #endif
1038                 return 1;
1039         }
1040 }
1041
1042 static int
1043 syncrepl_del_nonpresent(
1044         LDAP *ld,
1045         Operation *op
1046 )
1047 {
1048         Backend* be = op->o_bd;
1049         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1050         slap_callback   cb;
1051         struct berval   base_bv = {0, NULL};
1052         Filter *filter;
1053         SlapReply       rs = {REP_RESULT};
1054         struct berval   filterstr_bv = {0, NULL};
1055         struct nonpresent_entry *np_list, *np_prev;
1056
1057         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1058         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1059         ch_free( base_bv.bv_val );
1060
1061         filter = str2filter( si->filterstr );
1062
1063         cb.sc_response = nonpresent_callback;
1064         cb.sc_private = si;
1065
1066         op->o_callback = &cb;
1067         op->o_tag = LDAP_REQ_SEARCH;
1068         op->ors_scope = si->scope;
1069         op->ors_deref = LDAP_DEREF_NEVER;
1070         op->ors_slimit = -1;
1071         op->ors_tlimit = -1;
1072         op->ors_attrsonly = 0;
1073         op->ors_attrs = NULL;
1074         op->ors_filter = filter;
1075         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1076
1077         be->be_search( op, &rs );
1078
1079         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1080                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1081                 while ( np_list != NULL ) {
1082                         LDAP_LIST_REMOVE( np_list, np_link );
1083                         np_prev = np_list;
1084                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1085                         op->o_tag = LDAP_REQ_DELETE;
1086                         op->o_callback = &cb;
1087                         cb.sc_response = null_callback;
1088                         cb.sc_private = si;
1089                         op->o_req_dn = *np_prev->dn;
1090                         op->o_req_ndn = *np_prev->ndn;
1091                         op->o_bd->be_delete( op, &rs );
1092                         ber_bvfree( np_prev->dn );
1093                         ber_bvfree( np_prev->ndn );
1094                         op->o_req_dn.bv_val = NULL;
1095                         op->o_req_ndn.bv_val = NULL;
1096                         ch_free( np_prev );
1097                 }
1098         }
1099
1100         if ( op->o_req_dn.bv_val )
1101                 ch_free( op->o_req_dn.bv_val );
1102         if ( op->o_req_ndn.bv_val )
1103                 ch_free( op->o_req_ndn.bv_val );
1104         filter_free( op->ors_filter );
1105         ch_free( op->ors_filterstr.bv_val );
1106 }
1107
1108
1109 static void
1110 syncrepl_add_glue(
1111         LDAP *ld,
1112         Operation* op,
1113         Entry *e,
1114         Modifications* modlist,
1115         int syncstate,
1116         struct berval* syncUUID,
1117         struct berval* syncCookie
1118 )
1119 {
1120         Backend *be = op->o_bd;
1121         syncinfo_t *si = op->o_callback->sc_private;
1122         struct berval   uuid_bv = {0, NULL};
1123         slap_callback cb;
1124         Attribute       *a;
1125         int     rc;
1126         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1127         int levels = 0;
1128         int i, j, k;
1129         struct berval dn = {0, NULL};
1130         struct berval pdn = {0, NULL};
1131         struct berval ndn = {0, NULL};
1132         struct berval rdn = {0, NULL};
1133         Entry   *glue;
1134         SlapReply       rs = {REP_RESULT};
1135         Connection *conn = op->o_conn;
1136
1137         op->o_tag = LDAP_REQ_ADD;
1138         op->o_callback = &cb;
1139         cb.sc_response = null_callback;
1140         cb.sc_private = si;
1141
1142         ber_dupbv( &dn, &e->e_nname );
1143         ber_dupbv( &pdn, &e->e_nname );
1144
1145         while ( !be_issuffix ( be, &pdn )) {
1146                 dnParent( &dn, &pdn );
1147                 ch_free( dn.bv_val );
1148                 ber_dupbv( &dn, &pdn );
1149                 levels++;
1150         }
1151
1152         for ( i = 0; i <= levels; i++ ) {
1153                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1154                 ch_free( dn.bv_val );
1155                 ch_free( pdn.bv_val );
1156                 ber_dupbv( &dn, &e->e_nname );
1157                 ber_dupbv( &pdn, &e->e_nname );
1158                 j = levels - i;
1159                 for ( k = 0; k < j; k++ ) {
1160                         dnParent( &dn, &pdn );
1161                         ch_free( dn.bv_val );
1162                         ber_dupbv( &dn, &pdn );
1163                 }
1164
1165                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1166                 ber_dupbv( &glue->e_name, &pdn );
1167                 ber_dupbv( &glue->e_nname, &ndn );
1168                 ch_free( dn.bv_val );
1169                 ch_free( pdn.bv_val );
1170                 ch_free( ndn.bv_val );
1171
1172                 a = ch_calloc( 1, sizeof( Attribute ));
1173                 a->a_desc = slap_schema.si_ad_objectClass;
1174                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1175                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1176                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1177                 a->a_vals[2].bv_len = 0;
1178                 a->a_vals[2].bv_val = NULL;
1179                 a->a_next = glue->e_attrs;
1180                 glue->e_attrs = a;
1181
1182                 a = ch_calloc( 1, sizeof( Attribute ));
1183                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1184                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1185                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1186                 a->a_vals[1].bv_len = 0;
1187                 a->a_vals[1].bv_val = NULL;
1188                 a->a_next = glue->e_attrs;
1189                 glue->e_attrs = a;
1190
1191                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1192                         op->o_req_dn = e->e_name;
1193                         op->o_req_ndn = e->e_nname;
1194                         op->ora_e = e;
1195                         rc = be->be_add ( op, &rs );
1196                         if ( rc == LDAP_SUCCESS )
1197                                 be_entry_release_w( op, e );
1198                         else 
1199                                 entry_free( e );
1200                         entry_free( glue );
1201                 } else {
1202                         op->o_req_dn = glue->e_name;
1203                         op->o_req_ndn = glue->e_nname;
1204                         op->ora_e = glue;
1205                         rc = be->be_add ( op, &rs );
1206                         if ( rc == LDAP_SUCCESS ) {
1207                                 be_entry_release_w( op, glue );
1208                         } else {
1209                         /* incl. ALREADY EXIST */
1210                                 entry_free( glue );
1211                         }
1212                 }
1213         }
1214
1215         return;
1216 }
1217
1218 static void
1219 syncrepl_updateCookie(
1220         LDAP *ld,
1221         Operation *op,
1222         struct berval *pdn,
1223         struct berval *syncCookie
1224 )
1225 {
1226         Backend *be = op->o_bd;
1227         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1228         Modifications *ml;
1229         Modifications *mlnext;
1230         Modifications *mod;
1231         Modifications *modlist;
1232         Modifications **modtail = &modlist;
1233
1234         struct berval* ocbva = NULL;
1235         struct berval* cnbva = NULL;
1236         struct berval* ssbva = NULL;
1237         struct berval* scbva = NULL;
1238
1239         char substr[64];
1240         char rdnstr[67];
1241         const char      *text;
1242         char txtbuf[SLAP_TEXT_BUFLEN];
1243         size_t textlen = sizeof txtbuf;
1244
1245         Entry* e;
1246         int rc;
1247
1248         struct berval sub_bv = { 0, NULL };
1249         struct berval psubrdn = { 0, NULL };
1250         
1251         slap_callback cb;
1252         SlapReply       rs = {REP_RESULT};
1253
1254         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1255         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1256         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1257         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1258
1259         /* update in memory cookie */
1260         if ( si->syncCookie != NULL ) {
1261                 ber_bvfree( si->syncCookie );
1262         }
1263         si->syncCookie = ber_dupbv( NULL, syncCookie );
1264         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1265         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1266         ber_str2bv( "syncConsumerSubentry",
1267                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1268         ocbva[3].bv_len = 0;
1269         ocbva[3].bv_val = NULL;
1270
1271         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1272         mod->sml_op = LDAP_MOD_REPLACE;
1273         mod->sml_next = NULL;
1274         mod->sml_desc = NULL;
1275         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1276         mod->sml_bvalues = ocbva;
1277         mod->sml_nvalues = ocbva;
1278         *modtail = mod;
1279         modtail = &mod->sml_next;
1280
1281         sprintf( substr, "syncrepl%d", si->id );
1282         sprintf( rdnstr, "cn=%s", substr );
1283         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1284         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1285         cnbva[1].bv_len = 0;
1286         cnbva[1].bv_val = NULL;
1287         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1288         mod->sml_op = LDAP_MOD_REPLACE;
1289         mod->sml_next = NULL;
1290         mod->sml_desc = NULL;
1291         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1292         mod->sml_bvalues = cnbva;
1293         mod->sml_nvalues = cnbva;
1294         *modtail = mod;
1295         modtail = &mod->sml_next;
1296
1297         ber_dupbv( &scbva[0], si->syncCookie );
1298         scbva[1].bv_len = 0;
1299         scbva[1].bv_val = NULL;
1300         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1301         mod->sml_op = LDAP_MOD_REPLACE;
1302         mod->sml_next = NULL;
1303         mod->sml_desc = NULL;
1304         ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1305                                                 1, &mod->sml_type );
1306         mod->sml_bvalues = scbva;
1307         mod->sml_nvalues = scbva;
1308         *modtail = mod;
1309         modtail = &mod->sml_next;
1310
1311         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1312         ssbva[1].bv_len = 0;
1313         ssbva[1].bv_val = NULL;
1314         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1315         mod->sml_op = LDAP_MOD_REPLACE;
1316         mod->sml_next = NULL;
1317         mod->sml_desc = NULL;
1318         ber_str2bv( "subtreeSpecification",
1319                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1320         mod->sml_bvalues = ssbva;
1321         mod->sml_nvalues = ssbva;
1322         *modtail = mod;
1323         modtail = &mod->sml_next;
1324
1325         rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1326
1327         if ( rc != LDAP_SUCCESS ) {
1328 #ifdef NEW_LOGGING
1329                 LDAP_LOG( OPERATION, ERR,
1330                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1331 #else
1332                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1333                          text, 0, 0 );
1334 #endif
1335         }
1336
1337         op->o_tag = LDAP_REQ_ADD;
1338         rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1339
1340         if( rc != LDAP_SUCCESS ) {
1341 #ifdef NEW_LOGGING
1342                 LDAP_LOG( OPERATION, ERR,
1343                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1344 #else
1345                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1346                          text, 0, 0 );
1347 #endif
1348         }
1349
1350         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1351
1352         build_new_dn( &sub_bv, pdn, &psubrdn );
1353         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1354         ch_free( sub_bv.bv_val );
1355         ch_free( psubrdn.bv_val );
1356
1357         e->e_attrs = NULL;
1358
1359         rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1360
1361         if( rc != LDAP_SUCCESS ) {
1362 #ifdef NEW_LOGGING
1363                 LDAP_LOG( OPERATION, ERR,
1364                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1365 #else
1366                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1367                          text, 0, 0 );
1368 #endif
1369         }
1370
1371         cb.sc_response = null_callback;
1372         cb.sc_private = si;
1373
1374         op->o_callback = &cb;
1375         op->o_req_dn = e->e_name;
1376         op->o_req_ndn = e->e_nname;
1377
1378         /* update persistent cookie */
1379 update_cookie_retry:
1380         op->o_tag = LDAP_REQ_MODIFY;
1381         op->orm_modlist = modlist;
1382         rc = be->be_modify( op, &rs );
1383         if ( rc != LDAP_SUCCESS ) {
1384                 if ( rc == LDAP_REFERRAL ||
1385                          rc == LDAP_NO_SUCH_OBJECT ||
1386                          rc == DB_NOTFOUND ) {
1387                         op->o_tag = LDAP_REQ_ADD;
1388                         op->ora_e = e;
1389                         rc = be->be_add( op, &rs );
1390                         if ( rc != LDAP_SUCCESS ) {
1391                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1392                                         goto update_cookie_retry;
1393                                 } else if ( rc == LDAP_REFERRAL ||
1394                                                         rc == LDAP_NO_SUCH_OBJECT ||
1395                                                         rc == DB_NOTFOUND ) {
1396 #ifdef NEW_LOGGING
1397                                         LDAP_LOG( OPERATION, ERR,
1398                                                 "cookie will be non-persistent\n",
1399                                                 0, 0, 0 );
1400 #else
1401                                         Debug( LDAP_DEBUG_ANY,
1402                                                 "cookie will be non-persistent\n",
1403                                                 0, 0, 0 );
1404 #endif
1405                                 } else {
1406 #ifdef NEW_LOGGING
1407                                         LDAP_LOG( OPERATION, ERR,
1408                                                 "be_add failed (%d)\n",
1409                                                 rc, 0, 0 );
1410 #else
1411                                         Debug( LDAP_DEBUG_ANY,
1412                                                 "be_add failed (%d)\n",
1413                                                 rc, 0, 0 );
1414 #endif
1415                                 }
1416                         } else {
1417                                 goto done;
1418                         }
1419                 } else {
1420 #ifdef NEW_LOGGING
1421                         LDAP_LOG( OPERATION, ERR,
1422                                 "be_modify failed (%d)\n", rc, 0, 0 );
1423 #else
1424                         Debug( LDAP_DEBUG_ANY,
1425                                 "be_modify failed (%d)\n", rc, 0, 0 );
1426 #endif
1427                 }
1428         }
1429
1430         if ( e != NULL )
1431                 entry_free( e );
1432
1433 done :
1434
1435         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1436                 mlnext = ml->sml_next;
1437                 free( ml );
1438         }
1439
1440         return;
1441 }
1442
1443
1444 static
1445 int slap_mods_check_syncrepl(
1446         Operation *op,
1447         Modifications **mlp,
1448         const char **text,
1449         char *textbuf,
1450         size_t textlen,
1451         void *ctx )
1452 {
1453         int rc;
1454         Backend *be = op->o_bd;
1455         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1456         AttributeDescription** descs;
1457         int i;
1458         Modifications *prevml = NULL;
1459         Modifications *nextml = NULL;
1460         Modifications *ml = *mlp;
1461
1462         while ( ml != NULL ) {
1463                 AttributeDescription *ad = NULL;
1464
1465                 /* convert to attribute description */
1466                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1467
1468                 if( rc != LDAP_SUCCESS ) {
1469                         snprintf( textbuf, textlen, "%s: %s",
1470                                                 ml->sml_type.bv_val, *text );
1471                         *text = textbuf;
1472                         return rc;
1473                 }
1474
1475                 ad = ml->sml_desc;
1476
1477                 if ( si->lastmod == LASTMOD_REQ ) {
1478                         descs = del_descs_lastmod;
1479                 } else {
1480                         descs = del_descs;
1481                 }
1482
1483                 for ( i = 0; descs[i] != NULL; i++ ) {
1484                         if ( ad == descs[i] ) {
1485                                 if ( prevml == NULL ) {
1486                                         mlp = &ml->sml_next;
1487                                         prevml = NULL;
1488                                 } else {
1489                                         prevml->sml_next = ml->sml_next;
1490                                 }
1491                                 slap_mod_free( &ml->sml_mod, 0 );
1492                                 nextml = ml->sml_next;
1493                                 free( ml );
1494                                 ml = nextml;
1495                                 continue;
1496                         }
1497                 }
1498
1499                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1500                                 && !slap_ad_is_binary( ad )) {
1501                         /* attribute requires binary transfer */
1502                         snprintf( textbuf, textlen,
1503                                         "%s: requires ;binary transfer",
1504                                         ml->sml_type.bv_val );
1505                         *text = textbuf;
1506                         return LDAP_UNDEFINED_TYPE;
1507                 }
1508
1509                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1510                                         && slap_ad_is_binary( ad )) {
1511                         /* attribute requires binary transfer */
1512                         snprintf( textbuf, textlen,
1513                                         "%s: disallows ;binary transfer",
1514                                         ml->sml_type.bv_val );
1515                         *text = textbuf;
1516                         return LDAP_UNDEFINED_TYPE;
1517                 }
1518
1519                 if( slap_ad_is_tag_range( ad )) {
1520                         /* attribute requires binary transfer */
1521                         snprintf( textbuf, textlen,
1522                                         "%s: inappropriate use of tag range option",
1523                                         ml->sml_type.bv_val );
1524                         *text = textbuf;
1525                         return LDAP_UNDEFINED_TYPE;
1526                 }
1527
1528                 if ( is_at_obsolete( ad->ad_type ) &&
1529                                 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1530                         /*
1531                          * attribute is obsolete,
1532                          * only allow replace/delete with no values
1533                          */
1534                         snprintf( textbuf, textlen,
1535                                         "%s: attribute is obsolete",
1536                                         ml->sml_type.bv_val );
1537                         *text = textbuf;
1538                         return LDAP_CONSTRAINT_VIOLATION;
1539                 }
1540
1541                 /*
1542                  * check values
1543                  */
1544                 if( ml->sml_values != NULL ) {
1545                         ber_len_t nvals;
1546                         slap_syntax_validate_func *validate =
1547                         ad->ad_type->sat_syntax->ssyn_validate;
1548                         slap_syntax_transform_func *pretty =
1549                         ad->ad_type->sat_syntax->ssyn_pretty;
1550
1551                         if( !pretty && !validate ) {
1552                                 *text = "no validator for syntax";
1553                                 snprintf( textbuf, textlen,
1554                                                 "%s: no validator for syntax %s",
1555                                                 ml->sml_type.bv_val,
1556                                                 ad->ad_type->sat_syntax->ssyn_oid );
1557                                 *text = textbuf;
1558                                 return LDAP_INVALID_SYNTAX;
1559                         }
1560
1561                         /*
1562                          * check that each value is valid per syntax
1563                          * and pretty if appropriate
1564                          */
1565                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1566                                 struct berval pval = {0, NULL};
1567                                 if( pretty ) {
1568                                         rc = pretty( ad->ad_type->sat_syntax,
1569                                                         &ml->sml_values[nvals], &pval, ctx );
1570                                 } else {
1571                                         rc = validate( ad->ad_type->sat_syntax,
1572                                                         &ml->sml_values[nvals] );
1573                                 }
1574
1575                                 if( rc != 0 ) {
1576                                         snprintf( textbuf, textlen,
1577                                                         "%s: value #%ld invalid per syntax",
1578                                                         ml->sml_type.bv_val, (long) nvals );
1579                                         *text = textbuf;
1580                                         return LDAP_INVALID_SYNTAX;
1581                                 }
1582
1583                                 if( pretty ) {
1584                                         ber_memfree( ml->sml_values[nvals].bv_val );
1585                                         ml->sml_values[nvals] = pval;
1586                                 }
1587                         }
1588
1589                         /*
1590                          * a rough single value check... an additional check is needed
1591                          * to catch add of single value to existing single valued attribute
1592                          */
1593                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1594                                         && nvals > 1 && is_at_single_value( ad->ad_type )) {
1595                                 snprintf( textbuf, textlen,
1596                                                 "%s: multiple values provided",
1597                                                 ml->sml_type.bv_val );
1598                                 *text = textbuf;
1599                                 return LDAP_CONSTRAINT_VIOLATION;
1600                         }
1601
1602                         if( nvals && ad->ad_type->sat_equality &&
1603                                         ad->ad_type->sat_equality->smr_normalize ) {
1604                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1605                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1606                                         rc = ad->ad_type->sat_equality->smr_normalize( 0,
1607                                                         ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1608                                                         &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1609                                         if( rc ) {
1610 #ifdef NEW_LOGGING
1611                                                 LDAP_LOG( OPERATION, DETAIL1,
1612                                                                 "str2entry:  NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1613 #else
1614                                                 Debug( LDAP_DEBUG_ANY,
1615                                                                 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1616 #endif
1617                                                 snprintf( textbuf, textlen,
1618                                                                 "%s: value #%ld normalization failed",
1619                                                                 ml->sml_type.bv_val, (long) nvals );
1620                                                 *text = textbuf;
1621                                                 return rc;
1622                                         }
1623                                 }
1624                                 ml->sml_nvalues[nvals].bv_val = NULL;
1625                                 ml->sml_nvalues[nvals].bv_len = 0;
1626                         }
1627                 }
1628                 prevml = ml;
1629                 ml = ml->sml_next;
1630         }
1631
1632         return LDAP_SUCCESS;
1633 }
1634
1635 static
1636 int slap_mods_opattrs_syncrepl(
1637         Operation *op,
1638         Modifications *mods,
1639         Modifications **modtail,
1640         const char **text,
1641         char *textbuf, size_t textlen )
1642 {
1643         struct berval name = {0, NULL};
1644         struct berval timestamp = {0, NULL};
1645         struct berval csn = {0, NULL};
1646         struct berval nname = {0, NULL};
1647         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1648         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1649         Modifications *mod;
1650         Backend *be = op->o_bd;
1651         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1652
1653         int mop = LDAP_MOD_REPLACE;
1654
1655         assert( modtail != NULL );
1656         assert( *modtail == NULL );
1657
1658         if( si->lastmod == LASTMOD_GEN ) {
1659                 struct tm *ltm;
1660                 time_t now = slap_get_time();
1661
1662                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1663                 ltm = gmtime( &now );
1664                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1665
1666                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1667                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1668                 csn.bv_val = csnbuf;
1669
1670                 timestamp.bv_val = timebuf;
1671                 timestamp.bv_len = strlen(timebuf);
1672
1673                 if( op->o_dn.bv_len == 0 ) {
1674                         name.bv_val = SLAPD_ANONYMOUS;
1675                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1676                         nname = name;
1677                 } else {
1678                         name = op->o_dn;
1679                         nname = op->o_ndn;
1680                 }
1681         }
1682
1683         if( op->o_tag == LDAP_REQ_ADD ) {
1684                 struct berval tmpval = {0, NULL};
1685
1686                 if( global_schemacheck ) {
1687                         int rc = mods_structural_class( mods, &tmpval,
1688                                                                         text, textbuf, textlen );
1689                         if( rc != LDAP_SUCCESS ) {
1690                                 return rc;
1691                         }
1692
1693                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1694                         mod->sml_op = mop;
1695                         mod->sml_type.bv_val = NULL;
1696                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1697                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1698                         ber_dupbv( &mod->sml_values[0], &tmpval );
1699                         mod->sml_values[1].bv_len = 0;
1700                         mod->sml_values[1].bv_val = NULL;
1701                         assert( mod->sml_values[0].bv_val );
1702                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1703                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1704                         mod->sml_nvalues[1].bv_len = 0;
1705                         mod->sml_nvalues[1].bv_val = NULL;
1706                         assert( mod->sml_nvalues[0].bv_val );
1707                         *modtail = mod;
1708                         modtail = &mod->sml_next;
1709                 }
1710
1711                 if( si->lastmod == LASTMOD_GEN ) {
1712                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1713                         mod->sml_op = mop;
1714                         mod->sml_type.bv_val = NULL;
1715                         mod->sml_desc = slap_schema.si_ad_creatorsName;
1716                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1717                         ber_dupbv( &mod->sml_values[0], &name );
1718                         mod->sml_values[1].bv_len = 0;
1719                         mod->sml_values[1].bv_val = NULL;
1720                         assert( mod->sml_values[0].bv_val );
1721                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1722                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1723                         mod->sml_nvalues[1].bv_len = 0;
1724                         mod->sml_nvalues[1].bv_val = NULL;
1725                         assert( mod->sml_nvalues[0].bv_val );
1726                         *modtail = mod;
1727                         modtail = &mod->sml_next;
1728
1729                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1730                         mod->sml_op = mop;
1731                         mod->sml_type.bv_val = NULL;
1732                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
1733                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1734                         ber_dupbv( &mod->sml_values[0], &timestamp );
1735                         mod->sml_values[1].bv_len = 0;
1736                         mod->sml_values[1].bv_val = NULL;
1737                         assert( mod->sml_values[0].bv_val );
1738                         mod->sml_nvalues = NULL;
1739                         *modtail = mod;
1740                         modtail = &mod->sml_next;
1741                 }
1742         }
1743
1744         if( si->lastmod == LASTMOD_GEN ) {
1745                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1746                 mod->sml_op = mop;
1747                 mod->sml_type.bv_val = NULL;
1748                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1749                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1750                 ber_dupbv( &mod->sml_values[0], &csn );
1751                 mod->sml_values[1].bv_len = 0;
1752                 mod->sml_values[1].bv_val = NULL;
1753                 assert( mod->sml_values[0].bv_val );
1754                 mod->sml_nvalues = NULL;
1755                 *modtail = mod;
1756                 modtail = &mod->sml_next;
1757
1758                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1759                 mod->sml_op = mop;
1760                 mod->sml_type.bv_val = NULL;
1761                 mod->sml_desc = slap_schema.si_ad_modifiersName;
1762                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1763                 ber_dupbv( &mod->sml_values[0], &name );
1764                 mod->sml_values[1].bv_len = 0;
1765                 mod->sml_values[1].bv_val = NULL;
1766                 assert( mod->sml_values[0].bv_val );
1767                 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1768                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1769                 mod->sml_nvalues[1].bv_len = 0;
1770                 mod->sml_nvalues[1].bv_val = NULL;
1771                 assert( mod->sml_nvalues[0].bv_val );
1772                 *modtail = mod;
1773                 modtail = &mod->sml_next;
1774
1775                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1776                 mod->sml_op = mop;
1777                 mod->sml_type.bv_val = NULL;
1778                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1779                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1780                 ber_dupbv( &mod->sml_values[0], &timestamp );
1781                 mod->sml_values[1].bv_len = 0;
1782                 mod->sml_values[1].bv_val = NULL;
1783                 assert( mod->sml_values[0].bv_val );
1784                 mod->sml_nvalues = NULL;
1785                 *modtail = mod;
1786                 modtail = &mod->sml_next;
1787         }
1788
1789         *modtail = NULL;
1790         return LDAP_SUCCESS;
1791 }
1792
1793
1794 static
1795 int slap_mods2entry_syncrepl(
1796         Modifications *mods,
1797         Entry **e,
1798         int repl_user,
1799         const char **text,
1800         char *textbuf, size_t textlen )
1801 {
1802         Attribute **tail = &(*e)->e_attrs;
1803         assert( *tail == NULL );
1804
1805         *text = textbuf;
1806
1807         for( ; mods != NULL; mods = mods->sml_next ) {
1808                 Attribute *attr;
1809
1810                 assert( mods->sml_desc != NULL );
1811
1812                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1813
1814                 if( attr != NULL ) {
1815 #define SLURPD_FRIENDLY
1816 #ifdef SLURPD_FRIENDLY
1817                         ber_len_t i,j;
1818
1819                         if( !repl_user ) {
1820                                 snprintf( textbuf, textlen,
1821                                         "attribute '%s' provided more than once",
1822                                         mods->sml_desc->ad_cname.bv_val );
1823                                 return LDAP_TYPE_OR_VALUE_EXISTS;
1824                         }
1825
1826                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
1827                                 /* count them */
1828                         }
1829                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
1830                                 /* count them */
1831                         }
1832                         j++;    /* NULL */
1833                         
1834                         attr->a_vals = ch_realloc( attr->a_vals,
1835                                 sizeof( struct berval ) * (i+j) );
1836
1837                         /* should check for duplicates */
1838
1839                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1840                                 sizeof( struct berval ) * j );
1841
1842                         if( attr->a_nvals ) {
1843                                 attr->a_nvals = ch_realloc( attr->a_nvals,
1844                                         sizeof( struct berval ) * (i+j) );
1845
1846                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1847                                         sizeof( struct berval ) * j );
1848
1849                                 /* trim the mods array */
1850                                 ch_free( mods->sml_nvalues );
1851                                 mods->sml_nvalues = NULL;
1852                         }
1853
1854                         continue;
1855 #else
1856                         snprintf( textbuf, textlen,
1857                                 "attribute '%s' provided more than once",
1858                                 mods->sml_desc->ad_cname.bv_val );
1859                         return LDAP_TYPE_OR_VALUE_EXISTS;
1860 #endif
1861                 }
1862
1863                 if( mods->sml_values[1].bv_val != NULL ) {
1864                         /* check for duplicates */
1865                         int             i, j;
1866                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1867
1868                         /* check if the values we're adding already exist */
1869                         if( mr == NULL || !mr->smr_match ) {
1870                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1871                                         /* test asserted values against themselves */
1872                                         for( j = 0; j < i; j++ ) {
1873                                                 if ( bvmatch( &mods->sml_bvalues[i],
1874                                                         &mods->sml_bvalues[j] ) ) {
1875                                                         /* value exists already */
1876                                                         snprintf( textbuf, textlen,
1877                                                                 "%s: value #%d provided more than once",
1878                                                                 mods->sml_desc->ad_cname.bv_val, j );
1879                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
1880                                                 }
1881                                         }
1882                                 }
1883
1884                         } else {
1885                                 int             rc;
1886                                 const char      *text = NULL;
1887                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
1888                                 
1889                                 rc = modify_check_duplicates( mods->sml_desc, mr,
1890                                                 NULL, mods->sml_bvalues, 0,
1891                                                 &text, textbuf, sizeof( textbuf ) );
1892
1893                                 if ( rc != LDAP_SUCCESS ) {
1894                                         return rc;
1895                                 }
1896                         }
1897                 }
1898
1899                 attr = ch_calloc( 1, sizeof(Attribute) );
1900
1901                 /* move ad to attr structure */
1902                 attr->a_desc = mods->sml_desc;
1903
1904                 /* move values to attr structure */
1905                 /*      should check for duplicates */
1906                 attr->a_vals = mods->sml_values;
1907
1908                 attr->a_nvals = mods->sml_nvalues;
1909
1910                 *tail = attr;
1911                 tail = &attr->a_next;
1912         }
1913
1914         return LDAP_SUCCESS;
1915 }
1916
1917 void
1918 avl_ber_bvfree( void *bv )
1919 {
1920         if( bv == NULL ) {
1921                 return;
1922         }
1923         if ( ((struct berval *)bv)->bv_val != NULL ) {
1924                 ber_memfree ( ((struct berval *)bv)->bv_val );
1925         }
1926         ber_memfree ( (char *) bv );
1927 }
1928
1929 static int
1930 cookie_callback(
1931         Operation* op,
1932         SlapReply* rs
1933 )
1934 {
1935         syncinfo_t *si = op->o_callback->sc_private;
1936         Attribute *a;
1937
1938         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1939
1940         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1941
1942         if ( a == NULL ) {
1943                 si->syncCookie = NULL;
1944         } else {
1945                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1946         }
1947         return LDAP_SUCCESS;
1948 }
1949
1950 static int
1951 dn_callback(
1952         Operation*      op,
1953         SlapReply*      rs
1954 )
1955 {
1956         syncinfo_t *si = op->o_callback->sc_private;
1957         
1958         if ( rs->sr_type == REP_SEARCH ) {
1959                 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1960         }
1961
1962         return LDAP_SUCCESS;
1963 }
1964
1965 static int
1966 nonpresent_callback(
1967         Operation*      op,
1968         SlapReply*      rs
1969 )
1970 {
1971         syncinfo_t *si = op->o_callback->sc_private;
1972         Attribute *a;
1973         int count = 0;
1974         struct berval* present_uuid = NULL;
1975         slap_callback cb;
1976         SlapReply       rs_cb = {REP_RESULT};
1977         struct nonpresent_entry *np_entry;
1978
1979         if ( rs->sr_type == REP_RESULT ) {
1980                 count = avl_free( si->presentlist, avl_ber_bvfree );
1981                 return LDAP_SUCCESS;
1982         } else if ( rs->sr_type == REP_SEARCH ) {
1983                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1984
1985                 if ( a == NULL )
1986                         return 0;
1987
1988                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1989
1990                 if ( present_uuid == NULL ) {
1991                         np_entry = (struct nonpresent_entry *)
1992                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1993                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1994                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1995                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1996                 } else {
1997                         avl_delete( &si->presentlist,
1998                                         &a->a_vals[0], syncuuid_cmp );
1999                 }
2000                 return LDAP_SUCCESS;
2001         } else {
2002                 return LDAP_SUCCESS;
2003         }
2004
2005 }
2006
2007 static int
2008 null_callback(
2009         Operation*      op,
2010         SlapReply*      rs
2011 )
2012 {
2013         if ( rs->sr_err != LDAP_SUCCESS &&
2014                  rs->sr_err != LDAP_REFERRAL &&
2015                  rs->sr_err != LDAP_ALREADY_EXISTS &&
2016                  rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2017                  rs->sr_err != DB_NOTFOUND ) {
2018 #ifdef NEW_LOGGING
2019                 LDAP_LOG( OPERATION, ERR,
2020                         "null_callback : error code 0x%x\n",
2021                         rs->sr_err, 0, 0 );
2022 #else
2023                 Debug( LDAP_DEBUG_ANY,
2024                         "null_callback : error code 0x%x\n",
2025                         rs->sr_err, 0, 0 );
2026 #endif
2027         }
2028         return LDAP_SUCCESS;
2029 }
2030
2031
2032 char **
2033 str2clist( char **out, char *in, const char *brkstr )
2034 {
2035         char    *str;
2036         char    *s;
2037         char    *lasts;
2038         int     i, j;
2039         const char *text;
2040         char    **new;
2041
2042         /* find last element in list */
2043         for (i = 0; out && out[i]; i++);
2044         
2045         /* protect the input string from strtok */
2046         str = ch_strdup( in );
2047
2048         /* Count words in string */
2049         j=1;
2050         for ( s = str; *s; s++ ) {
2051                 if ( strchr( brkstr, *s ) != NULL ) {
2052                         j++;
2053                 }
2054         }
2055
2056         out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2057         new = out + i;
2058         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2059                 s != NULL;
2060                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2061         {
2062                 *new = ch_strdup( s );
2063                 new++;
2064         }
2065
2066         *new = NULL;
2067         free( str );
2068         return( out );
2069 }
2070
2071 #endif