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