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