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