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 );
997 } else if ( rc == LDAP_REFERRAL ||
998 rc == LDAP_NO_SUCH_OBJECT ) {
999 syncrepl_add_glue( si, ld, op, e,
1001 syncUUID, syncCookie);
1006 LDAP_LOG( OPERATION, ERR,
1007 "be_modify failed (%d)\n",
1010 Debug( LDAP_DEBUG_ANY,
1011 "be_modify failed (%d)\n",
1023 LDAP_LOG( OPERATION, ERR,
1024 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1026 Debug( LDAP_DEBUG_ANY,
1027 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1033 case LDAP_SYNC_DELETE :
1034 /* Already deleted */
1039 LDAP_LOG( OPERATION, ERR,
1040 "unknown syncstate\n", 0, 0, 0 );
1042 Debug( LDAP_DEBUG_ANY,
1043 "unknown syncstate\n", 0, 0, 0 );
1050 syncrepl_del_nonpresent(
1056 Backend* be = op->o_bd;
1058 struct berval base_bv = {0, NULL};
1060 SlapReply rs = {REP_RESULT};
1061 struct berval filterstr_bv = {0, NULL};
1062 struct nonpresent_entry *np_list, *np_prev;
1064 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1065 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1066 ch_free( base_bv.bv_val );
1068 filter = str2filter( si->filterstr );
1070 cb.sc_response = nonpresent_callback;
1073 op->o_callback = &cb;
1074 op->o_tag = LDAP_REQ_SEARCH;
1075 op->ors_scope = si->scope;
1076 op->ors_deref = LDAP_DEREF_NEVER;
1079 op->ors_attrsonly = 0;
1080 op->ors_attrs = NULL;
1081 op->ors_filter = filter;
1082 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1084 be->be_search( op, &rs );
1086 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1087 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1088 while ( np_list != NULL ) {
1089 LDAP_LIST_REMOVE( np_list, np_link );
1091 np_list = LDAP_LIST_NEXT( np_list, np_link );
1092 op->o_tag = LDAP_REQ_DELETE;
1093 op->o_callback = &cb;
1094 cb.sc_response = null_callback;
1096 op->o_req_dn = *np_prev->dn;
1097 op->o_req_ndn = *np_prev->ndn;
1098 op->o_bd->be_delete( op, &rs );
1099 ber_bvfree( np_prev->dn );
1100 ber_bvfree( np_prev->ndn );
1101 op->o_req_dn.bv_val = NULL;
1102 op->o_req_ndn.bv_val = NULL;
1107 if ( op->o_req_dn.bv_val )
1108 ch_free( op->o_req_dn.bv_val );
1109 if ( op->o_req_ndn.bv_val )
1110 ch_free( op->o_req_ndn.bv_val );
1111 filter_free( op->ors_filter );
1112 ch_free( op->ors_filterstr.bv_val );
1124 Modifications* modlist,
1126 struct berval* syncUUID,
1127 struct berval* syncCookie
1130 Backend *be = op->o_bd;
1131 struct berval uuid_bv = {0, NULL};
1135 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1138 struct berval dn = {0, NULL};
1139 struct berval pdn = {0, NULL};
1140 struct berval ndn = {0, NULL};
1141 struct berval rdn = {0, NULL};
1143 SlapReply rs = {REP_RESULT};
1144 Connection *conn = op->o_conn;
1147 op->o_tag = LDAP_REQ_ADD;
1148 op->o_callback = &cb;
1149 cb.sc_response = null_callback;
1152 ber_dupbv( &dn, &e->e_nname );
1153 ber_dupbv( &pdn, &e->e_nname );
1156 while ( !be_issuffix ( be, &pdn )) {
1157 dnParent( &dn, &pdn );
1158 dn.bv_val = pdn.bv_val;
1159 dn.bv_len = pdn.bv_len;
1164 for ( i = 0; i <= levels; i++ ) {
1165 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1166 glue->e_private = NULL;
1167 ber_dupbv( &dn, &e->e_nname );
1171 for ( k = 0; k < j; k++ ) {
1172 dnParent( &dn, &pdn );
1173 dn.bv_val = pdn.bv_val;
1174 dn.bv_len = pdn.bv_len;
1177 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1178 ber_dupbv( &glue->e_name, &pdn );
1179 ber_dupbv( &glue->e_nname, &ndn );
1181 ch_free( pdn.bv_val );
1182 ch_free( ndn.bv_val );
1184 a = ch_calloc( 1, sizeof( Attribute ));
1185 a->a_desc = slap_schema.si_ad_objectClass;
1187 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1188 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1189 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1190 a->a_vals[2].bv_len = 0;
1191 a->a_vals[2].bv_val = NULL;
1193 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1194 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1195 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1196 a->a_nvals[2].bv_len = 0;
1197 a->a_nvals[2].bv_val = NULL;
1199 a->a_next = glue->e_attrs;
1202 a = ch_calloc( 1, sizeof( Attribute ));
1203 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1205 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1206 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1207 a->a_vals[1].bv_len = 0;
1208 a->a_vals[1].bv_val = NULL;
1210 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1211 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1212 a->a_nvals[1].bv_len = 0;
1213 a->a_nvals[1].bv_val = NULL;
1215 a->a_next = glue->e_attrs;
1218 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1219 op->o_req_dn = e->e_name;
1220 op->o_req_ndn = e->e_nname;
1222 rc = be->be_add ( op, &rs );
1223 if ( rc == LDAP_SUCCESS )
1224 be_entry_release_w( op, e );
1229 op->o_req_dn = glue->e_name;
1230 op->o_req_ndn = glue->e_nname;
1232 rc = be->be_add ( op, &rs );
1233 if ( rc == LDAP_SUCCESS ) {
1234 be_entry_release_w( op, glue );
1236 /* incl. ALREADY EXIST */
1246 syncrepl_updateCookie(
1251 struct berval *syncCookie
1254 Backend *be = op->o_bd;
1256 Modifications *mlnext;
1258 Modifications *modlist;
1259 Modifications **modtail = &modlist;
1261 struct berval* ocbva = NULL;
1262 struct berval* cnbva = NULL;
1263 struct berval* ssbva = NULL;
1264 struct berval* scbva = NULL;
1269 char txtbuf[SLAP_TEXT_BUFLEN];
1270 size_t textlen = sizeof txtbuf;
1275 struct berval sub_bv = { 0, NULL };
1276 struct berval psubrdn = { 0, NULL };
1279 SlapReply rs = {REP_RESULT};
1281 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1282 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1283 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1284 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1286 /* update in memory cookie */
1287 if ( si->syncCookie != NULL ) {
1288 ber_bvfree( si->syncCookie );
1290 si->syncCookie = ber_dupbv( NULL, syncCookie );
1291 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1292 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1293 ber_str2bv( "syncConsumerSubentry",
1294 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1295 ocbva[3].bv_len = 0;
1296 ocbva[3].bv_val = NULL;
1298 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1299 mod->sml_op = LDAP_MOD_REPLACE;
1300 mod->sml_next = NULL;
1301 mod->sml_desc = NULL;
1302 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1303 mod->sml_bvalues = ocbva;
1304 mod->sml_nvalues = ocbva;
1306 modtail = &mod->sml_next;
1308 sprintf( substr, "syncrepl%d", si->id );
1309 sprintf( rdnstr, "cn=%s", substr );
1310 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1311 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1312 cnbva[1].bv_len = 0;
1313 cnbva[1].bv_val = NULL;
1314 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1315 mod->sml_op = LDAP_MOD_REPLACE;
1316 mod->sml_next = NULL;
1317 mod->sml_desc = NULL;
1318 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1319 mod->sml_bvalues = cnbva;
1320 mod->sml_nvalues = cnbva;
1322 modtail = &mod->sml_next;
1324 ber_dupbv( &scbva[0], si->syncCookie );
1325 scbva[1].bv_len = 0;
1326 scbva[1].bv_val = NULL;
1327 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1328 mod->sml_op = LDAP_MOD_REPLACE;
1329 mod->sml_next = NULL;
1330 mod->sml_desc = NULL;
1331 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1332 1, &mod->sml_type );
1333 mod->sml_bvalues = scbva;
1334 mod->sml_nvalues = scbva;
1336 modtail = &mod->sml_next;
1338 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1339 ssbva[1].bv_len = 0;
1340 ssbva[1].bv_val = NULL;
1341 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1342 mod->sml_op = LDAP_MOD_REPLACE;
1343 mod->sml_next = NULL;
1344 mod->sml_desc = NULL;
1345 ber_str2bv( "subtreeSpecification",
1346 strlen("subtreeSpecification"), 1, &mod->sml_type );
1347 mod->sml_bvalues = ssbva;
1348 mod->sml_nvalues = ssbva;
1350 modtail = &mod->sml_next;
1352 rc = slap_mods_check_syncrepl( si, op, &modlist,
1353 &text, txtbuf, textlen, NULL );
1355 if ( rc != LDAP_SUCCESS ) {
1357 LDAP_LOG( OPERATION, ERR,
1358 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1360 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1365 op->o_tag = LDAP_REQ_ADD;
1366 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1367 &text,txtbuf, textlen );
1369 if( rc != LDAP_SUCCESS ) {
1371 LDAP_LOG( OPERATION, ERR,
1372 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1374 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1379 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1381 build_new_dn( &sub_bv, pdn, &psubrdn );
1382 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1383 ch_free( sub_bv.bv_val );
1384 ch_free( psubrdn.bv_val );
1388 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1390 if( rc != LDAP_SUCCESS ) {
1392 LDAP_LOG( OPERATION, ERR,
1393 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1395 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1400 cb.sc_response = null_callback;
1403 op->o_callback = &cb;
1404 op->o_req_dn = e->e_name;
1405 op->o_req_ndn = e->e_nname;
1407 /* update persistent cookie */
1408 update_cookie_retry:
1409 op->o_tag = LDAP_REQ_MODIFY;
1410 op->orm_modlist = modlist;
1411 rc = be->be_modify( op, &rs );
1412 if ( rc != LDAP_SUCCESS ) {
1413 if ( rc == LDAP_REFERRAL ||
1414 rc == LDAP_NO_SUCH_OBJECT ) {
1415 op->o_tag = LDAP_REQ_ADD;
1417 rc = be->be_add( op, &rs );
1418 if ( rc != LDAP_SUCCESS ) {
1419 if ( rc == LDAP_ALREADY_EXISTS ) {
1420 goto update_cookie_retry;
1421 } else if ( rc == LDAP_REFERRAL ||
1422 rc == LDAP_NO_SUCH_OBJECT ) {
1424 LDAP_LOG( OPERATION, ERR,
1425 "cookie will be non-persistent\n",
1428 Debug( LDAP_DEBUG_ANY,
1429 "cookie will be non-persistent\n",
1434 LDAP_LOG( OPERATION, ERR,
1435 "be_add failed (%d)\n",
1438 Debug( LDAP_DEBUG_ANY,
1439 "be_add failed (%d)\n",
1448 LDAP_LOG( OPERATION, ERR,
1449 "be_modify failed (%d)\n", rc, 0, 0 );
1451 Debug( LDAP_DEBUG_ANY,
1452 "be_modify failed (%d)\n", rc, 0, 0 );
1462 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1463 mlnext = ml->sml_next;
1472 int slap_mods_check_syncrepl(
1475 Modifications **mlp,
1482 Backend *be = op->o_bd;
1483 AttributeDescription** descs;
1485 Modifications *prevml = NULL;
1486 Modifications *nextml = NULL;
1487 Modifications *ml = *mlp;
1489 while ( ml != NULL ) {
1490 AttributeDescription *ad = NULL;
1492 /* convert to attribute description */
1493 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1495 if( rc != LDAP_SUCCESS ) {
1496 snprintf( textbuf, textlen, "%s: %s",
1497 ml->sml_type.bv_val, *text );
1504 if ( si->lastmod == LASTMOD_REQ ) {
1505 descs = del_descs_lastmod;
1510 for ( i = 0; descs[i] != NULL; i++ ) {
1511 if ( ad == descs[i] ) {
1512 if ( prevml == NULL ) {
1513 mlp = &ml->sml_next;
1516 prevml->sml_next = ml->sml_next;
1518 slap_mod_free( &ml->sml_mod, 0 );
1519 nextml = ml->sml_next;
1526 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1527 && !slap_ad_is_binary( ad )) {
1528 /* attribute requires binary transfer */
1529 snprintf( textbuf, textlen,
1530 "%s: requires ;binary transfer",
1531 ml->sml_type.bv_val );
1533 return LDAP_UNDEFINED_TYPE;
1536 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1537 && slap_ad_is_binary( ad )) {
1538 /* attribute requires binary transfer */
1539 snprintf( textbuf, textlen,
1540 "%s: disallows ;binary transfer",
1541 ml->sml_type.bv_val );
1543 return LDAP_UNDEFINED_TYPE;
1546 if( slap_ad_is_tag_range( ad )) {
1547 /* attribute requires binary transfer */
1548 snprintf( textbuf, textlen,
1549 "%s: inappropriate use of tag range option",
1550 ml->sml_type.bv_val );
1552 return LDAP_UNDEFINED_TYPE;
1555 if ( is_at_obsolete( ad->ad_type ) &&
1556 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1558 * attribute is obsolete,
1559 * only allow replace/delete with no values
1561 snprintf( textbuf, textlen,
1562 "%s: attribute is obsolete",
1563 ml->sml_type.bv_val );
1565 return LDAP_CONSTRAINT_VIOLATION;
1571 if( ml->sml_values != NULL ) {
1573 slap_syntax_validate_func *validate =
1574 ad->ad_type->sat_syntax->ssyn_validate;
1575 slap_syntax_transform_func *pretty =
1576 ad->ad_type->sat_syntax->ssyn_pretty;
1578 if( !pretty && !validate ) {
1579 *text = "no validator for syntax";
1580 snprintf( textbuf, textlen,
1581 "%s: no validator for syntax %s",
1582 ml->sml_type.bv_val,
1583 ad->ad_type->sat_syntax->ssyn_oid );
1585 return LDAP_INVALID_SYNTAX;
1589 * check that each value is valid per syntax
1590 * and pretty if appropriate
1592 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1593 struct berval pval = {0, NULL};
1595 rc = pretty( ad->ad_type->sat_syntax,
1596 &ml->sml_values[nvals], &pval, ctx );
1598 rc = validate( ad->ad_type->sat_syntax,
1599 &ml->sml_values[nvals] );
1603 snprintf( textbuf, textlen,
1604 "%s: value #%ld invalid per syntax",
1605 ml->sml_type.bv_val, (long) nvals );
1607 return LDAP_INVALID_SYNTAX;
1611 ber_memfree( ml->sml_values[nvals].bv_val );
1612 ml->sml_values[nvals] = pval;
1617 * a rough single value check... an additional check is needed
1618 * to catch add of single value to existing single valued attribute
1620 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1621 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1622 snprintf( textbuf, textlen,
1623 "%s: multiple values provided",
1624 ml->sml_type.bv_val );
1626 return LDAP_CONSTRAINT_VIOLATION;
1629 if( nvals && ad->ad_type->sat_equality &&
1630 ad->ad_type->sat_equality->smr_normalize ) {
1631 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1632 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1633 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1634 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1635 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1638 LDAP_LOG( OPERATION, DETAIL1,
1639 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1641 Debug( LDAP_DEBUG_ANY,
1642 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1644 snprintf( textbuf, textlen,
1645 "%s: value #%ld normalization failed",
1646 ml->sml_type.bv_val, (long) nvals );
1651 ml->sml_nvalues[nvals].bv_val = NULL;
1652 ml->sml_nvalues[nvals].bv_len = 0;
1659 return LDAP_SUCCESS;
1663 int slap_mods_opattrs_syncrepl(
1666 Modifications *mods,
1667 Modifications **modtail,
1669 char *textbuf, size_t textlen )
1671 struct berval name = {0, NULL};
1672 struct berval timestamp = {0, NULL};
1673 struct berval csn = {0, NULL};
1674 struct berval nname = {0, NULL};
1675 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1676 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1678 Backend *be = op->o_bd;
1680 int mop = LDAP_MOD_REPLACE;
1682 assert( modtail != NULL );
1683 assert( *modtail == NULL );
1685 if( si->lastmod == LASTMOD_GEN ) {
1687 time_t now = slap_get_time();
1689 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1690 ltm = gmtime( &now );
1691 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1693 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1694 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1695 csn.bv_val = csnbuf;
1697 timestamp.bv_val = timebuf;
1698 timestamp.bv_len = strlen(timebuf);
1700 if( op->o_dn.bv_len == 0 ) {
1701 name.bv_val = SLAPD_ANONYMOUS;
1702 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1710 if( op->o_tag == LDAP_REQ_ADD ) {
1711 struct berval tmpval = {0, NULL};
1713 if( global_schemacheck ) {
1714 int rc = mods_structural_class( mods, &tmpval,
1715 text, textbuf, textlen );
1716 if( rc != LDAP_SUCCESS ) {
1720 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1722 mod->sml_type.bv_val = NULL;
1723 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1724 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1725 ber_dupbv( &mod->sml_values[0], &tmpval );
1726 mod->sml_values[1].bv_len = 0;
1727 mod->sml_values[1].bv_val = NULL;
1728 assert( mod->sml_values[0].bv_val );
1729 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1730 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1731 mod->sml_nvalues[1].bv_len = 0;
1732 mod->sml_nvalues[1].bv_val = NULL;
1733 assert( mod->sml_nvalues[0].bv_val );
1735 modtail = &mod->sml_next;
1738 if( si->lastmod == LASTMOD_GEN ) {
1739 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1741 mod->sml_type.bv_val = NULL;
1742 mod->sml_desc = slap_schema.si_ad_creatorsName;
1743 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1744 ber_dupbv( &mod->sml_values[0], &name );
1745 mod->sml_values[1].bv_len = 0;
1746 mod->sml_values[1].bv_val = NULL;
1747 assert( mod->sml_values[0].bv_val );
1748 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1749 ber_dupbv( &mod->sml_nvalues[0], &nname );
1750 mod->sml_nvalues[1].bv_len = 0;
1751 mod->sml_nvalues[1].bv_val = NULL;
1752 assert( mod->sml_nvalues[0].bv_val );
1754 modtail = &mod->sml_next;
1756 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1758 mod->sml_type.bv_val = NULL;
1759 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1760 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1761 ber_dupbv( &mod->sml_values[0], ×tamp );
1762 mod->sml_values[1].bv_len = 0;
1763 mod->sml_values[1].bv_val = NULL;
1764 assert( mod->sml_values[0].bv_val );
1765 mod->sml_nvalues = NULL;
1767 modtail = &mod->sml_next;
1771 if( si->lastmod == LASTMOD_GEN ) {
1772 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1774 mod->sml_type.bv_val = NULL;
1775 mod->sml_desc = slap_schema.si_ad_entryCSN;
1776 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1777 ber_dupbv( &mod->sml_values[0], &csn );
1778 mod->sml_values[1].bv_len = 0;
1779 mod->sml_values[1].bv_val = NULL;
1780 assert( mod->sml_values[0].bv_val );
1781 mod->sml_nvalues = NULL;
1783 modtail = &mod->sml_next;
1785 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1787 mod->sml_type.bv_val = NULL;
1788 mod->sml_desc = slap_schema.si_ad_modifiersName;
1789 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1790 ber_dupbv( &mod->sml_values[0], &name );
1791 mod->sml_values[1].bv_len = 0;
1792 mod->sml_values[1].bv_val = NULL;
1793 assert( mod->sml_values[0].bv_val );
1794 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1795 ber_dupbv( &mod->sml_nvalues[0], &nname );
1796 mod->sml_nvalues[1].bv_len = 0;
1797 mod->sml_nvalues[1].bv_val = NULL;
1798 assert( mod->sml_nvalues[0].bv_val );
1800 modtail = &mod->sml_next;
1802 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1804 mod->sml_type.bv_val = NULL;
1805 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1806 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1807 ber_dupbv( &mod->sml_values[0], ×tamp );
1808 mod->sml_values[1].bv_len = 0;
1809 mod->sml_values[1].bv_val = NULL;
1810 assert( mod->sml_values[0].bv_val );
1811 mod->sml_nvalues = NULL;
1813 modtail = &mod->sml_next;
1817 return LDAP_SUCCESS;
1822 int slap_mods2entry_syncrepl(
1824 Modifications *mods,
1828 char *textbuf, size_t textlen )
1830 Attribute **tail = &(*e)->e_attrs;
1831 assert( *tail == NULL );
1835 for( ; mods != NULL; mods = mods->sml_next ) {
1838 assert( mods->sml_desc != NULL );
1840 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1842 if( attr != NULL ) {
1843 #define SLURPD_FRIENDLY
1844 #ifdef SLURPD_FRIENDLY
1848 snprintf( textbuf, textlen,
1849 "attribute '%s' provided more than once",
1850 mods->sml_desc->ad_cname.bv_val );
1851 return LDAP_TYPE_OR_VALUE_EXISTS;
1854 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1857 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1862 attr->a_vals = ch_realloc( attr->a_vals,
1863 sizeof( struct berval ) * (i+j) );
1865 /* should check for duplicates */
1867 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1868 sizeof( struct berval ) * j );
1870 if( attr->a_nvals ) {
1871 attr->a_nvals = ch_realloc( attr->a_nvals,
1872 sizeof( struct berval ) * (i+j) );
1874 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1875 sizeof( struct berval ) * j );
1877 /* trim the mods array */
1878 ch_free( mods->sml_nvalues );
1879 mods->sml_nvalues = NULL;
1884 snprintf( textbuf, textlen,
1885 "attribute '%s' provided more than once",
1886 mods->sml_desc->ad_cname.bv_val );
1887 return LDAP_TYPE_OR_VALUE_EXISTS;
1891 if( mods->sml_values[1].bv_val != NULL ) {
1892 /* check for duplicates */
1894 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1896 /* check if the values we're adding already exist */
1897 if( mr == NULL || !mr->smr_match ) {
1898 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1899 /* test asserted values against themselves */
1900 for( j = 0; j < i; j++ ) {
1901 if ( bvmatch( &mods->sml_bvalues[i],
1902 &mods->sml_bvalues[j] ) ) {
1903 /* value exists already */
1904 snprintf( textbuf, textlen,
1905 "%s: value #%d provided more than once",
1906 mods->sml_desc->ad_cname.bv_val, j );
1907 return LDAP_TYPE_OR_VALUE_EXISTS;
1914 const char *text = NULL;
1915 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1917 rc = modify_check_duplicates( mods->sml_desc, mr,
1918 NULL, mods->sml_bvalues, 0,
1919 &text, textbuf, sizeof( textbuf ) );
1921 if ( rc != LDAP_SUCCESS ) {
1927 attr = ch_calloc( 1, sizeof(Attribute) );
1929 /* move ad to attr structure */
1930 attr->a_desc = mods->sml_desc;
1932 /* move values to attr structure */
1933 /* should check for duplicates */
1934 attr->a_vals = mods->sml_values;
1936 attr->a_nvals = mods->sml_nvalues;
1939 tail = &attr->a_next;
1942 return LDAP_SUCCESS;
1946 avl_ber_bvfree( void *bv )
1951 if ( ((struct berval *)bv)->bv_val != NULL ) {
1952 ber_memfree ( ((struct berval *)bv)->bv_val );
1954 ber_memfree ( (char *) bv );
1963 syncinfo_t *si = op->o_callback->sc_private;
1966 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1968 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1971 si->syncCookie = NULL;
1973 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1975 return LDAP_SUCCESS;
1984 syncinfo_t *si = op->o_callback->sc_private;
1986 if ( rs->sr_type == REP_SEARCH ) {
1987 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1990 return LDAP_SUCCESS;
1994 nonpresent_callback(
1999 syncinfo_t *si = op->o_callback->sc_private;
2002 struct berval* present_uuid = NULL;
2004 SlapReply rs_cb = {REP_RESULT};
2005 struct nonpresent_entry *np_entry;
2007 if ( rs->sr_type == REP_RESULT ) {
2008 count = avl_free( si->presentlist, avl_ber_bvfree );
2009 return LDAP_SUCCESS;
2010 } else if ( rs->sr_type == REP_SEARCH ) {
2011 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2016 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
2018 if ( present_uuid == NULL ) {
2019 np_entry = (struct nonpresent_entry *)
2020 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2021 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
2022 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2023 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2025 avl_delete( &si->presentlist,
2026 &a->a_vals[0], syncuuid_cmp );
2028 return LDAP_SUCCESS;
2030 return LDAP_SUCCESS;
2041 syncinfo_t *si = op->o_callback->sc_private;
2043 if ( rs->sr_err != LDAP_SUCCESS &&
2044 rs->sr_err != LDAP_REFERRAL &&
2045 rs->sr_err != LDAP_ALREADY_EXISTS &&
2046 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2048 LDAP_LOG( OPERATION, ERR,
2049 "null_callback : error code 0x%x\n",
2052 Debug( LDAP_DEBUG_ANY,
2053 "null_callback : error code 0x%x\n",
2057 return LDAP_SUCCESS;
2062 str2clist( char **out, char *in, const char *brkstr )
2071 /* find last element in list */
2072 for (i = 0; out && out[i]; i++);
2074 /* protect the input string from strtok */
2075 str = ch_strdup( in );
2077 /* Count words in string */
2079 for ( s = str; *s; s++ ) {
2080 if ( strchr( brkstr, *s ) != NULL ) {
2085 out = ch_realloc( out, ( i + j + 1 ) * sizeof( char * ) );
2087 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2089 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2091 *new = ch_strdup( s );