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;
125 /* setup LDAP SYNC control */
126 sync_ber = ber_alloc_t( LBER_USE_DER );
127 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, NULL );
129 if ( si->syncCookie ) {
130 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
132 ber_printf( sync_ber, "{e}", abs(si->type) );
135 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
136 ber_free( sync_ber, 1 );
139 ber_free( sync_ber, 1 );
141 ctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), NULL );
143 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
144 c[0].ldctl_value = (*sync_bvalp);
145 c[0].ldctl_iscritical = si->type < 0;
149 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
150 c[1].ldctl_value.bv_val = si->authzId;
151 c[1].ldctl_value.bv_len = strlen( si->authzId );
152 c[1].ldctl_iscritical = 1;
160 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
162 ber_bvfree( sync_bvalp );
165 if ( err != LDAP_OPT_SUCCESS )
166 fprintf( stderr, "Could not set controls : %d\n", err );
168 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
169 si->attrs, si->attrsonly, sctrls, cctrls,
170 si->tlimit, si->slimit, msgidp );
180 struct re_s* rtask = arg;
181 syncinfo_t *si = ( syncinfo_t * ) rtask->arg;
182 Backend *be = si->be;
184 SlapReply rs = {REP_RESULT};
187 LDAPControl **sctrls = NULL;
188 LDAPControl **rctrls = NULL;
189 LDAPControl *rctrlp = NULL;
190 BerElement *sync_ber = NULL;
191 struct berval *sync_bvalp = NULL;
193 BerElement *ctrl_ber = NULL;
194 BerElement *res_ber = NULL;
197 LDAPMessage *res = NULL;
198 LDAPMessage *msg = NULL;
202 int nresponses, nreferences, nextended, npartial;
203 int nresponses_psearch;
205 int cancel_msgid = -1;
207 struct berval *retdata = NULL;
209 int sync_info_arrived = 0;
213 struct berval syncUUID = { 0, NULL };
214 struct berval syncCookie = { 0, NULL };
219 int syncinfo_arrived = 0;
220 int cancel_response = 0;
223 AttributeDescription** descs = NULL;
235 struct berval base_bv = { 0, NULL };
236 struct berval pbase = { 0, NULL };
237 struct berval nbase = { 0, NULL };
238 struct berval sub_bv = { 0, NULL };
239 struct berval psubrdn = { 0, NULL };
240 struct berval nsubrdn = { 0, NULL };
241 struct berval psub = { 0, NULL };
242 struct berval nsub = { 0, NULL };
244 Modifications *modlist = NULL;
245 Modifications *ml, *mlnext;
246 char *def_filter_str = NULL;
249 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
251 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
257 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
258 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
262 /* Init connection to master */
264 rc = ldap_initialize( &ld, si->masteruri );
265 if ( rc != LDAP_SUCCESS ) {
267 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
268 "ldap_initialize failed (%s)\n",
269 si->masteruri, 0, 0 );
271 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
272 "ldap_initialize failed (%s)\n",
273 si->masteruri, 0, 0 );
277 op.o_protocol = LDAP_VERSION3;
278 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
283 rc = ldap_start_tls_s( ld, NULL, NULL );
284 if( rc != LDAP_SUCCESS ) {
286 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
287 "%s: ldap_start_tls failed (%d)\n",
288 si->tls == TLS_CRITICAL ? "Error" : "Warning",
291 Debug( LDAP_DEBUG_ANY,
292 "%s: ldap_start_tls failed (%d)\n",
293 si->tls == TLS_CRITICAL ? "Error" : "Warning",
296 if( si->tls == TLS_CRITICAL )
301 if ( si->bindmethod == LDAP_AUTH_SASL ) {
302 #ifdef HAVE_CYRUS_SASL
305 if ( si->secprops != NULL ) {
306 int err = ldap_set_option( ld,
307 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
309 if( err != LDAP_OPT_SUCCESS ) {
311 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
312 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
313 si->mastername, si->secprops, 0 );
315 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
316 "(%s,SECPROPS,\"%s\") failed!\n",
317 si->mastername, si->secprops, NULL );
323 defaults = lutil_sasl_defaults( ld,
330 rc = ldap_sasl_interactive_bind_s( ld,
338 if ( rc != LDAP_SUCCESS ) {
340 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
341 "ldap_sasl_interactive_bind_s failed (%d)\n",
344 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
345 "ldap_sasl_interactive_bind_s failed (%d)\n",
350 #else /* HAVE_CYRUS_SASL */
351 fprintf( stderr, "not compiled with SASL support\n" );
355 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
356 if ( rc != LDAP_SUCCESS ) {
358 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
359 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
361 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
362 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
368 /* set thread context in syncinfo */
371 /* set memory context */
373 #define SLAB_SIZE 1048576
375 memctx = sl_mem_create( memsiz, ctx );
376 op.o_tmpmemctx = memctx;
377 op.o_tmpmfuncs = &sl_mfuncs;
379 op.o_tmpmemctx = NULL;
380 op.o_tmpmfuncs = &ch_mfuncs;
383 op.o_tag = LDAP_REQ_SEARCH;
384 op.o_dn = si->updatedn;
385 op.o_ndn = si->updatedn;
387 op.o_time = slap_get_time();
388 op.o_managedsait = 1;
389 op.o_threadctx = si->ctx;
392 op.o_connid = op.o_conn->c_connid;
393 op.ors_scope = LDAP_SCOPE_BASE;
394 op.ors_deref = LDAP_DEREF_NEVER;
397 op.ors_attrsonly = 0;
399 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
400 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
404 conn.c_send_ldap_result = slap_send_ldap_result;
405 conn.c_send_search_entry = slap_send_search_entry;
406 conn.c_send_search_reference = slap_send_search_reference;
408 /* get syncrepl cookie of shadow replica from subentry */
409 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
410 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
412 sprintf( substr, "cn=syncrepl%d", si->id );
413 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
414 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
416 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
417 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
419 ch_free( base_bv.bv_val );
420 ch_free( pbase.bv_val );
421 ch_free( nbase.bv_val );
422 ch_free( sub_bv.bv_val );
423 ch_free( psubrdn.bv_val );
424 ch_free( nsubrdn.bv_val );
426 /* set callback function */
427 cb.sc_response = cookie_callback;
430 /* search subentry to retrieve cookie */
431 si->syncCookie = NULL;
432 be->be_search( &op, &rs );
434 ch_free( op.o_req_dn.bv_val );
435 ch_free( op.o_req_ndn.bv_val );
436 filter_free( op.ors_filter );
437 ch_free( op.ors_filterstr.bv_val );
439 psub = be->be_nsuffix[0];
441 /* Delete Attributes */
442 if ( si->lastmod == LASTMOD_REQ ) {
443 descs = del_descs_lastmod;
448 for ( i = 0; descs[i] != NULL; i++ ) {
449 for ( j = 0; si->attrs[j] != NULL; j++ ) {
450 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
451 ch_free( si->attrs[j] );
452 for ( k = j; si->attrs[k] != NULL; k++ ) {
453 si->attrs[k] = si->attrs[k+1];
461 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
463 if ( si->lastmod == LASTMOD_REQ ) {
464 descs = add_descs_lastmod;
469 for ( i = 0; descs[i] != NULL; i++ ) {
470 tmp = ( char ** ) ch_realloc( si->attrs,
471 ( n + 2 ) * sizeof( char * ));
474 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
476 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
480 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
481 si->attrs[ n ] = NULL;
484 rc = ldap_sync_search( si, ld, NULL, NULL, &msgid );
485 if( rc != LDAP_SUCCESS ) {
486 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
487 ldap_err2string( rc ), rc );
491 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
493 for ( msg = ldap_first_message( ld, res );
495 msg = ldap_next_message( ld, msg ) )
497 switch( ldap_msgtype( msg ) ) {
498 case LDAP_RES_SEARCH_ENTRY:
499 entry = syncrepl_message_to_entry( si, ld, &op, msg,
500 &modlist, &syncstate, &syncUUID, &syncCookie );
501 rc_efree = syncrepl_entry( si, ld, &op, entry, modlist,
502 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
503 if ( syncCookie.bv_len ) {
504 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
508 for ( ml = modlist; ml != NULL; ml = mlnext ) {
509 mlnext = ml->sml_next;
514 case LDAP_RES_SEARCH_REFERENCE:
516 LDAP_LOG( OPERATION, ERR,
517 "do_syncrepl : reference received\n", 0, 0, 0 );
519 Debug( LDAP_DEBUG_ANY,
520 "do_syncrepl : reference received\n", 0, 0, 0 );
524 case LDAP_RES_SEARCH_RESULT:
525 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
528 ctrl_ber = ber_alloc_t( LBER_USE_DER );
529 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
530 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
531 ber_reset( ctrl_ber, 1 );
533 ber_scanf( ctrl_ber, "{" /*"}"*/);
534 if ( ber_peek_tag( ctrl_ber, &len )
535 == LDAP_SYNC_TAG_COOKIE ) {
536 ber_scanf( ctrl_ber, "o", &syncCookie );
539 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
540 if ( cancel_response ) {
541 if ( syncCookie.bv_len ) {
542 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
545 ber_free( ctrl_ber, 1 );
550 ber_free( ctrl_ber, 1 );
554 if ( syncCookie.bv_len ) {
555 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
557 syncrepl_del_nonpresent( si, ld, &op );
559 ber_free( ctrl_ber, 1 );
564 case LDAP_RES_INTERMEDIATE:
565 rc = ldap_parse_intermediate( ld, msg,
566 &retoid, &retdata, NULL, 0 );
567 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
568 sync_info_arrived = 1;
569 res_ber = ber_init( retdata );
570 ber_scanf( res_ber, "{e" /*"}"*/, &syncstate );
572 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
573 syncrepl_del_nonpresent( si, ld, &op );
574 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
576 LDAP_LOG( OPERATION, ERR,
577 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
579 Debug( LDAP_DEBUG_ANY,
580 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
584 if ( ber_peek_tag( res_ber, &len )
585 == LDAP_SYNC_TAG_COOKIE ) {
586 ber_scanf( res_ber, /*"{"*/ "o}", &syncCookie );
587 if ( syncCookie.bv_len ) {
588 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
591 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
593 LDAP_LOG( OPERATION, ERR,
594 "do_syncrepl : cookie required\n", 0, 0, 0 );
596 Debug( LDAP_DEBUG_ANY,
597 "do_syncrepl : cookie required\n", 0, 0, 0 );
602 ldap_memfree( retoid );
603 ber_bvfree( retdata );
604 ber_free( res_ber, 1 );
608 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
609 " unknown intermediate "
610 "response\n", 0, 0, 0 );
612 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
613 "unknown intermediate response (%d)\n",
616 ldap_memfree( retoid );
617 ber_bvfree( retdata );
623 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
624 "unknown message\n", 0, 0, 0 );
626 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
627 "unknown message\n", 0, 0, 0 );
638 LDAP_LOG( OPERATION, ERR,
639 "do_syncrepl : unknown result\n", 0, 0, 0 );
641 Debug( LDAP_DEBUG_ANY,
642 "do_syncrepl : unknown result\n", 0, 0, 0 );
647 if ( syncCookie.bv_val )
648 ch_free( syncCookie.bv_val );
649 if ( syncUUID.bv_val )
650 ch_free( syncUUID.bv_val );
656 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
657 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
658 if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
659 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
661 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
663 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
669 syncrepl_message_to_entry(
674 Modifications **modlist,
676 struct berval *syncUUID,
677 struct berval *syncCookie
681 BerElement *ber = NULL;
683 struct berval bv = {0, NULL};
686 Modifications **modtail = modlist;
687 Backend *be = op->o_bd;
690 char txtbuf[SLAP_TEXT_BUFLEN];
691 size_t textlen = sizeof txtbuf;
693 struct berval **bvals = NULL;
695 struct berval bdn = {0, NULL};
697 struct berval empty_bv = { 0, NULL };
703 LDAPControl** rctrls = NULL;
704 BerElement* ctrl_ber;
710 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
712 LDAP_LOG( OPERATION, ERR,
713 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
715 Debug( LDAP_DEBUG_ANY,
716 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
721 op->o_tag = LDAP_REQ_ADD;
723 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
725 if ( rc != LDAP_SUCCESS ) {
727 LDAP_LOG( OPERATION, ERR,
728 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
730 Debug( LDAP_DEBUG_ANY,
731 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
736 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
737 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
741 while ( ber_remaining( ber ) ) {
742 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
744 if ( tag == LBER_ERROR ) break;
745 if ( tmp.sml_type.bv_val == NULL ) break;
747 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
749 mod->sml_op = LDAP_MOD_REPLACE;
750 mod->sml_next = NULL;
751 mod->sml_desc = NULL;
752 mod->sml_type = tmp.sml_type;
753 mod->sml_bvalues = tmp.sml_bvalues;
754 mod->sml_nvalues = tmp.sml_bvalues;
757 modtail = &mod->sml_next;
760 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
762 LDAP_LOG( OPERATION, ERR,
763 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
765 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
772 tmpber = ldap_get_message_ber( msg );
773 ber = ber_dup( tmpber );
775 ber_scanf( ber, "{xx" );
777 rc = ldap_int_get_controls( ber, &rctrls );
779 if ( rc != LDAP_SUCCESS ) {
781 LDAP_LOG( OPERATION, ERR,
782 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
784 Debug( LDAP_DEBUG_ANY,
785 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
792 ctrl_ber = ber_alloc_t( LBER_USE_DER );
793 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
794 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
795 ber_reset( ctrl_ber, 1 );
796 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
797 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
798 ber_scanf( ctrl_ber, "o}", syncCookie );
800 ber_free( ctrl_ber, 1 );
803 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
804 " rctrls absent\n", 0, 0, 0 );
806 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
807 " rctrls absent\n", 0, 0, 0 );
811 if ( *syncstate == LDAP_SYNC_PRESENT ) {
814 } else if ( *syncstate == LDAP_SYNC_DELETE ) {
818 if ( *modlist == NULL ) {
820 LDAP_LOG( OPERATION, ERR,
821 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
823 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
828 rc = slap_mods_check_syncrepl( si, op, modlist,
829 &text, txtbuf, textlen, NULL );
831 if ( rc != LDAP_SUCCESS ) {
833 LDAP_LOG( OPERATION, ERR,
834 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
836 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
842 rc = slap_mods_opattrs_syncrepl( si, op, *modlist, modtail,
843 &text,txtbuf, textlen );
845 if( rc != LDAP_SUCCESS ) {
847 LDAP_LOG( OPERATION, ERR,
848 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
850 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
856 rc = slap_mods2entry_syncrepl( si, *modlist, &e, 1, &text, txtbuf, textlen);
857 if( rc != LDAP_SUCCESS ) {
859 LDAP_LOG( OPERATION, ERR,
860 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
862 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
875 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
877 const struct berval *uuid1 = v_uuid1;
878 const struct berval *uuid2 = v_uuid2;
879 int rc = uuid1->bv_len - uuid2->bv_len;
881 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
890 Modifications* modlist,
892 struct berval* syncUUID,
893 struct berval* syncCookie,
897 Backend *be = op->o_bd;
899 struct berval csn_bv = {0, NULL};
900 struct berval *syncuuid_bv = NULL;
901 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
903 SlapReply rs = {REP_RESULT};
904 int rc = LDAP_SUCCESS;
906 struct berval base_bv = {0, NULL};
914 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
915 syncuuid_bv = ber_dupbv( NULL, syncUUID );
916 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
917 syncuuid_cmp, avl_dup_error );
920 if ( syncstate == LDAP_SYNC_PRESENT ) {
927 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
929 strcpy( filterstr, "entryUUID=" );
930 strcat( filterstr, syncUUID->bv_val );
933 si->syncUUID = syncUUID;
934 si->syncUUID_ndn = NULL;
936 filter = str2filter( filterstr );
937 ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
938 ch_free( filterstr );
939 op->ors_filter = filter;
940 op->ors_scope = LDAP_SCOPE_SUBTREE;
942 /* get syncrepl cookie of shadow replica from subentry */
943 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
944 dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
945 ch_free( base_bv.bv_val );
947 /* set callback function */
948 op->o_callback = &cb;
949 cb.sc_response = dn_callback;
952 be->be_search( op, &rs );
954 ch_free( op->o_req_dn.bv_val );
955 ch_free( op->o_req_ndn.bv_val );
956 filter_free( op->ors_filter );
957 ch_free( op->ors_filterstr.bv_val );
959 cb.sc_response = null_callback;
964 if ( si->syncUUID_ndn ) {
965 op->o_req_dn = *si->syncUUID_ndn;
966 op->o_req_ndn = *si->syncUUID_ndn;
967 op->o_tag = LDAP_REQ_DELETE;
968 rc = be->be_delete( op, &rs );
971 switch ( syncstate ) {
973 case LDAP_SYNC_MODIFY :
975 if ( rc == LDAP_SUCCESS ||
976 rc == LDAP_REFERRAL ||
977 rc == LDAP_NO_SUCH_OBJECT ) {
979 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
980 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
983 op->o_tag = LDAP_REQ_ADD;
985 op->o_req_dn = e->e_name;
986 op->o_req_ndn = e->e_nname;
987 rc = be->be_add( op, &rs );
989 if ( rc != LDAP_SUCCESS ) {
990 if ( rc == LDAP_ALREADY_EXISTS ) {
991 op->o_tag = LDAP_REQ_MODIFY;
992 op->orm_modlist = modlist;
993 op->o_req_dn = e->e_name;
994 op->o_req_ndn = e->e_nname;
995 rc = be->be_modify( op, &rs );
996 } else if ( rc == LDAP_REFERRAL ||
997 rc == LDAP_NO_SUCH_OBJECT ) {
998 syncrepl_add_glue( si, ld, op, e,
1000 syncUUID, syncCookie);
1003 LDAP_LOG( OPERATION, ERR,
1004 "be_modify failed (%d)\n",
1007 Debug( LDAP_DEBUG_ANY,
1008 "be_modify failed (%d)\n",
1017 LDAP_LOG( OPERATION, ERR,
1018 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1020 Debug( LDAP_DEBUG_ANY,
1021 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1028 case LDAP_SYNC_DELETE :
1029 /* Already deleted */
1034 LDAP_LOG( OPERATION, ERR,
1035 "unknown syncstate\n", 0, 0, 0 );
1037 Debug( LDAP_DEBUG_ANY,
1038 "unknown syncstate\n", 0, 0, 0 );
1045 syncrepl_del_nonpresent(
1051 Backend* be = op->o_bd;
1053 struct berval base_bv = {0, NULL};
1055 SlapReply rs = {REP_RESULT};
1056 struct berval filterstr_bv = {0, NULL};
1057 struct nonpresent_entry *np_list, *np_prev;
1059 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1060 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1061 ch_free( base_bv.bv_val );
1063 filter = str2filter( si->filterstr );
1065 cb.sc_response = nonpresent_callback;
1068 op->o_callback = &cb;
1069 op->o_tag = LDAP_REQ_SEARCH;
1070 op->ors_scope = si->scope;
1071 op->ors_deref = LDAP_DEREF_NEVER;
1072 op->ors_slimit = -1;
1073 op->ors_tlimit = -1;
1074 op->ors_attrsonly = 0;
1075 op->ors_attrs = NULL;
1076 op->ors_filter = filter;
1077 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1079 be->be_search( op, &rs );
1081 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1082 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1083 while ( np_list != NULL ) {
1084 LDAP_LIST_REMOVE( np_list, np_link );
1086 np_list = LDAP_LIST_NEXT( np_list, np_link );
1087 op->o_tag = LDAP_REQ_DELETE;
1088 op->o_callback = &cb;
1089 cb.sc_response = null_callback;
1091 op->o_req_dn = *np_prev->dn;
1092 op->o_req_ndn = *np_prev->ndn;
1093 op->o_bd->be_delete( op, &rs );
1094 ber_bvfree( np_prev->dn );
1095 ber_bvfree( np_prev->ndn );
1096 op->o_req_dn.bv_val = NULL;
1097 op->o_req_ndn.bv_val = NULL;
1102 if ( op->o_req_dn.bv_val )
1103 ch_free( op->o_req_dn.bv_val );
1104 if ( op->o_req_ndn.bv_val )
1105 ch_free( op->o_req_ndn.bv_val );
1106 filter_free( op->ors_filter );
1107 ch_free( op->ors_filterstr.bv_val );
1117 Modifications* modlist,
1119 struct berval* syncUUID,
1120 struct berval* syncCookie
1123 Backend *be = op->o_bd;
1124 struct berval uuid_bv = {0, NULL};
1128 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1131 struct berval dn = {0, NULL};
1132 struct berval pdn = {0, NULL};
1133 struct berval ndn = {0, NULL};
1134 struct berval rdn = {0, NULL};
1136 SlapReply rs = {REP_RESULT};
1137 Connection *conn = op->o_conn;
1139 op->o_tag = LDAP_REQ_ADD;
1140 op->o_callback = &cb;
1141 cb.sc_response = null_callback;
1144 ber_dupbv( &dn, &e->e_nname );
1145 ber_dupbv( &pdn, &e->e_nname );
1147 while ( !be_issuffix ( be, &pdn )) {
1148 dnParent( &dn, &pdn );
1149 ch_free( dn.bv_val );
1150 ber_dupbv( &dn, &pdn );
1154 for ( i = 0; i <= levels; i++ ) {
1155 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1156 ch_free( dn.bv_val );
1157 ch_free( pdn.bv_val );
1158 ber_dupbv( &dn, &e->e_nname );
1159 ber_dupbv( &pdn, &e->e_nname );
1161 for ( k = 0; k < j; k++ ) {
1162 dnParent( &dn, &pdn );
1163 ch_free( dn.bv_val );
1164 ber_dupbv( &dn, &pdn );
1167 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1168 ber_dupbv( &glue->e_name, &pdn );
1169 ber_dupbv( &glue->e_nname, &ndn );
1170 ch_free( dn.bv_val );
1171 ch_free( pdn.bv_val );
1172 ch_free( ndn.bv_val );
1174 a = ch_calloc( 1, sizeof( Attribute ));
1175 a->a_desc = slap_schema.si_ad_objectClass;
1176 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1177 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1178 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1179 a->a_vals[2].bv_len = 0;
1180 a->a_vals[2].bv_val = NULL;
1181 a->a_next = glue->e_attrs;
1184 a = ch_calloc( 1, sizeof( Attribute ));
1185 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1186 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1187 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1188 a->a_vals[1].bv_len = 0;
1189 a->a_vals[1].bv_val = NULL;
1190 a->a_next = glue->e_attrs;
1193 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1194 op->o_req_dn = e->e_name;
1195 op->o_req_ndn = e->e_nname;
1197 rc = be->be_add ( op, &rs );
1198 if ( rc == LDAP_SUCCESS )
1199 be_entry_release_w( op, e );
1204 op->o_req_dn = glue->e_name;
1205 op->o_req_ndn = glue->e_nname;
1207 rc = be->be_add ( op, &rs );
1208 if ( rc == LDAP_SUCCESS ) {
1209 be_entry_release_w( op, glue );
1211 /* incl. ALREADY EXIST */
1221 syncrepl_updateCookie(
1226 struct berval *syncCookie
1229 Backend *be = op->o_bd;
1231 Modifications *mlnext;
1233 Modifications *modlist;
1234 Modifications **modtail = &modlist;
1236 struct berval* ocbva = NULL;
1237 struct berval* cnbva = NULL;
1238 struct berval* ssbva = NULL;
1239 struct berval* scbva = NULL;
1244 char txtbuf[SLAP_TEXT_BUFLEN];
1245 size_t textlen = sizeof txtbuf;
1250 struct berval sub_bv = { 0, NULL };
1251 struct berval psubrdn = { 0, NULL };
1254 SlapReply rs = {REP_RESULT};
1256 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1257 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1258 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1259 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1261 /* update in memory cookie */
1262 if ( si->syncCookie != NULL ) {
1263 ber_bvfree( si->syncCookie );
1265 si->syncCookie = ber_dupbv( NULL, syncCookie );
1266 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1267 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1268 ber_str2bv( "syncConsumerSubentry",
1269 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1270 ocbva[3].bv_len = 0;
1271 ocbva[3].bv_val = NULL;
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"), 1, &mod->sml_type );
1278 mod->sml_bvalues = ocbva;
1279 mod->sml_nvalues = ocbva;
1281 modtail = &mod->sml_next;
1283 sprintf( substr, "syncrepl%d", si->id );
1284 sprintf( rdnstr, "cn=%s", substr );
1285 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1286 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1287 cnbva[1].bv_len = 0;
1288 cnbva[1].bv_val = NULL;
1289 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1290 mod->sml_op = LDAP_MOD_REPLACE;
1291 mod->sml_next = NULL;
1292 mod->sml_desc = NULL;
1293 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1294 mod->sml_bvalues = cnbva;
1295 mod->sml_nvalues = cnbva;
1297 modtail = &mod->sml_next;
1299 ber_dupbv( &scbva[0], si->syncCookie );
1300 scbva[1].bv_len = 0;
1301 scbva[1].bv_val = NULL;
1302 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1303 mod->sml_op = LDAP_MOD_REPLACE;
1304 mod->sml_next = NULL;
1305 mod->sml_desc = NULL;
1306 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1307 1, &mod->sml_type );
1308 mod->sml_bvalues = scbva;
1309 mod->sml_nvalues = scbva;
1311 modtail = &mod->sml_next;
1313 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1314 ssbva[1].bv_len = 0;
1315 ssbva[1].bv_val = NULL;
1316 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1317 mod->sml_op = LDAP_MOD_REPLACE;
1318 mod->sml_next = NULL;
1319 mod->sml_desc = NULL;
1320 ber_str2bv( "subtreeSpecification",
1321 strlen("subtreeSpecification"), 1, &mod->sml_type );
1322 mod->sml_bvalues = ssbva;
1323 mod->sml_nvalues = ssbva;
1325 modtail = &mod->sml_next;
1327 rc = slap_mods_check_syncrepl( si, op, &modlist,
1328 &text, txtbuf, textlen, NULL );
1330 if ( rc != LDAP_SUCCESS ) {
1332 LDAP_LOG( OPERATION, ERR,
1333 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1335 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1340 op->o_tag = LDAP_REQ_ADD;
1341 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1342 &text,txtbuf, textlen );
1344 if( rc != LDAP_SUCCESS ) {
1346 LDAP_LOG( OPERATION, ERR,
1347 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1349 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1354 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1356 build_new_dn( &sub_bv, pdn, &psubrdn );
1357 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1358 ch_free( sub_bv.bv_val );
1359 ch_free( psubrdn.bv_val );
1363 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1365 if( rc != LDAP_SUCCESS ) {
1367 LDAP_LOG( OPERATION, ERR,
1368 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1370 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1375 cb.sc_response = null_callback;
1378 op->o_callback = &cb;
1379 op->o_req_dn = e->e_name;
1380 op->o_req_ndn = e->e_nname;
1382 /* update persistent cookie */
1383 update_cookie_retry:
1384 op->o_tag = LDAP_REQ_MODIFY;
1385 op->orm_modlist = modlist;
1386 rc = be->be_modify( op, &rs );
1387 if ( rc != LDAP_SUCCESS ) {
1388 if ( rc == LDAP_REFERRAL ||
1389 rc == LDAP_NO_SUCH_OBJECT ) {
1390 op->o_tag = LDAP_REQ_ADD;
1392 rc = be->be_add( op, &rs );
1393 if ( rc != LDAP_SUCCESS ) {
1394 if ( rc == LDAP_ALREADY_EXISTS ) {
1395 goto update_cookie_retry;
1396 } else if ( rc == LDAP_REFERRAL ||
1397 rc == LDAP_NO_SUCH_OBJECT ) {
1399 LDAP_LOG( OPERATION, ERR,
1400 "cookie will be non-persistent\n",
1403 Debug( LDAP_DEBUG_ANY,
1404 "cookie will be non-persistent\n",
1409 LDAP_LOG( OPERATION, ERR,
1410 "be_add failed (%d)\n",
1413 Debug( LDAP_DEBUG_ANY,
1414 "be_add failed (%d)\n",
1423 LDAP_LOG( OPERATION, ERR,
1424 "be_modify failed (%d)\n", rc, 0, 0 );
1426 Debug( LDAP_DEBUG_ANY,
1427 "be_modify failed (%d)\n", rc, 0, 0 );
1437 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1438 mlnext = ml->sml_next;
1447 int slap_mods_check_syncrepl(
1450 Modifications **mlp,
1457 Backend *be = op->o_bd;
1458 AttributeDescription** descs;
1460 Modifications *prevml = NULL;
1461 Modifications *nextml = NULL;
1462 Modifications *ml = *mlp;
1464 while ( ml != NULL ) {
1465 AttributeDescription *ad = NULL;
1467 /* convert to attribute description */
1468 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1470 if( rc != LDAP_SUCCESS ) {
1471 snprintf( textbuf, textlen, "%s: %s",
1472 ml->sml_type.bv_val, *text );
1479 if ( si->lastmod == LASTMOD_REQ ) {
1480 descs = del_descs_lastmod;
1485 for ( i = 0; descs[i] != NULL; i++ ) {
1486 if ( ad == descs[i] ) {
1487 if ( prevml == NULL ) {
1488 mlp = &ml->sml_next;
1491 prevml->sml_next = ml->sml_next;
1493 slap_mod_free( &ml->sml_mod, 0 );
1494 nextml = ml->sml_next;
1501 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1502 && !slap_ad_is_binary( ad )) {
1503 /* attribute requires binary transfer */
1504 snprintf( textbuf, textlen,
1505 "%s: requires ;binary transfer",
1506 ml->sml_type.bv_val );
1508 return LDAP_UNDEFINED_TYPE;
1511 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1512 && slap_ad_is_binary( ad )) {
1513 /* attribute requires binary transfer */
1514 snprintf( textbuf, textlen,
1515 "%s: disallows ;binary transfer",
1516 ml->sml_type.bv_val );
1518 return LDAP_UNDEFINED_TYPE;
1521 if( slap_ad_is_tag_range( ad )) {
1522 /* attribute requires binary transfer */
1523 snprintf( textbuf, textlen,
1524 "%s: inappropriate use of tag range option",
1525 ml->sml_type.bv_val );
1527 return LDAP_UNDEFINED_TYPE;
1530 if ( is_at_obsolete( ad->ad_type ) &&
1531 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1533 * attribute is obsolete,
1534 * only allow replace/delete with no values
1536 snprintf( textbuf, textlen,
1537 "%s: attribute is obsolete",
1538 ml->sml_type.bv_val );
1540 return LDAP_CONSTRAINT_VIOLATION;
1546 if( ml->sml_values != NULL ) {
1548 slap_syntax_validate_func *validate =
1549 ad->ad_type->sat_syntax->ssyn_validate;
1550 slap_syntax_transform_func *pretty =
1551 ad->ad_type->sat_syntax->ssyn_pretty;
1553 if( !pretty && !validate ) {
1554 *text = "no validator for syntax";
1555 snprintf( textbuf, textlen,
1556 "%s: no validator for syntax %s",
1557 ml->sml_type.bv_val,
1558 ad->ad_type->sat_syntax->ssyn_oid );
1560 return LDAP_INVALID_SYNTAX;
1564 * check that each value is valid per syntax
1565 * and pretty if appropriate
1567 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1568 struct berval pval = {0, NULL};
1570 rc = pretty( ad->ad_type->sat_syntax,
1571 &ml->sml_values[nvals], &pval, ctx );
1573 rc = validate( ad->ad_type->sat_syntax,
1574 &ml->sml_values[nvals] );
1578 snprintf( textbuf, textlen,
1579 "%s: value #%ld invalid per syntax",
1580 ml->sml_type.bv_val, (long) nvals );
1582 return LDAP_INVALID_SYNTAX;
1586 ber_memfree( ml->sml_values[nvals].bv_val );
1587 ml->sml_values[nvals] = pval;
1592 * a rough single value check... an additional check is needed
1593 * to catch add of single value to existing single valued attribute
1595 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1596 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1597 snprintf( textbuf, textlen,
1598 "%s: multiple values provided",
1599 ml->sml_type.bv_val );
1601 return LDAP_CONSTRAINT_VIOLATION;
1604 if( nvals && ad->ad_type->sat_equality &&
1605 ad->ad_type->sat_equality->smr_normalize ) {
1606 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1607 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1608 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1609 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1610 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1613 LDAP_LOG( OPERATION, DETAIL1,
1614 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1616 Debug( LDAP_DEBUG_ANY,
1617 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1619 snprintf( textbuf, textlen,
1620 "%s: value #%ld normalization failed",
1621 ml->sml_type.bv_val, (long) nvals );
1626 ml->sml_nvalues[nvals].bv_val = NULL;
1627 ml->sml_nvalues[nvals].bv_len = 0;
1634 return LDAP_SUCCESS;
1638 int slap_mods_opattrs_syncrepl(
1641 Modifications *mods,
1642 Modifications **modtail,
1644 char *textbuf, size_t textlen )
1646 struct berval name = {0, NULL};
1647 struct berval timestamp = {0, NULL};
1648 struct berval csn = {0, NULL};
1649 struct berval nname = {0, NULL};
1650 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1651 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1653 Backend *be = op->o_bd;
1655 int mop = LDAP_MOD_REPLACE;
1657 assert( modtail != NULL );
1658 assert( *modtail == NULL );
1660 if( si->lastmod == LASTMOD_GEN ) {
1662 time_t now = slap_get_time();
1664 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1665 ltm = gmtime( &now );
1666 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1668 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1669 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1670 csn.bv_val = csnbuf;
1672 timestamp.bv_val = timebuf;
1673 timestamp.bv_len = strlen(timebuf);
1675 if( op->o_dn.bv_len == 0 ) {
1676 name.bv_val = SLAPD_ANONYMOUS;
1677 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1685 if( op->o_tag == LDAP_REQ_ADD ) {
1686 struct berval tmpval = {0, NULL};
1688 if( global_schemacheck ) {
1689 int rc = mods_structural_class( mods, &tmpval,
1690 text, textbuf, textlen );
1691 if( rc != LDAP_SUCCESS ) {
1695 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1697 mod->sml_type.bv_val = NULL;
1698 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1699 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1700 ber_dupbv( &mod->sml_values[0], &tmpval );
1701 mod->sml_values[1].bv_len = 0;
1702 mod->sml_values[1].bv_val = NULL;
1703 assert( mod->sml_values[0].bv_val );
1704 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1705 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1706 mod->sml_nvalues[1].bv_len = 0;
1707 mod->sml_nvalues[1].bv_val = NULL;
1708 assert( mod->sml_nvalues[0].bv_val );
1710 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_creatorsName;
1718 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1719 ber_dupbv( &mod->sml_values[0], &name );
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 = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1724 ber_dupbv( &mod->sml_nvalues[0], &nname );
1725 mod->sml_nvalues[1].bv_len = 0;
1726 mod->sml_nvalues[1].bv_val = NULL;
1727 assert( mod->sml_nvalues[0].bv_val );
1729 modtail = &mod->sml_next;
1731 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1733 mod->sml_type.bv_val = NULL;
1734 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1735 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1736 ber_dupbv( &mod->sml_values[0], ×tamp );
1737 mod->sml_values[1].bv_len = 0;
1738 mod->sml_values[1].bv_val = NULL;
1739 assert( mod->sml_values[0].bv_val );
1740 mod->sml_nvalues = NULL;
1742 modtail = &mod->sml_next;
1746 if( si->lastmod == LASTMOD_GEN ) {
1747 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1749 mod->sml_type.bv_val = NULL;
1750 mod->sml_desc = slap_schema.si_ad_entryCSN;
1751 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1752 ber_dupbv( &mod->sml_values[0], &csn );
1753 mod->sml_values[1].bv_len = 0;
1754 mod->sml_values[1].bv_val = NULL;
1755 assert( mod->sml_values[0].bv_val );
1756 mod->sml_nvalues = NULL;
1758 modtail = &mod->sml_next;
1760 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1762 mod->sml_type.bv_val = NULL;
1763 mod->sml_desc = slap_schema.si_ad_modifiersName;
1764 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1765 ber_dupbv( &mod->sml_values[0], &name );
1766 mod->sml_values[1].bv_len = 0;
1767 mod->sml_values[1].bv_val = NULL;
1768 assert( mod->sml_values[0].bv_val );
1769 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1770 ber_dupbv( &mod->sml_nvalues[0], &nname );
1771 mod->sml_nvalues[1].bv_len = 0;
1772 mod->sml_nvalues[1].bv_val = NULL;
1773 assert( mod->sml_nvalues[0].bv_val );
1775 modtail = &mod->sml_next;
1777 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1779 mod->sml_type.bv_val = NULL;
1780 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1781 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1782 ber_dupbv( &mod->sml_values[0], ×tamp );
1783 mod->sml_values[1].bv_len = 0;
1784 mod->sml_values[1].bv_val = NULL;
1785 assert( mod->sml_values[0].bv_val );
1786 mod->sml_nvalues = NULL;
1788 modtail = &mod->sml_next;
1792 return LDAP_SUCCESS;
1797 int slap_mods2entry_syncrepl(
1799 Modifications *mods,
1803 char *textbuf, size_t textlen )
1805 Attribute **tail = &(*e)->e_attrs;
1806 assert( *tail == NULL );
1810 for( ; mods != NULL; mods = mods->sml_next ) {
1813 assert( mods->sml_desc != NULL );
1815 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1817 if( attr != NULL ) {
1818 #define SLURPD_FRIENDLY
1819 #ifdef SLURPD_FRIENDLY
1823 snprintf( textbuf, textlen,
1824 "attribute '%s' provided more than once",
1825 mods->sml_desc->ad_cname.bv_val );
1826 return LDAP_TYPE_OR_VALUE_EXISTS;
1829 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1832 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1837 attr->a_vals = ch_realloc( attr->a_vals,
1838 sizeof( struct berval ) * (i+j) );
1840 /* should check for duplicates */
1842 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1843 sizeof( struct berval ) * j );
1845 if( attr->a_nvals ) {
1846 attr->a_nvals = ch_realloc( attr->a_nvals,
1847 sizeof( struct berval ) * (i+j) );
1849 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1850 sizeof( struct berval ) * j );
1852 /* trim the mods array */
1853 ch_free( mods->sml_nvalues );
1854 mods->sml_nvalues = NULL;
1859 snprintf( textbuf, textlen,
1860 "attribute '%s' provided more than once",
1861 mods->sml_desc->ad_cname.bv_val );
1862 return LDAP_TYPE_OR_VALUE_EXISTS;
1866 if( mods->sml_values[1].bv_val != NULL ) {
1867 /* check for duplicates */
1869 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1871 /* check if the values we're adding already exist */
1872 if( mr == NULL || !mr->smr_match ) {
1873 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1874 /* test asserted values against themselves */
1875 for( j = 0; j < i; j++ ) {
1876 if ( bvmatch( &mods->sml_bvalues[i],
1877 &mods->sml_bvalues[j] ) ) {
1878 /* value exists already */
1879 snprintf( textbuf, textlen,
1880 "%s: value #%d provided more than once",
1881 mods->sml_desc->ad_cname.bv_val, j );
1882 return LDAP_TYPE_OR_VALUE_EXISTS;
1889 const char *text = NULL;
1890 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1892 rc = modify_check_duplicates( mods->sml_desc, mr,
1893 NULL, mods->sml_bvalues, 0,
1894 &text, textbuf, sizeof( textbuf ) );
1896 if ( rc != LDAP_SUCCESS ) {
1902 attr = ch_calloc( 1, sizeof(Attribute) );
1904 /* move ad to attr structure */
1905 attr->a_desc = mods->sml_desc;
1907 /* move values to attr structure */
1908 /* should check for duplicates */
1909 attr->a_vals = mods->sml_values;
1911 attr->a_nvals = mods->sml_nvalues;
1914 tail = &attr->a_next;
1917 return LDAP_SUCCESS;
1921 avl_ber_bvfree( void *bv )
1926 if ( ((struct berval *)bv)->bv_val != NULL ) {
1927 ber_memfree ( ((struct berval *)bv)->bv_val );
1929 ber_memfree ( (char *) bv );
1938 syncinfo_t *si = op->o_callback->sc_private;
1941 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1943 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1946 si->syncCookie = NULL;
1948 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1950 return LDAP_SUCCESS;
1959 syncinfo_t *si = op->o_callback->sc_private;
1961 if ( rs->sr_type == REP_SEARCH ) {
1962 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1965 return LDAP_SUCCESS;
1969 nonpresent_callback(
1974 syncinfo_t *si = op->o_callback->sc_private;
1977 struct berval* present_uuid = NULL;
1979 SlapReply rs_cb = {REP_RESULT};
1980 struct nonpresent_entry *np_entry;
1982 if ( rs->sr_type == REP_RESULT ) {
1983 count = avl_free( si->presentlist, avl_ber_bvfree );
1984 return LDAP_SUCCESS;
1985 } else if ( rs->sr_type == REP_SEARCH ) {
1986 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1991 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1993 if ( present_uuid == NULL ) {
1994 np_entry = (struct nonpresent_entry *)
1995 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1996 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1997 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1998 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2000 avl_delete( &si->presentlist,
2001 &a->a_vals[0], syncuuid_cmp );
2003 return LDAP_SUCCESS;
2005 return LDAP_SUCCESS;
2016 syncinfo_t *si = op->o_callback->sc_private;
2018 if ( rs->sr_err != LDAP_SUCCESS &&
2019 rs->sr_err != LDAP_REFERRAL &&
2020 rs->sr_err != LDAP_ALREADY_EXISTS &&
2021 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2023 LDAP_LOG( OPERATION, ERR,
2024 "null_callback : error code 0x%x\n",
2027 Debug( LDAP_DEBUG_ANY,
2028 "null_callback : error code 0x%x\n",
2032 return LDAP_SUCCESS;
2037 str2clist( char **out, char *in, const char *brkstr )
2046 /* find last element in list */
2047 for (i = 0; out && out[i]; i++);
2049 /* protect the input string from strtok */
2050 str = ch_strdup( in );
2052 /* Count words in string */
2054 for ( s = str; *s; s++ ) {
2055 if ( strchr( brkstr, *s ) != NULL ) {
2060 out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2062 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2064 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2066 *new = ch_strdup( s );