3 * Replication Engine which uses the LDAP Sync protocol
5 /* Copyright (c) 2003 by International Business Machines, Inc.
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
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.
27 #include <ac/string.h>
28 #include <ac/socket.h>
34 #include "lutil_ldap.h"
41 syncrepl_message_to_entry ( LDAP *, Operation *, LDAPMessage *,
42 Modifications **, int*, struct berval *, struct berval * );
45 syncrepl_entry( LDAP *, Operation*, Entry*, Modifications*,
46 int, struct berval*, struct berval*, int );
49 syncrepl_del_nonpresent( LDAP *, Operation * );
52 syncrepl_add_glue( LDAP *, Operation*, Entry*, Modifications*, int,
53 struct berval*, struct berval* );
56 syncrepl_updateCookie( LDAP *, Operation *, struct berval *, struct berval * );
59 slap_mods_check_syncrepl( Operation *, Modifications **,
60 const char **, char *, size_t, void *ctx );
63 slap_mods_opattrs_syncrepl( Operation *, Modifications *, Modifications **,
64 const char **, char *, size_t );
67 slap_mods2entry_syncrepl( Modifications *, Entry **, int,
68 const char **, char *, size_t );
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 * );
75 static AttributeDescription **add_descs;
76 static AttributeDescription **add_descs_lastmod;
77 static AttributeDescription **del_descs;
78 static AttributeDescription **del_descs_lastmod;
80 struct runqueue_s syncrepl_rq;
85 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
86 add_descs[0] = slap_schema.si_ad_objectClass;
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;
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;
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;
121 struct re_s* rtask = arg;
122 Backend *be = rtask->arg;
123 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
125 SlapReply rs = {REP_RESULT};
128 LDAPControl **sctrls = NULL;
129 LDAPControl **rctrls = NULL;
130 LDAPControl *rctrlp = NULL;
131 BerElement *sync_ber = NULL;
132 struct berval *sync_bvalp = NULL;
134 BerElement *ctrl_ber = NULL;
135 BerElement *res_ber = NULL;
138 LDAPMessage *res = NULL;
139 LDAPMessage *msg = NULL;
143 int nresponses, nreferences, nextended, npartial;
144 int nresponses_psearch;
146 int cancel_msgid = -1;
148 struct berval *retdata = NULL;
150 int sync_info_arrived = 0;
154 struct berval syncUUID = { 0, NULL };
155 struct berval syncCookie = { 0, NULL };
160 int syncinfo_arrived = 0;
161 int cancel_response = 0;
164 AttributeDescription** descs = NULL;
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 };
185 Modifications *modlist = NULL;
186 Modifications *ml, *mlnext;
187 char *def_filter_str = NULL;
190 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
192 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
198 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
199 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
203 /* Init connection to master */
205 if ( ldap_is_ldap_url( si->masteruri )) {
206 rc = ldap_initialize( &ld, si->masteruri );
207 if ( rc != LDAP_SUCCESS ) {
209 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
210 "ldap_initialize failed (%s)\n",
211 si->masteruri, 0, 0 );
213 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
214 "ldap_initialize failed (%s)\n",
215 si->masteruri, 0, 0 );
219 ld = ldap_init( si->mastername, si->masterport );
222 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
223 "ldap_init failed (%s:%s)\n",
224 si->mastername, si->masterport, 0 );
226 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
227 "ldap_init failed (%s:%s)\n",
228 si->mastername, si->masterport, 0 );
233 op.o_protocol = LDAP_VERSION3;
234 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
239 rc = ldap_start_tls_s( ld, NULL, NULL );
240 if( rc != LDAP_SUCCESS ) {
242 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
243 "%s: ldap_start_tls failed (%d)\n",
244 si->tls == TLS_CRITICAL ? "Error" : "Warning",
247 Debug( LDAP_DEBUG_ANY,
248 "%s: ldap_start_tls failed (%d)\n",
249 si->tls == TLS_CRITICAL ? "Error" : "Warning",
252 if( si->tls == TLS_CRITICAL )
257 if ( si->bindmethod == LDAP_AUTH_SASL ) {
258 #ifdef HAVE_CYRUS_SASL
261 if ( si->secprops != NULL ) {
262 int err = ldap_set_option( ld,
263 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
265 if( err != LDAP_OPT_SUCCESS ) {
267 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
268 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
269 si->mastername, si->secprops, 0 );
271 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
272 "(%s,SECPROPS,\"%s\") failed!\n",
273 si->mastername, si->secprops, NULL );
279 defaults = lutil_sasl_defaults( ld,
286 rc = ldap_sasl_interactive_bind_s( ld,
294 if ( rc != LDAP_SUCCESS ) {
296 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
297 "ldap_sasl_interactive_bind_s failed (%d)\n",
300 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
301 "ldap_sasl_interactive_bind_s failed (%d)\n",
306 #else /* HAVE_CYRUS_SASL */
307 fprintf( stderr, "not compiled with SASL support\n" );
311 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
312 if ( rc != LDAP_SUCCESS ) {
314 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
315 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
317 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
318 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
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;
331 op.o_time = slap_get_time();
332 op.o_managedsait = 1;
333 op.o_threadctx = si->ctx;
336 op.o_connid = op.o_conn->c_connid;
337 op.ors_scope = LDAP_SCOPE_BASE;
338 op.ors_deref = LDAP_DEREF_NEVER;
341 op.ors_attrsonly = 0;
343 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
344 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
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;
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 );
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 );
360 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
361 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
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 );
370 /* set callback function */
371 cb.sc_response = cookie_callback;
374 /* search subentry to retrieve cookie */
375 si->syncCookie = NULL;
376 be->be_search( &op, &rs );
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 );
383 psub = be->be_nsuffix[0];
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 );
389 if ( si->syncCookie ) {
390 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
392 ber_printf( sync_ber, "{e}", abs(si->type) );
395 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
396 ber_free( sync_ber, 1 );
399 ber_free( sync_ber, 1 );
401 sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
403 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
404 c[0].ldctl_value = (*sync_bvalp);
405 c[0].ldctl_iscritical = si->type < 0;
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;
420 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
422 ber_bvfree( sync_bvalp );
425 if ( err != LDAP_OPT_SUCCESS )
426 fprintf( stderr, "Could not set controls : %d\n", err );
428 /* Delete Attributes */
429 if ( si->lastmod == LASTMOD_REQ ) {
430 descs = del_descs_lastmod;
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];
448 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
450 if ( si->lastmod == LASTMOD_REQ ) {
451 descs = add_descs_lastmod;
456 for ( i = 0; descs[i] != NULL; i++ ) {
457 tmp = ( char ** ) ch_realloc( si->attrs,
458 ( n + 2 ) * sizeof( char * ));
461 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
463 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
467 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
468 si->attrs[ n ] = NULL;
471 /* Send LDAP SYNC search */
473 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
474 si->attrs, si->attrsonly, NULL, NULL,
477 if( rc != LDAP_SUCCESS ) {
478 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
479 ldap_err2string( rc ), rc );
483 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
485 for ( msg = ldap_first_message( ld, res );
487 msg = ldap_next_message( ld, msg ) )
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 );
500 for ( ml = modlist; ml != NULL; ml = mlnext ) {
501 mlnext = ml->sml_next;
506 case LDAP_RES_SEARCH_REFERENCE:
508 LDAP_LOG( OPERATION, ERR,
509 "do_syncrepl : reference received\n", 0, 0, 0 );
511 Debug( LDAP_DEBUG_ANY,
512 "do_syncrepl : reference received\n", 0, 0, 0 );
516 case LDAP_RES_SEARCH_RESULT:
517 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
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 );
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 );
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 );
538 ber_free( ctrl_ber, 1 );
543 ber_free( ctrl_ber, 1 );
547 if ( syncCookie.bv_len ) {
548 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
550 syncrepl_del_nonpresent( ld, &op );
552 ber_free( ctrl_ber, 1 );
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 );
565 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
566 syncrepl_del_nonpresent( ld, &op );
567 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
569 LDAP_LOG( OPERATION, ERR,
570 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
572 Debug( LDAP_DEBUG_ANY,
573 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
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 );
585 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
587 LDAP_LOG( OPERATION, ERR,
588 "do_syncrepl : cookie required\n", 0, 0, 0 );
590 Debug( LDAP_DEBUG_ANY,
591 "do_syncrepl : cookie required\n", 0, 0, 0 );
596 ldap_memfree( retoid );
597 ber_bvfree( retdata );
598 ber_free( res_ber, 1 );
602 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
603 " unknown intermediate "
604 "response\n", 0, 0, 0 );
606 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
607 "unknown intermediate "
608 "response\n", 0, 0, 0 );
610 ldap_memfree( retoid );
611 ber_bvfree( retdata );
617 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
618 "unknown message\n", 0, 0, 0 );
620 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
621 "unknown message\n", 0, 0, 0 );
632 LDAP_LOG( OPERATION, ERR,
633 "do_syncrepl : unknown result\n", 0, 0, 0 );
635 Debug( LDAP_DEBUG_ANY,
636 "do_syncrepl : unknown result\n", 0, 0, 0 );
641 if ( syncCookie.bv_val )
642 ch_free( syncCookie.bv_val );
643 if ( syncUUID.bv_val )
644 ch_free( syncUUID.bv_val );
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 );
655 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
657 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
663 syncrepl_message_to_entry(
667 Modifications **modlist,
669 struct berval *syncUUID,
670 struct berval *syncCookie
674 BerElement *ber = NULL;
676 struct berval bv = {0, NULL};
679 Modifications **modtail = modlist;
680 Backend *be = op->o_bd;
683 char txtbuf[SLAP_TEXT_BUFLEN];
684 size_t textlen = sizeof txtbuf;
686 struct berval **bvals = NULL;
688 struct berval bdn = {0, NULL};
690 struct berval empty_bv = { 0, NULL };
694 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
698 LDAPControl** rctrls = NULL;
699 BerElement* ctrl_ber;
703 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
705 LDAP_LOG( OPERATION, ERR,
706 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
708 Debug( LDAP_DEBUG_ANY,
709 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
714 op->o_tag = LDAP_REQ_ADD;
716 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
718 if ( rc != LDAP_SUCCESS ) {
720 LDAP_LOG( OPERATION, ERR,
721 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
723 Debug( LDAP_DEBUG_ANY,
724 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
729 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
730 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
734 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
736 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
738 if ( tmp.sml_type.bv_val == NULL ) break;
740 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
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;
750 modtail = &mod->sml_next;
753 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
755 LDAP_LOG( OPERATION, ERR,
756 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
758 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
765 tmpber = ldap_get_message_ber( msg );
766 ber = ber_dup( tmpber );
768 ber_scanf( ber, "{xx" );
770 rc = ldap_int_get_controls( ber, &rctrls );
772 if ( rc != LDAP_SUCCESS ) {
774 LDAP_LOG( OPERATION, ERR,
775 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
777 Debug( LDAP_DEBUG_ANY,
778 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
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 );
793 ber_free( ctrl_ber, 1 );
796 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
797 " rctrls absent\n", 0, 0, 0 );
799 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
800 " rctrls absent\n", 0, 0, 0 );
804 if ( *syncstate == LDAP_SYNC_PRESENT ) {
809 if ( *modlist == NULL ) {
811 LDAP_LOG( OPERATION, ERR,
812 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
814 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
819 rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
821 if ( rc != LDAP_SUCCESS ) {
823 LDAP_LOG( OPERATION, ERR,
824 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
826 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
832 rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
833 &text,txtbuf, textlen );
835 if( rc != LDAP_SUCCESS ) {
837 LDAP_LOG( OPERATION, ERR,
838 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
840 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
846 rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
847 if( rc != LDAP_SUCCESS ) {
849 LDAP_LOG( OPERATION, ERR,
850 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
852 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
865 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
867 const struct berval *uuid1 = v_uuid1;
868 const struct berval *uuid2 = v_uuid2;
869 int rc = uuid1->bv_len - uuid2->bv_len;
871 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
879 Modifications* modlist,
881 struct berval* syncUUID,
882 struct berval* syncCookie,
886 Backend *be = op->o_bd;
887 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
889 struct berval csn_bv = {0, NULL};
890 struct berval *syncuuid_bv = NULL;
891 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
893 SlapReply rs = {REP_RESULT};
896 #if 0 /* FIXME : UUID search required first */
898 struct berval filterstr_bv;
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 );
911 if ( syncstate == LDAP_SYNC_PRESENT ) {
918 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
919 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
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 );
929 si->syncUUID = syncUUID;
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 );
937 op->o_req_dn = e->e_name;
938 op->o_req_ndn = e->e_nname;
940 op->o_callback = &cb;
941 cb.sc_response = null_callback;
944 switch ( syncstate ) {
946 case LDAP_SYNC_MODIFY :
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;
957 rc = be->be_add( op, &rs );
958 if ( rc != LDAP_SUCCESS ) {
959 if ( rc == LDAP_ALREADY_EXISTS ) {
961 } else if ( rc == LDAP_REFERRAL ||
962 rc == LDAP_NO_SUCH_OBJECT ||
963 rc == DB_NOTFOUND ) {
964 syncrepl_add_glue(ld, op, e,
966 syncUUID, syncCookie);
969 LDAP_LOG( OPERATION, ERR,
970 "be_add failed (%d)\n",
973 Debug( LDAP_DEBUG_ANY,
974 "be_add failed (%d)\n",
983 LDAP_LOG( OPERATION, ERR,
984 "be_modify failed (%d)\n", rc, 0, 0 );
986 Debug( LDAP_DEBUG_ANY,
987 "be_modify failed (%d)\n", rc, 0, 0 );
994 case LDAP_SYNC_DELETE :
995 op->o_tag = LDAP_REQ_DELETE;
996 be->be_delete( op, &rs );
1001 LDAP_LOG( OPERATION, ERR,
1002 "unknown syncstate\n", 0, 0, 0 );
1004 Debug( LDAP_DEBUG_ANY,
1005 "unknown syncstate\n", 0, 0, 0 );
1012 syncrepl_del_nonpresent(
1017 Backend* be = op->o_bd;
1018 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1020 struct berval base_bv = {0, NULL};
1022 SlapReply rs = {REP_RESULT};
1023 struct berval filterstr_bv = {0, NULL};
1024 struct nonpresent_entry *np_list, *np_prev;
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 );
1030 filter = str2filter( si->filterstr );
1032 cb.sc_response = nonpresent_callback;
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 );
1046 be->be_search( op, &rs );
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 );
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;
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;
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 );
1083 Modifications* modlist,
1085 struct berval* syncUUID,
1086 struct berval* syncCookie
1089 Backend *be = op->o_bd;
1090 syncinfo_t *si = op->o_callback->sc_private;
1091 struct berval uuid_bv = {0, NULL};
1095 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1098 struct berval dn = {0, NULL};
1099 struct berval pdn = {0, NULL};
1100 struct berval ndn = {0, NULL};
1101 struct berval rdn = {0, NULL};
1103 SlapReply rs = {REP_RESULT};
1104 Connection *conn = op->o_conn;
1106 op->o_tag = LDAP_REQ_ADD;
1107 op->o_callback = &cb;
1108 cb.sc_response = null_callback;
1111 ber_dupbv( &dn, &e->e_nname );
1112 ber_dupbv( &pdn, &e->e_nname );
1114 while ( !be_issuffix ( be, &pdn )) {
1115 dnParent( &dn, &pdn );
1116 ch_free( dn.bv_val );
1117 ber_dupbv( &dn, &pdn );
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 );
1128 for ( k = 0; k < j; k++ ) {
1129 dnParent( &dn, &pdn );
1130 ch_free( dn.bv_val );
1131 ber_dupbv( &dn, &pdn );
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 );
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;
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;
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;
1164 rc = be->be_add ( op, &rs );
1165 if ( rc == LDAP_SUCCESS )
1166 be_entry_release_w( op, e );
1171 op->o_req_dn = glue->e_name;
1172 op->o_req_ndn = glue->e_nname;
1174 rc = be->be_add ( op, &rs );
1175 if ( rc == LDAP_SUCCESS ) {
1176 be_entry_release_w( op, glue );
1178 /* incl. ALREADY EXIST */
1188 syncrepl_updateCookie(
1192 struct berval *syncCookie
1195 Backend *be = op->o_bd;
1196 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1198 Modifications *mlnext;
1200 Modifications *modlist;
1201 Modifications **modtail = &modlist;
1203 struct berval* ocbva = NULL;
1204 struct berval* cnbva = NULL;
1205 struct berval* ssbva = NULL;
1206 struct berval* scbva = NULL;
1211 char txtbuf[SLAP_TEXT_BUFLEN];
1212 size_t textlen = sizeof txtbuf;
1217 struct berval sub_bv = { 0, NULL };
1218 struct berval psubrdn = { 0, NULL };
1221 SlapReply rs = {REP_RESULT};
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 ));
1228 /* update in memory cookie */
1229 if ( si->syncCookie != NULL ) {
1230 ber_bvfree( si->syncCookie );
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;
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;
1248 modtail = &mod->sml_next;
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;
1264 modtail = &mod->sml_next;
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;
1278 modtail = &mod->sml_next;
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;
1292 modtail = &mod->sml_next;
1294 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1296 if ( rc != LDAP_SUCCESS ) {
1298 LDAP_LOG( OPERATION, ERR,
1299 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1301 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1306 op->o_tag = LDAP_REQ_ADD;
1307 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1309 if( rc != LDAP_SUCCESS ) {
1311 LDAP_LOG( OPERATION, ERR,
1312 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1314 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1319 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
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 );
1328 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1330 if( rc != LDAP_SUCCESS ) {
1332 LDAP_LOG( OPERATION, ERR,
1333 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1335 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1340 cb.sc_response = null_callback;
1343 op->o_callback = &cb;
1344 op->o_req_dn = e->e_name;
1345 op->o_req_ndn = e->e_nname;
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;
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 ) {
1366 LDAP_LOG( OPERATION, ERR,
1367 "cookie will be non-persistent\n",
1370 Debug( LDAP_DEBUG_ANY,
1371 "cookie will be non-persistent\n",
1376 LDAP_LOG( OPERATION, ERR,
1377 "be_add failed (%d)\n",
1380 Debug( LDAP_DEBUG_ANY,
1381 "be_add failed (%d)\n",
1390 LDAP_LOG( OPERATION, ERR,
1391 "be_modify failed (%d)\n", rc, 0, 0 );
1393 Debug( LDAP_DEBUG_ANY,
1394 "be_modify failed (%d)\n", rc, 0, 0 );
1404 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1405 mlnext = ml->sml_next;
1414 int slap_mods_check_syncrepl(
1416 Modifications **mlp,
1423 Backend *be = op->o_bd;
1424 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1425 AttributeDescription** descs;
1427 Modifications *prevml = NULL;
1428 Modifications *nextml = NULL;
1429 Modifications *ml = *mlp;
1431 while ( ml != NULL ) {
1432 AttributeDescription *ad = NULL;
1434 /* convert to attribute description */
1435 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1437 if( rc != LDAP_SUCCESS ) {
1438 snprintf( textbuf, textlen, "%s: %s",
1439 ml->sml_type.bv_val, *text );
1446 if ( si->lastmod == LASTMOD_REQ ) {
1447 descs = del_descs_lastmod;
1452 for ( i = 0; descs[i] != NULL; i++ ) {
1453 if ( ad == descs[i] ) {
1454 if ( prevml == NULL ) {
1455 mlp = &ml->sml_next;
1458 prevml->sml_next = ml->sml_next;
1460 slap_mod_free( &ml->sml_mod, 0 );
1461 nextml = ml->sml_next;
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 );
1475 return LDAP_UNDEFINED_TYPE;
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 );
1485 return LDAP_UNDEFINED_TYPE;
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 );
1494 return LDAP_UNDEFINED_TYPE;
1497 if ( is_at_obsolete( ad->ad_type ) &&
1498 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1500 * attribute is obsolete,
1501 * only allow replace/delete with no values
1503 snprintf( textbuf, textlen,
1504 "%s: attribute is obsolete",
1505 ml->sml_type.bv_val );
1507 return LDAP_CONSTRAINT_VIOLATION;
1513 if( ml->sml_values != NULL ) {
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;
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 );
1527 return LDAP_INVALID_SYNTAX;
1531 * check that each value is valid per syntax
1532 * and pretty if appropriate
1534 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1535 struct berval pval = {0, NULL};
1537 rc = pretty( ad->ad_type->sat_syntax,
1538 &ml->sml_values[nvals], &pval, ctx );
1540 rc = validate( ad->ad_type->sat_syntax,
1541 &ml->sml_values[nvals] );
1545 snprintf( textbuf, textlen,
1546 "%s: value #%ld invalid per syntax",
1547 ml->sml_type.bv_val, (long) nvals );
1549 return LDAP_INVALID_SYNTAX;
1553 ber_memfree( ml->sml_values[nvals].bv_val );
1554 ml->sml_values[nvals] = pval;
1559 * a rough single value check... an additional check is needed
1560 * to catch add of single value to existing single valued attribute
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 );
1568 return LDAP_CONSTRAINT_VIOLATION;
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 );
1580 LDAP_LOG( OPERATION, DETAIL1,
1581 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1583 Debug( LDAP_DEBUG_ANY,
1584 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1586 snprintf( textbuf, textlen,
1587 "%s: value #%ld normalization failed",
1588 ml->sml_type.bv_val, (long) nvals );
1593 ml->sml_nvalues[nvals].bv_val = NULL;
1594 ml->sml_nvalues[nvals].bv_len = 0;
1601 return LDAP_SUCCESS;
1605 int slap_mods_opattrs_syncrepl(
1607 Modifications *mods,
1608 Modifications **modtail,
1610 char *textbuf, size_t textlen )
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 ];
1619 Backend *be = op->o_bd;
1620 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1622 int mop = LDAP_MOD_REPLACE;
1624 assert( modtail != NULL );
1625 assert( *modtail == NULL );
1627 if( si->lastmod == LASTMOD_GEN ) {
1629 time_t now = slap_get_time();
1631 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1632 ltm = gmtime( &now );
1633 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1635 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1636 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1637 csn.bv_val = csnbuf;
1639 timestamp.bv_val = timebuf;
1640 timestamp.bv_len = strlen(timebuf);
1642 if( op->o_dn.bv_len == 0 ) {
1643 name.bv_val = SLAPD_ANONYMOUS;
1644 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1652 if( op->o_tag == LDAP_REQ_ADD ) {
1653 struct berval tmpval = {0, NULL};
1655 if( global_schemacheck ) {
1656 int rc = mods_structural_class( mods, &tmpval,
1657 text, textbuf, textlen );
1658 if( rc != LDAP_SUCCESS ) {
1662 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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 );
1677 modtail = &mod->sml_next;
1680 if( si->lastmod == LASTMOD_GEN ) {
1681 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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 );
1696 modtail = &mod->sml_next;
1698 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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], ×tamp );
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;
1709 modtail = &mod->sml_next;
1713 if( si->lastmod == LASTMOD_GEN ) {
1714 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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;
1725 modtail = &mod->sml_next;
1727 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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 );
1742 modtail = &mod->sml_next;
1744 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
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], ×tamp );
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;
1755 modtail = &mod->sml_next;
1759 return LDAP_SUCCESS;
1764 int slap_mods2entry_syncrepl(
1765 Modifications *mods,
1769 char *textbuf, size_t textlen )
1771 Attribute **tail = &(*e)->e_attrs;
1772 assert( *tail == NULL );
1776 for( ; mods != NULL; mods = mods->sml_next ) {
1779 assert( mods->sml_desc != NULL );
1781 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1783 if( attr != NULL ) {
1784 #define SLURPD_FRIENDLY
1785 #ifdef SLURPD_FRIENDLY
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;
1795 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1798 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1803 attr->a_vals = ch_realloc( attr->a_vals,
1804 sizeof( struct berval ) * (i+j) );
1806 /* should check for duplicates */
1808 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1809 sizeof( struct berval ) * j );
1811 if( attr->a_nvals ) {
1812 attr->a_nvals = ch_realloc( attr->a_nvals,
1813 sizeof( struct berval ) * (i+j) );
1815 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1816 sizeof( struct berval ) * j );
1818 /* trim the mods array */
1819 ch_free( mods->sml_nvalues );
1820 mods->sml_nvalues = NULL;
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;
1832 if( mods->sml_values[1].bv_val != NULL ) {
1833 /* check for duplicates */
1835 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
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;
1855 const char *text = NULL;
1856 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1858 rc = modify_check_duplicates( mods->sml_desc, mr,
1859 NULL, mods->sml_bvalues, 0,
1860 &text, textbuf, sizeof( textbuf ) );
1862 if ( rc != LDAP_SUCCESS ) {
1868 attr = ch_calloc( 1, sizeof(Attribute) );
1870 /* move ad to attr structure */
1871 attr->a_desc = mods->sml_desc;
1873 /* move values to attr structure */
1874 /* should check for duplicates */
1875 attr->a_vals = mods->sml_values;
1877 attr->a_nvals = mods->sml_nvalues;
1880 tail = &attr->a_next;
1883 return LDAP_SUCCESS;
1887 avl_ber_bvfree( void *bv )
1892 if ( ((struct berval *)bv)->bv_val != NULL ) {
1893 ber_memfree ( ((struct berval *)bv)->bv_val );
1895 ber_memfree ( (char *) bv );
1904 syncinfo_t *si = op->o_callback->sc_private;
1907 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1909 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1912 si->syncCookie = NULL;
1914 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1916 return LDAP_SUCCESS;
1920 nonpresent_callback(
1925 syncinfo_t *si = op->o_callback->sc_private;
1928 struct berval* present_uuid = NULL;
1930 SlapReply rs_cb = {REP_RESULT};
1931 struct nonpresent_entry *np_entry;
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 );
1942 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
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 );
1951 avl_delete( &si->presentlist,
1952 &a->a_vals[0], syncuuid_cmp );
1954 return LDAP_SUCCESS;
1956 return LDAP_SUCCESS;
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 ) {
1973 LDAP_LOG( OPERATION, ERR,
1974 "null_callback : error code 0x%x\n",
1977 Debug( LDAP_DEBUG_ANY,
1978 "null_callback : error code 0x%x\n",
1982 return LDAP_SUCCESS;