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