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_del_nonpresent( syncinfo_t *, LDAP *, Operation * );
44 syncrepl_add_glue( syncinfo_t *, LDAP *, Operation*, Entry*, Modifications*,
45 int, struct berval*, struct berval* );
48 slap_mods_check_syncrepl( syncinfo_t *, Operation *, Modifications **,
49 const char **, char *, size_t, void *ctx );
52 slap_mods_opattrs_syncrepl( syncinfo_t *, Operation *, Modifications *,
53 Modifications **, const char **, char *, size_t );
56 slap_mods2entry_syncrepl( syncinfo_t *, Modifications *, Entry **, int,
57 const char **, char *, size_t );
59 /* callback functions */
60 static int cookie_callback( struct slap_op *, struct slap_rep * );
61 static int dn_callback( struct slap_op *, struct slap_rep * );
62 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
63 static int null_callback( struct slap_op *, struct slap_rep * );
65 static AttributeDescription **add_descs;
66 static AttributeDescription **add_descs_lastmod;
67 static AttributeDescription **del_descs;
68 static AttributeDescription **del_descs_lastmod;
70 struct runqueue_s syncrepl_rq;
75 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
76 add_descs[0] = slap_schema.si_ad_objectClass;
79 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
80 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
81 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
82 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
83 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
84 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
85 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
86 add_descs_lastmod[6] = NULL;
88 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
89 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
90 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
91 del_descs[2] = slap_schema.si_ad_hasSubordinates;
92 del_descs[3] = slap_schema.si_ad_creatorsName;
93 del_descs[4] = slap_schema.si_ad_modifiersName;
94 del_descs[5] = slap_schema.si_ad_createTimestamp;
95 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
96 del_descs[7] = slap_schema.si_ad_entryCSN;
99 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
100 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
101 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
102 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
103 del_descs_lastmod[3] = NULL;
110 LDAPControl **sctrls,
111 LDAPControl **cctrls,
119 BerElement *sync_ber = NULL;
120 struct berval *sync_bvalp = NULL;
124 struct timeval timeout;
126 /* setup LDAP SYNC control */
127 sync_ber = ber_alloc_t( LBER_USE_DER );
128 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, NULL );
130 if ( si->syncCookie ) {
131 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
133 ber_printf( sync_ber, "{e}", abs(si->type) );
136 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
137 ber_free( sync_ber, 1 );
140 ber_free( sync_ber, 1 );
142 ctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), NULL );
144 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
145 c[0].ldctl_value = (*sync_bvalp);
146 c[0].ldctl_iscritical = si->type < 0;
150 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
151 c[1].ldctl_value.bv_val = si->authzId;
152 c[1].ldctl_value.bv_len = strlen( si->authzId );
153 c[1].ldctl_iscritical = 1;
161 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
163 ber_bvfree( sync_bvalp );
166 if ( err != LDAP_OPT_SUCCESS )
167 fprintf( stderr, "Could not set controls : %d\n", err );
169 timeout.tv_sec = si->tlimit > 0 ? si->tlimit : 1;
171 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
172 si->attrs, si->attrsonly, sctrls, cctrls,
173 si->tlimit < 0 ? NULL : &timeout,
174 si->slimit, msgidp );
184 struct re_s* rtask = arg;
185 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
186 Backend *be = si->be;
188 SlapReply rs = {REP_RESULT};
191 LDAPControl **sctrls = NULL;
192 LDAPControl **rctrls = NULL;
193 LDAPControl *rctrlp = NULL;
194 BerElement *sync_ber = NULL;
195 struct berval *sync_bvalp = NULL;
197 BerElement *ctrl_ber = NULL;
198 BerElement *res_ber = NULL;
201 LDAPMessage *res = NULL;
202 LDAPMessage *msg = NULL;
206 int nresponses, nreferences, nextended, npartial;
207 int nresponses_psearch;
209 int cancel_msgid = -1;
211 struct berval *retdata = NULL;
213 int sync_info_arrived = 0;
217 struct berval syncUUID = { 0, NULL };
218 struct berval syncCookie = { 0, NULL };
223 int syncinfo_arrived = 0;
224 int cancel_response = 0;
227 AttributeDescription** descs = NULL;
239 struct berval base_bv = { 0, NULL };
240 struct berval pbase = { 0, NULL };
241 struct berval nbase = { 0, NULL };
242 struct berval sub_bv = { 0, NULL };
243 struct berval psubrdn = { 0, NULL };
244 struct berval nsubrdn = { 0, NULL };
245 struct berval psub = { 0, NULL };
246 struct berval nsub = { 0, NULL };
248 Modifications *modlist = NULL;
249 Modifications *ml, *mlnext;
250 char *def_filter_str = NULL;
253 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
255 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
261 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
262 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
266 /* Init connection to master */
268 rc = ldap_initialize( &ld, si->masteruri );
269 if ( rc != LDAP_SUCCESS ) {
271 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
272 "ldap_initialize failed (%s)\n",
273 si->masteruri, 0, 0 );
275 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
276 "ldap_initialize failed (%s)\n",
277 si->masteruri, 0, 0 );
281 op.o_protocol = LDAP_VERSION3;
282 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
287 rc = ldap_start_tls_s( ld, NULL, NULL );
288 if( rc != LDAP_SUCCESS ) {
290 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
291 "%s: ldap_start_tls failed (%d)\n",
292 si->tls == TLS_CRITICAL ? "Error" : "Warning",
295 Debug( LDAP_DEBUG_ANY,
296 "%s: ldap_start_tls failed (%d)\n",
297 si->tls == TLS_CRITICAL ? "Error" : "Warning",
300 if( si->tls == TLS_CRITICAL )
305 if ( si->bindmethod == LDAP_AUTH_SASL ) {
306 #ifdef HAVE_CYRUS_SASL
309 if ( si->secprops != NULL ) {
310 int err = ldap_set_option( ld,
311 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
313 if( err != LDAP_OPT_SUCCESS ) {
315 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
316 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
317 si->masteruri, si->secprops, 0 );
319 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
320 "(%s,SECPROPS,\"%s\") failed!\n",
321 si->masteruri, si->secprops, NULL );
327 defaults = lutil_sasl_defaults( ld,
334 rc = ldap_sasl_interactive_bind_s( ld,
342 if ( rc != LDAP_SUCCESS ) {
344 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
345 "ldap_sasl_interactive_bind_s failed (%d)\n",
348 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
349 "ldap_sasl_interactive_bind_s failed (%d)\n",
354 #else /* HAVE_CYRUS_SASL */
355 fprintf( stderr, "not compiled with SASL support\n" );
359 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
360 if ( rc != LDAP_SUCCESS ) {
362 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
363 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
365 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
366 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
372 /* set thread context in syncinfo */
375 /* set memory context */
376 #define SLAB_SIZE 1048576
378 memctx = sl_mem_create( memsiz, ctx );
379 op.o_tmpmemctx = memctx;
380 op.o_tmpmfuncs = &sl_mfuncs;
382 op.o_tag = LDAP_REQ_SEARCH;
383 op.o_dn = si->updatedn;
384 op.o_ndn = si->updatedn;
386 op.o_time = slap_get_time();
387 op.o_managedsait = 1;
388 op.o_threadctx = si->ctx;
391 op.o_connid = op.o_conn->c_connid;
392 op.ors_scope = LDAP_SCOPE_BASE;
393 op.ors_deref = LDAP_DEREF_NEVER;
396 op.ors_attrsonly = 0;
398 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
399 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
403 conn.c_send_ldap_result = slap_send_ldap_result;
404 conn.c_send_search_entry = slap_send_search_entry;
405 conn.c_send_search_reference = slap_send_search_reference;
407 /* get syncrepl cookie of shadow replica from subentry */
408 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
409 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
411 sprintf( substr, "cn=syncrepl%d", si->id );
412 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
413 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
415 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
416 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
418 ch_free( base_bv.bv_val );
419 ch_free( pbase.bv_val );
420 ch_free( nbase.bv_val );
421 ch_free( sub_bv.bv_val );
422 ch_free( psubrdn.bv_val );
423 ch_free( nsubrdn.bv_val );
425 /* set callback function */
426 cb.sc_response = cookie_callback;
429 /* search subentry to retrieve cookie */
430 si->syncCookie = NULL;
431 be->be_search( &op, &rs );
433 ch_free( op.o_req_dn.bv_val );
434 ch_free( op.o_req_ndn.bv_val );
435 filter_free( op.ors_filter );
436 ch_free( op.ors_filterstr.bv_val );
438 psub = be->be_nsuffix[0];
440 /* Delete Attributes */
441 if ( si->lastmod == LASTMOD_REQ ) {
442 descs = del_descs_lastmod;
447 for ( i = 0; descs[i] != NULL; i++ ) {
448 for ( j = 0; si->attrs[j] != NULL; j++ ) {
449 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
450 ch_free( si->attrs[j] );
451 for ( k = j; si->attrs[k] != NULL; k++ ) {
452 si->attrs[k] = si->attrs[k+1];
460 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
462 if ( si->lastmod == LASTMOD_REQ ) {
463 descs = add_descs_lastmod;
468 for ( i = 0; descs[i] != NULL; i++ ) {
469 tmp = ( char ** ) ch_realloc( si->attrs,
470 ( n + 2 ) * sizeof( char * ));
473 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
475 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
479 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
480 si->attrs[ n ] = NULL;
483 rc = ldap_sync_search( si, ld, NULL, NULL, &msgid );
484 if( rc != LDAP_SUCCESS ) {
485 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
486 ldap_err2string( rc ), rc );
490 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
492 for ( msg = ldap_first_message( ld, res );
494 msg = ldap_next_message( ld, msg ) )
496 switch( ldap_msgtype( msg ) ) {
497 case LDAP_RES_SEARCH_ENTRY:
498 entry = syncrepl_message_to_entry( si, ld, &op, msg,
499 &modlist, &syncstate, &syncUUID, &syncCookie );
500 rc_efree = syncrepl_entry( si, ld, &op, entry, modlist,
501 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
502 if ( syncCookie.bv_len ) {
503 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
507 for ( ml = modlist; ml != NULL; ml = mlnext ) {
508 mlnext = ml->sml_next;
513 case LDAP_RES_SEARCH_REFERENCE:
515 LDAP_LOG( OPERATION, ERR,
516 "do_syncrepl : reference received\n", 0, 0, 0 );
518 Debug( LDAP_DEBUG_ANY,
519 "do_syncrepl : reference received\n", 0, 0, 0 );
523 case LDAP_RES_SEARCH_RESULT:
524 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
527 ctrl_ber = ber_alloc_t( LBER_USE_DER );
528 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
529 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
530 ber_reset( ctrl_ber, 1 );
532 ber_scanf( ctrl_ber, "{" /*"}"*/);
533 if ( ber_peek_tag( ctrl_ber, &len )
534 == LDAP_SYNC_TAG_COOKIE ) {
535 ber_scanf( ctrl_ber, "o", &syncCookie );
538 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
539 if ( cancel_response ) {
540 if ( syncCookie.bv_len ) {
541 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
544 ber_free( ctrl_ber, 1 );
549 ber_free( ctrl_ber, 1 );
553 if ( syncCookie.bv_len ) {
554 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
556 syncrepl_del_nonpresent( si, ld, &op );
558 ber_free( ctrl_ber, 1 );
563 case LDAP_RES_INTERMEDIATE:
564 rc = ldap_parse_intermediate( ld, msg,
565 &retoid, &retdata, NULL, 0 );
566 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
567 sync_info_arrived = 1;
568 res_ber = ber_init( retdata );
569 ber_scanf( res_ber, "{e" /*"}"*/, &syncstate );
571 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
572 syncrepl_del_nonpresent( si, ld, &op );
573 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
575 LDAP_LOG( OPERATION, ERR,
576 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
578 Debug( LDAP_DEBUG_ANY,
579 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
583 if ( ber_peek_tag( res_ber, &len )
584 == LDAP_SYNC_TAG_COOKIE ) {
585 ber_scanf( res_ber, /*"{"*/ "o}", &syncCookie );
586 if ( syncCookie.bv_len ) {
587 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
590 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
592 LDAP_LOG( OPERATION, ERR,
593 "do_syncrepl : cookie required\n", 0, 0, 0 );
595 Debug( LDAP_DEBUG_ANY,
596 "do_syncrepl : cookie required\n", 0, 0, 0 );
601 ldap_memfree( retoid );
602 ber_bvfree( retdata );
603 ber_free( res_ber, 1 );
607 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
608 " unknown intermediate "
609 "response\n", 0, 0, 0 );
611 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
612 "unknown intermediate response (%d)\n",
615 ldap_memfree( retoid );
616 ber_bvfree( retdata );
622 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
623 "unknown message\n", 0, 0, 0 );
625 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
626 "unknown message\n", 0, 0, 0 );
637 LDAP_LOG( OPERATION, ERR,
638 "do_syncrepl : unknown result\n", 0, 0, 0 );
640 Debug( LDAP_DEBUG_ANY,
641 "do_syncrepl : unknown result\n", 0, 0, 0 );
646 if ( syncCookie.bv_val )
647 ch_free( syncCookie.bv_val );
648 if ( syncUUID.bv_val )
649 ch_free( syncUUID.bv_val );
655 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
656 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
657 if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
658 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
660 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
662 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
668 syncrepl_message_to_entry(
673 Modifications **modlist,
675 struct berval *syncUUID,
676 struct berval *syncCookie
680 BerElement *ber = NULL;
682 struct berval bv = {0, NULL};
685 Modifications **modtail = modlist;
686 Backend *be = op->o_bd;
689 char txtbuf[SLAP_TEXT_BUFLEN];
690 size_t textlen = sizeof txtbuf;
692 struct berval **bvals = NULL;
694 struct berval bdn = {0, NULL};
696 struct berval empty_bv = { 0, NULL };
702 LDAPControl** rctrls = NULL;
703 BerElement* ctrl_ber;
709 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
711 LDAP_LOG( OPERATION, ERR,
712 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
714 Debug( LDAP_DEBUG_ANY,
715 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
720 op->o_tag = LDAP_REQ_ADD;
722 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
724 if ( rc != LDAP_SUCCESS ) {
726 LDAP_LOG( OPERATION, ERR,
727 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
729 Debug( LDAP_DEBUG_ANY,
730 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
735 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
736 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, NULL );
740 while ( ber_remaining( ber ) ) {
741 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
743 if ( tag == LBER_ERROR ) break;
744 if ( tmp.sml_type.bv_val == NULL ) break;
746 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
748 mod->sml_op = LDAP_MOD_REPLACE;
749 mod->sml_next = NULL;
750 mod->sml_desc = NULL;
751 mod->sml_type = tmp.sml_type;
752 mod->sml_bvalues = tmp.sml_bvalues;
753 mod->sml_nvalues = tmp.sml_bvalues;
756 modtail = &mod->sml_next;
759 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
761 LDAP_LOG( OPERATION, ERR,
762 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
764 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
771 tmpber = ldap_get_message_ber( msg );
772 ber = ber_dup( tmpber );
774 ber_scanf( ber, "{xx" );
776 rc = ldap_int_get_controls( ber, &rctrls );
778 if ( rc != LDAP_SUCCESS ) {
780 LDAP_LOG( OPERATION, ERR,
781 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
783 Debug( LDAP_DEBUG_ANY,
784 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
791 ctrl_ber = ber_alloc_t( LBER_USE_DER );
792 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
793 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
794 ber_reset( ctrl_ber, 1 );
795 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
796 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
797 ber_scanf( ctrl_ber, "o}", syncCookie );
799 ber_free( ctrl_ber, 1 );
802 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
803 " rctrls absent\n", 0, 0, 0 );
805 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
806 " rctrls absent\n", 0, 0, 0 );
810 if ( *syncstate == LDAP_SYNC_PRESENT ) {
813 } else if ( *syncstate == LDAP_SYNC_DELETE ) {
817 if ( *modlist == NULL ) {
819 LDAP_LOG( OPERATION, ERR,
820 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
822 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
827 rc = slap_mods_check_syncrepl( si, op, modlist,
828 &text, txtbuf, textlen, NULL );
830 if ( rc != LDAP_SUCCESS ) {
832 LDAP_LOG( OPERATION, ERR,
833 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
835 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
841 rc = slap_mods_opattrs_syncrepl( si, op, *modlist, modtail,
842 &text,txtbuf, textlen );
844 if( rc != LDAP_SUCCESS ) {
846 LDAP_LOG( OPERATION, ERR,
847 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
849 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
855 rc = slap_mods2entry_syncrepl( si, *modlist, &e, 1, &text, txtbuf, textlen);
856 if( rc != LDAP_SUCCESS ) {
858 LDAP_LOG( OPERATION, ERR,
859 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
861 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
874 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
876 const struct berval *uuid1 = v_uuid1;
877 const struct berval *uuid2 = v_uuid2;
878 int rc = uuid1->bv_len - uuid2->bv_len;
880 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
889 Modifications* modlist,
891 struct berval* syncUUID,
892 struct berval* syncCookie,
896 Backend *be = op->o_bd;
898 struct berval csn_bv = {0, NULL};
899 struct berval *syncuuid_bv = NULL;
900 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
902 SlapReply rs = {REP_RESULT};
903 int rc = LDAP_SUCCESS;
905 struct berval base_bv = {0, NULL};
913 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
914 syncuuid_bv = ber_dupbv( NULL, syncUUID );
915 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
916 syncuuid_cmp, avl_dup_error );
919 if ( syncstate == LDAP_SYNC_PRESENT ) {
926 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
928 strcpy( filterstr, "entryUUID=" );
929 strcat( filterstr, syncUUID->bv_val );
932 si->syncUUID = syncUUID;
933 si->syncUUID_ndn = NULL;
935 filter = str2filter( filterstr );
936 ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
937 ch_free( filterstr );
938 op->ors_filter = filter;
939 op->ors_scope = LDAP_SCOPE_SUBTREE;
941 /* get syncrepl cookie of shadow replica from subentry */
942 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
943 dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
944 ch_free( base_bv.bv_val );
946 /* set callback function */
947 op->o_callback = &cb;
948 cb.sc_response = dn_callback;
951 be->be_search( op, &rs );
953 ch_free( op->o_req_dn.bv_val );
954 ch_free( op->o_req_ndn.bv_val );
955 filter_free( op->ors_filter );
956 ch_free( op->ors_filterstr.bv_val );
958 cb.sc_response = null_callback;
963 if ( si->syncUUID_ndn ) {
964 op->o_req_dn = *si->syncUUID_ndn;
965 op->o_req_ndn = *si->syncUUID_ndn;
966 op->o_tag = LDAP_REQ_DELETE;
967 rc = be->be_delete( op, &rs );
970 switch ( syncstate ) {
972 case LDAP_SYNC_MODIFY :
974 if ( rc == LDAP_SUCCESS ||
975 rc == LDAP_REFERRAL ||
976 rc == LDAP_NO_SUCH_OBJECT ) {
978 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
979 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
982 op->o_tag = LDAP_REQ_ADD;
984 op->o_req_dn = e->e_name;
985 op->o_req_ndn = e->e_nname;
986 rc = be->be_add( op, &rs );
988 if ( rc != LDAP_SUCCESS ) {
989 if ( rc == LDAP_ALREADY_EXISTS ) {
990 op->o_tag = LDAP_REQ_MODIFY;
991 op->orm_modlist = modlist;
992 op->o_req_dn = e->e_name;
993 op->o_req_ndn = e->e_nname;
994 rc = be->be_modify( op, &rs );
995 } else if ( rc == LDAP_REFERRAL ||
996 rc == LDAP_NO_SUCH_OBJECT ) {
997 syncrepl_add_glue( si, ld, op, e,
999 syncUUID, syncCookie);
1002 LDAP_LOG( OPERATION, ERR,
1003 "be_modify failed (%d)\n",
1006 Debug( LDAP_DEBUG_ANY,
1007 "be_modify failed (%d)\n",
1016 LDAP_LOG( OPERATION, ERR,
1017 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1019 Debug( LDAP_DEBUG_ANY,
1020 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1027 case LDAP_SYNC_DELETE :
1028 /* Already deleted */
1033 LDAP_LOG( OPERATION, ERR,
1034 "unknown syncstate\n", 0, 0, 0 );
1036 Debug( LDAP_DEBUG_ANY,
1037 "unknown syncstate\n", 0, 0, 0 );
1044 syncrepl_del_nonpresent(
1050 Backend* be = op->o_bd;
1052 struct berval base_bv = {0, NULL};
1054 SlapReply rs = {REP_RESULT};
1055 struct berval filterstr_bv = {0, NULL};
1056 struct nonpresent_entry *np_list, *np_prev;
1058 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1059 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1060 ch_free( base_bv.bv_val );
1062 filter = str2filter( si->filterstr );
1064 cb.sc_response = nonpresent_callback;
1067 op->o_callback = &cb;
1068 op->o_tag = LDAP_REQ_SEARCH;
1069 op->ors_scope = si->scope;
1070 op->ors_deref = LDAP_DEREF_NEVER;
1073 op->ors_attrsonly = 0;
1074 op->ors_attrs = NULL;
1075 op->ors_filter = filter;
1076 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1078 be->be_search( op, &rs );
1080 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1081 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1082 while ( np_list != NULL ) {
1083 LDAP_LIST_REMOVE( np_list, np_link );
1085 np_list = LDAP_LIST_NEXT( np_list, np_link );
1086 op->o_tag = LDAP_REQ_DELETE;
1087 op->o_callback = &cb;
1088 cb.sc_response = null_callback;
1090 op->o_req_dn = *np_prev->dn;
1091 op->o_req_ndn = *np_prev->ndn;
1092 op->o_bd->be_delete( op, &rs );
1093 ber_bvfree( np_prev->dn );
1094 ber_bvfree( np_prev->ndn );
1095 op->o_req_dn.bv_val = NULL;
1096 op->o_req_ndn.bv_val = NULL;
1101 if ( op->o_req_dn.bv_val )
1102 ch_free( op->o_req_dn.bv_val );
1103 if ( op->o_req_ndn.bv_val )
1104 ch_free( op->o_req_ndn.bv_val );
1105 filter_free( op->ors_filter );
1106 ch_free( op->ors_filterstr.bv_val );
1118 Modifications* modlist,
1120 struct berval* syncUUID,
1121 struct berval* syncCookie
1124 Backend *be = op->o_bd;
1125 struct berval uuid_bv = {0, NULL};
1129 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1132 struct berval dn = {0, NULL};
1133 struct berval pdn = {0, NULL};
1134 struct berval ndn = {0, NULL};
1135 struct berval rdn = {0, NULL};
1137 SlapReply rs = {REP_RESULT};
1138 Connection *conn = op->o_conn;
1140 op->o_tag = LDAP_REQ_ADD;
1141 op->o_callback = &cb;
1142 cb.sc_response = null_callback;
1145 ber_dupbv( &dn, &e->e_nname );
1146 ber_dupbv( &pdn, &e->e_nname );
1148 while ( !be_issuffix ( be, &pdn )) {
1149 dnParent( &dn, &pdn );
1150 ch_free( dn.bv_val );
1151 ber_dupbv( &dn, &pdn );
1155 for ( i = 0; i <= levels; i++ ) {
1156 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1157 ch_free( dn.bv_val );
1158 ch_free( pdn.bv_val );
1159 ber_dupbv( &dn, &e->e_nname );
1160 ber_dupbv( &pdn, &e->e_nname );
1162 for ( k = 0; k < j; k++ ) {
1163 dnParent( &dn, &pdn );
1164 ch_free( dn.bv_val );
1165 ber_dupbv( &dn, &pdn );
1168 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1169 ber_dupbv( &glue->e_name, &pdn );
1170 ber_dupbv( &glue->e_nname, &ndn );
1171 ch_free( dn.bv_val );
1172 ch_free( pdn.bv_val );
1173 ch_free( ndn.bv_val );
1175 a = ch_calloc( 1, sizeof( Attribute ));
1176 a->a_desc = slap_schema.si_ad_objectClass;
1177 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1178 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1179 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1180 a->a_vals[2].bv_len = 0;
1181 a->a_vals[2].bv_val = NULL;
1182 a->a_next = glue->e_attrs;
1185 a = ch_calloc( 1, sizeof( Attribute ));
1186 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1187 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1188 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1189 a->a_vals[1].bv_len = 0;
1190 a->a_vals[1].bv_val = NULL;
1191 a->a_next = glue->e_attrs;
1194 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1195 op->o_req_dn = e->e_name;
1196 op->o_req_ndn = e->e_nname;
1198 rc = be->be_add ( op, &rs );
1199 if ( rc == LDAP_SUCCESS )
1200 be_entry_release_w( op, e );
1205 op->o_req_dn = glue->e_name;
1206 op->o_req_ndn = glue->e_nname;
1208 rc = be->be_add ( op, &rs );
1209 if ( rc == LDAP_SUCCESS ) {
1210 be_entry_release_w( op, glue );
1212 /* incl. ALREADY EXIST */
1222 syncrepl_updateCookie(
1227 struct berval *syncCookie
1230 Backend *be = op->o_bd;
1232 Modifications *mlnext;
1234 Modifications *modlist;
1235 Modifications **modtail = &modlist;
1237 struct berval* ocbva = NULL;
1238 struct berval* cnbva = NULL;
1239 struct berval* ssbva = NULL;
1240 struct berval* scbva = NULL;
1245 char txtbuf[SLAP_TEXT_BUFLEN];
1246 size_t textlen = sizeof txtbuf;
1251 struct berval sub_bv = { 0, NULL };
1252 struct berval psubrdn = { 0, NULL };
1255 SlapReply rs = {REP_RESULT};
1257 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1258 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1259 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1260 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1262 /* update in memory cookie */
1263 if ( si->syncCookie != NULL ) {
1264 ber_bvfree( si->syncCookie );
1266 si->syncCookie = ber_dupbv( NULL, syncCookie );
1267 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1268 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1269 ber_str2bv( "syncConsumerSubentry",
1270 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1271 ocbva[3].bv_len = 0;
1272 ocbva[3].bv_val = NULL;
1274 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1275 mod->sml_op = LDAP_MOD_REPLACE;
1276 mod->sml_next = NULL;
1277 mod->sml_desc = NULL;
1278 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1279 mod->sml_bvalues = ocbva;
1280 mod->sml_nvalues = ocbva;
1282 modtail = &mod->sml_next;
1284 sprintf( substr, "syncrepl%d", si->id );
1285 sprintf( rdnstr, "cn=%s", substr );
1286 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1287 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1288 cnbva[1].bv_len = 0;
1289 cnbva[1].bv_val = NULL;
1290 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1291 mod->sml_op = LDAP_MOD_REPLACE;
1292 mod->sml_next = NULL;
1293 mod->sml_desc = NULL;
1294 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1295 mod->sml_bvalues = cnbva;
1296 mod->sml_nvalues = cnbva;
1298 modtail = &mod->sml_next;
1300 ber_dupbv( &scbva[0], si->syncCookie );
1301 scbva[1].bv_len = 0;
1302 scbva[1].bv_val = NULL;
1303 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1304 mod->sml_op = LDAP_MOD_REPLACE;
1305 mod->sml_next = NULL;
1306 mod->sml_desc = NULL;
1307 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1308 1, &mod->sml_type );
1309 mod->sml_bvalues = scbva;
1310 mod->sml_nvalues = scbva;
1312 modtail = &mod->sml_next;
1314 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1315 ssbva[1].bv_len = 0;
1316 ssbva[1].bv_val = NULL;
1317 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1318 mod->sml_op = LDAP_MOD_REPLACE;
1319 mod->sml_next = NULL;
1320 mod->sml_desc = NULL;
1321 ber_str2bv( "subtreeSpecification",
1322 strlen("subtreeSpecification"), 1, &mod->sml_type );
1323 mod->sml_bvalues = ssbva;
1324 mod->sml_nvalues = ssbva;
1326 modtail = &mod->sml_next;
1328 rc = slap_mods_check_syncrepl( si, op, &modlist,
1329 &text, txtbuf, textlen, NULL );
1331 if ( rc != LDAP_SUCCESS ) {
1333 LDAP_LOG( OPERATION, ERR,
1334 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1336 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1341 op->o_tag = LDAP_REQ_ADD;
1342 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1343 &text,txtbuf, textlen );
1345 if( rc != LDAP_SUCCESS ) {
1347 LDAP_LOG( OPERATION, ERR,
1348 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1350 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1355 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1357 build_new_dn( &sub_bv, pdn, &psubrdn );
1358 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1359 ch_free( sub_bv.bv_val );
1360 ch_free( psubrdn.bv_val );
1364 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1366 if( rc != LDAP_SUCCESS ) {
1368 LDAP_LOG( OPERATION, ERR,
1369 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1371 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1376 cb.sc_response = null_callback;
1379 op->o_callback = &cb;
1380 op->o_req_dn = e->e_name;
1381 op->o_req_ndn = e->e_nname;
1383 /* update persistent cookie */
1384 update_cookie_retry:
1385 op->o_tag = LDAP_REQ_MODIFY;
1386 op->orm_modlist = modlist;
1387 rc = be->be_modify( op, &rs );
1388 if ( rc != LDAP_SUCCESS ) {
1389 if ( rc == LDAP_REFERRAL ||
1390 rc == LDAP_NO_SUCH_OBJECT ) {
1391 op->o_tag = LDAP_REQ_ADD;
1393 rc = be->be_add( op, &rs );
1394 if ( rc != LDAP_SUCCESS ) {
1395 if ( rc == LDAP_ALREADY_EXISTS ) {
1396 goto update_cookie_retry;
1397 } else if ( rc == LDAP_REFERRAL ||
1398 rc == LDAP_NO_SUCH_OBJECT ) {
1400 LDAP_LOG( OPERATION, ERR,
1401 "cookie will be non-persistent\n",
1404 Debug( LDAP_DEBUG_ANY,
1405 "cookie will be non-persistent\n",
1410 LDAP_LOG( OPERATION, ERR,
1411 "be_add failed (%d)\n",
1414 Debug( LDAP_DEBUG_ANY,
1415 "be_add failed (%d)\n",
1424 LDAP_LOG( OPERATION, ERR,
1425 "be_modify failed (%d)\n", rc, 0, 0 );
1427 Debug( LDAP_DEBUG_ANY,
1428 "be_modify failed (%d)\n", rc, 0, 0 );
1438 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1439 mlnext = ml->sml_next;
1448 int slap_mods_check_syncrepl(
1451 Modifications **mlp,
1458 Backend *be = op->o_bd;
1459 AttributeDescription** descs;
1461 Modifications *prevml = NULL;
1462 Modifications *nextml = NULL;
1463 Modifications *ml = *mlp;
1465 while ( ml != NULL ) {
1466 AttributeDescription *ad = NULL;
1468 /* convert to attribute description */
1469 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1471 if( rc != LDAP_SUCCESS ) {
1472 snprintf( textbuf, textlen, "%s: %s",
1473 ml->sml_type.bv_val, *text );
1480 if ( si->lastmod == LASTMOD_REQ ) {
1481 descs = del_descs_lastmod;
1486 for ( i = 0; descs[i] != NULL; i++ ) {
1487 if ( ad == descs[i] ) {
1488 if ( prevml == NULL ) {
1489 mlp = &ml->sml_next;
1492 prevml->sml_next = ml->sml_next;
1494 slap_mod_free( &ml->sml_mod, 0 );
1495 nextml = ml->sml_next;
1502 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1503 && !slap_ad_is_binary( ad )) {
1504 /* attribute requires binary transfer */
1505 snprintf( textbuf, textlen,
1506 "%s: requires ;binary transfer",
1507 ml->sml_type.bv_val );
1509 return LDAP_UNDEFINED_TYPE;
1512 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1513 && slap_ad_is_binary( ad )) {
1514 /* attribute requires binary transfer */
1515 snprintf( textbuf, textlen,
1516 "%s: disallows ;binary transfer",
1517 ml->sml_type.bv_val );
1519 return LDAP_UNDEFINED_TYPE;
1522 if( slap_ad_is_tag_range( ad )) {
1523 /* attribute requires binary transfer */
1524 snprintf( textbuf, textlen,
1525 "%s: inappropriate use of tag range option",
1526 ml->sml_type.bv_val );
1528 return LDAP_UNDEFINED_TYPE;
1531 if ( is_at_obsolete( ad->ad_type ) &&
1532 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1534 * attribute is obsolete,
1535 * only allow replace/delete with no values
1537 snprintf( textbuf, textlen,
1538 "%s: attribute is obsolete",
1539 ml->sml_type.bv_val );
1541 return LDAP_CONSTRAINT_VIOLATION;
1547 if( ml->sml_values != NULL ) {
1549 slap_syntax_validate_func *validate =
1550 ad->ad_type->sat_syntax->ssyn_validate;
1551 slap_syntax_transform_func *pretty =
1552 ad->ad_type->sat_syntax->ssyn_pretty;
1554 if( !pretty && !validate ) {
1555 *text = "no validator for syntax";
1556 snprintf( textbuf, textlen,
1557 "%s: no validator for syntax %s",
1558 ml->sml_type.bv_val,
1559 ad->ad_type->sat_syntax->ssyn_oid );
1561 return LDAP_INVALID_SYNTAX;
1565 * check that each value is valid per syntax
1566 * and pretty if appropriate
1568 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1569 struct berval pval = {0, NULL};
1571 rc = pretty( ad->ad_type->sat_syntax,
1572 &ml->sml_values[nvals], &pval, ctx );
1574 rc = validate( ad->ad_type->sat_syntax,
1575 &ml->sml_values[nvals] );
1579 snprintf( textbuf, textlen,
1580 "%s: value #%ld invalid per syntax",
1581 ml->sml_type.bv_val, (long) nvals );
1583 return LDAP_INVALID_SYNTAX;
1587 ber_memfree( ml->sml_values[nvals].bv_val );
1588 ml->sml_values[nvals] = pval;
1593 * a rough single value check... an additional check is needed
1594 * to catch add of single value to existing single valued attribute
1596 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1597 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1598 snprintf( textbuf, textlen,
1599 "%s: multiple values provided",
1600 ml->sml_type.bv_val );
1602 return LDAP_CONSTRAINT_VIOLATION;
1605 if( nvals && ad->ad_type->sat_equality &&
1606 ad->ad_type->sat_equality->smr_normalize ) {
1607 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1608 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1609 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1610 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1611 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1614 LDAP_LOG( OPERATION, DETAIL1,
1615 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1617 Debug( LDAP_DEBUG_ANY,
1618 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1620 snprintf( textbuf, textlen,
1621 "%s: value #%ld normalization failed",
1622 ml->sml_type.bv_val, (long) nvals );
1627 ml->sml_nvalues[nvals].bv_val = NULL;
1628 ml->sml_nvalues[nvals].bv_len = 0;
1635 return LDAP_SUCCESS;
1639 int slap_mods_opattrs_syncrepl(
1642 Modifications *mods,
1643 Modifications **modtail,
1645 char *textbuf, size_t textlen )
1647 struct berval name = {0, NULL};
1648 struct berval timestamp = {0, NULL};
1649 struct berval csn = {0, NULL};
1650 struct berval nname = {0, NULL};
1651 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1652 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1654 Backend *be = op->o_bd;
1656 int mop = LDAP_MOD_REPLACE;
1658 assert( modtail != NULL );
1659 assert( *modtail == NULL );
1661 if( si->lastmod == LASTMOD_GEN ) {
1663 time_t now = slap_get_time();
1665 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1666 ltm = gmtime( &now );
1667 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1669 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1670 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1671 csn.bv_val = csnbuf;
1673 timestamp.bv_val = timebuf;
1674 timestamp.bv_len = strlen(timebuf);
1676 if( op->o_dn.bv_len == 0 ) {
1677 name.bv_val = SLAPD_ANONYMOUS;
1678 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1686 if( op->o_tag == LDAP_REQ_ADD ) {
1687 struct berval tmpval = {0, NULL};
1689 if( global_schemacheck ) {
1690 int rc = mods_structural_class( mods, &tmpval,
1691 text, textbuf, textlen );
1692 if( rc != LDAP_SUCCESS ) {
1696 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1698 mod->sml_type.bv_val = NULL;
1699 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1700 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1701 ber_dupbv( &mod->sml_values[0], &tmpval );
1702 mod->sml_values[1].bv_len = 0;
1703 mod->sml_values[1].bv_val = NULL;
1704 assert( mod->sml_values[0].bv_val );
1705 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1706 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1707 mod->sml_nvalues[1].bv_len = 0;
1708 mod->sml_nvalues[1].bv_val = NULL;
1709 assert( mod->sml_nvalues[0].bv_val );
1711 modtail = &mod->sml_next;
1714 if( si->lastmod == LASTMOD_GEN ) {
1715 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1717 mod->sml_type.bv_val = NULL;
1718 mod->sml_desc = slap_schema.si_ad_creatorsName;
1719 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1720 ber_dupbv( &mod->sml_values[0], &name );
1721 mod->sml_values[1].bv_len = 0;
1722 mod->sml_values[1].bv_val = NULL;
1723 assert( mod->sml_values[0].bv_val );
1724 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1725 ber_dupbv( &mod->sml_nvalues[0], &nname );
1726 mod->sml_nvalues[1].bv_len = 0;
1727 mod->sml_nvalues[1].bv_val = NULL;
1728 assert( mod->sml_nvalues[0].bv_val );
1730 modtail = &mod->sml_next;
1732 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1734 mod->sml_type.bv_val = NULL;
1735 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1736 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1737 ber_dupbv( &mod->sml_values[0], ×tamp );
1738 mod->sml_values[1].bv_len = 0;
1739 mod->sml_values[1].bv_val = NULL;
1740 assert( mod->sml_values[0].bv_val );
1741 mod->sml_nvalues = NULL;
1743 modtail = &mod->sml_next;
1747 if( si->lastmod == LASTMOD_GEN ) {
1748 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1750 mod->sml_type.bv_val = NULL;
1751 mod->sml_desc = slap_schema.si_ad_entryCSN;
1752 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1753 ber_dupbv( &mod->sml_values[0], &csn );
1754 mod->sml_values[1].bv_len = 0;
1755 mod->sml_values[1].bv_val = NULL;
1756 assert( mod->sml_values[0].bv_val );
1757 mod->sml_nvalues = NULL;
1759 modtail = &mod->sml_next;
1761 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1763 mod->sml_type.bv_val = NULL;
1764 mod->sml_desc = slap_schema.si_ad_modifiersName;
1765 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1766 ber_dupbv( &mod->sml_values[0], &name );
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 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1771 ber_dupbv( &mod->sml_nvalues[0], &nname );
1772 mod->sml_nvalues[1].bv_len = 0;
1773 mod->sml_nvalues[1].bv_val = NULL;
1774 assert( mod->sml_nvalues[0].bv_val );
1776 modtail = &mod->sml_next;
1778 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1780 mod->sml_type.bv_val = NULL;
1781 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1782 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1783 ber_dupbv( &mod->sml_values[0], ×tamp );
1784 mod->sml_values[1].bv_len = 0;
1785 mod->sml_values[1].bv_val = NULL;
1786 assert( mod->sml_values[0].bv_val );
1787 mod->sml_nvalues = NULL;
1789 modtail = &mod->sml_next;
1793 return LDAP_SUCCESS;
1798 int slap_mods2entry_syncrepl(
1800 Modifications *mods,
1804 char *textbuf, size_t textlen )
1806 Attribute **tail = &(*e)->e_attrs;
1807 assert( *tail == NULL );
1811 for( ; mods != NULL; mods = mods->sml_next ) {
1814 assert( mods->sml_desc != NULL );
1816 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1818 if( attr != NULL ) {
1819 #define SLURPD_FRIENDLY
1820 #ifdef SLURPD_FRIENDLY
1824 snprintf( textbuf, textlen,
1825 "attribute '%s' provided more than once",
1826 mods->sml_desc->ad_cname.bv_val );
1827 return LDAP_TYPE_OR_VALUE_EXISTS;
1830 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1833 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1838 attr->a_vals = ch_realloc( attr->a_vals,
1839 sizeof( struct berval ) * (i+j) );
1841 /* should check for duplicates */
1843 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1844 sizeof( struct berval ) * j );
1846 if( attr->a_nvals ) {
1847 attr->a_nvals = ch_realloc( attr->a_nvals,
1848 sizeof( struct berval ) * (i+j) );
1850 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1851 sizeof( struct berval ) * j );
1853 /* trim the mods array */
1854 ch_free( mods->sml_nvalues );
1855 mods->sml_nvalues = NULL;
1860 snprintf( textbuf, textlen,
1861 "attribute '%s' provided more than once",
1862 mods->sml_desc->ad_cname.bv_val );
1863 return LDAP_TYPE_OR_VALUE_EXISTS;
1867 if( mods->sml_values[1].bv_val != NULL ) {
1868 /* check for duplicates */
1870 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1872 /* check if the values we're adding already exist */
1873 if( mr == NULL || !mr->smr_match ) {
1874 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1875 /* test asserted values against themselves */
1876 for( j = 0; j < i; j++ ) {
1877 if ( bvmatch( &mods->sml_bvalues[i],
1878 &mods->sml_bvalues[j] ) ) {
1879 /* value exists already */
1880 snprintf( textbuf, textlen,
1881 "%s: value #%d provided more than once",
1882 mods->sml_desc->ad_cname.bv_val, j );
1883 return LDAP_TYPE_OR_VALUE_EXISTS;
1890 const char *text = NULL;
1891 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1893 rc = modify_check_duplicates( mods->sml_desc, mr,
1894 NULL, mods->sml_bvalues, 0,
1895 &text, textbuf, sizeof( textbuf ) );
1897 if ( rc != LDAP_SUCCESS ) {
1903 attr = ch_calloc( 1, sizeof(Attribute) );
1905 /* move ad to attr structure */
1906 attr->a_desc = mods->sml_desc;
1908 /* move values to attr structure */
1909 /* should check for duplicates */
1910 attr->a_vals = mods->sml_values;
1912 attr->a_nvals = mods->sml_nvalues;
1915 tail = &attr->a_next;
1918 return LDAP_SUCCESS;
1922 avl_ber_bvfree( void *bv )
1927 if ( ((struct berval *)bv)->bv_val != NULL ) {
1928 ber_memfree ( ((struct berval *)bv)->bv_val );
1930 ber_memfree ( (char *) bv );
1939 syncinfo_t *si = op->o_callback->sc_private;
1942 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1944 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1947 si->syncCookie = NULL;
1949 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1951 return LDAP_SUCCESS;
1960 syncinfo_t *si = op->o_callback->sc_private;
1962 if ( rs->sr_type == REP_SEARCH ) {
1963 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1966 return LDAP_SUCCESS;
1970 nonpresent_callback(
1975 syncinfo_t *si = op->o_callback->sc_private;
1978 struct berval* present_uuid = NULL;
1980 SlapReply rs_cb = {REP_RESULT};
1981 struct nonpresent_entry *np_entry;
1983 if ( rs->sr_type == REP_RESULT ) {
1984 count = avl_free( si->presentlist, avl_ber_bvfree );
1985 return LDAP_SUCCESS;
1986 } else if ( rs->sr_type == REP_SEARCH ) {
1987 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1992 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1994 if ( present_uuid == NULL ) {
1995 np_entry = (struct nonpresent_entry *)
1996 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1997 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1998 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1999 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2001 avl_delete( &si->presentlist,
2002 &a->a_vals[0], syncuuid_cmp );
2004 return LDAP_SUCCESS;
2006 return LDAP_SUCCESS;
2017 syncinfo_t *si = op->o_callback->sc_private;
2019 if ( rs->sr_err != LDAP_SUCCESS &&
2020 rs->sr_err != LDAP_REFERRAL &&
2021 rs->sr_err != LDAP_ALREADY_EXISTS &&
2022 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2024 LDAP_LOG( OPERATION, ERR,
2025 "null_callback : error code 0x%x\n",
2028 Debug( LDAP_DEBUG_ANY,
2029 "null_callback : error code 0x%x\n",
2033 return LDAP_SUCCESS;
2038 str2clist( char **out, char *in, const char *brkstr )
2047 /* find last element in list */
2048 for (i = 0; out && out[i]; i++);
2050 /* protect the input string from strtok */
2051 str = ch_strdup( in );
2053 /* Count words in string */
2055 for ( s = str; *s; s++ ) {
2056 if ( strchr( brkstr, *s ) != NULL ) {
2061 out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2063 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2065 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2067 *new = ch_strdup( s );