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 );
1119 Modifications* modlist,
1121 struct berval* syncUUID,
1122 struct berval* syncCookie
1125 Backend *be = op->o_bd;
1126 struct berval uuid_bv = {0, NULL};
1130 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1133 struct berval dn = {0, NULL};
1134 struct berval pdn = {0, NULL};
1135 struct berval ndn = {0, NULL};
1136 struct berval rdn = {0, NULL};
1138 SlapReply rs = {REP_RESULT};
1139 Connection *conn = op->o_conn;
1141 op->o_tag = LDAP_REQ_ADD;
1142 op->o_callback = &cb;
1143 cb.sc_response = null_callback;
1146 ber_dupbv( &dn, &e->e_nname );
1147 ber_dupbv( &pdn, &e->e_nname );
1149 while ( !be_issuffix ( be, &pdn )) {
1150 dnParent( &dn, &pdn );
1151 ch_free( dn.bv_val );
1152 ber_dupbv( &dn, &pdn );
1156 for ( i = 0; i <= levels; i++ ) {
1157 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1158 ch_free( dn.bv_val );
1159 ch_free( pdn.bv_val );
1160 ber_dupbv( &dn, &e->e_nname );
1161 ber_dupbv( &pdn, &e->e_nname );
1163 for ( k = 0; k < j; k++ ) {
1164 dnParent( &dn, &pdn );
1165 ch_free( dn.bv_val );
1166 ber_dupbv( &dn, &pdn );
1169 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1170 ber_dupbv( &glue->e_name, &pdn );
1171 ber_dupbv( &glue->e_nname, &ndn );
1172 ch_free( dn.bv_val );
1173 ch_free( pdn.bv_val );
1174 ch_free( ndn.bv_val );
1176 a = ch_calloc( 1, sizeof( Attribute ));
1177 a->a_desc = slap_schema.si_ad_objectClass;
1178 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1179 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1180 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1181 a->a_vals[2].bv_len = 0;
1182 a->a_vals[2].bv_val = NULL;
1183 a->a_next = glue->e_attrs;
1186 a = ch_calloc( 1, sizeof( Attribute ));
1187 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1188 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1189 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1190 a->a_vals[1].bv_len = 0;
1191 a->a_vals[1].bv_val = NULL;
1192 a->a_next = glue->e_attrs;
1195 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1196 op->o_req_dn = e->e_name;
1197 op->o_req_ndn = e->e_nname;
1199 rc = be->be_add ( op, &rs );
1200 if ( rc == LDAP_SUCCESS )
1201 be_entry_release_w( op, e );
1206 op->o_req_dn = glue->e_name;
1207 op->o_req_ndn = glue->e_nname;
1209 rc = be->be_add ( op, &rs );
1210 if ( rc == LDAP_SUCCESS ) {
1211 be_entry_release_w( op, glue );
1213 /* incl. ALREADY EXIST */
1223 syncrepl_updateCookie(
1228 struct berval *syncCookie
1231 Backend *be = op->o_bd;
1233 Modifications *mlnext;
1235 Modifications *modlist;
1236 Modifications **modtail = &modlist;
1238 struct berval* ocbva = NULL;
1239 struct berval* cnbva = NULL;
1240 struct berval* ssbva = NULL;
1241 struct berval* scbva = NULL;
1246 char txtbuf[SLAP_TEXT_BUFLEN];
1247 size_t textlen = sizeof txtbuf;
1252 struct berval sub_bv = { 0, NULL };
1253 struct berval psubrdn = { 0, NULL };
1256 SlapReply rs = {REP_RESULT};
1258 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1259 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1260 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1261 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1263 /* update in memory cookie */
1264 if ( si->syncCookie != NULL ) {
1265 ber_bvfree( si->syncCookie );
1267 si->syncCookie = ber_dupbv( NULL, syncCookie );
1268 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1269 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1270 ber_str2bv( "syncConsumerSubentry",
1271 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1272 ocbva[3].bv_len = 0;
1273 ocbva[3].bv_val = NULL;
1275 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1276 mod->sml_op = LDAP_MOD_REPLACE;
1277 mod->sml_next = NULL;
1278 mod->sml_desc = NULL;
1279 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1280 mod->sml_bvalues = ocbva;
1281 mod->sml_nvalues = ocbva;
1283 modtail = &mod->sml_next;
1285 sprintf( substr, "syncrepl%d", si->id );
1286 sprintf( rdnstr, "cn=%s", substr );
1287 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1288 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1289 cnbva[1].bv_len = 0;
1290 cnbva[1].bv_val = NULL;
1291 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1292 mod->sml_op = LDAP_MOD_REPLACE;
1293 mod->sml_next = NULL;
1294 mod->sml_desc = NULL;
1295 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1296 mod->sml_bvalues = cnbva;
1297 mod->sml_nvalues = cnbva;
1299 modtail = &mod->sml_next;
1301 ber_dupbv( &scbva[0], si->syncCookie );
1302 scbva[1].bv_len = 0;
1303 scbva[1].bv_val = NULL;
1304 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1305 mod->sml_op = LDAP_MOD_REPLACE;
1306 mod->sml_next = NULL;
1307 mod->sml_desc = NULL;
1308 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1309 1, &mod->sml_type );
1310 mod->sml_bvalues = scbva;
1311 mod->sml_nvalues = scbva;
1313 modtail = &mod->sml_next;
1315 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1316 ssbva[1].bv_len = 0;
1317 ssbva[1].bv_val = NULL;
1318 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1319 mod->sml_op = LDAP_MOD_REPLACE;
1320 mod->sml_next = NULL;
1321 mod->sml_desc = NULL;
1322 ber_str2bv( "subtreeSpecification",
1323 strlen("subtreeSpecification"), 1, &mod->sml_type );
1324 mod->sml_bvalues = ssbva;
1325 mod->sml_nvalues = ssbva;
1327 modtail = &mod->sml_next;
1329 rc = slap_mods_check_syncrepl( si, op, &modlist,
1330 &text, txtbuf, textlen, NULL );
1332 if ( rc != LDAP_SUCCESS ) {
1334 LDAP_LOG( OPERATION, ERR,
1335 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1337 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1342 op->o_tag = LDAP_REQ_ADD;
1343 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1344 &text,txtbuf, textlen );
1346 if( rc != LDAP_SUCCESS ) {
1348 LDAP_LOG( OPERATION, ERR,
1349 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1351 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1356 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1358 build_new_dn( &sub_bv, pdn, &psubrdn );
1359 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1360 ch_free( sub_bv.bv_val );
1361 ch_free( psubrdn.bv_val );
1365 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1367 if( rc != LDAP_SUCCESS ) {
1369 LDAP_LOG( OPERATION, ERR,
1370 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1372 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1377 cb.sc_response = null_callback;
1380 op->o_callback = &cb;
1381 op->o_req_dn = e->e_name;
1382 op->o_req_ndn = e->e_nname;
1384 /* update persistent cookie */
1385 update_cookie_retry:
1386 op->o_tag = LDAP_REQ_MODIFY;
1387 op->orm_modlist = modlist;
1388 rc = be->be_modify( op, &rs );
1389 if ( rc != LDAP_SUCCESS ) {
1390 if ( rc == LDAP_REFERRAL ||
1391 rc == LDAP_NO_SUCH_OBJECT ) {
1392 op->o_tag = LDAP_REQ_ADD;
1394 rc = be->be_add( op, &rs );
1395 if ( rc != LDAP_SUCCESS ) {
1396 if ( rc == LDAP_ALREADY_EXISTS ) {
1397 goto update_cookie_retry;
1398 } else if ( rc == LDAP_REFERRAL ||
1399 rc == LDAP_NO_SUCH_OBJECT ) {
1401 LDAP_LOG( OPERATION, ERR,
1402 "cookie will be non-persistent\n",
1405 Debug( LDAP_DEBUG_ANY,
1406 "cookie will be non-persistent\n",
1411 LDAP_LOG( OPERATION, ERR,
1412 "be_add failed (%d)\n",
1415 Debug( LDAP_DEBUG_ANY,
1416 "be_add failed (%d)\n",
1425 LDAP_LOG( OPERATION, ERR,
1426 "be_modify failed (%d)\n", rc, 0, 0 );
1428 Debug( LDAP_DEBUG_ANY,
1429 "be_modify failed (%d)\n", rc, 0, 0 );
1439 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1440 mlnext = ml->sml_next;
1449 int slap_mods_check_syncrepl(
1452 Modifications **mlp,
1459 Backend *be = op->o_bd;
1460 AttributeDescription** descs;
1462 Modifications *prevml = NULL;
1463 Modifications *nextml = NULL;
1464 Modifications *ml = *mlp;
1466 while ( ml != NULL ) {
1467 AttributeDescription *ad = NULL;
1469 /* convert to attribute description */
1470 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1472 if( rc != LDAP_SUCCESS ) {
1473 snprintf( textbuf, textlen, "%s: %s",
1474 ml->sml_type.bv_val, *text );
1481 if ( si->lastmod == LASTMOD_REQ ) {
1482 descs = del_descs_lastmod;
1487 for ( i = 0; descs[i] != NULL; i++ ) {
1488 if ( ad == descs[i] ) {
1489 if ( prevml == NULL ) {
1490 mlp = &ml->sml_next;
1493 prevml->sml_next = ml->sml_next;
1495 slap_mod_free( &ml->sml_mod, 0 );
1496 nextml = ml->sml_next;
1503 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1504 && !slap_ad_is_binary( ad )) {
1505 /* attribute requires binary transfer */
1506 snprintf( textbuf, textlen,
1507 "%s: requires ;binary transfer",
1508 ml->sml_type.bv_val );
1510 return LDAP_UNDEFINED_TYPE;
1513 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1514 && slap_ad_is_binary( ad )) {
1515 /* attribute requires binary transfer */
1516 snprintf( textbuf, textlen,
1517 "%s: disallows ;binary transfer",
1518 ml->sml_type.bv_val );
1520 return LDAP_UNDEFINED_TYPE;
1523 if( slap_ad_is_tag_range( ad )) {
1524 /* attribute requires binary transfer */
1525 snprintf( textbuf, textlen,
1526 "%s: inappropriate use of tag range option",
1527 ml->sml_type.bv_val );
1529 return LDAP_UNDEFINED_TYPE;
1532 if ( is_at_obsolete( ad->ad_type ) &&
1533 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1535 * attribute is obsolete,
1536 * only allow replace/delete with no values
1538 snprintf( textbuf, textlen,
1539 "%s: attribute is obsolete",
1540 ml->sml_type.bv_val );
1542 return LDAP_CONSTRAINT_VIOLATION;
1548 if( ml->sml_values != NULL ) {
1550 slap_syntax_validate_func *validate =
1551 ad->ad_type->sat_syntax->ssyn_validate;
1552 slap_syntax_transform_func *pretty =
1553 ad->ad_type->sat_syntax->ssyn_pretty;
1555 if( !pretty && !validate ) {
1556 *text = "no validator for syntax";
1557 snprintf( textbuf, textlen,
1558 "%s: no validator for syntax %s",
1559 ml->sml_type.bv_val,
1560 ad->ad_type->sat_syntax->ssyn_oid );
1562 return LDAP_INVALID_SYNTAX;
1566 * check that each value is valid per syntax
1567 * and pretty if appropriate
1569 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1570 struct berval pval = {0, NULL};
1572 rc = pretty( ad->ad_type->sat_syntax,
1573 &ml->sml_values[nvals], &pval, ctx );
1575 rc = validate( ad->ad_type->sat_syntax,
1576 &ml->sml_values[nvals] );
1580 snprintf( textbuf, textlen,
1581 "%s: value #%ld invalid per syntax",
1582 ml->sml_type.bv_val, (long) nvals );
1584 return LDAP_INVALID_SYNTAX;
1588 ber_memfree( ml->sml_values[nvals].bv_val );
1589 ml->sml_values[nvals] = pval;
1594 * a rough single value check... an additional check is needed
1595 * to catch add of single value to existing single valued attribute
1597 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1598 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1599 snprintf( textbuf, textlen,
1600 "%s: multiple values provided",
1601 ml->sml_type.bv_val );
1603 return LDAP_CONSTRAINT_VIOLATION;
1606 if( nvals && ad->ad_type->sat_equality &&
1607 ad->ad_type->sat_equality->smr_normalize ) {
1608 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1609 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1610 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1611 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1612 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1615 LDAP_LOG( OPERATION, DETAIL1,
1616 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1618 Debug( LDAP_DEBUG_ANY,
1619 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1621 snprintf( textbuf, textlen,
1622 "%s: value #%ld normalization failed",
1623 ml->sml_type.bv_val, (long) nvals );
1628 ml->sml_nvalues[nvals].bv_val = NULL;
1629 ml->sml_nvalues[nvals].bv_len = 0;
1636 return LDAP_SUCCESS;
1640 int slap_mods_opattrs_syncrepl(
1643 Modifications *mods,
1644 Modifications **modtail,
1646 char *textbuf, size_t textlen )
1648 struct berval name = {0, NULL};
1649 struct berval timestamp = {0, NULL};
1650 struct berval csn = {0, NULL};
1651 struct berval nname = {0, NULL};
1652 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1653 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1655 Backend *be = op->o_bd;
1657 int mop = LDAP_MOD_REPLACE;
1659 assert( modtail != NULL );
1660 assert( *modtail == NULL );
1662 if( si->lastmod == LASTMOD_GEN ) {
1664 time_t now = slap_get_time();
1666 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1667 ltm = gmtime( &now );
1668 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1670 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1671 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1672 csn.bv_val = csnbuf;
1674 timestamp.bv_val = timebuf;
1675 timestamp.bv_len = strlen(timebuf);
1677 if( op->o_dn.bv_len == 0 ) {
1678 name.bv_val = SLAPD_ANONYMOUS;
1679 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1687 if( op->o_tag == LDAP_REQ_ADD ) {
1688 struct berval tmpval = {0, NULL};
1690 if( global_schemacheck ) {
1691 int rc = mods_structural_class( mods, &tmpval,
1692 text, textbuf, textlen );
1693 if( rc != LDAP_SUCCESS ) {
1697 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1699 mod->sml_type.bv_val = NULL;
1700 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1701 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1702 ber_dupbv( &mod->sml_values[0], &tmpval );
1703 mod->sml_values[1].bv_len = 0;
1704 mod->sml_values[1].bv_val = NULL;
1705 assert( mod->sml_values[0].bv_val );
1706 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1707 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1708 mod->sml_nvalues[1].bv_len = 0;
1709 mod->sml_nvalues[1].bv_val = NULL;
1710 assert( mod->sml_nvalues[0].bv_val );
1712 modtail = &mod->sml_next;
1715 if( si->lastmod == LASTMOD_GEN ) {
1716 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1718 mod->sml_type.bv_val = NULL;
1719 mod->sml_desc = slap_schema.si_ad_creatorsName;
1720 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1721 ber_dupbv( &mod->sml_values[0], &name );
1722 mod->sml_values[1].bv_len = 0;
1723 mod->sml_values[1].bv_val = NULL;
1724 assert( mod->sml_values[0].bv_val );
1725 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1726 ber_dupbv( &mod->sml_nvalues[0], &nname );
1727 mod->sml_nvalues[1].bv_len = 0;
1728 mod->sml_nvalues[1].bv_val = NULL;
1729 assert( mod->sml_nvalues[0].bv_val );
1731 modtail = &mod->sml_next;
1733 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1735 mod->sml_type.bv_val = NULL;
1736 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1737 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1738 ber_dupbv( &mod->sml_values[0], ×tamp );
1739 mod->sml_values[1].bv_len = 0;
1740 mod->sml_values[1].bv_val = NULL;
1741 assert( mod->sml_values[0].bv_val );
1742 mod->sml_nvalues = NULL;
1744 modtail = &mod->sml_next;
1748 if( si->lastmod == LASTMOD_GEN ) {
1749 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1751 mod->sml_type.bv_val = NULL;
1752 mod->sml_desc = slap_schema.si_ad_entryCSN;
1753 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1754 ber_dupbv( &mod->sml_values[0], &csn );
1755 mod->sml_values[1].bv_len = 0;
1756 mod->sml_values[1].bv_val = NULL;
1757 assert( mod->sml_values[0].bv_val );
1758 mod->sml_nvalues = NULL;
1760 modtail = &mod->sml_next;
1762 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1764 mod->sml_type.bv_val = NULL;
1765 mod->sml_desc = slap_schema.si_ad_modifiersName;
1766 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1767 ber_dupbv( &mod->sml_values[0], &name );
1768 mod->sml_values[1].bv_len = 0;
1769 mod->sml_values[1].bv_val = NULL;
1770 assert( mod->sml_values[0].bv_val );
1771 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1772 ber_dupbv( &mod->sml_nvalues[0], &nname );
1773 mod->sml_nvalues[1].bv_len = 0;
1774 mod->sml_nvalues[1].bv_val = NULL;
1775 assert( mod->sml_nvalues[0].bv_val );
1777 modtail = &mod->sml_next;
1779 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1781 mod->sml_type.bv_val = NULL;
1782 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1783 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1784 ber_dupbv( &mod->sml_values[0], ×tamp );
1785 mod->sml_values[1].bv_len = 0;
1786 mod->sml_values[1].bv_val = NULL;
1787 assert( mod->sml_values[0].bv_val );
1788 mod->sml_nvalues = NULL;
1790 modtail = &mod->sml_next;
1794 return LDAP_SUCCESS;
1799 int slap_mods2entry_syncrepl(
1801 Modifications *mods,
1805 char *textbuf, size_t textlen )
1807 Attribute **tail = &(*e)->e_attrs;
1808 assert( *tail == NULL );
1812 for( ; mods != NULL; mods = mods->sml_next ) {
1815 assert( mods->sml_desc != NULL );
1817 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1819 if( attr != NULL ) {
1820 #define SLURPD_FRIENDLY
1821 #ifdef SLURPD_FRIENDLY
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;
1831 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1834 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1839 attr->a_vals = ch_realloc( attr->a_vals,
1840 sizeof( struct berval ) * (i+j) );
1842 /* should check for duplicates */
1844 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1845 sizeof( struct berval ) * j );
1847 if( attr->a_nvals ) {
1848 attr->a_nvals = ch_realloc( attr->a_nvals,
1849 sizeof( struct berval ) * (i+j) );
1851 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1852 sizeof( struct berval ) * j );
1854 /* trim the mods array */
1855 ch_free( mods->sml_nvalues );
1856 mods->sml_nvalues = NULL;
1861 snprintf( textbuf, textlen,
1862 "attribute '%s' provided more than once",
1863 mods->sml_desc->ad_cname.bv_val );
1864 return LDAP_TYPE_OR_VALUE_EXISTS;
1868 if( mods->sml_values[1].bv_val != NULL ) {
1869 /* check for duplicates */
1871 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1873 /* check if the values we're adding already exist */
1874 if( mr == NULL || !mr->smr_match ) {
1875 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1876 /* test asserted values against themselves */
1877 for( j = 0; j < i; j++ ) {
1878 if ( bvmatch( &mods->sml_bvalues[i],
1879 &mods->sml_bvalues[j] ) ) {
1880 /* value exists already */
1881 snprintf( textbuf, textlen,
1882 "%s: value #%d provided more than once",
1883 mods->sml_desc->ad_cname.bv_val, j );
1884 return LDAP_TYPE_OR_VALUE_EXISTS;
1891 const char *text = NULL;
1892 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1894 rc = modify_check_duplicates( mods->sml_desc, mr,
1895 NULL, mods->sml_bvalues, 0,
1896 &text, textbuf, sizeof( textbuf ) );
1898 if ( rc != LDAP_SUCCESS ) {
1904 attr = ch_calloc( 1, sizeof(Attribute) );
1906 /* move ad to attr structure */
1907 attr->a_desc = mods->sml_desc;
1909 /* move values to attr structure */
1910 /* should check for duplicates */
1911 attr->a_vals = mods->sml_values;
1913 attr->a_nvals = mods->sml_nvalues;
1916 tail = &attr->a_next;
1919 return LDAP_SUCCESS;
1923 avl_ber_bvfree( void *bv )
1928 if ( ((struct berval *)bv)->bv_val != NULL ) {
1929 ber_memfree ( ((struct berval *)bv)->bv_val );
1931 ber_memfree ( (char *) bv );
1940 syncinfo_t *si = op->o_callback->sc_private;
1943 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1945 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1948 si->syncCookie = NULL;
1950 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1952 return LDAP_SUCCESS;
1961 syncinfo_t *si = op->o_callback->sc_private;
1963 if ( rs->sr_type == REP_SEARCH ) {
1964 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1967 return LDAP_SUCCESS;
1971 nonpresent_callback(
1976 syncinfo_t *si = op->o_callback->sc_private;
1979 struct berval* present_uuid = NULL;
1981 SlapReply rs_cb = {REP_RESULT};
1982 struct nonpresent_entry *np_entry;
1984 if ( rs->sr_type == REP_RESULT ) {
1985 count = avl_free( si->presentlist, avl_ber_bvfree );
1986 return LDAP_SUCCESS;
1987 } else if ( rs->sr_type == REP_SEARCH ) {
1988 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1993 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1995 if ( present_uuid == NULL ) {
1996 np_entry = (struct nonpresent_entry *)
1997 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1998 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1999 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2000 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2002 avl_delete( &si->presentlist,
2003 &a->a_vals[0], syncuuid_cmp );
2005 return LDAP_SUCCESS;
2007 return LDAP_SUCCESS;
2018 syncinfo_t *si = op->o_callback->sc_private;
2020 if ( rs->sr_err != LDAP_SUCCESS &&
2021 rs->sr_err != LDAP_REFERRAL &&
2022 rs->sr_err != LDAP_ALREADY_EXISTS &&
2023 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2025 LDAP_LOG( OPERATION, ERR,
2026 "null_callback : error code 0x%x\n",
2029 Debug( LDAP_DEBUG_ANY,
2030 "null_callback : error code 0x%x\n",
2034 return LDAP_SUCCESS;
2039 str2clist( char **out, char *in, const char *brkstr )
2048 /* find last element in list */
2049 for (i = 0; out && out[i]; i++);
2051 /* protect the input string from strtok */
2052 str = ch_strdup( in );
2054 /* Count words in string */
2056 for ( s = str; *s; s++ ) {
2057 if ( strchr( brkstr, *s ) != NULL ) {
2062 out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2064 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2066 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2068 *new = ch_strdup( s );