]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
4c5222d465491362d3d72ddea2052d8232dbefec
[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_tmpmfuncs = &ch_mfuncs;
329         op.o_tag = LDAP_REQ_SEARCH;
330         op.o_dn = si->updatedn;
331         op.o_ndn = si->updatedn;
332         op.o_callback = &cb;
333         op.o_time = slap_get_time();
334         op.o_managedsait = 1;
335         op.o_threadctx = si->ctx;
336         op.o_bd = be;
337         op.o_conn = &conn;
338         op.o_connid = op.o_conn->c_connid;
339         op.ors_scope = LDAP_SCOPE_BASE;
340         op.ors_deref = LDAP_DEREF_NEVER;
341         op.ors_slimit = -1;
342         op.ors_tlimit = -1;
343         op.ors_attrsonly = 0;
344         op.ors_attrs = NULL;
345         op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
346         ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
347                                 &op.ors_filterstr );
348
349         si->conn = &conn;
350         conn.c_send_ldap_result = slap_send_ldap_result;
351         conn.c_send_search_entry = slap_send_search_entry;
352         conn.c_send_search_reference = slap_send_search_reference;
353
354         /* get syncrepl cookie of shadow replica from subentry */
355         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
356         dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
357
358         sprintf( substr, "cn=syncrepl%d", si->id );
359         ber_str2bv( substr, strlen(substr), 1, &sub_bv );
360         dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
361
362         build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
363         build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
364
365         ch_free( base_bv.bv_val );
366         ch_free( pbase.bv_val );
367         ch_free( nbase.bv_val );
368         ch_free( sub_bv.bv_val );
369         ch_free( psubrdn.bv_val );
370         ch_free( nsubrdn.bv_val );
371
372         /* set callback function */
373         cb.sc_response = cookie_callback;
374         cb.sc_private = si;
375
376         /* search subentry to retrieve cookie */
377         si->syncCookie = NULL;
378         be->be_search( &op, &rs );
379
380         ch_free( op.o_req_dn.bv_val );
381         ch_free( op.o_req_ndn.bv_val );
382         filter_free( op.ors_filter );
383         ch_free( op.ors_filterstr.bv_val );
384
385         psub = be->be_nsuffix[0];
386
387         /* setup LDAP SYNC control */
388         sync_ber = ber_alloc_t( LBER_USE_DER );
389         ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
390
391         if ( si->syncCookie ) {
392                 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
393         } else {
394                 ber_printf( sync_ber, "{e}", abs(si->type) );
395         }
396
397         if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
398                 ber_free( sync_ber, 1 );
399                 return NULL;
400         }
401         ber_free( sync_ber, 1 );
402
403         sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
404
405         c[0].ldctl_oid = LDAP_CONTROL_SYNC;
406         c[0].ldctl_value = (*sync_bvalp);
407         c[0].ldctl_iscritical = si->type < 0;
408         sctrls[0] = &c[0];
409
410         if ( si->authzId ) {
411                 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
412                 c[1].ldctl_value.bv_val = si->authzId;
413                 c[1].ldctl_value.bv_len = strlen( si->authzId );
414                 c[1].ldctl_iscritical = 1;
415                 sctrls[1] = &c[1];
416         } else {
417                 sctrls[1] = NULL;
418         }
419
420         sctrls[2] = NULL;
421
422         err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
423
424         ber_bvfree( sync_bvalp );
425         ch_free( sctrls );
426
427         if ( err != LDAP_OPT_SUCCESS )
428                 fprintf( stderr, "Could not set controls : %d\n", err );
429
430         /* Delete Attributes */
431         if ( si->lastmod == LASTMOD_REQ ) {
432                 descs = del_descs_lastmod;
433         } else {
434                 descs = del_descs;
435         }
436
437         for ( i = 0; descs[i] != NULL; i++ ) {
438                 for ( j = 0; si->attrs[j] != NULL; j++ ) {
439                         if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
440                                 ch_free( si->attrs[j] );
441                                 for ( k = j; si->attrs[k] != NULL; k++ ) {
442                                         si->attrs[k] = si->attrs[k+1];
443                                 }
444                         }
445                 }
446         }
447
448         /* Add Attributes */
449
450         for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
451         
452         if ( si->lastmod == LASTMOD_REQ ) {
453                 descs = add_descs_lastmod;
454         } else {
455                 descs = add_descs;
456         }
457
458         for ( i = 0; descs[i] != NULL; i++ ) {
459                 tmp = ( char ** ) ch_realloc( si->attrs,
460                                 ( n + 2 ) * sizeof( char * ));
461                 if ( tmp == NULL ) {
462 #ifdef NEW_LOGGING
463                         LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
464 #else
465                         Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
466 #endif
467                 }
468                 si->attrs = tmp;
469                 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
470                 si->attrs[ n ] = NULL;
471         }
472
473         /* Send LDAP SYNC search */
474
475         rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
476                                 si->attrs, si->attrsonly, NULL, NULL,
477                                 NULL, -1, &msgid );
478
479         if( rc != LDAP_SUCCESS ) {
480                 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
481                                         ldap_err2string( rc ), rc );
482                 return NULL;
483         }
484
485         while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
486
487                 for ( msg = ldap_first_message( ld, res );
488                       msg != NULL;
489                       msg = ldap_next_message( ld, msg ) )
490                 {
491                         switch( ldap_msgtype( msg ) ) {
492                         case LDAP_RES_SEARCH_ENTRY:
493                                 entry = syncrepl_message_to_entry( ld, &op, msg,
494                                         &modlist, &syncstate, &syncUUID, &syncCookie );
495                                 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
496                                                 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
497                                 if ( syncCookie.bv_len ) {
498                                         syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
499                                 }
500                                 if ( rc_efree )
501                                         entry_free( entry );
502                                 for ( ml = modlist; ml != NULL; ml = mlnext ) {
503                                         mlnext = ml->sml_next;
504                                         ber_memfree( ml );
505                                 }
506                                 break;
507
508                         case LDAP_RES_SEARCH_REFERENCE:
509 #ifdef NEW_LOGGING
510                                 LDAP_LOG( OPERATION, ERR,
511                                         "do_syncrepl : reference received\n", 0, 0, 0 );
512 #else
513                                 Debug( LDAP_DEBUG_ANY,
514                                         "do_syncrepl : reference received\n", 0, 0, 0 );
515 #endif
516                                 break;
517
518                         case LDAP_RES_SEARCH_RESULT:
519                                 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
520                                 if ( rctrls ) {
521                                         rctrlp = *rctrls;
522                                         ctrl_ber = ber_alloc_t( LBER_USE_DER );
523                                         ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
524                                         ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
525                                         ber_reset( ctrl_ber, 1 );
526
527                                         ber_scanf( ctrl_ber, "{" );
528                                         if ( ber_peek_tag( ctrl_ber, &len )
529                                                 == LDAP_SYNC_TAG_COOKIE ) {
530                                                 ber_scanf( ctrl_ber, "o", &syncCookie );
531                                         }
532                                 }
533                                 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
534                                         if ( cancel_response ) {
535                                                 if ( syncCookie.bv_len ) {
536                                                         ber_bvfree( si->syncCookie );
537                                                         si->syncCookie = ber_dupbv( NULL, &syncCookie );
538                                                 }
539                                                 if ( ctrl_ber )
540                                                         ber_free( ctrl_ber, 1 );
541                                                 goto done;
542                                         }
543                                         else {
544                                                 if ( ctrl_ber )
545                                                         ber_free( ctrl_ber, 1 );
546                                                 break;
547                                         }
548                                 } else {
549                                         if ( syncCookie.bv_len ) {
550                                                 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
551                                         }
552                                         syncrepl_del_nonpresent( ld, &op );
553                                         if ( ctrl_ber )
554                                                 ber_free( ctrl_ber, 1 );
555                                         goto done;
556                                 }
557                                 break;
558
559                         case LDAP_RES_INTERMEDIATE:
560                                 ldap_parse_intermediate( ld, msg, &retoid, &retdata, NULL, 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         } else if ( *syncstate == LDAP_SYNC_DELETE ) {
811                 goto done;
812         }
813
814         if ( *modlist == NULL ) {
815 #ifdef NEW_LOGGING
816                 LDAP_LOG( OPERATION, ERR,
817                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
818 #else
819                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
820                                 0, 0, 0 );
821 #endif
822         }
823
824         rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
825
826         if ( rc != LDAP_SUCCESS ) {
827 #ifdef NEW_LOGGING
828                 LDAP_LOG( OPERATION, ERR,
829                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
830 #else
831                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
832                                 text, 0, 0 );
833 #endif
834                 return NULL;
835         }
836         
837         rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
838                                          &text,txtbuf, textlen );
839         
840         if( rc != LDAP_SUCCESS ) {
841 #ifdef NEW_LOGGING
842                 LDAP_LOG( OPERATION, ERR,
843                                 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
844 #else
845                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
846                                 text, 0, 0 );
847 #endif
848                 return NULL;
849         }
850
851         rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
852         if( rc != LDAP_SUCCESS ) {
853 #ifdef NEW_LOGGING
854                 LDAP_LOG( OPERATION, ERR,
855                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
856 #else
857                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
858                                 text, 0, 0 );
859 #endif
860         }
861
862 done:
863
864         ber_free ( ber, 0 );
865
866         return e;
867 }
868
869 int
870 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
871 {
872         const struct berval *uuid1 = v_uuid1;
873         const struct berval *uuid2 = v_uuid2;
874         int rc = uuid1->bv_len - uuid2->bv_len;
875         if ( rc ) return rc;
876         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
877 }
878
879 static int
880 syncrepl_entry(
881         LDAP *ld,
882         Operation *op,
883         Entry* e,
884         Modifications* modlist,
885         int syncstate,
886         struct berval* syncUUID,
887         struct berval* syncCookie,
888         int refresh
889 )
890 {
891         Backend *be = op->o_bd;
892         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
893         slap_callback   cb;
894         struct berval   csn_bv = {0, NULL};
895         struct berval   *syncuuid_bv = NULL;
896         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
897
898         SlapReply       rs = {REP_RESULT};
899         int rc = LDAP_SUCCESS;
900
901         struct berval base_bv = {0, NULL};
902
903         char *filterstr;
904         Filter *filter;
905
906         Attribute *a;
907
908         if ( refresh &&
909                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
910                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
911                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
912                                                 syncuuid_cmp, avl_dup_error );
913         }
914
915         if ( syncstate == LDAP_SYNC_PRESENT ) {
916                 if ( e )
917                         return 1;
918                 else
919                         return 0;
920         }
921
922         filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
923                                                                         op->o_tmpmemctx ); 
924         strcpy( filterstr, "entryUUID=" );
925         strcat( filterstr, syncUUID->bv_val );
926
927         si->e = e;
928         si->syncUUID = syncUUID;
929         si->syncUUID_ndn = NULL;
930
931         filter = str2filter( filterstr );
932         ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
933         ch_free( filterstr );
934         op->ors_filter = filter;
935         op->ors_scope = LDAP_SCOPE_SUBTREE;
936
937         /* get syncrepl cookie of shadow replica from subentry */
938         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
939         dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
940         ch_free( base_bv.bv_val );
941
942         /* set callback function */
943         op->o_callback = &cb;
944         cb.sc_response = dn_callback;
945         cb.sc_private = si;
946
947         be->be_search( op, &rs );
948
949         ch_free( op->o_req_dn.bv_val );
950         ch_free( op->o_req_ndn.bv_val );
951         filter_free( op->ors_filter );
952         ch_free( op->ors_filterstr.bv_val );
953
954         cb.sc_response = null_callback;
955
956         rc = LDAP_SUCCESS;
957
958         if ( si->syncUUID_ndn ) {
959                 op->o_req_dn = *si->syncUUID_ndn;
960                 op->o_req_ndn = *si->syncUUID_ndn;
961                 op->o_tag = LDAP_REQ_DELETE;
962                 rc = be->be_delete( op, &rs );
963         }
964
965         switch ( syncstate ) {
966         case LDAP_SYNC_ADD :
967         case LDAP_SYNC_MODIFY :
968
969                 if ( rc == LDAP_SUCCESS ||
970                          rc == LDAP_REFERRAL ||
971                          rc == LDAP_NO_SUCH_OBJECT ) {
972
973                         if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
974                                 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
975                         }
976
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
983                         if ( rc != LDAP_SUCCESS ) {
984                                 if ( rc == LDAP_ALREADY_EXISTS ) {      
985                                         op->o_tag = LDAP_REQ_MODIFY;
986                                         op->orm_modlist = modlist;
987                                         op->o_req_dn = e->e_name;
988                                         op->o_req_ndn = e->e_nname;
989                                         rc = be->be_modify( op, &rs );
990                                 } else if ( rc == LDAP_REFERRAL ||
991                                                         rc == LDAP_NO_SUCH_OBJECT ) {
992                                         syncrepl_add_glue(ld, op, e,
993                                                 modlist, syncstate,
994                                                 syncUUID, syncCookie);
995                                 } else {
996 #ifdef NEW_LOGGING
997                                         LDAP_LOG( OPERATION, ERR,
998                                                 "be_modify failed (%d)\n",
999                                                 rc, 0, 0 );
1000 #else
1001                                         Debug( LDAP_DEBUG_ANY,
1002                                                 "be_modify failed (%d)\n",
1003                                                 rc, 0, 0 );
1004 #endif
1005                                 }
1006                         } else {
1007                                 return 0;
1008                         }
1009                 } else {
1010 #ifdef NEW_LOGGING
1011                         LDAP_LOG( OPERATION, ERR,
1012                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1013 #else
1014                         Debug( LDAP_DEBUG_ANY,
1015                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1016 #endif
1017                 }
1018
1019                 si->e = NULL;
1020                 return 1;
1021
1022         case LDAP_SYNC_DELETE :
1023                 /* Already deleted */
1024                 return 1;
1025
1026         default :
1027 #ifdef NEW_LOGGING
1028                 LDAP_LOG( OPERATION, ERR,
1029                         "unknown syncstate\n", 0, 0, 0 );
1030 #else
1031                 Debug( LDAP_DEBUG_ANY,
1032                         "unknown syncstate\n", 0, 0, 0 );
1033 #endif
1034                 return 1;
1035         }
1036 }
1037
1038 static int
1039 syncrepl_del_nonpresent(
1040         LDAP *ld,
1041         Operation *op
1042 )
1043 {
1044         Backend* be = op->o_bd;
1045         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1046         slap_callback   cb;
1047         struct berval   base_bv = {0, NULL};
1048         Filter *filter;
1049         SlapReply       rs = {REP_RESULT};
1050         struct berval   filterstr_bv = {0, NULL};
1051         struct nonpresent_entry *np_list, *np_prev;
1052
1053         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1054         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1055         ch_free( base_bv.bv_val );
1056
1057         filter = str2filter( si->filterstr );
1058
1059         cb.sc_response = nonpresent_callback;
1060         cb.sc_private = si;
1061
1062         op->o_callback = &cb;
1063         op->o_tag = LDAP_REQ_SEARCH;
1064         op->ors_scope = si->scope;
1065         op->ors_deref = LDAP_DEREF_NEVER;
1066         op->ors_slimit = -1;
1067         op->ors_tlimit = -1;
1068         op->ors_attrsonly = 0;
1069         op->ors_attrs = NULL;
1070         op->ors_filter = filter;
1071         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1072
1073         be->be_search( op, &rs );
1074
1075         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1076                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1077                 while ( np_list != NULL ) {
1078                         LDAP_LIST_REMOVE( np_list, np_link );
1079                         np_prev = np_list;
1080                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1081                         op->o_tag = LDAP_REQ_DELETE;
1082                         op->o_callback = &cb;
1083                         cb.sc_response = null_callback;
1084                         cb.sc_private = si;
1085                         op->o_req_dn = *np_prev->dn;
1086                         op->o_req_ndn = *np_prev->ndn;
1087                         op->o_bd->be_delete( op, &rs );
1088                         ber_bvfree( np_prev->dn );
1089                         ber_bvfree( np_prev->ndn );
1090                         op->o_req_dn.bv_val = NULL;
1091                         op->o_req_ndn.bv_val = NULL;
1092                         ch_free( np_prev );
1093                 }
1094         }
1095
1096         if ( op->o_req_dn.bv_val )
1097                 ch_free( op->o_req_dn.bv_val );
1098         if ( op->o_req_ndn.bv_val )
1099                 ch_free( op->o_req_ndn.bv_val );
1100         filter_free( op->ors_filter );
1101         ch_free( op->ors_filterstr.bv_val );
1102 }
1103
1104
1105 static void
1106 syncrepl_add_glue(
1107         LDAP *ld,
1108         Operation* op,
1109         Entry *e,
1110         Modifications* modlist,
1111         int syncstate,
1112         struct berval* syncUUID,
1113         struct berval* syncCookie
1114 )
1115 {
1116         Backend *be = op->o_bd;
1117         syncinfo_t *si = op->o_callback->sc_private;
1118         struct berval   uuid_bv = {0, NULL};
1119         slap_callback cb;
1120         Attribute       *a;
1121         int     rc;
1122         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1123         int levels = 0;
1124         int i, j, k;
1125         struct berval dn = {0, NULL};
1126         struct berval pdn = {0, NULL};
1127         struct berval ndn = {0, NULL};
1128         struct berval rdn = {0, NULL};
1129         Entry   *glue;
1130         SlapReply       rs = {REP_RESULT};
1131         Connection *conn = op->o_conn;
1132
1133         op->o_tag = LDAP_REQ_ADD;
1134         op->o_callback = &cb;
1135         cb.sc_response = null_callback;
1136         cb.sc_private = si;
1137
1138         ber_dupbv( &dn, &e->e_nname );
1139         ber_dupbv( &pdn, &e->e_nname );
1140
1141         while ( !be_issuffix ( be, &pdn )) {
1142                 dnParent( &dn, &pdn );
1143                 ch_free( dn.bv_val );
1144                 ber_dupbv( &dn, &pdn );
1145                 levels++;
1146         }
1147
1148         for ( i = 0; i <= levels; i++ ) {
1149                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1150                 ch_free( dn.bv_val );
1151                 ch_free( pdn.bv_val );
1152                 ber_dupbv( &dn, &e->e_nname );
1153                 ber_dupbv( &pdn, &e->e_nname );
1154                 j = levels - i;
1155                 for ( k = 0; k < j; k++ ) {
1156                         dnParent( &dn, &pdn );
1157                         ch_free( dn.bv_val );
1158                         ber_dupbv( &dn, &pdn );
1159                 }
1160
1161                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1162                 ber_dupbv( &glue->e_name, &pdn );
1163                 ber_dupbv( &glue->e_nname, &ndn );
1164                 ch_free( dn.bv_val );
1165                 ch_free( pdn.bv_val );
1166                 ch_free( ndn.bv_val );
1167
1168                 a = ch_calloc( 1, sizeof( Attribute ));
1169                 a->a_desc = slap_schema.si_ad_objectClass;
1170                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1171                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1172                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1173                 a->a_vals[2].bv_len = 0;
1174                 a->a_vals[2].bv_val = NULL;
1175                 a->a_next = glue->e_attrs;
1176                 glue->e_attrs = a;
1177
1178                 a = ch_calloc( 1, sizeof( Attribute ));
1179                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1180                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1181                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1182                 a->a_vals[1].bv_len = 0;
1183                 a->a_vals[1].bv_val = NULL;
1184                 a->a_next = glue->e_attrs;
1185                 glue->e_attrs = a;
1186
1187                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1188                         op->o_req_dn = e->e_name;
1189                         op->o_req_ndn = e->e_nname;
1190                         op->ora_e = e;
1191                         rc = be->be_add ( op, &rs );
1192                         if ( rc == LDAP_SUCCESS )
1193                                 be_entry_release_w( op, e );
1194                         else 
1195                                 entry_free( e );
1196                         entry_free( glue );
1197                 } else {
1198                         op->o_req_dn = glue->e_name;
1199                         op->o_req_ndn = glue->e_nname;
1200                         op->ora_e = glue;
1201                         rc = be->be_add ( op, &rs );
1202                         if ( rc == LDAP_SUCCESS ) {
1203                                 be_entry_release_w( op, glue );
1204                         } else {
1205                         /* incl. ALREADY EXIST */
1206                                 entry_free( glue );
1207                         }
1208                 }
1209         }
1210
1211         return;
1212 }
1213
1214 static void
1215 syncrepl_updateCookie(
1216         LDAP *ld,
1217         Operation *op,
1218         struct berval *pdn,
1219         struct berval *syncCookie
1220 )
1221 {
1222         Backend *be = op->o_bd;
1223         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1224         Modifications *ml;
1225         Modifications *mlnext;
1226         Modifications *mod;
1227         Modifications *modlist;
1228         Modifications **modtail = &modlist;
1229
1230         struct berval* ocbva = NULL;
1231         struct berval* cnbva = NULL;
1232         struct berval* ssbva = NULL;
1233         struct berval* scbva = NULL;
1234
1235         char substr[64];
1236         char rdnstr[67];
1237         const char      *text;
1238         char txtbuf[SLAP_TEXT_BUFLEN];
1239         size_t textlen = sizeof txtbuf;
1240
1241         Entry* e;
1242         int rc;
1243
1244         struct berval sub_bv = { 0, NULL };
1245         struct berval psubrdn = { 0, NULL };
1246         
1247         slap_callback cb;
1248         SlapReply       rs = {REP_RESULT};
1249
1250         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1251         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1252         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1253         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1254
1255         /* update in memory cookie */
1256         if ( si->syncCookie != NULL ) {
1257                 ber_bvfree( si->syncCookie );
1258         }
1259         si->syncCookie = ber_dupbv( NULL, syncCookie );
1260         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1261         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1262         ber_str2bv( "syncConsumerSubentry",
1263                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1264         ocbva[3].bv_len = 0;
1265         ocbva[3].bv_val = NULL;
1266
1267         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1268         mod->sml_op = LDAP_MOD_REPLACE;
1269         mod->sml_next = NULL;
1270         mod->sml_desc = NULL;
1271         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1272         mod->sml_bvalues = ocbva;
1273         mod->sml_nvalues = ocbva;
1274         *modtail = mod;
1275         modtail = &mod->sml_next;
1276
1277         sprintf( substr, "syncrepl%d", si->id );
1278         sprintf( rdnstr, "cn=%s", substr );
1279         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1280         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1281         cnbva[1].bv_len = 0;
1282         cnbva[1].bv_val = NULL;
1283         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1284         mod->sml_op = LDAP_MOD_REPLACE;
1285         mod->sml_next = NULL;
1286         mod->sml_desc = NULL;
1287         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1288         mod->sml_bvalues = cnbva;
1289         mod->sml_nvalues = cnbva;
1290         *modtail = mod;
1291         modtail = &mod->sml_next;
1292
1293         ber_dupbv( &scbva[0], si->syncCookie );
1294         scbva[1].bv_len = 0;
1295         scbva[1].bv_val = NULL;
1296         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1297         mod->sml_op = LDAP_MOD_REPLACE;
1298         mod->sml_next = NULL;
1299         mod->sml_desc = NULL;
1300         ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1301                                                 1, &mod->sml_type );
1302         mod->sml_bvalues = scbva;
1303         mod->sml_nvalues = scbva;
1304         *modtail = mod;
1305         modtail = &mod->sml_next;
1306
1307         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1308         ssbva[1].bv_len = 0;
1309         ssbva[1].bv_val = NULL;
1310         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1311         mod->sml_op = LDAP_MOD_REPLACE;
1312         mod->sml_next = NULL;
1313         mod->sml_desc = NULL;
1314         ber_str2bv( "subtreeSpecification",
1315                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1316         mod->sml_bvalues = ssbva;
1317         mod->sml_nvalues = ssbva;
1318         *modtail = mod;
1319         modtail = &mod->sml_next;
1320
1321         rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1322
1323         if ( rc != LDAP_SUCCESS ) {
1324 #ifdef NEW_LOGGING
1325                 LDAP_LOG( OPERATION, ERR,
1326                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1327 #else
1328                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1329                          text, 0, 0 );
1330 #endif
1331         }
1332
1333         op->o_tag = LDAP_REQ_ADD;
1334         rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1335
1336         if( rc != LDAP_SUCCESS ) {
1337 #ifdef NEW_LOGGING
1338                 LDAP_LOG( OPERATION, ERR,
1339                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1340 #else
1341                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1342                          text, 0, 0 );
1343 #endif
1344         }
1345
1346         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1347
1348         build_new_dn( &sub_bv, pdn, &psubrdn );
1349         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1350         ch_free( sub_bv.bv_val );
1351         ch_free( psubrdn.bv_val );
1352
1353         e->e_attrs = NULL;
1354
1355         rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1356
1357         if( rc != LDAP_SUCCESS ) {
1358 #ifdef NEW_LOGGING
1359                 LDAP_LOG( OPERATION, ERR,
1360                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1361 #else
1362                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1363                          text, 0, 0 );
1364 #endif
1365         }
1366
1367         cb.sc_response = null_callback;
1368         cb.sc_private = si;
1369
1370         op->o_callback = &cb;
1371         op->o_req_dn = e->e_name;
1372         op->o_req_ndn = e->e_nname;
1373
1374         /* update persistent cookie */
1375 update_cookie_retry:
1376         op->o_tag = LDAP_REQ_MODIFY;
1377         op->orm_modlist = modlist;
1378         rc = be->be_modify( op, &rs );
1379         if ( rc != LDAP_SUCCESS ) {
1380                 if ( rc == LDAP_REFERRAL ||
1381                          rc == LDAP_NO_SUCH_OBJECT ) {
1382                         op->o_tag = LDAP_REQ_ADD;
1383                         op->ora_e = e;
1384                         rc = be->be_add( op, &rs );
1385                         if ( rc != LDAP_SUCCESS ) {
1386                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1387                                         goto update_cookie_retry;
1388                                 } else if ( rc == LDAP_REFERRAL ||
1389                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1390 #ifdef NEW_LOGGING
1391                                         LDAP_LOG( OPERATION, ERR,
1392                                                 "cookie will be non-persistent\n",
1393                                                 0, 0, 0 );
1394 #else
1395                                         Debug( LDAP_DEBUG_ANY,
1396                                                 "cookie will be non-persistent\n",
1397                                                 0, 0, 0 );
1398 #endif
1399                                 } else {
1400 #ifdef NEW_LOGGING
1401                                         LDAP_LOG( OPERATION, ERR,
1402                                                 "be_add failed (%d)\n",
1403                                                 rc, 0, 0 );
1404 #else
1405                                         Debug( LDAP_DEBUG_ANY,
1406                                                 "be_add failed (%d)\n",
1407                                                 rc, 0, 0 );
1408 #endif
1409                                 }
1410                         } else {
1411                                 goto done;
1412                         }
1413                 } else {
1414 #ifdef NEW_LOGGING
1415                         LDAP_LOG( OPERATION, ERR,
1416                                 "be_modify failed (%d)\n", rc, 0, 0 );
1417 #else
1418                         Debug( LDAP_DEBUG_ANY,
1419                                 "be_modify failed (%d)\n", rc, 0, 0 );
1420 #endif
1421                 }
1422         }
1423
1424         if ( e != NULL )
1425                 entry_free( e );
1426
1427 done :
1428
1429         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1430                 mlnext = ml->sml_next;
1431                 free( ml );
1432         }
1433
1434         return;
1435 }
1436
1437
1438 static
1439 int slap_mods_check_syncrepl(
1440         Operation *op,
1441         Modifications **mlp,
1442         const char **text,
1443         char *textbuf,
1444         size_t textlen,
1445         void *ctx )
1446 {
1447         int rc;
1448         Backend *be = op->o_bd;
1449         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1450         AttributeDescription** descs;
1451         int i;
1452         Modifications *prevml = NULL;
1453         Modifications *nextml = NULL;
1454         Modifications *ml = *mlp;
1455
1456         while ( ml != NULL ) {
1457                 AttributeDescription *ad = NULL;
1458
1459                 /* convert to attribute description */
1460                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1461
1462                 if( rc != LDAP_SUCCESS ) {
1463                         snprintf( textbuf, textlen, "%s: %s",
1464                                                 ml->sml_type.bv_val, *text );
1465                         *text = textbuf;
1466                         return rc;
1467                 }
1468
1469                 ad = ml->sml_desc;
1470
1471                 if ( si->lastmod == LASTMOD_REQ ) {
1472                         descs = del_descs_lastmod;
1473                 } else {
1474                         descs = del_descs;
1475                 }
1476
1477                 for ( i = 0; descs[i] != NULL; i++ ) {
1478                         if ( ad == descs[i] ) {
1479                                 if ( prevml == NULL ) {
1480                                         mlp = &ml->sml_next;
1481                                         prevml = NULL;
1482                                 } else {
1483                                         prevml->sml_next = ml->sml_next;
1484                                 }
1485                                 slap_mod_free( &ml->sml_mod, 0 );
1486                                 nextml = ml->sml_next;
1487                                 free( ml );
1488                                 ml = nextml;
1489                                 continue;
1490                         }
1491                 }
1492
1493                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1494                                 && !slap_ad_is_binary( ad )) {
1495                         /* attribute requires binary transfer */
1496                         snprintf( textbuf, textlen,
1497                                         "%s: requires ;binary transfer",
1498                                         ml->sml_type.bv_val );
1499                         *text = textbuf;
1500                         return LDAP_UNDEFINED_TYPE;
1501                 }
1502
1503                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1504                                         && slap_ad_is_binary( ad )) {
1505                         /* attribute requires binary transfer */
1506                         snprintf( textbuf, textlen,
1507                                         "%s: disallows ;binary transfer",
1508                                         ml->sml_type.bv_val );
1509                         *text = textbuf;
1510                         return LDAP_UNDEFINED_TYPE;
1511                 }
1512
1513                 if( slap_ad_is_tag_range( ad )) {
1514                         /* attribute requires binary transfer */
1515                         snprintf( textbuf, textlen,
1516                                         "%s: inappropriate use of tag range option",
1517                                         ml->sml_type.bv_val );
1518                         *text = textbuf;
1519                         return LDAP_UNDEFINED_TYPE;
1520                 }
1521
1522                 if ( is_at_obsolete( ad->ad_type ) &&
1523                                 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1524                         /*
1525                          * attribute is obsolete,
1526                          * only allow replace/delete with no values
1527                          */
1528                         snprintf( textbuf, textlen,
1529                                         "%s: attribute is obsolete",
1530                                         ml->sml_type.bv_val );
1531                         *text = textbuf;
1532                         return LDAP_CONSTRAINT_VIOLATION;
1533                 }
1534
1535                 /*
1536                  * check values
1537                  */
1538                 if( ml->sml_values != NULL ) {
1539                         ber_len_t nvals;
1540                         slap_syntax_validate_func *validate =
1541                         ad->ad_type->sat_syntax->ssyn_validate;
1542                         slap_syntax_transform_func *pretty =
1543                         ad->ad_type->sat_syntax->ssyn_pretty;
1544
1545                         if( !pretty && !validate ) {
1546                                 *text = "no validator for syntax";
1547                                 snprintf( textbuf, textlen,
1548                                                 "%s: no validator for syntax %s",
1549                                                 ml->sml_type.bv_val,
1550                                                 ad->ad_type->sat_syntax->ssyn_oid );
1551                                 *text = textbuf;
1552                                 return LDAP_INVALID_SYNTAX;
1553                         }
1554
1555                         /*
1556                          * check that each value is valid per syntax
1557                          * and pretty if appropriate
1558                          */
1559                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1560                                 struct berval pval = {0, NULL};
1561                                 if( pretty ) {
1562                                         rc = pretty( ad->ad_type->sat_syntax,
1563                                                         &ml->sml_values[nvals], &pval, ctx );
1564                                 } else {
1565                                         rc = validate( ad->ad_type->sat_syntax,
1566                                                         &ml->sml_values[nvals] );
1567                                 }
1568
1569                                 if( rc != 0 ) {
1570                                         snprintf( textbuf, textlen,
1571                                                         "%s: value #%ld invalid per syntax",
1572                                                         ml->sml_type.bv_val, (long) nvals );
1573                                         *text = textbuf;
1574                                         return LDAP_INVALID_SYNTAX;
1575                                 }
1576
1577                                 if( pretty ) {
1578                                         ber_memfree( ml->sml_values[nvals].bv_val );
1579                                         ml->sml_values[nvals] = pval;
1580                                 }
1581                         }
1582
1583                         /*
1584                          * a rough single value check... an additional check is needed
1585                          * to catch add of single value to existing single valued attribute
1586                          */
1587                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1588                                         && nvals > 1 && is_at_single_value( ad->ad_type )) {
1589                                 snprintf( textbuf, textlen,
1590                                                 "%s: multiple values provided",
1591                                                 ml->sml_type.bv_val );
1592                                 *text = textbuf;
1593                                 return LDAP_CONSTRAINT_VIOLATION;
1594                         }
1595
1596                         if( nvals && ad->ad_type->sat_equality &&
1597                                         ad->ad_type->sat_equality->smr_normalize ) {
1598                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1599                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1600                                         rc = ad->ad_type->sat_equality->smr_normalize( 0,
1601                                                         ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1602                                                         &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1603                                         if( rc ) {
1604 #ifdef NEW_LOGGING
1605                                                 LDAP_LOG( OPERATION, DETAIL1,
1606                                                                 "str2entry:  NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1607 #else
1608                                                 Debug( LDAP_DEBUG_ANY,
1609                                                                 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1610 #endif
1611                                                 snprintf( textbuf, textlen,
1612                                                                 "%s: value #%ld normalization failed",
1613                                                                 ml->sml_type.bv_val, (long) nvals );
1614                                                 *text = textbuf;
1615                                                 return rc;
1616                                         }
1617                                 }
1618                                 ml->sml_nvalues[nvals].bv_val = NULL;
1619                                 ml->sml_nvalues[nvals].bv_len = 0;
1620                         }
1621                 }
1622                 prevml = ml;
1623                 ml = ml->sml_next;
1624         }
1625
1626         return LDAP_SUCCESS;
1627 }
1628
1629 static
1630 int slap_mods_opattrs_syncrepl(
1631         Operation *op,
1632         Modifications *mods,
1633         Modifications **modtail,
1634         const char **text,
1635         char *textbuf, size_t textlen )
1636 {
1637         struct berval name = {0, NULL};
1638         struct berval timestamp = {0, NULL};
1639         struct berval csn = {0, NULL};
1640         struct berval nname = {0, NULL};
1641         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1642         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1643         Modifications *mod;
1644         Backend *be = op->o_bd;
1645         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1646
1647         int mop = LDAP_MOD_REPLACE;
1648
1649         assert( modtail != NULL );
1650         assert( *modtail == NULL );
1651
1652         if( si->lastmod == LASTMOD_GEN ) {
1653                 struct tm *ltm;
1654                 time_t now = slap_get_time();
1655
1656                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1657                 ltm = gmtime( &now );
1658                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1659
1660                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1661                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1662                 csn.bv_val = csnbuf;
1663
1664                 timestamp.bv_val = timebuf;
1665                 timestamp.bv_len = strlen(timebuf);
1666
1667                 if( op->o_dn.bv_len == 0 ) {
1668                         name.bv_val = SLAPD_ANONYMOUS;
1669                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1670                         nname = name;
1671                 } else {
1672                         name = op->o_dn;
1673                         nname = op->o_ndn;
1674                 }
1675         }
1676
1677         if( op->o_tag == LDAP_REQ_ADD ) {
1678                 struct berval tmpval = {0, NULL};
1679
1680                 if( global_schemacheck ) {
1681                         int rc = mods_structural_class( mods, &tmpval,
1682                                                                         text, textbuf, textlen );
1683                         if( rc != LDAP_SUCCESS ) {
1684                                 return rc;
1685                         }
1686
1687                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1688                         mod->sml_op = mop;
1689                         mod->sml_type.bv_val = NULL;
1690                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1691                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1692                         ber_dupbv( &mod->sml_values[0], &tmpval );
1693                         mod->sml_values[1].bv_len = 0;
1694                         mod->sml_values[1].bv_val = NULL;
1695                         assert( mod->sml_values[0].bv_val );
1696                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1697                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1698                         mod->sml_nvalues[1].bv_len = 0;
1699                         mod->sml_nvalues[1].bv_val = NULL;
1700                         assert( mod->sml_nvalues[0].bv_val );
1701                         *modtail = mod;
1702                         modtail = &mod->sml_next;
1703                 }
1704
1705                 if( si->lastmod == LASTMOD_GEN ) {
1706                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1707                         mod->sml_op = mop;
1708                         mod->sml_type.bv_val = NULL;
1709                         mod->sml_desc = slap_schema.si_ad_creatorsName;
1710                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1711                         ber_dupbv( &mod->sml_values[0], &name );
1712                         mod->sml_values[1].bv_len = 0;
1713                         mod->sml_values[1].bv_val = NULL;
1714                         assert( mod->sml_values[0].bv_val );
1715                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1716                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1717                         mod->sml_nvalues[1].bv_len = 0;
1718                         mod->sml_nvalues[1].bv_val = NULL;
1719                         assert( mod->sml_nvalues[0].bv_val );
1720                         *modtail = mod;
1721                         modtail = &mod->sml_next;
1722
1723                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1724                         mod->sml_op = mop;
1725                         mod->sml_type.bv_val = NULL;
1726                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
1727                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1728                         ber_dupbv( &mod->sml_values[0], &timestamp );
1729                         mod->sml_values[1].bv_len = 0;
1730                         mod->sml_values[1].bv_val = NULL;
1731                         assert( mod->sml_values[0].bv_val );
1732                         mod->sml_nvalues = NULL;
1733                         *modtail = mod;
1734                         modtail = &mod->sml_next;
1735                 }
1736         }
1737
1738         if( si->lastmod == LASTMOD_GEN ) {
1739                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1740                 mod->sml_op = mop;
1741                 mod->sml_type.bv_val = NULL;
1742                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1743                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1744                 ber_dupbv( &mod->sml_values[0], &csn );
1745                 mod->sml_values[1].bv_len = 0;
1746                 mod->sml_values[1].bv_val = NULL;
1747                 assert( mod->sml_values[0].bv_val );
1748                 mod->sml_nvalues = NULL;
1749                 *modtail = mod;
1750                 modtail = &mod->sml_next;
1751
1752                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1753                 mod->sml_op = mop;
1754                 mod->sml_type.bv_val = NULL;
1755                 mod->sml_desc = slap_schema.si_ad_modifiersName;
1756                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1757                 ber_dupbv( &mod->sml_values[0], &name );
1758                 mod->sml_values[1].bv_len = 0;
1759                 mod->sml_values[1].bv_val = NULL;
1760                 assert( mod->sml_values[0].bv_val );
1761                 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1762                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1763                 mod->sml_nvalues[1].bv_len = 0;
1764                 mod->sml_nvalues[1].bv_val = NULL;
1765                 assert( mod->sml_nvalues[0].bv_val );
1766                 *modtail = mod;
1767                 modtail = &mod->sml_next;
1768
1769                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1770                 mod->sml_op = mop;
1771                 mod->sml_type.bv_val = NULL;
1772                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1773                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1774                 ber_dupbv( &mod->sml_values[0], &timestamp );
1775                 mod->sml_values[1].bv_len = 0;
1776                 mod->sml_values[1].bv_val = NULL;
1777                 assert( mod->sml_values[0].bv_val );
1778                 mod->sml_nvalues = NULL;
1779                 *modtail = mod;
1780                 modtail = &mod->sml_next;
1781         }
1782
1783         *modtail = NULL;
1784         return LDAP_SUCCESS;
1785 }
1786
1787
1788 static
1789 int slap_mods2entry_syncrepl(
1790         Modifications *mods,
1791         Entry **e,
1792         int repl_user,
1793         const char **text,
1794         char *textbuf, size_t textlen )
1795 {
1796         Attribute **tail = &(*e)->e_attrs;
1797         assert( *tail == NULL );
1798
1799         *text = textbuf;
1800
1801         for( ; mods != NULL; mods = mods->sml_next ) {
1802                 Attribute *attr;
1803
1804                 assert( mods->sml_desc != NULL );
1805
1806                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1807
1808                 if( attr != NULL ) {
1809 #define SLURPD_FRIENDLY
1810 #ifdef SLURPD_FRIENDLY
1811                         ber_len_t i,j;
1812
1813                         if( !repl_user ) {
1814                                 snprintf( textbuf, textlen,
1815                                         "attribute '%s' provided more than once",
1816                                         mods->sml_desc->ad_cname.bv_val );
1817                                 return LDAP_TYPE_OR_VALUE_EXISTS;
1818                         }
1819
1820                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
1821                                 /* count them */
1822                         }
1823                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
1824                                 /* count them */
1825                         }
1826                         j++;    /* NULL */
1827                         
1828                         attr->a_vals = ch_realloc( attr->a_vals,
1829                                 sizeof( struct berval ) * (i+j) );
1830
1831                         /* should check for duplicates */
1832
1833                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1834                                 sizeof( struct berval ) * j );
1835
1836                         if( attr->a_nvals ) {
1837                                 attr->a_nvals = ch_realloc( attr->a_nvals,
1838                                         sizeof( struct berval ) * (i+j) );
1839
1840                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1841                                         sizeof( struct berval ) * j );
1842
1843                                 /* trim the mods array */
1844                                 ch_free( mods->sml_nvalues );
1845                                 mods->sml_nvalues = NULL;
1846                         }
1847
1848                         continue;
1849 #else
1850                         snprintf( textbuf, textlen,
1851                                 "attribute '%s' provided more than once",
1852                                 mods->sml_desc->ad_cname.bv_val );
1853                         return LDAP_TYPE_OR_VALUE_EXISTS;
1854 #endif
1855                 }
1856
1857                 if( mods->sml_values[1].bv_val != NULL ) {
1858                         /* check for duplicates */
1859                         int             i, j;
1860                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1861
1862                         /* check if the values we're adding already exist */
1863                         if( mr == NULL || !mr->smr_match ) {
1864                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1865                                         /* test asserted values against themselves */
1866                                         for( j = 0; j < i; j++ ) {
1867                                                 if ( bvmatch( &mods->sml_bvalues[i],
1868                                                         &mods->sml_bvalues[j] ) ) {
1869                                                         /* value exists already */
1870                                                         snprintf( textbuf, textlen,
1871                                                                 "%s: value #%d provided more than once",
1872                                                                 mods->sml_desc->ad_cname.bv_val, j );
1873                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
1874                                                 }
1875                                         }
1876                                 }
1877
1878                         } else {
1879                                 int             rc;
1880                                 const char      *text = NULL;
1881                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
1882                                 
1883                                 rc = modify_check_duplicates( mods->sml_desc, mr,
1884                                                 NULL, mods->sml_bvalues, 0,
1885                                                 &text, textbuf, sizeof( textbuf ) );
1886
1887                                 if ( rc != LDAP_SUCCESS ) {
1888                                         return rc;
1889                                 }
1890                         }
1891                 }
1892
1893                 attr = ch_calloc( 1, sizeof(Attribute) );
1894
1895                 /* move ad to attr structure */
1896                 attr->a_desc = mods->sml_desc;
1897
1898                 /* move values to attr structure */
1899                 /*      should check for duplicates */
1900                 attr->a_vals = mods->sml_values;
1901
1902                 attr->a_nvals = mods->sml_nvalues;
1903
1904                 *tail = attr;
1905                 tail = &attr->a_next;
1906         }
1907
1908         return LDAP_SUCCESS;
1909 }
1910
1911 void
1912 avl_ber_bvfree( void *bv )
1913 {
1914         if( bv == NULL ) {
1915                 return;
1916         }
1917         if ( ((struct berval *)bv)->bv_val != NULL ) {
1918                 ber_memfree ( ((struct berval *)bv)->bv_val );
1919         }
1920         ber_memfree ( (char *) bv );
1921 }
1922
1923 static int
1924 cookie_callback(
1925         Operation* op,
1926         SlapReply* rs
1927 )
1928 {
1929         syncinfo_t *si = op->o_callback->sc_private;
1930         Attribute *a;
1931
1932         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1933
1934         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1935
1936         if ( a == NULL ) {
1937                 si->syncCookie = NULL;
1938         } else {
1939                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1940         }
1941         return LDAP_SUCCESS;
1942 }
1943
1944 static int
1945 dn_callback(
1946         Operation*      op,
1947         SlapReply*      rs
1948 )
1949 {
1950         syncinfo_t *si = op->o_callback->sc_private;
1951         
1952         if ( rs->sr_type == REP_SEARCH ) {
1953                 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1954         }
1955
1956         return LDAP_SUCCESS;
1957 }
1958
1959 static int
1960 nonpresent_callback(
1961         Operation*      op,
1962         SlapReply*      rs
1963 )
1964 {
1965         syncinfo_t *si = op->o_callback->sc_private;
1966         Attribute *a;
1967         int count = 0;
1968         struct berval* present_uuid = NULL;
1969         slap_callback cb;
1970         SlapReply       rs_cb = {REP_RESULT};
1971         struct nonpresent_entry *np_entry;
1972
1973         if ( rs->sr_type == REP_RESULT ) {
1974                 count = avl_free( si->presentlist, avl_ber_bvfree );
1975                 return LDAP_SUCCESS;
1976         } else if ( rs->sr_type == REP_SEARCH ) {
1977                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1978
1979                 if ( a == NULL )
1980                         return 0;
1981
1982                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1983
1984                 if ( present_uuid == NULL ) {
1985                         np_entry = (struct nonpresent_entry *)
1986                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1987                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1988                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1989                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1990                 } else {
1991                         avl_delete( &si->presentlist,
1992                                         &a->a_vals[0], syncuuid_cmp );
1993                 }
1994                 return LDAP_SUCCESS;
1995         } else {
1996                 return LDAP_SUCCESS;
1997         }
1998
1999 }
2000
2001 static int
2002 null_callback(
2003         Operation*      op,
2004         SlapReply*      rs
2005 )
2006 {
2007         if ( rs->sr_err != LDAP_SUCCESS &&
2008                  rs->sr_err != LDAP_REFERRAL &&
2009                  rs->sr_err != LDAP_ALREADY_EXISTS &&
2010                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2011 #ifdef NEW_LOGGING
2012                 LDAP_LOG( OPERATION, ERR,
2013                         "null_callback : error code 0x%x\n",
2014                         rs->sr_err, 0, 0 );
2015 #else
2016                 Debug( LDAP_DEBUG_ANY,
2017                         "null_callback : error code 0x%x\n",
2018                         rs->sr_err, 0, 0 );
2019 #endif
2020         }
2021         return LDAP_SUCCESS;
2022 }
2023
2024
2025 char **
2026 str2clist( char **out, char *in, const char *brkstr )
2027 {
2028         char    *str;
2029         char    *s;
2030         char    *lasts;
2031         int     i, j;
2032         const char *text;
2033         char    **new;
2034
2035         /* find last element in list */
2036         for (i = 0; out && out[i]; i++);
2037         
2038         /* protect the input string from strtok */
2039         str = ch_strdup( in );
2040
2041         /* Count words in string */
2042         j=1;
2043         for ( s = str; *s; s++ ) {
2044                 if ( strchr( brkstr, *s ) != NULL ) {
2045                         j++;
2046                 }
2047         }
2048
2049         out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2050         new = out + i;
2051         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2052                 s != NULL;
2053                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2054         {
2055                 *new = ch_strdup( s );
2056                 new++;
2057         }
2058
2059         *new = NULL;
2060         free( str );
2061         return( out );
2062 }
2063
2064 #endif