]> git.sur5r.net Git - openldap/blob - servers/slapd/syncrepl.c
a89fe518cf96c3f94c73a4855cdf9a1a1cea0d5c
[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 = be->be_rootdn;
324         op.o_ndn = be->be_rootndn;
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++ ] = ( char * ) strndup( descs[i]->ad_cname.bv_val,
463                                                        descs[i]->ad_cname.bv_len );
464                 si->attrs[ n ] = NULL;
465         }
466
467         /* Send LDAP SYNC search */
468
469         rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
470                                 si->attrs, si->attrsonly, NULL, NULL,
471                                 NULL, -1, &msgid );
472
473         if( rc != LDAP_SUCCESS ) {
474                 fprintf( stderr, "syncrepl: ldap_search_ext (%d)\n",
475                                         ldap_err2string( rc ), rc );
476                 return NULL;
477         }
478
479         while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
480
481                 for ( msg = ldap_first_message( ld, res );
482                       msg != NULL;
483                       msg = ldap_next_message( ld, msg ) )
484                 {
485                         switch( ldap_msgtype( msg ) ) {
486                         case LDAP_RES_SEARCH_ENTRY:
487                                 entry = syncrepl_message_to_entry( ld, &op, msg,
488                                         &modlist, &syncstate, &syncUUID, &syncCookie );
489                                 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
490                                                 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
491                                 if ( syncCookie.bv_len ) {
492                                         syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
493                                 }
494                                 if ( rc_efree )
495                                         entry_free( entry );
496                                 for ( ml = modlist; ml != NULL; ml = mlnext ) {
497                                         mlnext = ml->sml_next;
498                                         free( ml );
499                                 }
500                                 break;
501
502                         case LDAP_RES_SEARCH_REFERENCE:
503 #ifdef NEW_LOGGING
504                                 LDAP_LOG( OPERATION, ERR,
505                                         "do_syncrepl : reference received\n", 0, 0, 0 );
506 #else
507                                 Debug( LDAP_DEBUG_ANY,
508                                         "do_syncrepl : reference received\n", 0, 0, 0 );
509 #endif
510                                 break;
511
512                         case LDAP_RES_SEARCH_RESULT:
513                                 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
514                                 if ( rctrls ) {
515                                         rctrlp = *rctrls;
516                                         ctrl_ber = ber_alloc_t( LBER_USE_DER );
517                                         ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
518                                         ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
519                                         ber_reset( ctrl_ber, 1 );
520
521                                         ber_scanf( ctrl_ber, "{" );
522                                         if ( ber_peek_tag( ctrl_ber, &len )
523                                                 == LDAP_SYNC_TAG_COOKIE ) {
524                                                 ber_scanf( ctrl_ber, "o", &syncCookie );
525                                         }
526                                 }
527                                 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
528                                         if ( cancel_response ) {
529                                                 if ( syncCookie.bv_len ) {
530                                                         ber_bvfree( si->syncCookie );
531                                                         si->syncCookie = ber_dupbv( NULL, &syncCookie );
532                                                 }
533                                                 if ( ctrl_ber )
534                                                         ber_free( ctrl_ber, 1 );
535                                                 goto done;
536                                         }
537                                         else {
538                                                 if ( ctrl_ber )
539                                                         ber_free( ctrl_ber, 1 );
540                                                 break;
541                                         }
542                                 } else {
543                                         if ( syncCookie.bv_len ) {
544                                                 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
545                                         }
546                                         syncrepl_del_nonpresent( ld, &op );
547                                         if ( ctrl_ber )
548                                                 ber_free( ctrl_ber, 1 );
549                                         goto done;
550                                 }
551                                 break;
552
553                         case LDAP_RES_INTERMEDIATE_RESP:
554                                 ldap_parse_intermediate_resp_result( ld, msg,
555                                                 &retoid, &retdata, 0 );
556                                 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
557                                         sync_info_arrived = 1;
558                                         res_ber = ber_init( retdata );
559                                         ber_scanf( res_ber, "{e", &syncstate );
560
561                                         if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
562                                                 syncrepl_del_nonpresent( ld, &op );
563                                         } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
564 #ifdef NEW_LOGGING
565                                                 LDAP_LOG( OPERATION, ERR,
566                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
567 #else
568                                                 Debug( LDAP_DEBUG_ANY,
569                                                         "do_syncrepl : unknown sync info\n", 0, 0, 0 );
570 #endif
571                                         }
572
573                                         if ( ber_peek_tag( res_ber, &len )
574                                                                 == LDAP_SYNC_TAG_COOKIE ) {
575                                                 ber_scanf( res_ber, "o}", &syncCookie );
576                                                 if ( syncCookie.bv_len ) {
577                                                         ber_bvfree( si->syncCookie );
578                                                         si->syncCookie = ber_dupbv( NULL, &syncCookie );
579                                                 }
580                                         } else {
581                                                 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
582 #ifdef NEW_LOGGING
583                                                         LDAP_LOG( OPERATION, ERR,
584                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
585 #else
586                                                         Debug( LDAP_DEBUG_ANY,
587                                                                 "do_syncrepl : cookie required\n", 0, 0, 0 );
588 #endif
589                                                 }
590                                         }
591
592                                         ldap_memfree( retoid );
593                                         ber_bvfree( retdata );
594                                         ber_free( res_ber, 1 );
595                                         break;
596                                 } else {
597 #ifdef NEW_LOGGING
598                                         LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
599                                                 " unknown intermediate "
600                                                 "response\n", 0, 0, 0 );
601 #else
602                                         Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
603                                                 "unknown intermediate "
604                                                 "response\n", 0, 0, 0 );
605 #endif
606                                         ldap_memfree( retoid );
607                                         ber_bvfree( retdata );
608                                         break;
609                                 }
610                                 break;
611                         default:
612 #ifdef NEW_LOGGING
613                                 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
614                                         "unknown message\n", 0, 0, 0 );
615 #else
616                                 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
617                                         "unknown message\n", 0, 0, 0 );
618 #endif
619                                 break;
620
621                         }
622                 }
623                 ldap_msgfree( res );
624         }
625
626         if ( rc == -1 ) {
627 #ifdef NEW_LOGGING
628                 LDAP_LOG( OPERATION, ERR,
629                         "do_syncrepl : unknown result\n", 0, 0, 0 );
630 #else
631                 Debug( LDAP_DEBUG_ANY,
632                         "do_syncrepl : unknown result\n", 0, 0, 0 );
633 #endif
634                 return NULL;
635         }
636
637 done:
638         if ( syncCookie.bv_val )
639                 ch_free( syncCookie.bv_val );
640         if ( syncUUID.bv_val )
641                 ch_free( syncUUID.bv_val );
642
643         if ( res )
644                 ldap_msgfree( res );
645         ldap_unbind( ld );
646         return NULL;
647 }
648
649 static Entry*
650 syncrepl_message_to_entry(
651         LDAP            *ld,
652         Operation       *op,
653         LDAPMessage     *msg,
654         Modifications   **modlist,
655         int             *syncstate,
656         struct berval   *syncUUID,
657         struct berval   *syncCookie
658 )
659 {
660         Entry           *e;
661         BerElement      *ber = NULL;
662         BerElement      *tmpber;
663         struct berval   bv = {0, NULL};
664         Modifications   tmp;
665         Modifications   *mod;
666         Modifications   **modtail = modlist;
667         Backend         *be = op->o_bd;
668
669         const char      *text;
670         char txtbuf[SLAP_TEXT_BUFLEN];
671         size_t textlen = sizeof txtbuf;
672
673         struct berval   **bvals = NULL;
674         char            *dn;
675         struct berval   bdn = {0, NULL};
676         Attribute       *attr;
677         struct berval   empty_bv = { 0, NULL };
678         int             rc;
679         char            *a;
680
681         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
682
683         ber_len_t       len;
684         LDAPControl*    rctrlp;
685         LDAPControl**   rctrls = NULL;
686         BerElement*     ctrl_ber;
687
688         *modlist = NULL;
689
690         if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
691 #ifdef NEW_LOGGING
692                 LDAP_LOG( OPERATION, ERR,
693                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
694 #else
695                 Debug( LDAP_DEBUG_ANY,
696                         "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
697 #endif
698                 return NULL;
699         }
700
701         op->o_tag = LDAP_REQ_ADD;
702
703         rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
704
705         if ( rc != LDAP_SUCCESS ) {
706 #ifdef NEW_LOGGING
707                 LDAP_LOG( OPERATION, ERR,
708                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
709 #else
710                 Debug( LDAP_DEBUG_ANY,
711                         "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
712 #endif
713                 return NULL;
714         }
715
716         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
717         dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
718
719         e->e_attrs = NULL;
720
721         for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
722                   rc == LDAP_SUCCESS;
723                   rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
724         {
725                 if ( tmp.sml_type.bv_val == NULL ) break;
726
727                 mod  = (Modifications *) ch_malloc( sizeof(Modifications) );
728
729                 mod->sml_op = LDAP_MOD_REPLACE;
730                 mod->sml_next = NULL;
731                 mod->sml_desc = NULL;
732                 mod->sml_type = tmp.sml_type;
733                 mod->sml_bvalues = tmp.sml_bvalues;
734                 mod->sml_nvalues = tmp.sml_bvalues;
735
736                 *modtail = mod;
737                 modtail = &mod->sml_next;
738         }
739
740         if ( ber_scanf( ber, "}") == LBER_ERROR ) {
741 #ifdef NEW_LOGGING
742                 LDAP_LOG( OPERATION, ERR,
743                                 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
744 #else
745                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
746                                 0, 0, 0 );
747 #endif
748                 return NULL;
749         }
750
751         ber_free( ber, 0 );
752         tmpber = ldap_get_message_ber( msg );
753         ber = ber_dup( tmpber );
754
755         ber_scanf( ber, "{xx" );
756
757         rc = ldap_int_get_controls( ber, &rctrls );
758
759         if ( rc != LDAP_SUCCESS ) {
760 #ifdef NEW_LOGGING
761                 LDAP_LOG( OPERATION, ERR,
762                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
763 #else
764                 Debug( LDAP_DEBUG_ANY,
765                         "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
766 #endif
767                 return NULL;
768         }
769
770         if ( rctrls ) {
771                 rctrlp = *rctrls;
772                 ctrl_ber = ber_alloc_t( LBER_USE_DER );
773                 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
774                 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
775                 ber_reset( ctrl_ber, 1 );
776                 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
777                 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
778                         ber_scanf( ctrl_ber, "o}", syncCookie );
779                 }
780                 ber_free( ctrl_ber, 1 );
781         } else {
782 #ifdef NEW_LOGGING
783                 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
784                         " rctrls absent\n", 0, 0, 0 );
785 #else
786                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
787                         " rctrls absent\n", 0, 0, 0 );
788 #endif
789         }
790
791         if ( *syncstate == LDAP_SYNC_PRESENT ) {
792                 e = NULL;
793                 goto done;
794         }
795
796         if ( *modlist == NULL ) {
797 #ifdef NEW_LOGGING
798                 LDAP_LOG( OPERATION, ERR,
799                                 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
800 #else
801                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
802                                 0, 0, 0 );
803 #endif
804         }
805
806         rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
807
808         if ( rc != LDAP_SUCCESS ) {
809 #ifdef NEW_LOGGING
810                 LDAP_LOG( OPERATION, ERR,
811                                 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
812 #else
813                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
814                                 text, 0, 0 );
815 #endif
816                 return NULL;
817         }
818         
819         rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
820                                          &text,txtbuf, textlen );
821         
822         if( rc != LDAP_SUCCESS ) {
823 #ifdef NEW_LOGGING
824                 LDAP_LOG( OPERATION, ERR,
825                                 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
826 #else
827                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
828                                 text, 0, 0 );
829 #endif
830                 return NULL;
831         }
832
833         rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
834         if( rc != LDAP_SUCCESS ) {
835 #ifdef NEW_LOGGING
836                 LDAP_LOG( OPERATION, ERR,
837                                 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
838 #else
839                 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
840                                 text, 0, 0 );
841 #endif
842         }
843
844 done:
845
846         ber_free ( ber, 0 );
847
848         return e;
849 }
850
851 int 
852 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
853 {
854         const struct berval *uuid1 = v_uuid1;
855         const struct berval *uuid2 = v_uuid2;
856         int rc = uuid1->bv_len - uuid2->bv_len;
857         if ( rc ) return rc;
858         return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
859 }
860
861 static int
862 syncrepl_entry(
863         LDAP *ld,
864         Operation *op,
865         Entry* e,
866         Modifications* modlist,
867         int syncstate,
868         struct berval* syncUUID,
869         struct berval* syncCookie,
870         int refresh
871 )
872 {
873         Backend *be = op->o_bd;
874         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
875         slap_callback   cb;
876         struct berval   csn_bv = {0, NULL};
877         struct berval   *syncuuid_bv = NULL;
878         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
879
880         SlapReply       rs = {REP_RESULT};
881         int rc;
882
883 #if 0 /* FIXME : UUID search required first */
884         char *filterstr;
885         struct berval filterstr_bv;
886         Filter *filter;
887 #endif
888
889         Attribute *a;
890
891         if ( refresh &&
892                         ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
893                 syncuuid_bv = ber_dupbv( NULL, syncUUID );
894                 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
895                                                 syncuuid_cmp, avl_dup_error );
896         }
897
898         if ( syncstate == LDAP_SYNC_PRESENT ) {
899                 if ( e )
900                         return 1;
901                 else
902                         return 0;
903         }
904
905         if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
906                 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
907         }
908
909 #if 0 /* FIXME : UUID search required first */
910         filterstr = (char *) ch_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1 ); 
911         strcpy( filterstr, "entryUUID=" );
912         strcat( filterstr, syncUUID->bv_val );
913 #endif
914
915         si->e = e;
916         si->syncUUID = syncUUID;
917
918 #if 0 /* FIXME : UUID search required first */
919         filter = str2filter( filterstr );
920         ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
921         ch_free( filterstr );
922 #endif
923
924         op->o_req_dn = e->e_name;
925         op->o_req_ndn = e->e_nname;
926
927         op->o_callback = &cb;
928         cb.sc_response = null_callback;
929         cb.sc_private = si;
930
931         switch ( syncstate ) {
932         case LDAP_SYNC_ADD :
933         case LDAP_SYNC_MODIFY :
934 sync_add_retry:
935                 op->o_tag = LDAP_REQ_MODIFY;
936                 op->orm_modlist = modlist;
937                 rc = be->be_modify( op, &rs );
938                 if ( rc != LDAP_SUCCESS ) {
939                         if ( rc == LDAP_REFERRAL ||
940                                  rc == LDAP_NO_SUCH_OBJECT ||
941                                  rc == DB_NOTFOUND ) {
942                                 op->o_tag = LDAP_REQ_ADD;
943                                 op->ora_e = e;
944                                 rc = be->be_add( op, &rs );
945                                 if ( rc != LDAP_SUCCESS ) {
946                                         if ( rc == LDAP_ALREADY_EXISTS ) {
947                                                 goto sync_add_retry;
948                                         } else if ( rc == LDAP_REFERRAL ||
949                                                                 rc == LDAP_NO_SUCH_OBJECT ||
950                                                                 rc == DB_NOTFOUND ) {
951                                                 syncrepl_add_glue(ld, op, e,
952                                                         modlist, syncstate,
953                                                         syncUUID, syncCookie);
954                                         } else {
955 #ifdef NEW_LOGGING
956                                                 LDAP_LOG( OPERATION, ERR,
957                                                         "be_add failed (%d)\n",
958                                                         rc, 0, 0 );
959 #else
960                                                 Debug( LDAP_DEBUG_ANY,
961                                                         "be_add failed (%d)\n",
962                                                         rc, 0, 0 );
963 #endif
964                                         }
965                                 } else {
966                                         return 0;
967                                 }
968                         } else {
969 #ifdef NEW_LOGGING
970                                 LDAP_LOG( OPERATION, ERR,
971                                         "be_modify failed (%d)\n", rc, 0, 0 );
972 #else
973                                 Debug( LDAP_DEBUG_ANY,
974                                         "be_modify failed (%d)\n", rc, 0, 0 );
975 #endif
976                         }
977                 }
978
979                 si->e = NULL;
980                 return 1;
981         case LDAP_SYNC_DELETE :
982                 op->o_tag = LDAP_REQ_DELETE;
983                 be->be_delete( op, &rs );
984                 si->e = NULL;
985                 return 1;
986         default :
987 #ifdef NEW_LOGGING
988                 LDAP_LOG( OPERATION, ERR,
989                         "unknown syncstate\n", 0, 0, 0 );
990 #else
991                 Debug( LDAP_DEBUG_ANY,
992                         "unknown syncstate\n", 0, 0, 0 );
993 #endif
994                 return 1;
995         }
996 }
997
998 static int
999 syncrepl_del_nonpresent(
1000         LDAP *ld,
1001         Operation *op
1002 )
1003 {
1004         Backend* be = op->o_bd;
1005         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1006         slap_callback   cb;
1007         struct berval   base_bv = {0, NULL};
1008         Filter *filter;
1009         SlapReply       rs = {REP_RESULT};
1010         struct berval   filterstr_bv = {0, NULL};
1011         struct nonpresent_entry *np_list, *np_prev;
1012
1013         ber_str2bv( si->base, strlen(si->base), 1, &base_bv ); 
1014         dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1015         ch_free( base_bv.bv_val );
1016
1017         filter = str2filter( si->filterstr );
1018
1019         cb.sc_response = nonpresent_callback;
1020         cb.sc_private = si;
1021
1022         op->o_callback = &cb;
1023         op->o_tag = LDAP_REQ_SEARCH;
1024         op->ors_scope = si->scope;
1025         op->ors_deref = LDAP_DEREF_NEVER;
1026         op->ors_slimit = -1;
1027         op->ors_tlimit = -1;
1028         op->ors_attrsonly = 0;
1029         op->ors_attrs = NULL;
1030         op->ors_filter = filter;
1031         ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1032
1033         be->be_search( op, &rs );
1034
1035         if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1036                 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1037                 while ( np_list != NULL ) {
1038                         LDAP_LIST_REMOVE( np_list, np_link );
1039                         np_prev = np_list;
1040                         np_list = LDAP_LIST_NEXT( np_list, np_link );
1041                         op->o_tag = LDAP_REQ_DELETE;
1042                         op->o_callback = &cb;
1043                         cb.sc_response = null_callback;
1044                         cb.sc_private = si;
1045                         op->o_req_dn = *np_prev->dn;
1046                         op->o_req_ndn = *np_prev->ndn;
1047                         op->o_bd->be_delete( op, &rs );
1048                         ber_bvfree( np_prev->dn );
1049                         ber_bvfree( np_prev->ndn );
1050                         op->o_req_dn.bv_val = NULL;
1051                         op->o_req_ndn.bv_val = NULL;
1052                         ch_free( np_prev );
1053                 }
1054         }
1055
1056         if ( op->o_req_dn.bv_val )
1057                 ch_free( op->o_req_dn.bv_val );
1058         if ( op->o_req_ndn.bv_val )
1059                 ch_free( op->o_req_ndn.bv_val );
1060         filter_free( op->ors_filter );
1061         ch_free( op->ors_filterstr.bv_val );
1062 }
1063
1064
1065 static void
1066 syncrepl_add_glue(
1067         LDAP *ld,
1068         Operation* op,
1069         Entry *e,
1070         Modifications* modlist,
1071         int syncstate,
1072         struct berval* syncUUID,
1073         struct berval* syncCookie
1074 )
1075 {
1076         Backend *be = op->o_bd;
1077         syncinfo_t *si = op->o_callback->sc_private;
1078         struct berval   uuid_bv = {0, NULL};
1079         slap_callback cb;
1080         Attribute       *a;
1081         int     rc;
1082         char    uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1083         int levels = 0;
1084         int i, j, k;
1085         struct berval dn = {0, NULL};
1086         struct berval pdn = {0, NULL};
1087         struct berval ndn = {0, NULL};
1088         struct berval rdn = {0, NULL};
1089         Entry   *glue;
1090         SlapReply       rs = {REP_RESULT};
1091         Connection *conn = op->o_conn;
1092
1093         op->o_tag = LDAP_REQ_ADD;
1094         op->o_callback = &cb;
1095         cb.sc_response = null_callback;
1096         cb.sc_private = si;
1097
1098         ber_dupbv( &dn, &e->e_nname );
1099         ber_dupbv( &pdn, &e->e_nname );
1100
1101         while ( !be_issuffix ( be, &pdn )) {
1102                 dnParent( &dn, &pdn );
1103                 ch_free( dn.bv_val );
1104                 ber_dupbv( &dn, &pdn );
1105                 levels++;
1106         }
1107
1108         for ( i = 0; i <= levels; i++ ) {
1109                 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1110                 ch_free( dn.bv_val );
1111                 ch_free( pdn.bv_val );
1112                 ber_dupbv( &dn, &e->e_nname );
1113                 ber_dupbv( &pdn, &e->e_nname );
1114                 j = levels - i;
1115                 for ( k = 0; k < j; k++ ) {
1116                         dnParent( &dn, &pdn );
1117                         ch_free( dn.bv_val );
1118                         ber_dupbv( &dn, &pdn );
1119                 }
1120
1121                 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1122                 ber_dupbv( &glue->e_name, &pdn );
1123                 ber_dupbv( &glue->e_nname, &ndn );
1124                 ch_free( dn.bv_val );
1125                 ch_free( pdn.bv_val );
1126                 ch_free( ndn.bv_val );
1127
1128                 a = ch_calloc( 1, sizeof( Attribute ));
1129                 a->a_desc = slap_schema.si_ad_objectClass;
1130                 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1131                 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1132                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1133                 a->a_vals[2].bv_len = 0;
1134                 a->a_vals[2].bv_val = NULL;
1135                 a->a_next = glue->e_attrs;
1136                 glue->e_attrs = a;
1137
1138                 a = ch_calloc( 1, sizeof( Attribute ));
1139                 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1140                 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1141                 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1142                 a->a_vals[1].bv_len = 0;
1143                 a->a_vals[1].bv_val = NULL;
1144                 a->a_next = glue->e_attrs;
1145                 glue->e_attrs = a;
1146
1147                 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1148                         op->o_req_dn = e->e_name;
1149                         op->o_req_ndn = e->e_nname;
1150                         op->ora_e = e;
1151                         rc = be->be_add ( op, &rs );
1152                         if ( rc == LDAP_SUCCESS )
1153                                 be_entry_release_w( op, e );
1154                         else 
1155                                 entry_free( e );
1156                         entry_free( glue );
1157                 } else {
1158                         op->o_req_dn = glue->e_name;
1159                         op->o_req_ndn = glue->e_nname;
1160                         op->ora_e = glue;
1161                         rc = be->be_add ( op, &rs );
1162                         if ( rc == LDAP_SUCCESS ) {
1163                                 be_entry_release_w( op, glue );
1164                         } else {
1165                         /* incl. ALREADY EXIST */
1166                                 entry_free( glue );
1167                         }
1168                 }
1169         }
1170
1171         return;
1172 }
1173
1174 static void
1175 syncrepl_updateCookie(
1176         LDAP *ld,
1177         Operation *op,
1178         struct berval *pdn,
1179         struct berval *syncCookie
1180 )
1181 {
1182         Backend *be = op->o_bd;
1183         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1184         Modifications *ml;
1185         Modifications *mlnext;
1186         Modifications *mod;
1187         Modifications *modlist;
1188         Modifications **modtail = &modlist;
1189
1190         struct berval* ocbva = NULL;
1191         struct berval* cnbva = NULL;
1192         struct berval* ssbva = NULL;
1193         struct berval* scbva = NULL;
1194
1195         char substr[64];
1196         char rdnstr[67];
1197         const char      *text;
1198         char txtbuf[SLAP_TEXT_BUFLEN];
1199         size_t textlen = sizeof txtbuf;
1200
1201         Entry* e;
1202         int rc;
1203
1204         struct berval sub_bv = { 0, NULL };
1205         struct berval psubrdn = { 0, NULL };
1206         
1207         slap_callback cb;
1208         SlapReply       rs = {REP_RESULT};
1209
1210         ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1211         cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1212         ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1213         scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1214
1215         /* update in memory cookie */
1216         if ( si->syncCookie != NULL ) {
1217                 ber_bvfree( si->syncCookie );
1218         }
1219         si->syncCookie = ber_dupbv( NULL, syncCookie );
1220         ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1221         ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1222         ber_str2bv( "syncConsumerSubentry",
1223                         strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1224         ocbva[3].bv_len = 0;
1225         ocbva[3].bv_val = NULL;
1226
1227         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1228         mod->sml_op = LDAP_MOD_REPLACE;
1229         mod->sml_next = NULL;
1230         mod->sml_desc = NULL;
1231         ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1232         mod->sml_bvalues = ocbva;
1233         mod->sml_nvalues = ocbva;
1234         *modtail = mod;
1235         modtail = &mod->sml_next;
1236
1237         sprintf( substr, "syncrepl%d", si->id );
1238         sprintf( rdnstr, "cn=%s", substr );
1239         ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1240         ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1241         cnbva[1].bv_len = 0;
1242         cnbva[1].bv_val = NULL;
1243         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1244         mod->sml_op = LDAP_MOD_REPLACE;
1245         mod->sml_next = NULL;
1246         mod->sml_desc = NULL;
1247         ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1248         mod->sml_bvalues = cnbva;
1249         mod->sml_nvalues = cnbva;
1250         *modtail = mod;
1251         modtail = &mod->sml_next;
1252
1253         ber_dupbv( &scbva[0], si->syncCookie );
1254         scbva[1].bv_len = 0;
1255         scbva[1].bv_val = NULL;
1256         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1257         mod->sml_op = LDAP_MOD_REPLACE;
1258         mod->sml_next = NULL;
1259         mod->sml_desc = NULL;
1260         ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1261                                                 1, &mod->sml_type );
1262         mod->sml_bvalues = scbva;
1263         mod->sml_nvalues = scbva;
1264         *modtail = mod;
1265         modtail = &mod->sml_next;
1266
1267         ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1268         ssbva[1].bv_len = 0;
1269         ssbva[1].bv_val = NULL;
1270         mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1271         mod->sml_op = LDAP_MOD_REPLACE;
1272         mod->sml_next = NULL;
1273         mod->sml_desc = NULL;
1274         ber_str2bv( "subtreeSpecification",
1275                         strlen("subtreeSpecification"), 1, &mod->sml_type );
1276         mod->sml_bvalues = ssbva;
1277         mod->sml_nvalues = ssbva;
1278         *modtail = mod;
1279         modtail = &mod->sml_next;
1280
1281         rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1282
1283         if ( rc != LDAP_SUCCESS ) {
1284 #ifdef NEW_LOGGING
1285                 LDAP_LOG( OPERATION, ERR,
1286                                 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1287 #else
1288                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1289                          text, 0, 0 );
1290 #endif
1291         }
1292
1293         op->o_tag = LDAP_REQ_ADD;
1294         rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1295
1296         if( rc != LDAP_SUCCESS ) {
1297 #ifdef NEW_LOGGING
1298                 LDAP_LOG( OPERATION, ERR,
1299                                 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1300 #else
1301                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1302                          text, 0, 0 );
1303 #endif
1304         }
1305
1306         e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1307
1308         build_new_dn( &sub_bv, pdn, &psubrdn );
1309         dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1310         ch_free( sub_bv.bv_val );
1311         ch_free( psubrdn.bv_val );
1312
1313         e->e_attrs = NULL;
1314
1315         rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1316
1317         if( rc != LDAP_SUCCESS ) {
1318 #ifdef NEW_LOGGING
1319                 LDAP_LOG( OPERATION, ERR,
1320                                 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1321 #else
1322                 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1323                          text, 0, 0 );
1324 #endif
1325         }
1326
1327         cb.sc_response = null_callback;
1328         cb.sc_private = si;
1329
1330         op->o_callback = &cb;
1331         op->o_req_dn = e->e_name;
1332         op->o_req_ndn = e->e_nname;
1333
1334         /* update persistent cookie */
1335 update_cookie_retry:
1336         op->o_tag = LDAP_REQ_MODIFY;
1337         op->orm_modlist = modlist;
1338         rc = be->be_modify( op, &rs );
1339         if ( rc != LDAP_SUCCESS ) {
1340                 if ( rc == LDAP_REFERRAL ||
1341                          rc == LDAP_NO_SUCH_OBJECT ||
1342                          rc == DB_NOTFOUND ) {
1343                         op->o_tag = LDAP_REQ_ADD;
1344                         op->ora_e = e;
1345                         rc = be->be_add( op, &rs );
1346                         if ( rc != LDAP_SUCCESS ) {
1347                                 if ( rc == LDAP_ALREADY_EXISTS ) {
1348                                         goto update_cookie_retry;
1349                                 } else if ( rc == LDAP_REFERRAL ||
1350                                                         rc == LDAP_NO_SUCH_OBJECT ||
1351                                                         rc == DB_NOTFOUND ) {
1352 #ifdef NEW_LOGGING
1353                                         LDAP_LOG( OPERATION, ERR,
1354                                                 "cookie will be non-persistent\n",
1355                                                 0, 0, 0 );
1356 #else
1357                                         Debug( LDAP_DEBUG_ANY,
1358                                                 "cookie will be non-persistent\n",
1359                                                 0, 0, 0 );
1360 #endif
1361                                 } else {
1362 #ifdef NEW_LOGGING
1363                                         LDAP_LOG( OPERATION, ERR,
1364                                                 "be_add failed (%d)\n",
1365                                                 rc, 0, 0 );
1366 #else
1367                                         Debug( LDAP_DEBUG_ANY,
1368                                                 "be_add failed (%d)\n",
1369                                                 rc, 0, 0 );
1370 #endif
1371                                 }
1372                         } else {
1373                                 goto done;
1374                         }
1375                 } else {
1376 #ifdef NEW_LOGGING
1377                         LDAP_LOG( OPERATION, ERR,
1378                                 "be_modify failed (%d)\n", rc, 0, 0 );
1379 #else
1380                         Debug( LDAP_DEBUG_ANY,
1381                                 "be_modify failed (%d)\n", rc, 0, 0 );
1382 #endif
1383                 }
1384         }
1385
1386         if ( e != NULL )
1387                 entry_free( e );
1388
1389 done :
1390
1391         for ( ml = modlist; ml != NULL; ml = mlnext ) {
1392                 mlnext = ml->sml_next;
1393                 free( ml );
1394         }
1395
1396         return;
1397 }
1398
1399
1400 static
1401 int slap_mods_check_syncrepl(
1402         Operation *op,
1403         Modifications **mlp,
1404         const char **text,
1405         char *textbuf,
1406         size_t textlen,
1407         void *ctx )
1408 {
1409         int rc;
1410         Backend *be = op->o_bd;
1411         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1412         AttributeDescription** descs;
1413         int i;
1414         Modifications *prevml = NULL;
1415         Modifications *nextml = NULL;
1416         Modifications *ml = *mlp;
1417
1418         while ( ml != NULL ) {
1419                 AttributeDescription *ad = NULL;
1420
1421                 /* convert to attribute description */
1422                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1423
1424                 if( rc != LDAP_SUCCESS ) {
1425                         snprintf( textbuf, textlen, "%s: %s",
1426                                                 ml->sml_type.bv_val, *text );
1427                         *text = textbuf;
1428                         return rc;
1429                 }
1430
1431                 ad = ml->sml_desc;
1432
1433                 if ( si->lastmod == LASTMOD_REQ ) {
1434                         descs = del_descs_lastmod;
1435                 } else {
1436                         descs = del_descs;
1437                 }
1438
1439                 for ( i = 0; descs[i] != NULL; i++ ) {
1440                         if ( ad == descs[i] ) {
1441                                 if ( prevml == NULL ) {
1442                                         mlp = &ml->sml_next;
1443                                         prevml = NULL;
1444                                 } else {
1445                                         prevml->sml_next = ml->sml_next;
1446                                 }
1447                                 slap_mod_free( &ml->sml_mod, 0 );
1448                                 nextml = ml->sml_next;
1449                                 free( ml );
1450                                 ml = nextml;
1451                                 continue;
1452                         }
1453                 }
1454
1455                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1456                                 && !slap_ad_is_binary( ad )) {
1457                         /* attribute requires binary transfer */
1458                         snprintf( textbuf, textlen,
1459                                         "%s: requires ;binary transfer",
1460                                         ml->sml_type.bv_val );
1461                         *text = textbuf;
1462                         return LDAP_UNDEFINED_TYPE;
1463                 }
1464
1465                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1466                                         && slap_ad_is_binary( ad )) {
1467                         /* attribute requires binary transfer */
1468                         snprintf( textbuf, textlen,
1469                                         "%s: disallows ;binary transfer",
1470                                         ml->sml_type.bv_val );
1471                         *text = textbuf;
1472                         return LDAP_UNDEFINED_TYPE;
1473                 }
1474
1475                 if( slap_ad_is_tag_range( ad )) {
1476                         /* attribute requires binary transfer */
1477                         snprintf( textbuf, textlen,
1478                                         "%s: inappropriate use of tag range option",
1479                                         ml->sml_type.bv_val );
1480                         *text = textbuf;
1481                         return LDAP_UNDEFINED_TYPE;
1482                 }
1483
1484                 if ( is_at_obsolete( ad->ad_type ) &&
1485                                 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1486                         /*
1487                          * attribute is obsolete,
1488                          * only allow replace/delete with no values
1489                          */
1490                         snprintf( textbuf, textlen,
1491                                         "%s: attribute is obsolete",
1492                                         ml->sml_type.bv_val );
1493                         *text = textbuf;
1494                         return LDAP_CONSTRAINT_VIOLATION;
1495                 }
1496
1497                 /*
1498                  * check values
1499                  */
1500                 if( ml->sml_values != NULL ) {
1501                         ber_len_t nvals;
1502                         slap_syntax_validate_func *validate =
1503                         ad->ad_type->sat_syntax->ssyn_validate;
1504                         slap_syntax_transform_func *pretty =
1505                         ad->ad_type->sat_syntax->ssyn_pretty;
1506
1507                         if( !pretty && !validate ) {
1508                                 *text = "no validator for syntax";
1509                                 snprintf( textbuf, textlen,
1510                                                 "%s: no validator for syntax %s",
1511                                                 ml->sml_type.bv_val,
1512                                                 ad->ad_type->sat_syntax->ssyn_oid );
1513                                 *text = textbuf;
1514                                 return LDAP_INVALID_SYNTAX;
1515                         }
1516
1517                         /*
1518                          * check that each value is valid per syntax
1519                          * and pretty if appropriate
1520                          */
1521                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1522                                 struct berval pval = {0, NULL};
1523                                 if( pretty ) {
1524                                         rc = pretty( ad->ad_type->sat_syntax,
1525                                                         &ml->sml_values[nvals], &pval, ctx );
1526                                 } else {
1527                                         rc = validate( ad->ad_type->sat_syntax,
1528                                                         &ml->sml_values[nvals] );
1529                                 }
1530
1531                                 if( rc != 0 ) {
1532                                         snprintf( textbuf, textlen,
1533                                                         "%s: value #%ld invalid per syntax",
1534                                                         ml->sml_type.bv_val, (long) nvals );
1535                                         *text = textbuf;
1536                                         return LDAP_INVALID_SYNTAX;
1537                                 }
1538
1539                                 if( pretty ) {
1540                                         ber_memfree( ml->sml_values[nvals].bv_val );
1541                                         ml->sml_values[nvals] = pval;
1542                                 }
1543                         }
1544
1545                         /*
1546                          * a rough single value check... an additional check is needed
1547                          * to catch add of single value to existing single valued attribute
1548                          */
1549                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1550                                         && nvals > 1 && is_at_single_value( ad->ad_type )) {
1551                                 snprintf( textbuf, textlen,
1552                                                 "%s: multiple values provided",
1553                                                 ml->sml_type.bv_val );
1554                                 *text = textbuf;
1555                                 return LDAP_CONSTRAINT_VIOLATION;
1556                         }
1557
1558                         if( nvals && ad->ad_type->sat_equality &&
1559                                         ad->ad_type->sat_equality->smr_normalize ) {
1560                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1561                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1562                                         rc = ad->ad_type->sat_equality->smr_normalize( 0,
1563                                                         ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1564                                                         &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1565                                         if( rc ) {
1566 #ifdef NEW_LOGGING
1567                                                 LDAP_LOG( OPERATION, DETAIL1,
1568                                                                 "str2entry:  NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1569 #else
1570                                                 Debug( LDAP_DEBUG_ANY,
1571                                                                 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1572 #endif
1573                                                 snprintf( textbuf, textlen,
1574                                                                 "%s: value #%ld normalization failed",
1575                                                                 ml->sml_type.bv_val, (long) nvals );
1576                                                 *text = textbuf;
1577                                                 return rc;
1578                                         }
1579                                 }
1580                                 ml->sml_nvalues[nvals].bv_val = NULL;
1581                                 ml->sml_nvalues[nvals].bv_len = 0;
1582                         }
1583                 }
1584                 prevml = ml;
1585                 ml = ml->sml_next;
1586         }
1587
1588         return LDAP_SUCCESS;
1589 }
1590
1591 static
1592 int slap_mods_opattrs_syncrepl(
1593         Operation *op,
1594         Modifications *mods,
1595         Modifications **modtail,
1596         const char **text,
1597         char *textbuf, size_t textlen )
1598 {
1599         struct berval name = {0, NULL};
1600         struct berval timestamp = {0, NULL};
1601         struct berval csn = {0, NULL};
1602         struct berval nname = {0, NULL};
1603         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1604         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1605         Modifications *mod;
1606         Backend *be = op->o_bd;
1607         syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1608
1609         int mop = LDAP_MOD_REPLACE;
1610
1611         assert( modtail != NULL );
1612         assert( *modtail == NULL );
1613
1614         if( si->lastmod == LASTMOD_GEN ) {
1615                 struct tm *ltm;
1616                 time_t now = slap_get_time();
1617
1618                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1619                 ltm = gmtime( &now );
1620                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1621
1622                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1623                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1624                 csn.bv_val = csnbuf;
1625
1626                 timestamp.bv_val = timebuf;
1627                 timestamp.bv_len = strlen(timebuf);
1628
1629                 if( op->o_dn.bv_len == 0 ) {
1630                         name.bv_val = SLAPD_ANONYMOUS;
1631                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1632                         nname = name;
1633                 } else {
1634                         name = op->o_dn;
1635                         nname = op->o_ndn;
1636                 }
1637         }
1638
1639         if( op->o_tag == LDAP_REQ_ADD ) {
1640                 struct berval tmpval = {0, NULL};
1641
1642                 if( global_schemacheck ) {
1643                         int rc = mods_structural_class( mods, &tmpval,
1644                                                                         text, textbuf, textlen );
1645                         if( rc != LDAP_SUCCESS ) {
1646                                 return rc;
1647                         }
1648
1649                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1650                         mod->sml_op = mop;
1651                         mod->sml_type.bv_val = NULL;
1652                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1653                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1654                         ber_dupbv( &mod->sml_values[0], &tmpval );
1655                         mod->sml_values[1].bv_len = 0;
1656                         mod->sml_values[1].bv_val = NULL;
1657                         assert( mod->sml_values[0].bv_val );
1658                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1659                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1660                         mod->sml_nvalues[1].bv_len = 0;
1661                         mod->sml_nvalues[1].bv_val = NULL;
1662                         assert( mod->sml_nvalues[0].bv_val );
1663                         *modtail = mod;
1664                         modtail = &mod->sml_next;
1665                 }
1666
1667                 if( si->lastmod == LASTMOD_GEN ) {
1668                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1669                         mod->sml_op = mop;
1670                         mod->sml_type.bv_val = NULL;
1671                         mod->sml_desc = slap_schema.si_ad_creatorsName;
1672                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1673                         ber_dupbv( &mod->sml_values[0], &name );
1674                         mod->sml_values[1].bv_len = 0;
1675                         mod->sml_values[1].bv_val = NULL;
1676                         assert( mod->sml_values[0].bv_val );
1677                         mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1678                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1679                         mod->sml_nvalues[1].bv_len = 0;
1680                         mod->sml_nvalues[1].bv_val = NULL;
1681                         assert( mod->sml_nvalues[0].bv_val );
1682                         *modtail = mod;
1683                         modtail = &mod->sml_next;
1684
1685                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1686                         mod->sml_op = mop;
1687                         mod->sml_type.bv_val = NULL;
1688                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
1689                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1690                         ber_dupbv( &mod->sml_values[0], &timestamp );
1691                         mod->sml_values[1].bv_len = 0;
1692                         mod->sml_values[1].bv_val = NULL;
1693                         assert( mod->sml_values[0].bv_val );
1694                         mod->sml_nvalues = NULL;
1695                         *modtail = mod;
1696                         modtail = &mod->sml_next;
1697                 }
1698         }
1699
1700         if( si->lastmod == LASTMOD_GEN ) {
1701                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1702                 mod->sml_op = mop;
1703                 mod->sml_type.bv_val = NULL;
1704                 mod->sml_desc = slap_schema.si_ad_entryCSN;
1705                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1706                 ber_dupbv( &mod->sml_values[0], &csn );
1707                 mod->sml_values[1].bv_len = 0;
1708                 mod->sml_values[1].bv_val = NULL;
1709                 assert( mod->sml_values[0].bv_val );
1710                 mod->sml_nvalues = NULL;
1711                 *modtail = mod;
1712                 modtail = &mod->sml_next;
1713
1714                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1715                 mod->sml_op = mop;
1716                 mod->sml_type.bv_val = NULL;
1717                 mod->sml_desc = slap_schema.si_ad_modifiersName;
1718                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1719                 ber_dupbv( &mod->sml_values[0], &name );
1720                 mod->sml_values[1].bv_len = 0;
1721                 mod->sml_values[1].bv_val = NULL;
1722                 assert( mod->sml_values[0].bv_val );
1723                 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1724                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1725                 mod->sml_nvalues[1].bv_len = 0;
1726                 mod->sml_nvalues[1].bv_val = NULL;
1727                 assert( mod->sml_nvalues[0].bv_val );
1728                 *modtail = mod;
1729                 modtail = &mod->sml_next;
1730
1731                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1732                 mod->sml_op = mop;
1733                 mod->sml_type.bv_val = NULL;
1734                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1735                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1736                 ber_dupbv( &mod->sml_values[0], &timestamp );
1737                 mod->sml_values[1].bv_len = 0;
1738                 mod->sml_values[1].bv_val = NULL;
1739                 assert( mod->sml_values[0].bv_val );
1740                 mod->sml_nvalues = NULL;
1741                 *modtail = mod;
1742                 modtail = &mod->sml_next;
1743         }
1744
1745         *modtail = NULL;
1746         return LDAP_SUCCESS;
1747 }
1748
1749
1750 static
1751 int slap_mods2entry_syncrepl(
1752         Modifications *mods,
1753         Entry **e,
1754         int repl_user,
1755         const char **text,
1756         char *textbuf, size_t textlen )
1757 {
1758         Attribute **tail = &(*e)->e_attrs;
1759         assert( *tail == NULL );
1760
1761         *text = textbuf;
1762
1763         for( ; mods != NULL; mods = mods->sml_next ) {
1764                 Attribute *attr;
1765
1766                 assert( mods->sml_desc != NULL );
1767
1768                 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1769
1770                 if( attr != NULL ) {
1771 #define SLURPD_FRIENDLY
1772 #ifdef SLURPD_FRIENDLY
1773                         ber_len_t i,j;
1774
1775                         if( !repl_user ) {
1776                                 snprintf( textbuf, textlen,
1777                                         "attribute '%s' provided more than once",
1778                                         mods->sml_desc->ad_cname.bv_val );
1779                                 return LDAP_TYPE_OR_VALUE_EXISTS;
1780                         }
1781
1782                         for( i=0; attr->a_vals[i].bv_val; i++ ) {
1783                                 /* count them */
1784                         }
1785                         for( j=0; mods->sml_values[j].bv_val; j++ ) {
1786                                 /* count them */
1787                         }
1788                         j++;    /* NULL */
1789                         
1790                         attr->a_vals = ch_realloc( attr->a_vals,
1791                                 sizeof( struct berval ) * (i+j) );
1792
1793                         /* should check for duplicates */
1794
1795                         AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1796                                 sizeof( struct berval ) * j );
1797
1798                         if( attr->a_nvals ) {
1799                                 attr->a_nvals = ch_realloc( attr->a_nvals,
1800                                         sizeof( struct berval ) * (i+j) );
1801
1802                                 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1803                                         sizeof( struct berval ) * j );
1804
1805                                 /* trim the mods array */
1806                                 ch_free( mods->sml_nvalues );
1807                                 mods->sml_nvalues = NULL;
1808                         }
1809
1810                         continue;
1811 #else
1812                         snprintf( textbuf, textlen,
1813                                 "attribute '%s' provided more than once",
1814                                 mods->sml_desc->ad_cname.bv_val );
1815                         return LDAP_TYPE_OR_VALUE_EXISTS;
1816 #endif
1817                 }
1818
1819                 if( mods->sml_values[1].bv_val != NULL ) {
1820                         /* check for duplicates */
1821                         int             i, j;
1822                         MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1823
1824                         /* check if the values we're adding already exist */
1825                         if( mr == NULL || !mr->smr_match ) {
1826                                 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1827                                         /* test asserted values against themselves */
1828                                         for( j = 0; j < i; j++ ) {
1829                                                 if ( bvmatch( &mods->sml_bvalues[i],
1830                                                         &mods->sml_bvalues[j] ) ) {
1831                                                         /* value exists already */
1832                                                         snprintf( textbuf, textlen,
1833                                                                 "%s: value #%d provided more than once",
1834                                                                 mods->sml_desc->ad_cname.bv_val, j );
1835                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
1836                                                 }
1837                                         }
1838                                 }
1839
1840                         } else {
1841                                 int             rc;
1842                                 const char      *text = NULL;
1843                                 char            textbuf[ SLAP_TEXT_BUFLEN ]  = { '\0' };
1844                                 
1845                                 rc = modify_check_duplicates( mods->sml_desc, mr,
1846                                                 NULL, mods->sml_bvalues, 0,
1847                                                 &text, textbuf, sizeof( textbuf ) );
1848
1849                                 if ( rc != LDAP_SUCCESS ) {
1850                                         return rc;
1851                                 }
1852                         }
1853                 }
1854
1855                 attr = ch_calloc( 1, sizeof(Attribute) );
1856
1857                 /* move ad to attr structure */
1858                 attr->a_desc = mods->sml_desc;
1859
1860                 /* move values to attr structure */
1861                 /*      should check for duplicates */
1862                 attr->a_vals = mods->sml_values;
1863
1864                 attr->a_nvals = mods->sml_nvalues;
1865
1866                 *tail = attr;
1867                 tail = &attr->a_next;
1868         }
1869
1870         return LDAP_SUCCESS;
1871 }
1872
1873 void
1874 avl_ber_bvfree( void *bv )
1875 {
1876         if( bv == NULL ) {
1877                 return;
1878         }
1879         if ( ((struct berval *)bv)->bv_val != NULL ) {
1880                 ber_memfree ( ((struct berval *)bv)->bv_val );
1881         }
1882         ber_memfree ( (char *) bv );
1883 }
1884
1885 static int
1886 cookie_callback(
1887         Operation* op,
1888         SlapReply* rs
1889 )
1890 {
1891         syncinfo_t *si = op->o_callback->sc_private;
1892         Attribute *a;
1893
1894         if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1895
1896         a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1897
1898         if ( a == NULL ) {
1899                 si->syncCookie = NULL;
1900         } else {
1901                 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1902         }
1903         return LDAP_SUCCESS;
1904 }
1905
1906 static int
1907 nonpresent_callback(
1908         Operation*      op,
1909         SlapReply*      rs
1910 )
1911 {
1912         syncinfo_t *si = op->o_callback->sc_private;
1913         Attribute *a;
1914         int count = 0;
1915         struct berval* present_uuid = NULL;
1916         slap_callback cb;
1917         SlapReply       rs_cb = {REP_RESULT};
1918         struct nonpresent_entry *np_entry;
1919
1920         if ( rs->sr_type == REP_RESULT ) {
1921                 count = avl_free( si->presentlist, avl_ber_bvfree );
1922                 return LDAP_SUCCESS;
1923         } else if ( rs->sr_type == REP_SEARCH ) {
1924                 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1925
1926                 if ( a == NULL )
1927                         return 0;
1928
1929                 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1930
1931                 if ( present_uuid == NULL ) {
1932                         np_entry = (struct nonpresent_entry *)
1933                                                 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1934                         np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1935                         np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1936                         LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1937                 } else {
1938                         avl_delete( &si->presentlist,
1939                                         &a->a_vals[0], syncuuid_cmp );
1940                 }
1941                 return LDAP_SUCCESS;
1942         } else {
1943                 return LDAP_SUCCESS;
1944         }
1945
1946 }
1947
1948 static int
1949 null_callback(
1950         Operation*      op,
1951         SlapReply*      rs
1952 )
1953 {
1954         if ( rs->sr_err != LDAP_SUCCESS &&
1955                  rs->sr_err != LDAP_REFERRAL &&
1956                  rs->sr_err != LDAP_ALREADY_EXISTS &&
1957                  rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1958                  rs->sr_err != DB_NOTFOUND ) {
1959 #ifdef NEW_LOGGING
1960                 LDAP_LOG( OPERATION, ERR,
1961                         "null_callback : error code 0x%x\n",
1962                         rs->sr_err, 0, 0 );
1963 #else
1964                 Debug( LDAP_DEBUG_ANY,
1965                         "null_callback : error code 0x%x\n",
1966                         rs->sr_err, 0, 0 );
1967 #endif
1968         }
1969         return LDAP_SUCCESS;
1970 }
1971
1972 #endif