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