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