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