]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
923d4ecb72042866007818bcc703c7a1c5f4cc4b
[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:%s)\n",
225                                 si->mastername, si->masterport, 0 );
226 #else
227                         Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
228                                 "ldap_init failed (%s:%s)\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                                         free( 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         *modlist = NULL;
703
704         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
705 #ifdef NEW_LOGGING
706                 LDAP_LOG( OPERATION, ERR,
707                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
708 #else
709                 Debug( LDAP_DEBUG_ANY,
710                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
711 #endif
712                 return NULL;
713         }
714
715         op->o_tag = LDAP_REQ_ADD;
716
717         rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
718
719         if ( rc != LDAP_SUCCESS ) {
720 #ifdef NEW_LOGGING
721                 LDAP_LOG( OPERATION, ERR,
722                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
723 #else
724                 Debug( LDAP_DEBUG_ANY,
725                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
726 #endif
727                 return NULL;
728         }
729
730         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
731         dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
732
733         e->e_attrs = NULL;
734
735         for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
736                   rc == LDAP_SUCCESS;
737                   rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
738         {
739                 if ( tmp.sml_type.bv_val == NULL ) break;
740
741                 mod  = (Modifications *) ch_malloc( sizeof( Modifications ));
742
743                 mod->sml_op = LDAP_MOD_REPLACE;
744                 mod->sml_next = NULL;
745                 mod->sml_desc = NULL;
746                 mod->sml_type = tmp.sml_type;
747                 mod->sml_bvalues = tmp.sml_bvalues;
748                 mod->sml_nvalues = tmp.sml_bvalues;
749
750                 *modtail = mod;
751                 modtail = &mod->sml_next;
752         }
753
754         if ( ber_scanf( ber, "}") == LBER_ERROR ) {
755 #ifdef NEW_LOGGING
756                 LDAP_LOG( OPERATION, ERR,
757                                 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
758 #else
759                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
760                                 0, 0, 0 );
761 #endif
762                 return NULL;
763         }
764
765         ber_free( ber, 0 );
766         tmpber = ldap_get_message_ber( msg );
767         ber = ber_dup( tmpber );
768
769         ber_scanf( ber, "{xx" );
770
771         rc = ldap_int_get_controls( ber, &rctrls );
772
773         if ( rc != LDAP_SUCCESS ) {
774 #ifdef NEW_LOGGING
775                 LDAP_LOG( OPERATION, ERR,
776                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
777 #else
778                 Debug( LDAP_DEBUG_ANY,
779                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
780 #endif
781                 return NULL;
782         }
783
784         if ( rctrls ) {
785                 rctrlp = *rctrls;
786                 ctrl_ber = ber_alloc_t( LBER_USE_DER );
787                 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
788                 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
789                 ber_reset( ctrl_ber, 1 );
790                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
791                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
792                         ber_scanf( ctrl_ber, "o}", syncCookie );
793                 }
794                 ber_free( ctrl_ber, 1 );
795         } else {
796 #ifdef NEW_LOGGING
797                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
798                         " rctrls absent\n", 0, 0, 0 );
799 #else
800                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
801                         " rctrls absent\n", 0, 0, 0 );
802 #endif
803         }
804
805         if ( *syncstate == LDAP_SYNC_PRESENT ) {
806                 e = NULL;
807                 goto done;
808         }
809
810         if ( *modlist == NULL ) {
811 #ifdef NEW_LOGGING
812                 LDAP_LOG( OPERATION, ERR,
813                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
814 #else
815                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
816                                 0, 0, 0 );
817 #endif
818         }
819
820         rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
821
822         if ( rc != LDAP_SUCCESS ) {
823 #ifdef NEW_LOGGING
824                 LDAP_LOG( OPERATION, ERR,
825                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
826 #else
827                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
828                                 text, 0, 0 );
829 #endif
830                 return NULL;
831         }
832         
833         rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
834                                          &text,txtbuf, textlen );
835         
836         if( rc != LDAP_SUCCESS ) {
837 #ifdef NEW_LOGGING
838                 LDAP_LOG( OPERATION, ERR,
839                                 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
840 #else
841                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
842                                 text, 0, 0 );
843 #endif
844                 return NULL;
845         }
846
847         rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
848         if( rc != LDAP_SUCCESS ) {
849 #ifdef NEW_LOGGING
850                 LDAP_LOG( OPERATION, ERR,
851                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
852 #else
853                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
854                                 text, 0, 0 );
855 #endif
856         }
857
858 done:
859
860         ber_free ( ber, 0 );
861
862         return e;
863 }
864
865 int 
866 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
867 {
868         const struct berval *uuid1 = v_uuid1;
869         const struct berval *uuid2 = v_uuid2;
870         int rc = uuid1->bv_len - uuid2->bv_len;
871         if ( rc ) return rc;
872         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
873 }
874
875 static int
876 syncrepl_entry(
877         LDAP *ld,
878         Operation *op,
879         Entry* e,
880         Modifications* modlist,
881         int syncstate,
882         struct berval* syncUUID,
883         struct berval* syncCookie,
884         int refresh
885 )
886 {
887         Backend *be = op->o_bd;
888         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
889         slap_callback   cb;
890         struct berval   csn_bv = {0, NULL};
891         struct berval   *syncuuid_bv = NULL;
892         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
893
894         SlapReply       rs = {REP_RESULT};
895         int rc = LDAP_SUCCESS;
896
897         struct berval base_bv = {0, NULL};
898
899         char *filterstr;
900         Filter *filter;
901
902         Attribute *a;
903
904         if ( refresh &&
905                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
906                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
907                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
908                                                 syncuuid_cmp, avl_dup_error );
909         }
910
911         if ( syncstate == LDAP_SYNC_PRESENT ) {
912                 if ( e )
913                         return 1;
914                 else
915                         return 0;
916         }
917
918         if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
919                 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
920         }
921
922         filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1, op->o_tmpmemctx ); 
923         strcpy( filterstr, "entryUUID=" );
924         strcat( filterstr, syncUUID->bv_val );
925
926         si->e = e;
927         si->syncUUID = syncUUID;
928         si->syncUUID_ndn = NULL;
929
930         filter = str2filter( filterstr );
931         ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
932         ch_free( filterstr );
933         op->ors_filter = filter;
934         op->ors_scope = LDAP_SCOPE_SUBTREE;
935
936         /* get syncrepl cookie of shadow replica from subentry */
937         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
938         dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
939         ch_free( base_bv.bv_val );
940
941         /* set callback function */
942         op->o_callback = &cb;
943         cb.sc_response = dn_callback;
944         cb.sc_private = si;
945
946         be->be_search( op, &rs );
947
948         ch_free( op->o_req_dn.bv_val );
949         ch_free( op->o_req_ndn.bv_val );
950         filter_free( op->ors_filter );
951         ch_free( op->ors_filterstr.bv_val );
952
953         cb.sc_response = null_callback;
954
955         if ( si->syncUUID_ndn )
956                 printf("syncUUID_ndn = %s\n", si->syncUUID_ndn );
957
958         switch ( syncstate ) {
959         case LDAP_SYNC_ADD :
960         case LDAP_SYNC_MODIFY :
961
962                 rc = LDAP_SUCCESS;
963
964                 if ( si->syncUUID_ndn ) {
965                         op->o_req_dn = *si->syncUUID_ndn;
966                         op->o_req_ndn = *si->syncUUID_ndn;
967                         op->o_tag = LDAP_REQ_DELETE;
968                         rc = be->be_delete( op, &rs );
969                 }
970
971                 if ( rc == LDAP_SUCCESS ||
972                          rc == LDAP_REFERRAL ||
973                          rc == LDAP_NO_SUCH_OBJECT ||
974                          rc == DB_NOTFOUND ) {
975                         op->o_tag = LDAP_REQ_ADD;
976                         op->ora_e = e;
977                         op->o_req_dn = e->e_name;
978                         op->o_req_ndn = e->e_nname;
979                         rc = be->be_add( op, &rs );
980                         if ( rc != LDAP_SUCCESS ) {
981                                 if ( rc == LDAP_ALREADY_EXISTS ) {
982 #ifdef NEW_LOGGING
983                                         LDAP_LOG( OPERATION, ERR,
984                                                 "be_add failed : already exists (%d)\n",
985                                                 rc, 0, 0 );
986 #else
987                                         Debug( LDAP_DEBUG_ANY,
988                                                 "be_add failed : already exists (%d)\n",
989                                                 rc, 0, 0 );
990 #endif
991                                 } else if ( rc == LDAP_REFERRAL ||
992                                                         rc == LDAP_NO_SUCH_OBJECT ||
993                                                         rc == DB_NOTFOUND ) {
994                                         syncrepl_add_glue(ld, op, e,
995                                                 modlist, syncstate,
996                                                 syncUUID, syncCookie);
997                                 } else {
998 #ifdef NEW_LOGGING
999                                         LDAP_LOG( OPERATION, ERR,
1000                                                 "be_add failed (%d)\n",
1001                                                 rc, 0, 0 );
1002 #else
1003                                         Debug( LDAP_DEBUG_ANY,
1004                                                 "be_add failed (%d)\n",
1005                                                 rc, 0, 0 );
1006 #endif
1007                                 }
1008                         } else {
1009                                 return 0;
1010                         }
1011                 } else {
1012 #ifdef NEW_LOGGING
1013                         LDAP_LOG( OPERATION, ERR,
1014                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1015 #else
1016                         Debug( LDAP_DEBUG_ANY,
1017                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1018 #endif
1019                 }
1020
1021                 si->e = NULL;
1022                 return 1;
1023         case LDAP_SYNC_DELETE :
1024                 op->o_tag = LDAP_REQ_DELETE;
1025                 be->be_delete( op, &rs );
1026                 si->e = NULL;
1027                 return 1;
1028         default :
1029 #ifdef NEW_LOGGING
1030                 LDAP_LOG( OPERATION, ERR,
1031                         "unknown syncstate\n", 0, 0, 0 );
1032 #else
1033                 Debug( LDAP_DEBUG_ANY,
1034                         "unknown syncstate\n", 0, 0, 0 );
1035 #endif
1036                 return 1;
1037         }
1038 }
1039
1040 static int
1041 syncrepl_del_nonpresent(
1042         LDAP *ld,
1043         Operation *op
1044 )
1045 {
1046         Backend* be = op->o_bd;
1047         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1048         slap_callback   cb;
1049         struct berval   base_bv = {0, NULL};
1050         Filter *filter;
1051         SlapReply       rs = {REP_RESULT};
1052         struct berval   filterstr_bv = {0, NULL};
1053         struct nonpresent_entry *np_list, *np_prev;
1054
1055         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1056         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1057         ch_free( base_bv.bv_val );
1058
1059         filter = str2filter( si->filterstr );
1060
1061         cb.sc_response = nonpresent_callback;
1062         cb.sc_private = si;
1063
1064         op->o_callback = &cb;
1065         op->o_tag = LDAP_REQ_SEARCH;
1066         op->ors_scope = si->scope;
1067         op->ors_deref = LDAP_DEREF_NEVER;
1068         op->ors_slimit = -1;
1069         op->ors_tlimit = -1;
1070         op->ors_attrsonly = 0;
1071         op->ors_attrs = NULL;
1072         op->ors_filter = filter;
1073         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1074
1075         be->be_search( op, &rs );
1076
1077         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1078                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1079                 while ( np_list != NULL ) {
1080                         LDAP_LIST_REMOVE( np_list, np_link );
1081                         np_prev = np_list;
1082                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1083                         op->o_tag = LDAP_REQ_DELETE;
1084                         op->o_callback = &cb;
1085                         cb.sc_response = null_callback;
1086                         cb.sc_private = si;
1087                         op->o_req_dn = *np_prev->dn;
1088                         op->o_req_ndn = *np_prev->ndn;
1089                         op->o_bd->be_delete( op, &rs );
1090                         ber_bvfree( np_prev->dn );
1091                         ber_bvfree( np_prev->ndn );
1092                         op->o_req_dn.bv_val = NULL;
1093                         op->o_req_ndn.bv_val = NULL;
1094                         ch_free( np_prev );
1095                 }
1096         }
1097
1098         if ( op->o_req_dn.bv_val )
1099                 ch_free( op->o_req_dn.bv_val );
1100         if ( op->o_req_ndn.bv_val )
1101                 ch_free( op->o_req_ndn.bv_val );
1102         filter_free( op->ors_filter );
1103         ch_free( op->ors_filterstr.bv_val );
1104 }
1105
1106
1107 static void
1108 syncrepl_add_glue(
1109         LDAP *ld,
1110         Operation* op,
1111         Entry *e,
1112         Modifications* modlist,
1113         int syncstate,
1114         struct berval* syncUUID,
1115         struct berval* syncCookie
1116 )
1117 {
1118         Backend *be = op->o_bd;
1119         syncinfo_t *si = op->o_callback->sc_private;
1120         struct berval   uuid_bv = {0, NULL};
1121         slap_callback cb;
1122         Attribute       *a;
1123         int     rc;
1124         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1125         int levels = 0;
1126         int i, j, k;
1127         struct berval dn = {0, NULL};
1128         struct berval pdn = {0, NULL};
1129         struct berval ndn = {0, NULL};
1130         struct berval rdn = {0, NULL};
1131         Entry   *glue;
1132         SlapReply       rs = {REP_RESULT};
1133         Connection *conn = op->o_conn;
1134
1135         op->o_tag = LDAP_REQ_ADD;
1136         op->o_callback = &cb;
1137         cb.sc_response = null_callback;
1138         cb.sc_private = si;
1139
1140         ber_dupbv( &dn, &e->e_nname );
1141         ber_dupbv( &pdn, &e->e_nname );
1142
1143         while ( !be_issuffix ( be, &pdn )) {
1144                 dnParent( &dn, &pdn );
1145                 ch_free( dn.bv_val );
1146                 ber_dupbv( &dn, &pdn );
1147                 levels++;
1148         }
1149
1150         for ( i = 0; i <= levels; i++ ) {
1151                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1152                 ch_free( dn.bv_val );
1153                 ch_free( pdn.bv_val );
1154                 ber_dupbv( &dn, &e->e_nname );
1155                 ber_dupbv( &pdn, &e->e_nname );
1156                 j = levels - i;
1157                 for ( k = 0; k < j; k++ ) {
1158                         dnParent( &dn, &pdn );
1159                         ch_free( dn.bv_val );
1160                         ber_dupbv( &dn, &pdn );
1161                 }
1162
1163                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1164                 ber_dupbv( &glue->e_name, &pdn );
1165                 ber_dupbv( &glue->e_nname, &ndn );
1166                 ch_free( dn.bv_val );
1167                 ch_free( pdn.bv_val );
1168                 ch_free( ndn.bv_val );
1169
1170                 a = ch_calloc( 1, sizeof( Attribute ));
1171                 a->a_desc = slap_schema.si_ad_objectClass;
1172                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1173                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1174                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1175                 a->a_vals[2].bv_len = 0;
1176                 a->a_vals[2].bv_val = NULL;
1177                 a->a_next = glue->e_attrs;
1178                 glue->e_attrs = a;
1179
1180                 a = ch_calloc( 1, sizeof( Attribute ));
1181                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1182                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1183                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1184                 a->a_vals[1].bv_len = 0;
1185                 a->a_vals[1].bv_val = NULL;
1186                 a->a_next = glue->e_attrs;
1187                 glue->e_attrs = a;
1188
1189                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1190                         op->o_req_dn = e->e_name;
1191                         op->o_req_ndn = e->e_nname;
1192                         op->ora_e = e;
1193                         rc = be->be_add ( op, &rs );
1194                         if ( rc == LDAP_SUCCESS )
1195                                 be_entry_release_w( op, e );
1196                         else 
1197                                 entry_free( e );
1198                         entry_free( glue );
1199                 } else {
1200                         op->o_req_dn = glue->e_name;
1201                         op->o_req_ndn = glue->e_nname;
1202                         op->ora_e = glue;
1203                         rc = be->be_add ( op, &rs );
1204                         if ( rc == LDAP_SUCCESS ) {
1205                                 be_entry_release_w( op, glue );
1206                         } else {
1207                         /* incl. ALREADY EXIST */
1208                                 entry_free( glue );
1209                         }
1210                 }
1211         }
1212
1213         return;
1214 }
1215
1216 static void
1217 syncrepl_updateCookie(
1218         LDAP *ld,
1219         Operation *op,
1220         struct berval *pdn,
1221         struct berval *syncCookie
1222 )
1223 {
1224         Backend *be = op->o_bd;
1225         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1226         Modifications *ml;
1227         Modifications *mlnext;
1228         Modifications *mod;
1229         Modifications *modlist;
1230         Modifications **modtail = &modlist;
1231
1232         struct berval* ocbva = NULL;
1233         struct berval* cnbva = NULL;
1234         struct berval* ssbva = NULL;
1235         struct berval* scbva = NULL;
1236
1237         char substr[64];
1238         char rdnstr[67];
1239         const char      *text;
1240         char txtbuf[SLAP_TEXT_BUFLEN];
1241         size_t textlen = sizeof txtbuf;
1242
1243         Entry* e;
1244         int rc;
1245
1246         struct berval sub_bv = { 0, NULL };
1247         struct berval psubrdn = { 0, NULL };
1248         
1249         slap_callback cb;
1250         SlapReply       rs = {REP_RESULT};
1251
1252         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1253         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1254         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1255         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1256
1257         /* update in memory cookie */
1258         if ( si->syncCookie != NULL ) {
1259                 ber_bvfree( si->syncCookie );
1260         }
1261         si->syncCookie = ber_dupbv( NULL, syncCookie );
1262         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1263         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1264         ber_str2bv( "syncConsumerSubentry",
1265                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1266         ocbva[3].bv_len = 0;
1267         ocbva[3].bv_val = NULL;
1268
1269         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1270         mod->sml_op = LDAP_MOD_REPLACE;
1271         mod->sml_next = NULL;
1272         mod->sml_desc = NULL;
1273         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1274         mod->sml_bvalues = ocbva;
1275         mod->sml_nvalues = ocbva;
1276         *modtail = mod;
1277         modtail = &mod->sml_next;
1278
1279         sprintf( substr, "syncrepl%d", si->id );
1280         sprintf( rdnstr, "cn=%s", substr );
1281         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1282         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1283         cnbva[1].bv_len = 0;
1284         cnbva[1].bv_val = NULL;
1285         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1286         mod->sml_op = LDAP_MOD_REPLACE;
1287         mod->sml_next = NULL;
1288         mod->sml_desc = NULL;
1289         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1290         mod->sml_bvalues = cnbva;
1291         mod->sml_nvalues = cnbva;
1292         *modtail = mod;
1293         modtail = &mod->sml_next;
1294
1295         ber_dupbv( &scbva[0], si->syncCookie );
1296         scbva[1].bv_len = 0;
1297         scbva[1].bv_val = NULL;
1298         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1299         mod->sml_op = LDAP_MOD_REPLACE;
1300         mod->sml_next = NULL;
1301         mod->sml_desc = NULL;
1302         ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1303                                                 1, &mod->sml_type );
1304         mod->sml_bvalues = scbva;
1305         mod->sml_nvalues = scbva;
1306         *modtail = mod;
1307         modtail = &mod->sml_next;
1308
1309         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1310         ssbva[1].bv_len = 0;
1311         ssbva[1].bv_val = NULL;
1312         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1313         mod->sml_op = LDAP_MOD_REPLACE;
1314         mod->sml_next = NULL;
1315         mod->sml_desc = NULL;
1316         ber_str2bv( "subtreeSpecification",
1317                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1318         mod->sml_bvalues = ssbva;
1319         mod->sml_nvalues = ssbva;
1320         *modtail = mod;
1321         modtail = &mod->sml_next;
1322
1323         rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1324
1325         if ( rc != LDAP_SUCCESS ) {
1326 #ifdef NEW_LOGGING
1327                 LDAP_LOG( OPERATION, ERR,
1328                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1329 #else
1330                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1331                          text, 0, 0 );
1332 #endif
1333         }
1334
1335         op->o_tag = LDAP_REQ_ADD;
1336         rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1337
1338         if( rc != LDAP_SUCCESS ) {
1339 #ifdef NEW_LOGGING
1340                 LDAP_LOG( OPERATION, ERR,
1341                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1342 #else
1343                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1344                          text, 0, 0 );
1345 #endif
1346         }
1347
1348         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1349
1350         build_new_dn( &sub_bv, pdn, &psubrdn );
1351         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1352         ch_free( sub_bv.bv_val );
1353         ch_free( psubrdn.bv_val );
1354
1355         e->e_attrs = NULL;
1356
1357         rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1358
1359         if( rc != LDAP_SUCCESS ) {
1360 #ifdef NEW_LOGGING
1361                 LDAP_LOG( OPERATION, ERR,
1362                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1363 #else
1364                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1365                          text, 0, 0 );
1366 #endif
1367         }
1368
1369         cb.sc_response = null_callback;
1370         cb.sc_private = si;
1371
1372         op->o_callback = &cb;
1373         op->o_req_dn = e->e_name;
1374         op->o_req_ndn = e->e_nname;
1375
1376         /* update persistent cookie */
1377 update_cookie_retry:
1378         op->o_tag = LDAP_REQ_MODIFY;
1379         op->orm_modlist = modlist;
1380         rc = be->be_modify( op, &rs );
1381         if ( rc != LDAP_SUCCESS ) {
1382                 if ( rc == LDAP_REFERRAL ||
1383                          rc == LDAP_NO_SUCH_OBJECT ||
1384                          rc == DB_NOTFOUND ) {
1385                         op->o_tag = LDAP_REQ_ADD;
1386                         op->ora_e = e;
1387                         rc = be->be_add( op, &rs );
1388                         if ( rc != LDAP_SUCCESS ) {
1389                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1390                                         goto update_cookie_retry;
1391                                 } else if ( rc == LDAP_REFERRAL ||
1392                                                         rc == LDAP_NO_SUCH_OBJECT ||
1393                                                         rc == DB_NOTFOUND ) {
1394 #ifdef NEW_LOGGING
1395                                         LDAP_LOG( OPERATION, ERR,
1396                                                 "cookie will be non-persistent\n",
1397                                                 0, 0, 0 );
1398 #else
1399                                         Debug( LDAP_DEBUG_ANY,
1400                                                 "cookie will be non-persistent\n",
1401                                                 0, 0, 0 );
1402 #endif
1403                                 } else {
1404 #ifdef NEW_LOGGING
1405                                         LDAP_LOG( OPERATION, ERR,
1406                                                 "be_add failed (%d)\n",
1407                                                 rc, 0, 0 );
1408 #else
1409                                         Debug( LDAP_DEBUG_ANY,
1410                                                 "be_add failed (%d)\n",
1411                                                 rc, 0, 0 );
1412 #endif
1413                                 }
1414                         } else {
1415                                 goto done;
1416                         }
1417                 } else {
1418 #ifdef NEW_LOGGING
1419                         LDAP_LOG( OPERATION, ERR,
1420                                 "be_modify failed (%d)\n", rc, 0, 0 );
1421 #else
1422                         Debug( LDAP_DEBUG_ANY,
1423                                 "be_modify failed (%d)\n", rc, 0, 0 );
1424 #endif
1425                 }
1426         }
1427
1428         if ( e != NULL )
1429                 entry_free( e );
1430
1431 done :
1432
1433         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1434                 mlnext = ml->sml_next;
1435                 free( ml );
1436         }
1437
1438         return;
1439 }
1440
1441
1442 static
1443 int slap_mods_check_syncrepl(
1444         Operation *op,
1445         Modifications **mlp,
1446         const char **text,
1447         char *textbuf,
1448         size_t textlen,
1449         void *ctx )
1450 {
1451         int rc;
1452         Backend *be = op->o_bd;
1453         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1454         AttributeDescription** descs;
1455         int i;
1456         Modifications *prevml = NULL;
1457         Modifications *nextml = NULL;
1458         Modifications *ml = *mlp;
1459
1460         while ( ml != NULL ) {
1461                 AttributeDescription *ad = NULL;
1462
1463                 /* convert to attribute description */
1464                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1465
1466                 if( rc != LDAP_SUCCESS ) {
1467                         snprintf( textbuf, textlen, "%s: %s",
1468                                                 ml->sml_type.bv_val, *text );
1469                         *text = textbuf;
1470                         return rc;
1471                 }
1472
1473                 ad = ml->sml_desc;
1474
1475                 if ( si->lastmod == LASTMOD_REQ ) {
1476                         descs = del_descs_lastmod;
1477                 } else {
1478                         descs = del_descs;
1479                 }
1480
1481                 for ( i = 0; descs[i] != NULL; i++ ) {
1482                         if ( ad == descs[i] ) {
1483                                 if ( prevml == NULL ) {
1484                                         mlp = &ml->sml_next;
1485                                         prevml = NULL;
1486                                 } else {
1487                                         prevml->sml_next = ml->sml_next;
1488                                 }
1489                                 slap_mod_free( &ml->sml_mod, 0 );
1490                                 nextml = ml->sml_next;
1491                                 free( ml );
1492                                 ml = nextml;
1493                                 continue;
1494                         }
1495                 }
1496
1497                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1498                                 && !slap_ad_is_binary( ad )) {
1499                         /* attribute requires binary transfer */
1500                         snprintf( textbuf, textlen,
1501                                         "%s: requires ;binary transfer",
1502                                         ml->sml_type.bv_val );
1503                         *text = textbuf;
1504                         return LDAP_UNDEFINED_TYPE;
1505                 }
1506
1507                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1508                                         && slap_ad_is_binary( ad )) {
1509                         /* attribute requires binary transfer */
1510                         snprintf( textbuf, textlen,
1511                                         "%s: disallows ;binary transfer",
1512                                         ml->sml_type.bv_val );
1513                         *text = textbuf;
1514                         return LDAP_UNDEFINED_TYPE;
1515                 }
1516
1517                 if( slap_ad_is_tag_range( ad )) {
1518                         /* attribute requires binary transfer */
1519                         snprintf( textbuf, textlen,
1520                                         "%s: inappropriate use of tag range option",
1521                                         ml->sml_type.bv_val );
1522                         *text = textbuf;
1523                         return LDAP_UNDEFINED_TYPE;
1524                 }
1525
1526                 if ( is_at_obsolete( ad->ad_type ) &&
1527                                 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1528                         /*
1529                          * attribute is obsolete,
1530                          * only allow replace/delete with no values
1531                          */
1532                         snprintf( textbuf, textlen,
1533                                         "%s: attribute is obsolete",
1534                                         ml->sml_type.bv_val );
1535                         *text = textbuf;
1536                         return LDAP_CONSTRAINT_VIOLATION;
1537                 }
1538
1539                 /*
1540                  * check values
1541                  */
1542                 if( ml->sml_values != NULL ) {
1543                         ber_len_t nvals;
1544                         slap_syntax_validate_func *validate =
1545                         ad->ad_type->sat_syntax->ssyn_validate;
1546                         slap_syntax_transform_func *pretty =
1547                         ad->ad_type->sat_syntax->ssyn_pretty;
1548
1549                         if( !pretty && !validate ) {
1550                                 *text = "no validator for syntax";
1551                                 snprintf( textbuf, textlen,
1552                                                 "%s: no validator for syntax %s",
1553                                                 ml->sml_type.bv_val,
1554                                                 ad->ad_type->sat_syntax->ssyn_oid );
1555                                 *text = textbuf;
1556                                 return LDAP_INVALID_SYNTAX;
1557                         }
1558
1559                         /*
1560                          * check that each value is valid per syntax
1561                          * and pretty if appropriate
1562                          */
1563                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1564                                 struct berval pval = {0, NULL};
1565                                 if( pretty ) {
1566                                         rc = pretty( ad->ad_type->sat_syntax,
1567                                                         &ml->sml_values[nvals], &pval, ctx );
1568                                 } else {
1569                                         rc = validate( ad->ad_type->sat_syntax,
1570                                                         &ml->sml_values[nvals] );
1571                                 }
1572
1573                                 if( rc != 0 ) {
1574                                         snprintf( textbuf, textlen,
1575                                                         "%s: value #%ld invalid per syntax",
1576                                                         ml->sml_type.bv_val, (long) nvals );
1577                                         *text = textbuf;
1578                                         return LDAP_INVALID_SYNTAX;
1579                                 }
1580
1581                                 if( pretty ) {
1582                                         ber_memfree( ml->sml_values[nvals].bv_val );
1583                                         ml->sml_values[nvals] = pval;
1584                                 }
1585                         }
1586
1587                         /*
1588                          * a rough single value check... an additional check is needed
1589                          * to catch add of single value to existing single valued attribute
1590                          */
1591                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1592                                         && nvals > 1 && is_at_single_value( ad->ad_type )) {
1593                                 snprintf( textbuf, textlen,
1594                                                 "%s: multiple values provided",
1595                                                 ml->sml_type.bv_val );
1596                                 *text = textbuf;
1597                                 return LDAP_CONSTRAINT_VIOLATION;
1598                         }
1599
1600                         if( nvals && ad->ad_type->sat_equality &&
1601                                         ad->ad_type->sat_equality->smr_normalize ) {
1602                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1603                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1604                                         rc = ad->ad_type->sat_equality->smr_normalize( 0,
1605                                                         ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1606                                                         &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1607                                         if( rc ) {
1608 #ifdef NEW_LOGGING
1609                                                 LDAP_LOG( OPERATION, DETAIL1,
1610                                                                 "str2entry:  NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1611 #else
1612                                                 Debug( LDAP_DEBUG_ANY,
1613                                                                 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1614 #endif
1615                                                 snprintf( textbuf, textlen,
1616                                                                 "%s: value #%ld normalization failed",
1617                                                                 ml->sml_type.bv_val, (long) nvals );
1618                                                 *text = textbuf;
1619                                                 return rc;
1620                                         }
1621                                 }
1622                                 ml->sml_nvalues[nvals].bv_val = NULL;
1623                                 ml->sml_nvalues[nvals].bv_len = 0;
1624                         }
1625                 }
1626                 prevml = ml;
1627                 ml = ml->sml_next;
1628         }
1629
1630         return LDAP_SUCCESS;
1631 }
1632
1633 static
1634 int slap_mods_opattrs_syncrepl(
1635         Operation *op,
1636         Modifications *mods,
1637         Modifications **modtail,
1638         const char **text,
1639         char *textbuf, size_t textlen )
1640 {
1641         struct berval name = {0, NULL};
1642         struct berval timestamp = {0, NULL};
1643         struct berval csn = {0, NULL};
1644         struct berval nname = {0, NULL};
1645         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1646         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1647         Modifications *mod;
1648         Backend *be = op->o_bd;
1649         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1650
1651         int mop = LDAP_MOD_REPLACE;
1652
1653         assert( modtail != NULL );
1654         assert( *modtail == NULL );
1655
1656         if( si->lastmod == LASTMOD_GEN ) {
1657                 struct tm *ltm;
1658                 time_t now = slap_get_time();
1659
1660                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1661                 ltm = gmtime( &now );
1662                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1663
1664                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1665                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1666                 csn.bv_val = csnbuf;
1667
1668                 timestamp.bv_val = timebuf;
1669                 timestamp.bv_len = strlen(timebuf);
1670
1671                 if( op->o_dn.bv_len == 0 ) {
1672                         name.bv_val = SLAPD_ANONYMOUS;
1673                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1674                         nname = name;
1675                 } else {
1676                         name = op->o_dn;
1677                         nname = op->o_ndn;
1678                 }
1679         }
1680
1681         if( op->o_tag == LDAP_REQ_ADD ) {
1682                 struct berval tmpval = {0, NULL};
1683
1684                 if( global_schemacheck ) {
1685                         int rc = mods_structural_class( mods, &tmpval,
1686                                                                         text, textbuf, textlen );
1687                         if( rc != LDAP_SUCCESS ) {
1688                                 return rc;
1689                         }
1690
1691                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1692                         mod->sml_op = mop;
1693                         mod->sml_type.bv_val = NULL;
1694                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1695                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1696                         ber_dupbv( &mod->sml_values[0], &tmpval );
1697                         mod->sml_values[1].bv_len = 0;
1698                         mod->sml_values[1].bv_val = NULL;
1699                         assert( mod->sml_values[0].bv_val );
1700                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1701                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1702                         mod->sml_nvalues[1].bv_len = 0;
1703                         mod->sml_nvalues[1].bv_val = NULL;
1704                         assert( mod->sml_nvalues[0].bv_val );
1705                         *modtail = mod;
1706                         modtail = &mod->sml_next;
1707                 }
1708
1709                 if( si->lastmod == LASTMOD_GEN ) {
1710                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1711                         mod->sml_op = mop;
1712                         mod->sml_type.bv_val = NULL;
1713                         mod->sml_desc = slap_schema.si_ad_creatorsName;
1714                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1715                         ber_dupbv( &mod->sml_values[0], &name );
1716                         mod->sml_values[1].bv_len = 0;
1717                         mod->sml_values[1].bv_val = NULL;
1718                         assert( mod->sml_values[0].bv_val );
1719                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1720                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1721                         mod->sml_nvalues[1].bv_len = 0;
1722                         mod->sml_nvalues[1].bv_val = NULL;
1723                         assert( mod->sml_nvalues[0].bv_val );
1724                         *modtail = mod;
1725                         modtail = &mod->sml_next;
1726
1727                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1728                         mod->sml_op = mop;
1729                         mod->sml_type.bv_val = NULL;
1730                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
1731                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1732                         ber_dupbv( &mod->sml_values[0], &timestamp );
1733                         mod->sml_values[1].bv_len = 0;
1734                         mod->sml_values[1].bv_val = NULL;
1735                         assert( mod->sml_values[0].bv_val );
1736                         mod->sml_nvalues = NULL;
1737                         *modtail = mod;
1738                         modtail = &mod->sml_next;
1739                 }
1740         }
1741
1742         if( si->lastmod == LASTMOD_GEN ) {
1743                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1744                 mod->sml_op = mop;
1745                 mod->sml_type.bv_val = NULL;
1746                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1747                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1748                 ber_dupbv( &mod->sml_values[0], &csn );
1749                 mod->sml_values[1].bv_len = 0;
1750                 mod->sml_values[1].bv_val = NULL;
1751                 assert( mod->sml_values[0].bv_val );
1752                 mod->sml_nvalues = NULL;
1753                 *modtail = mod;
1754                 modtail = &mod->sml_next;
1755
1756                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1757                 mod->sml_op = mop;
1758                 mod->sml_type.bv_val = NULL;
1759                 mod->sml_desc = slap_schema.si_ad_modifiersName;
1760                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1761                 ber_dupbv( &mod->sml_values[0], &name );
1762                 mod->sml_values[1].bv_len = 0;
1763                 mod->sml_values[1].bv_val = NULL;
1764                 assert( mod->sml_values[0].bv_val );
1765                 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1766                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1767                 mod->sml_nvalues[1].bv_len = 0;
1768                 mod->sml_nvalues[1].bv_val = NULL;
1769                 assert( mod->sml_nvalues[0].bv_val );
1770                 *modtail = mod;
1771                 modtail = &mod->sml_next;
1772
1773                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1774                 mod->sml_op = mop;
1775                 mod->sml_type.bv_val = NULL;
1776                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1777                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1778                 ber_dupbv( &mod->sml_values[0], &timestamp );
1779                 mod->sml_values[1].bv_len = 0;
1780                 mod->sml_values[1].bv_val = NULL;
1781                 assert( mod->sml_values[0].bv_val );
1782                 mod->sml_nvalues = NULL;
1783                 *modtail = mod;
1784                 modtail = &mod->sml_next;
1785         }
1786
1787         *modtail = NULL;
1788         return LDAP_SUCCESS;
1789 }
1790
1791
1792 static
1793 int slap_mods2entry_syncrepl(
1794         Modifications *mods,
1795         Entry **e,
1796         int repl_user,
1797         const char **text,
1798         char *textbuf, size_t textlen )
1799 {
1800         Attribute **tail = &(*e)->e_attrs;
1801         assert( *tail == NULL );
1802
1803         *text = textbuf;
1804
1805         for( ; mods != NULL; mods = mods->sml_next ) {
1806                 Attribute *attr;
1807
1808                 assert( mods->sml_desc != NULL );
1809
1810                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1811
1812                 if( attr != NULL ) {
1813 #define SLURPD_FRIENDLY
1814 #ifdef SLURPD_FRIENDLY
1815                         ber_len_t i,j;
1816
1817                         if( !repl_user ) {
1818                                 snprintf( textbuf, textlen,
1819                                         "attribute '%s' provided more than once",
1820                                         mods->sml_desc->ad_cname.bv_val );
1821                                 return LDAP_TYPE_OR_VALUE_EXISTS;
1822                         }
1823
1824                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
1825                                 /* count them */
1826                         }
1827                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
1828                                 /* count them */
1829                         }
1830                         j++;    /* NULL */
1831                         
1832                         attr->a_vals = ch_realloc( attr->a_vals,
1833                                 sizeof( struct berval ) * (i+j) );
1834
1835                         /* should check for duplicates */
1836
1837                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1838                                 sizeof( struct berval ) * j );
1839
1840                         if( attr->a_nvals ) {
1841                                 attr->a_nvals = ch_realloc( attr->a_nvals,
1842                                         sizeof( struct berval ) * (i+j) );
1843
1844                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1845                                         sizeof( struct berval ) * j );
1846
1847                                 /* trim the mods array */
1848                                 ch_free( mods->sml_nvalues );
1849                                 mods->sml_nvalues = NULL;
1850                         }
1851
1852                         continue;
1853 #else
1854                         snprintf( textbuf, textlen,
1855                                 "attribute '%s' provided more than once",
1856                                 mods->sml_desc->ad_cname.bv_val );
1857                         return LDAP_TYPE_OR_VALUE_EXISTS;
1858 #endif
1859                 }
1860
1861                 if( mods->sml_values[1].bv_val != NULL ) {
1862                         /* check for duplicates */
1863                         int             i, j;
1864                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1865
1866                         /* check if the values we're adding already exist */
1867                         if( mr == NULL || !mr->smr_match ) {
1868                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1869                                         /* test asserted values against themselves */
1870                                         for( j = 0; j < i; j++ ) {
1871                                                 if ( bvmatch( &mods->sml_bvalues[i],
1872                                                         &mods->sml_bvalues[j] ) ) {
1873                                                         /* value exists already */
1874                                                         snprintf( textbuf, textlen,
1875                                                                 "%s: value #%d provided more than once",
1876                                                                 mods->sml_desc->ad_cname.bv_val, j );
1877                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
1878                                                 }
1879                                         }
1880                                 }
1881
1882                         } else {
1883                                 int             rc;
1884                                 const char      *text = NULL;
1885                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
1886                                 
1887                                 rc = modify_check_duplicates( mods->sml_desc, mr,
1888                                                 NULL, mods->sml_bvalues, 0,
1889                                                 &text, textbuf, sizeof( textbuf ) );
1890
1891                                 if ( rc != LDAP_SUCCESS ) {
1892                                         return rc;
1893                                 }
1894                         }
1895                 }
1896
1897                 attr = ch_calloc( 1, sizeof(Attribute) );
1898
1899                 /* move ad to attr structure */
1900                 attr->a_desc = mods->sml_desc;
1901
1902                 /* move values to attr structure */
1903                 /*      should check for duplicates */
1904                 attr->a_vals = mods->sml_values;
1905
1906                 attr->a_nvals = mods->sml_nvalues;
1907
1908                 *tail = attr;
1909                 tail = &attr->a_next;
1910         }
1911
1912         return LDAP_SUCCESS;
1913 }
1914
1915 void
1916 avl_ber_bvfree( void *bv )
1917 {
1918         if( bv == NULL ) {
1919                 return;
1920         }
1921         if ( ((struct berval *)bv)->bv_val != NULL ) {
1922                 ber_memfree ( ((struct berval *)bv)->bv_val );
1923         }
1924         ber_memfree ( (char *) bv );
1925 }
1926
1927 static int
1928 cookie_callback(
1929         Operation* op,
1930         SlapReply* rs
1931 )
1932 {
1933         syncinfo_t *si = op->o_callback->sc_private;
1934         Attribute *a;
1935
1936         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1937
1938         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1939
1940         if ( a == NULL ) {
1941                 si->syncCookie = NULL;
1942         } else {
1943                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1944         }
1945         return LDAP_SUCCESS;
1946 }
1947
1948 static int
1949 dn_callback(
1950         Operation*      op,
1951         SlapReply*      rs
1952 )
1953 {
1954         syncinfo_t *si = op->o_callback->sc_private;
1955         
1956         if ( rs->sr_type == REP_SEARCH ) {
1957                 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1958         }
1959
1960         return LDAP_SUCCESS;
1961 }
1962
1963 static int
1964 nonpresent_callback(
1965         Operation*      op,
1966         SlapReply*      rs
1967 )
1968 {
1969         syncinfo_t *si = op->o_callback->sc_private;
1970         Attribute *a;
1971         int count = 0;
1972         struct berval* present_uuid = NULL;
1973         slap_callback cb;
1974         SlapReply       rs_cb = {REP_RESULT};
1975         struct nonpresent_entry *np_entry;
1976
1977         if ( rs->sr_type == REP_RESULT ) {
1978                 count = avl_free( si->presentlist, avl_ber_bvfree );
1979                 return LDAP_SUCCESS;
1980         } else if ( rs->sr_type == REP_SEARCH ) {
1981                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1982
1983                 if ( a == NULL )
1984                         return 0;
1985
1986                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1987
1988                 if ( present_uuid == NULL ) {
1989                         np_entry = (struct nonpresent_entry *)
1990                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1991                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1992                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1993                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1994                 } else {
1995                         avl_delete( &si->presentlist,
1996                                         &a->a_vals[0], syncuuid_cmp );
1997                 }
1998                 return LDAP_SUCCESS;
1999         } else {
2000                 return LDAP_SUCCESS;
2001         }
2002
2003 }
2004
2005 static int
2006 null_callback(
2007         Operation*      op,
2008         SlapReply*      rs
2009 )
2010 {
2011         if ( rs->sr_err != LDAP_SUCCESS &&
2012                  rs->sr_err != LDAP_REFERRAL &&
2013                  rs->sr_err != LDAP_ALREADY_EXISTS &&
2014                  rs->sr_err != LDAP_NO_SUCH_OBJECT &&
2015                  rs->sr_err != DB_NOTFOUND ) {
2016 #ifdef NEW_LOGGING
2017                 LDAP_LOG( OPERATION, ERR,
2018                         "null_callback : error code 0x%x\n",
2019                         rs->sr_err, 0, 0 );
2020 #else
2021                 Debug( LDAP_DEBUG_ANY,
2022                         "null_callback : error code 0x%x\n",
2023                         rs->sr_err, 0, 0 );
2024 #endif
2025         }
2026         return LDAP_SUCCESS;
2027 }
2028
2029
2030 char **
2031 str2clist( char **out, char *in, const char *brkstr )
2032 {
2033         char    *str;
2034         char    *s;
2035         char    *lasts;
2036         int     i, j;
2037         const char *text;
2038         char    **new;
2039
2040         /* find last element in list */
2041         for (i = 0; out && out[i]; i++);
2042         
2043         /* protect the input string from strtok */
2044         str = ch_strdup( in );
2045
2046         /* Count words in string */
2047         j=1;
2048         for ( s = str; *s; s++ ) {
2049                 if ( strchr( brkstr, *s ) != NULL ) {
2050                         j++;
2051                 }
2052         }
2053
2054         out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2055         new = out + i;
2056         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2057                 s != NULL;
2058                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2059         {
2060                 *new = ch_strdup( s );
2061                 new++;
2062         }
2063
2064         *new = NULL;
2065         free( str );
2066         return( out );
2067 }
2068
2069 #endif