]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
ca6d5c3b8eacfa118e9cfe6bb7cb50225af51756
[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                                         si->e = NULL;
996                                         return 0;
997                                 } else if ( rc == LDAP_REFERRAL ||
998                                                         rc == LDAP_NO_SUCH_OBJECT ) {
999                                         syncrepl_add_glue( si, ld, op, e,
1000                                                 modlist, syncstate,
1001                                                 syncUUID, syncCookie);
1002                                         si->e = NULL;
1003                                         return 0;
1004                                 } else {
1005 #ifdef NEW_LOGGING
1006                                         LDAP_LOG( OPERATION, ERR,
1007                                                 "be_modify failed (%d)\n",
1008                                                 rc, 0, 0 );
1009 #else
1010                                         Debug( LDAP_DEBUG_ANY,
1011                                                 "be_modify failed (%d)\n",
1012                                                 rc, 0, 0 );
1013 #endif
1014                                         si->e = NULL;
1015                                         return 1;
1016                                 }
1017                         } else {
1018                                 si->e = NULL;
1019                                 return 0;
1020                         }
1021                 } else {
1022 #ifdef NEW_LOGGING
1023                         LDAP_LOG( OPERATION, ERR,
1024                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1025 #else
1026                         Debug( LDAP_DEBUG_ANY,
1027                                 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1028 #endif
1029                         si->e = NULL;
1030                         return 1;
1031                 }
1032
1033         case LDAP_SYNC_DELETE :
1034                 /* Already deleted */
1035                 return 1;
1036
1037         default :
1038 #ifdef NEW_LOGGING
1039                 LDAP_LOG( OPERATION, ERR,
1040                         "unknown syncstate\n", 0, 0, 0 );
1041 #else
1042                 Debug( LDAP_DEBUG_ANY,
1043                         "unknown syncstate\n", 0, 0, 0 );
1044 #endif
1045                 return 1;
1046         }
1047 }
1048
1049 static void
1050 syncrepl_del_nonpresent(
1051         syncinfo_t *si,
1052         LDAP *ld,
1053         Operation *op
1054 )
1055 {
1056         Backend* be = op->o_bd;
1057         slap_callback   cb;
1058         struct berval   base_bv = {0, NULL};
1059         Filter *filter;
1060         SlapReply       rs = {REP_RESULT};
1061         struct berval   filterstr_bv = {0, NULL};
1062         struct nonpresent_entry *np_list, *np_prev;
1063
1064         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1065         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1066         ch_free( base_bv.bv_val );
1067
1068         filter = str2filter( si->filterstr );
1069
1070         cb.sc_response = nonpresent_callback;
1071         cb.sc_private = si;
1072
1073         op->o_callback = &cb;
1074         op->o_tag = LDAP_REQ_SEARCH;
1075         op->ors_scope = si->scope;
1076         op->ors_deref = LDAP_DEREF_NEVER;
1077         op->ors_slimit = 0;
1078         op->ors_tlimit = 0;
1079         op->ors_attrsonly = 0;
1080         op->ors_attrs = NULL;
1081         op->ors_filter = filter;
1082         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1083
1084         be->be_search( op, &rs );
1085
1086         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1087                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1088                 while ( np_list != NULL ) {
1089                         LDAP_LIST_REMOVE( np_list, np_link );
1090                         np_prev = np_list;
1091                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1092                         op->o_tag = LDAP_REQ_DELETE;
1093                         op->o_callback = &cb;
1094                         cb.sc_response = null_callback;
1095                         cb.sc_private = si;
1096                         op->o_req_dn = *np_prev->dn;
1097                         op->o_req_ndn = *np_prev->ndn;
1098                         op->o_bd->be_delete( op, &rs );
1099                         ber_bvfree( np_prev->dn );
1100                         ber_bvfree( np_prev->ndn );
1101                         op->o_req_dn.bv_val = NULL;
1102                         op->o_req_ndn.bv_val = NULL;
1103                         ch_free( np_prev );
1104                 }
1105         }
1106
1107         if ( op->o_req_dn.bv_val )
1108                 ch_free( op->o_req_dn.bv_val );
1109         if ( op->o_req_ndn.bv_val )
1110                 ch_free( op->o_req_ndn.bv_val );
1111         filter_free( op->ors_filter );
1112         ch_free( op->ors_filterstr.bv_val );
1113
1114         return;
1115 }
1116
1117
1118 static void
1119 syncrepl_add_glue(
1120         syncinfo_t *si,
1121         LDAP *ld,
1122         Operation* op,
1123         Entry *e,
1124         Modifications* modlist,
1125         int syncstate,
1126         struct berval* syncUUID,
1127         struct berval* syncCookie
1128 )
1129 {
1130         Backend *be = op->o_bd;
1131         struct berval   uuid_bv = {0, NULL};
1132         slap_callback cb;
1133         Attribute       *a;
1134         int     rc;
1135         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1136         int levels = 0;
1137         int i, j, k;
1138         struct berval dn = {0, NULL};
1139         struct berval pdn = {0, NULL};
1140         struct berval ndn = {0, NULL};
1141         struct berval rdn = {0, NULL};
1142         Entry   *glue;
1143         SlapReply       rs = {REP_RESULT};
1144         Connection *conn = op->o_conn;
1145         char* ptr;
1146
1147         op->o_tag = LDAP_REQ_ADD;
1148         op->o_callback = &cb;
1149         cb.sc_response = null_callback;
1150         cb.sc_private = si;
1151
1152         ber_dupbv( &dn, &e->e_nname );
1153         ber_dupbv( &pdn, &e->e_nname );
1154
1155         ptr = dn.bv_val;
1156         while ( !be_issuffix ( be, &pdn )) {
1157                 dnParent( &dn, &pdn );
1158                 dn.bv_val = pdn.bv_val;
1159                 dn.bv_len = pdn.bv_len;
1160                 levels++;
1161         }
1162         ch_free( ptr );
1163
1164         for ( i = 0; i <= levels; i++ ) {
1165                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1166                 glue->e_private = NULL;
1167                 ber_dupbv( &dn, &e->e_nname );
1168                 j = levels - i;
1169
1170                 ptr = dn.bv_val;
1171                 for ( k = 0; k < j; k++ ) {
1172                         dnParent( &dn, &pdn );
1173                         dn.bv_val = pdn.bv_val;
1174                         dn.bv_len = pdn.bv_len;
1175                 }
1176
1177                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1178                 ber_dupbv( &glue->e_name, &pdn );
1179                 ber_dupbv( &glue->e_nname, &ndn );
1180                 ch_free( ptr );
1181                 ch_free( pdn.bv_val );
1182                 ch_free( ndn.bv_val );
1183
1184                 a = ch_calloc( 1, sizeof( Attribute ));
1185                 a->a_desc = slap_schema.si_ad_objectClass;
1186
1187                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1188                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1189                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1190                 a->a_vals[2].bv_len = 0;
1191                 a->a_vals[2].bv_val = NULL;
1192
1193                 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1194                 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1195                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1196                 a->a_nvals[2].bv_len = 0;
1197                 a->a_nvals[2].bv_val = NULL;
1198
1199                 a->a_next = glue->e_attrs;
1200                 glue->e_attrs = a;
1201
1202                 a = ch_calloc( 1, sizeof( Attribute ));
1203                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1204
1205                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1206                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1207                 a->a_vals[1].bv_len = 0;
1208                 a->a_vals[1].bv_val = NULL;
1209
1210                 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1211                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1212                 a->a_nvals[1].bv_len = 0;
1213                 a->a_nvals[1].bv_val = NULL;
1214
1215                 a->a_next = glue->e_attrs;
1216                 glue->e_attrs = a;
1217
1218                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1219                         op->o_req_dn = e->e_name;
1220                         op->o_req_ndn = e->e_nname;
1221                         op->ora_e = e;
1222                         rc = be->be_add ( op, &rs );
1223                         if ( rc == LDAP_SUCCESS )
1224                                 be_entry_release_w( op, e );
1225                         else 
1226                                 entry_free( e );
1227                         entry_free( glue );
1228                 } else {
1229                         op->o_req_dn = glue->e_name;
1230                         op->o_req_ndn = glue->e_nname;
1231                         op->ora_e = glue;
1232                         rc = be->be_add ( op, &rs );
1233                         if ( rc == LDAP_SUCCESS ) {
1234                                 be_entry_release_w( op, glue );
1235                         } else {
1236                         /* incl. ALREADY EXIST */
1237                                 entry_free( glue );
1238                         }
1239                 }
1240         }
1241
1242         return;
1243 }
1244
1245 void
1246 syncrepl_updateCookie(
1247         syncinfo_t *si,
1248         LDAP *ld,
1249         Operation *op,
1250         struct berval *pdn,
1251         struct berval *syncCookie
1252 )
1253 {
1254         Backend *be = op->o_bd;
1255         Modifications *ml;
1256         Modifications *mlnext;
1257         Modifications *mod;
1258         Modifications *modlist;
1259         Modifications **modtail = &modlist;
1260
1261         struct berval* ocbva = NULL;
1262         struct berval* cnbva = NULL;
1263         struct berval* ssbva = NULL;
1264         struct berval* scbva = NULL;
1265
1266         char substr[64];
1267         char rdnstr[67];
1268         const char      *text;
1269         char txtbuf[SLAP_TEXT_BUFLEN];
1270         size_t textlen = sizeof txtbuf;
1271
1272         Entry* e;
1273         int rc;
1274
1275         struct berval sub_bv = { 0, NULL };
1276         struct berval psubrdn = { 0, NULL };
1277         
1278         slap_callback cb;
1279         SlapReply       rs = {REP_RESULT};
1280
1281         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1282         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1283         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1284         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1285
1286         /* update in memory cookie */
1287         if ( si->syncCookie != NULL ) {
1288                 ber_bvfree( si->syncCookie );
1289         }
1290         si->syncCookie = ber_dupbv( NULL, syncCookie );
1291         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1292         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1293         ber_str2bv( "syncConsumerSubentry",
1294                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1295         ocbva[3].bv_len = 0;
1296         ocbva[3].bv_val = NULL;
1297
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( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1303         mod->sml_bvalues = ocbva;
1304         mod->sml_nvalues = ocbva;
1305         *modtail = mod;
1306         modtail = &mod->sml_next;
1307
1308         sprintf( substr, "syncrepl%d", si->id );
1309         sprintf( rdnstr, "cn=%s", substr );
1310         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1311         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1312         cnbva[1].bv_len = 0;
1313         cnbva[1].bv_val = NULL;
1314         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1315         mod->sml_op = LDAP_MOD_REPLACE;
1316         mod->sml_next = NULL;
1317         mod->sml_desc = NULL;
1318         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1319         mod->sml_bvalues = cnbva;
1320         mod->sml_nvalues = cnbva;
1321         *modtail = mod;
1322         modtail = &mod->sml_next;
1323
1324         ber_dupbv( &scbva[0], si->syncCookie );
1325         scbva[1].bv_len = 0;
1326         scbva[1].bv_val = NULL;
1327         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1328         mod->sml_op = LDAP_MOD_REPLACE;
1329         mod->sml_next = NULL;
1330         mod->sml_desc = NULL;
1331         ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1332                                                 1, &mod->sml_type );
1333         mod->sml_bvalues = scbva;
1334         mod->sml_nvalues = scbva;
1335         *modtail = mod;
1336         modtail = &mod->sml_next;
1337
1338         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1339         ssbva[1].bv_len = 0;
1340         ssbva[1].bv_val = NULL;
1341         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1342         mod->sml_op = LDAP_MOD_REPLACE;
1343         mod->sml_next = NULL;
1344         mod->sml_desc = NULL;
1345         ber_str2bv( "subtreeSpecification",
1346                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1347         mod->sml_bvalues = ssbva;
1348         mod->sml_nvalues = ssbva;
1349         *modtail = mod;
1350         modtail = &mod->sml_next;
1351
1352         rc = slap_mods_check_syncrepl( si, op, &modlist,
1353                                                                    &text, txtbuf, textlen, NULL );
1354
1355         if ( rc != LDAP_SUCCESS ) {
1356 #ifdef NEW_LOGGING
1357                 LDAP_LOG( OPERATION, ERR,
1358                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1359 #else
1360                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1361                          text, 0, 0 );
1362 #endif
1363         }
1364
1365         op->o_tag = LDAP_REQ_ADD;
1366         rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1367                                                                          &text,txtbuf, textlen );
1368
1369         if( rc != LDAP_SUCCESS ) {
1370 #ifdef NEW_LOGGING
1371                 LDAP_LOG( OPERATION, ERR,
1372                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1373 #else
1374                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1375                          text, 0, 0 );
1376 #endif
1377         }
1378
1379         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1380
1381         build_new_dn( &sub_bv, pdn, &psubrdn );
1382         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1383         ch_free( sub_bv.bv_val );
1384         ch_free( psubrdn.bv_val );
1385
1386         e->e_attrs = NULL;
1387
1388         rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1389
1390         if( rc != LDAP_SUCCESS ) {
1391 #ifdef NEW_LOGGING
1392                 LDAP_LOG( OPERATION, ERR,
1393                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1394 #else
1395                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1396                          text, 0, 0 );
1397 #endif
1398         }
1399
1400         cb.sc_response = null_callback;
1401         cb.sc_private = si;
1402
1403         op->o_callback = &cb;
1404         op->o_req_dn = e->e_name;
1405         op->o_req_ndn = e->e_nname;
1406
1407         /* update persistent cookie */
1408 update_cookie_retry:
1409         op->o_tag = LDAP_REQ_MODIFY;
1410         op->orm_modlist = modlist;
1411         rc = be->be_modify( op, &rs );
1412         if ( rc != LDAP_SUCCESS ) {
1413                 if ( rc == LDAP_REFERRAL ||
1414                          rc == LDAP_NO_SUCH_OBJECT ) {
1415                         op->o_tag = LDAP_REQ_ADD;
1416                         op->ora_e = e;
1417                         rc = be->be_add( op, &rs );
1418                         if ( rc != LDAP_SUCCESS ) {
1419                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1420                                         goto update_cookie_retry;
1421                                 } else if ( rc == LDAP_REFERRAL ||
1422                                                         rc == LDAP_NO_SUCH_OBJECT ) {
1423 #ifdef NEW_LOGGING
1424                                         LDAP_LOG( OPERATION, ERR,
1425                                                 "cookie will be non-persistent\n",
1426                                                 0, 0, 0 );
1427 #else
1428                                         Debug( LDAP_DEBUG_ANY,
1429                                                 "cookie will be non-persistent\n",
1430                                                 0, 0, 0 );
1431 #endif
1432                                 } else {
1433 #ifdef NEW_LOGGING
1434                                         LDAP_LOG( OPERATION, ERR,
1435                                                 "be_add failed (%d)\n",
1436                                                 rc, 0, 0 );
1437 #else
1438                                         Debug( LDAP_DEBUG_ANY,
1439                                                 "be_add failed (%d)\n",
1440                                                 rc, 0, 0 );
1441 #endif
1442                                 }
1443                         } else {
1444                                 goto done;
1445                         }
1446                 } else {
1447 #ifdef NEW_LOGGING
1448                         LDAP_LOG( OPERATION, ERR,
1449                                 "be_modify failed (%d)\n", rc, 0, 0 );
1450 #else
1451                         Debug( LDAP_DEBUG_ANY,
1452                                 "be_modify failed (%d)\n", rc, 0, 0 );
1453 #endif
1454                 }
1455         }
1456
1457         if ( e != NULL )
1458                 entry_free( e );
1459
1460 done :
1461
1462         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1463                 mlnext = ml->sml_next;
1464                 free( ml );
1465         }
1466
1467         return;
1468 }
1469
1470
1471 static
1472 int slap_mods_check_syncrepl(
1473         syncinfo_t* si,
1474         Operation *op,
1475         Modifications **mlp,
1476         const char **text,
1477         char *textbuf,
1478         size_t textlen,
1479         void *ctx )
1480 {
1481         int rc;
1482         Backend *be = op->o_bd;
1483         AttributeDescription** descs;
1484         int i;
1485         Modifications *prevml = NULL;
1486         Modifications *nextml = NULL;
1487         Modifications *ml = *mlp;
1488
1489         while ( ml != NULL ) {
1490                 AttributeDescription *ad = NULL;
1491
1492                 /* convert to attribute description */
1493                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1494
1495                 if( rc != LDAP_SUCCESS ) {
1496                         snprintf( textbuf, textlen, "%s: %s",
1497                                                 ml->sml_type.bv_val, *text );
1498                         *text = textbuf;
1499                         return rc;
1500                 }
1501
1502                 ad = ml->sml_desc;
1503
1504                 if ( si->lastmod == LASTMOD_REQ ) {
1505                         descs = del_descs_lastmod;
1506                 } else {
1507                         descs = del_descs;
1508                 }
1509
1510                 for ( i = 0; descs[i] != NULL; i++ ) {
1511                         if ( ad == descs[i] ) {
1512                                 if ( prevml == NULL ) {
1513                                         mlp = &ml->sml_next;
1514                                         prevml = NULL;
1515                                 } else {
1516                                         prevml->sml_next = ml->sml_next;
1517                                 }
1518                                 slap_mod_free( &ml->sml_mod, 0 );
1519                                 nextml = ml->sml_next;
1520                                 free( ml );
1521                                 ml = nextml;
1522                                 continue;
1523                         }
1524                 }
1525
1526                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1527                                 && !slap_ad_is_binary( ad )) {
1528                         /* attribute requires binary transfer */
1529                         snprintf( textbuf, textlen,
1530                                         "%s: requires ;binary transfer",
1531                                         ml->sml_type.bv_val );
1532                         *text = textbuf;
1533                         return LDAP_UNDEFINED_TYPE;
1534                 }
1535
1536                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1537                                         && slap_ad_is_binary( ad )) {
1538                         /* attribute requires binary transfer */
1539                         snprintf( textbuf, textlen,
1540                                         "%s: disallows ;binary transfer",
1541                                         ml->sml_type.bv_val );
1542                         *text = textbuf;
1543                         return LDAP_UNDEFINED_TYPE;
1544                 }
1545
1546                 if( slap_ad_is_tag_range( ad )) {
1547                         /* attribute requires binary transfer */
1548                         snprintf( textbuf, textlen,
1549                                         "%s: inappropriate use of tag range option",
1550                                         ml->sml_type.bv_val );
1551                         *text = textbuf;
1552                         return LDAP_UNDEFINED_TYPE;
1553                 }
1554
1555                 if ( is_at_obsolete( ad->ad_type ) &&
1556                                 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1557                         /*
1558                          * attribute is obsolete,
1559                          * only allow replace/delete with no values
1560                          */
1561                         snprintf( textbuf, textlen,
1562                                         "%s: attribute is obsolete",
1563                                         ml->sml_type.bv_val );
1564                         *text = textbuf;
1565                         return LDAP_CONSTRAINT_VIOLATION;
1566                 }
1567
1568                 /*
1569                  * check values
1570                  */
1571                 if( ml->sml_values != NULL ) {
1572                         ber_len_t nvals;
1573                         slap_syntax_validate_func *validate =
1574                         ad->ad_type->sat_syntax->ssyn_validate;
1575                         slap_syntax_transform_func *pretty =
1576                         ad->ad_type->sat_syntax->ssyn_pretty;
1577
1578                         if( !pretty && !validate ) {
1579                                 *text = "no validator for syntax";
1580                                 snprintf( textbuf, textlen,
1581                                                 "%s: no validator for syntax %s",
1582                                                 ml->sml_type.bv_val,
1583                                                 ad->ad_type->sat_syntax->ssyn_oid );
1584                                 *text = textbuf;
1585                                 return LDAP_INVALID_SYNTAX;
1586                         }
1587
1588                         /*
1589                          * check that each value is valid per syntax
1590                          * and pretty if appropriate
1591                          */
1592                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1593                                 struct berval pval = {0, NULL};
1594                                 if( pretty ) {
1595                                         rc = pretty( ad->ad_type->sat_syntax,
1596                                                         &ml->sml_values[nvals], &pval, ctx );
1597                                 } else {
1598                                         rc = validate( ad->ad_type->sat_syntax,
1599                                                         &ml->sml_values[nvals] );
1600                                 }
1601
1602                                 if( rc != 0 ) {
1603                                         snprintf( textbuf, textlen,
1604                                                         "%s: value #%ld invalid per syntax",
1605                                                         ml->sml_type.bv_val, (long) nvals );
1606                                         *text = textbuf;
1607                                         return LDAP_INVALID_SYNTAX;
1608                                 }
1609
1610                                 if( pretty ) {
1611                                         ber_memfree( ml->sml_values[nvals].bv_val );
1612                                         ml->sml_values[nvals] = pval;
1613                                 }
1614                         }
1615
1616                         /*
1617                          * a rough single value check... an additional check is needed
1618                          * to catch add of single value to existing single valued attribute
1619                          */
1620                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1621                                         && nvals > 1 && is_at_single_value( ad->ad_type )) {
1622                                 snprintf( textbuf, textlen,
1623                                                 "%s: multiple values provided",
1624                                                 ml->sml_type.bv_val );
1625                                 *text = textbuf;
1626                                 return LDAP_CONSTRAINT_VIOLATION;
1627                         }
1628
1629                         if( nvals && ad->ad_type->sat_equality &&
1630                                         ad->ad_type->sat_equality->smr_normalize ) {
1631                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1632                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1633                                         rc = ad->ad_type->sat_equality->smr_normalize( 0,
1634                                                         ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1635                                                         &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1636                                         if( rc ) {
1637 #ifdef NEW_LOGGING
1638                                                 LDAP_LOG( OPERATION, DETAIL1,
1639                                                                 "str2entry:  NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1640 #else
1641                                                 Debug( LDAP_DEBUG_ANY,
1642                                                                 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1643 #endif
1644                                                 snprintf( textbuf, textlen,
1645                                                                 "%s: value #%ld normalization failed",
1646                                                                 ml->sml_type.bv_val, (long) nvals );
1647                                                 *text = textbuf;
1648                                                 return rc;
1649                                         }
1650                                 }
1651                                 ml->sml_nvalues[nvals].bv_val = NULL;
1652                                 ml->sml_nvalues[nvals].bv_len = 0;
1653                         }
1654                 }
1655                 prevml = ml;
1656                 ml = ml->sml_next;
1657         }
1658
1659         return LDAP_SUCCESS;
1660 }
1661
1662 static
1663 int slap_mods_opattrs_syncrepl(
1664         syncinfo_t *si,
1665         Operation *op,
1666         Modifications *mods,
1667         Modifications **modtail,
1668         const char **text,
1669         char *textbuf, size_t textlen )
1670 {
1671         struct berval name = {0, NULL};
1672         struct berval timestamp = {0, NULL};
1673         struct berval csn = {0, NULL};
1674         struct berval nname = {0, NULL};
1675         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1676         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1677         Modifications *mod;
1678         Backend *be = op->o_bd;
1679
1680         int mop = LDAP_MOD_REPLACE;
1681
1682         assert( modtail != NULL );
1683         assert( *modtail == NULL );
1684
1685         if( si->lastmod == LASTMOD_GEN ) {
1686                 struct tm *ltm;
1687                 time_t now = slap_get_time();
1688
1689                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1690                 ltm = gmtime( &now );
1691                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1692
1693                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1694                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1695                 csn.bv_val = csnbuf;
1696
1697                 timestamp.bv_val = timebuf;
1698                 timestamp.bv_len = strlen(timebuf);
1699
1700                 if( op->o_dn.bv_len == 0 ) {
1701                         name.bv_val = SLAPD_ANONYMOUS;
1702                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1703                         nname = name;
1704                 } else {
1705                         name = op->o_dn;
1706                         nname = op->o_ndn;
1707                 }
1708         }
1709
1710         if( op->o_tag == LDAP_REQ_ADD ) {
1711                 struct berval tmpval = {0, NULL};
1712
1713                 if( global_schemacheck ) {
1714                         int rc = mods_structural_class( mods, &tmpval,
1715                                                                         text, textbuf, textlen );
1716                         if( rc != LDAP_SUCCESS ) {
1717                                 return rc;
1718                         }
1719
1720                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1721                         mod->sml_op = mop;
1722                         mod->sml_type.bv_val = NULL;
1723                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1724                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1725                         ber_dupbv( &mod->sml_values[0], &tmpval );
1726                         mod->sml_values[1].bv_len = 0;
1727                         mod->sml_values[1].bv_val = NULL;
1728                         assert( mod->sml_values[0].bv_val );
1729                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1730                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1731                         mod->sml_nvalues[1].bv_len = 0;
1732                         mod->sml_nvalues[1].bv_val = NULL;
1733                         assert( mod->sml_nvalues[0].bv_val );
1734                         *modtail = mod;
1735                         modtail = &mod->sml_next;
1736                 }
1737
1738                 if( si->lastmod == LASTMOD_GEN ) {
1739                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1740                         mod->sml_op = mop;
1741                         mod->sml_type.bv_val = NULL;
1742                         mod->sml_desc = slap_schema.si_ad_creatorsName;
1743                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1744                         ber_dupbv( &mod->sml_values[0], &name );
1745                         mod->sml_values[1].bv_len = 0;
1746                         mod->sml_values[1].bv_val = NULL;
1747                         assert( mod->sml_values[0].bv_val );
1748                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1749                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1750                         mod->sml_nvalues[1].bv_len = 0;
1751                         mod->sml_nvalues[1].bv_val = NULL;
1752                         assert( mod->sml_nvalues[0].bv_val );
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_createTimestamp;
1760                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1761                         ber_dupbv( &mod->sml_values[0], &timestamp );
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 = NULL;
1766                         *modtail = mod;
1767                         modtail = &mod->sml_next;
1768                 }
1769         }
1770
1771         if( si->lastmod == LASTMOD_GEN ) {
1772                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1773                 mod->sml_op = mop;
1774                 mod->sml_type.bv_val = NULL;
1775                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1776                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1777                 ber_dupbv( &mod->sml_values[0], &csn );
1778                 mod->sml_values[1].bv_len = 0;
1779                 mod->sml_values[1].bv_val = NULL;
1780                 assert( mod->sml_values[0].bv_val );
1781                 mod->sml_nvalues = NULL;
1782                 *modtail = mod;
1783                 modtail = &mod->sml_next;
1784
1785                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1786                 mod->sml_op = mop;
1787                 mod->sml_type.bv_val = NULL;
1788                 mod->sml_desc = slap_schema.si_ad_modifiersName;
1789                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1790                 ber_dupbv( &mod->sml_values[0], &name );
1791                 mod->sml_values[1].bv_len = 0;
1792                 mod->sml_values[1].bv_val = NULL;
1793                 assert( mod->sml_values[0].bv_val );
1794                 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1795                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1796                 mod->sml_nvalues[1].bv_len = 0;
1797                 mod->sml_nvalues[1].bv_val = NULL;
1798                 assert( mod->sml_nvalues[0].bv_val );
1799                 *modtail = mod;
1800                 modtail = &mod->sml_next;
1801
1802                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1803                 mod->sml_op = mop;
1804                 mod->sml_type.bv_val = NULL;
1805                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1806                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1807                 ber_dupbv( &mod->sml_values[0], &timestamp );
1808                 mod->sml_values[1].bv_len = 0;
1809                 mod->sml_values[1].bv_val = NULL;
1810                 assert( mod->sml_values[0].bv_val );
1811                 mod->sml_nvalues = NULL;
1812                 *modtail = mod;
1813                 modtail = &mod->sml_next;
1814         }
1815
1816         *modtail = NULL;
1817         return LDAP_SUCCESS;
1818 }
1819
1820
1821 static
1822 int slap_mods2entry_syncrepl(
1823         syncinfo_t *si,
1824         Modifications *mods,
1825         Entry **e,
1826         int repl_user,
1827         const char **text,
1828         char *textbuf, size_t textlen )
1829 {
1830         Attribute **tail = &(*e)->e_attrs;
1831         assert( *tail == NULL );
1832
1833         *text = textbuf;
1834
1835         for( ; mods != NULL; mods = mods->sml_next ) {
1836                 Attribute *attr;
1837
1838                 assert( mods->sml_desc != NULL );
1839
1840                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1841
1842                 if( attr != NULL ) {
1843 #define SLURPD_FRIENDLY
1844 #ifdef SLURPD_FRIENDLY
1845                         ber_len_t i,j;
1846
1847                         if( !repl_user ) {
1848                                 snprintf( textbuf, textlen,
1849                                         "attribute '%s' provided more than once",
1850                                         mods->sml_desc->ad_cname.bv_val );
1851                                 return LDAP_TYPE_OR_VALUE_EXISTS;
1852                         }
1853
1854                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
1855                                 /* count them */
1856                         }
1857                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
1858                                 /* count them */
1859                         }
1860                         j++;    /* NULL */
1861                         
1862                         attr->a_vals = ch_realloc( attr->a_vals,
1863                                 sizeof( struct berval ) * (i+j) );
1864
1865                         /* should check for duplicates */
1866
1867                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1868                                 sizeof( struct berval ) * j );
1869
1870                         if( attr->a_nvals ) {
1871                                 attr->a_nvals = ch_realloc( attr->a_nvals,
1872                                         sizeof( struct berval ) * (i+j) );
1873
1874                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1875                                         sizeof( struct berval ) * j );
1876
1877                                 /* trim the mods array */
1878                                 ch_free( mods->sml_nvalues );
1879                                 mods->sml_nvalues = NULL;
1880                         }
1881
1882                         continue;
1883 #else
1884                         snprintf( textbuf, textlen,
1885                                 "attribute '%s' provided more than once",
1886                                 mods->sml_desc->ad_cname.bv_val );
1887                         return LDAP_TYPE_OR_VALUE_EXISTS;
1888 #endif
1889                 }
1890
1891                 if( mods->sml_values[1].bv_val != NULL ) {
1892                         /* check for duplicates */
1893                         int             i, j;
1894                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1895
1896                         /* check if the values we're adding already exist */
1897                         if( mr == NULL || !mr->smr_match ) {
1898                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1899                                         /* test asserted values against themselves */
1900                                         for( j = 0; j < i; j++ ) {
1901                                                 if ( bvmatch( &mods->sml_bvalues[i],
1902                                                         &mods->sml_bvalues[j] ) ) {
1903                                                         /* value exists already */
1904                                                         snprintf( textbuf, textlen,
1905                                                                 "%s: value #%d provided more than once",
1906                                                                 mods->sml_desc->ad_cname.bv_val, j );
1907                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
1908                                                 }
1909                                         }
1910                                 }
1911
1912                         } else {
1913                                 int             rc;
1914                                 const char      *text = NULL;
1915                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
1916                                 
1917                                 rc = modify_check_duplicates( mods->sml_desc, mr,
1918                                                 NULL, mods->sml_bvalues, 0,
1919                                                 &text, textbuf, sizeof( textbuf ) );
1920
1921                                 if ( rc != LDAP_SUCCESS ) {
1922                                         return rc;
1923                                 }
1924                         }
1925                 }
1926
1927                 attr = ch_calloc( 1, sizeof(Attribute) );
1928
1929                 /* move ad to attr structure */
1930                 attr->a_desc = mods->sml_desc;
1931
1932                 /* move values to attr structure */
1933                 /*      should check for duplicates */
1934                 attr->a_vals = mods->sml_values;
1935
1936                 attr->a_nvals = mods->sml_nvalues;
1937
1938                 *tail = attr;
1939                 tail = &attr->a_next;
1940         }
1941
1942         return LDAP_SUCCESS;
1943 }
1944
1945 void
1946 avl_ber_bvfree( void *bv )
1947 {
1948         if( bv == NULL ) {
1949                 return;
1950         }
1951         if ( ((struct berval *)bv)->bv_val != NULL ) {
1952                 ber_memfree ( ((struct berval *)bv)->bv_val );
1953         }
1954         ber_memfree ( (char *) bv );
1955 }
1956
1957 static int
1958 cookie_callback(
1959         Operation* op,
1960         SlapReply* rs
1961 )
1962 {
1963         syncinfo_t *si = op->o_callback->sc_private;
1964         Attribute *a;
1965
1966         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1967
1968         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1969
1970         if ( a == NULL ) {
1971                 si->syncCookie = NULL;
1972         } else {
1973                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1974         }
1975         return LDAP_SUCCESS;
1976 }
1977
1978 static int
1979 dn_callback(
1980         Operation*      op,
1981         SlapReply*      rs
1982 )
1983 {
1984         syncinfo_t *si = op->o_callback->sc_private;
1985         
1986         if ( rs->sr_type == REP_SEARCH ) {
1987                 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1988         }
1989
1990         return LDAP_SUCCESS;
1991 }
1992
1993 static int
1994 nonpresent_callback(
1995         Operation*      op,
1996         SlapReply*      rs
1997 )
1998 {
1999         syncinfo_t *si = op->o_callback->sc_private;
2000         Attribute *a;
2001         int count = 0;
2002         struct berval* present_uuid = NULL;
2003         slap_callback cb;
2004         SlapReply       rs_cb = {REP_RESULT};
2005         struct nonpresent_entry *np_entry;
2006
2007         if ( rs->sr_type == REP_RESULT ) {
2008                 count = avl_free( si->presentlist, avl_ber_bvfree );
2009                 return LDAP_SUCCESS;
2010         } else if ( rs->sr_type == REP_SEARCH ) {
2011                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2012
2013                 if ( a == NULL )
2014                         return 0;
2015
2016                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
2017
2018                 if ( present_uuid == NULL ) {
2019                         np_entry = (struct nonpresent_entry *)
2020                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2021                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
2022                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2023                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2024                 } else {
2025                         avl_delete( &si->presentlist,
2026                                         &a->a_vals[0], syncuuid_cmp );
2027                 }
2028                 return LDAP_SUCCESS;
2029         } else {
2030                 return LDAP_SUCCESS;
2031         }
2032
2033 }
2034
2035 static int
2036 null_callback(
2037         Operation*      op,
2038         SlapReply*      rs
2039 )
2040 {
2041         syncinfo_t *si = op->o_callback->sc_private;
2042
2043         if ( rs->sr_err != LDAP_SUCCESS &&
2044                  rs->sr_err != LDAP_REFERRAL &&
2045                  rs->sr_err != LDAP_ALREADY_EXISTS &&
2046                  rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2047 #ifdef NEW_LOGGING
2048                 LDAP_LOG( OPERATION, ERR,
2049                         "null_callback : error code 0x%x\n",
2050                         rs->sr_err, 0, 0 );
2051 #else
2052                 Debug( LDAP_DEBUG_ANY,
2053                         "null_callback : error code 0x%x\n",
2054                         rs->sr_err, 0, 0 );
2055 #endif
2056         }
2057         return LDAP_SUCCESS;
2058 }
2059
2060
2061 char **
2062 str2clist( char **out, char *in, const char *brkstr )
2063 {
2064         char    *str;
2065         char    *s;
2066         char    *lasts;
2067         int     i, j;
2068         const char *text;
2069         char    **new;
2070
2071         /* find last element in list */
2072         for (i = 0; out && out[i]; i++);
2073         
2074         /* protect the input string from strtok */
2075         str = ch_strdup( in );
2076
2077         /* Count words in string */
2078         j=1;
2079         for ( s = str; *s; s++ ) {
2080                 if ( strchr( brkstr, *s ) != NULL ) {
2081                         j++;
2082                 }
2083         }
2084
2085         out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2086         new = out + i;
2087         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2088                 s != NULL;
2089                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2090         {
2091                 *new = ch_strdup( s );
2092                 new++;
2093         }
2094
2095         *new = NULL;
2096         free( str );
2097         return( out );
2098 }
2099
2100 #endif