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