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->provideruri );
269 if ( rc != LDAP_SUCCESS ) {
271 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
272 "ldap_initialize failed (%s)\n",
273 si->provideruri, 0, 0 );
275 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
276 "ldap_initialize failed (%s)\n",
277 si->provideruri, 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->provideruri, si->secprops, 0 );
319 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
320 "(%s,SECPROPS,\"%s\") failed!\n",
321 si->provideruri, 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 ber_dupbv( &dn, &e->e_nname );
1170 for ( k = 0; k < j; k++ ) {
1171 dnParent( &dn, &pdn );
1172 dn.bv_val = pdn.bv_val;
1173 dn.bv_len = pdn.bv_len;
1176 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1177 ber_dupbv( &glue->e_name, &pdn );
1178 ber_dupbv( &glue->e_nname, &ndn );
1180 ch_free( pdn.bv_val );
1181 ch_free( ndn.bv_val );
1183 a = ch_calloc( 1, sizeof( Attribute ));
1184 a->a_desc = slap_schema.si_ad_objectClass;
1186 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1187 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1188 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1189 a->a_vals[2].bv_len = 0;
1190 a->a_vals[2].bv_val = NULL;
1192 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1193 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1194 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1195 a->a_nvals[2].bv_len = 0;
1196 a->a_nvals[2].bv_val = NULL;
1198 a->a_next = glue->e_attrs;
1201 a = ch_calloc( 1, sizeof( Attribute ));
1202 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1204 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1205 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1206 a->a_vals[1].bv_len = 0;
1207 a->a_vals[1].bv_val = NULL;
1209 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1210 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1211 a->a_nvals[1].bv_len = 0;
1212 a->a_nvals[1].bv_val = NULL;
1214 a->a_next = glue->e_attrs;
1217 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1218 op->o_req_dn = e->e_name;
1219 op->o_req_ndn = e->e_nname;
1221 rc = be->be_add ( op, &rs );
1222 if ( rc == LDAP_SUCCESS )
1223 be_entry_release_w( op, e );
1228 op->o_req_dn = glue->e_name;
1229 op->o_req_ndn = glue->e_nname;
1231 rc = be->be_add ( op, &rs );
1232 if ( rc == LDAP_SUCCESS ) {
1233 be_entry_release_w( op, glue );
1235 /* incl. ALREADY EXIST */
1245 syncrepl_updateCookie(
1250 struct berval *syncCookie
1253 Backend *be = op->o_bd;
1255 Modifications *mlnext;
1257 Modifications *modlist;
1258 Modifications **modtail = &modlist;
1260 struct berval* ocbva = NULL;
1261 struct berval* cnbva = NULL;
1262 struct berval* ssbva = NULL;
1263 struct berval* scbva = NULL;
1268 char txtbuf[SLAP_TEXT_BUFLEN];
1269 size_t textlen = sizeof txtbuf;
1274 struct berval sub_bv = { 0, NULL };
1275 struct berval psubrdn = { 0, NULL };
1278 SlapReply rs = {REP_RESULT};
1280 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1281 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1282 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1283 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1285 /* update in memory cookie */
1286 if ( si->syncCookie != NULL ) {
1287 ber_bvfree( si->syncCookie );
1289 si->syncCookie = ber_dupbv( NULL, syncCookie );
1290 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1291 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1292 ber_str2bv( "syncConsumerSubentry",
1293 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1294 ocbva[3].bv_len = 0;
1295 ocbva[3].bv_val = NULL;
1297 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1298 mod->sml_op = LDAP_MOD_REPLACE;
1299 mod->sml_next = NULL;
1300 mod->sml_desc = NULL;
1301 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1302 mod->sml_bvalues = ocbva;
1303 mod->sml_nvalues = ocbva;
1305 modtail = &mod->sml_next;
1307 sprintf( substr, "syncrepl%d", si->id );
1308 sprintf( rdnstr, "cn=%s", substr );
1309 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1310 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1311 cnbva[1].bv_len = 0;
1312 cnbva[1].bv_val = NULL;
1313 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1314 mod->sml_op = LDAP_MOD_REPLACE;
1315 mod->sml_next = NULL;
1316 mod->sml_desc = NULL;
1317 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1318 mod->sml_bvalues = cnbva;
1319 mod->sml_nvalues = cnbva;
1321 modtail = &mod->sml_next;
1323 ber_dupbv( &scbva[0], si->syncCookie );
1324 scbva[1].bv_len = 0;
1325 scbva[1].bv_val = NULL;
1326 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1327 mod->sml_op = LDAP_MOD_REPLACE;
1328 mod->sml_next = NULL;
1329 mod->sml_desc = NULL;
1330 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1331 1, &mod->sml_type );
1332 mod->sml_bvalues = scbva;
1333 mod->sml_nvalues = scbva;
1335 modtail = &mod->sml_next;
1337 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1338 ssbva[1].bv_len = 0;
1339 ssbva[1].bv_val = NULL;
1340 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1341 mod->sml_op = LDAP_MOD_REPLACE;
1342 mod->sml_next = NULL;
1343 mod->sml_desc = NULL;
1344 ber_str2bv( "subtreeSpecification",
1345 strlen("subtreeSpecification"), 1, &mod->sml_type );
1346 mod->sml_bvalues = ssbva;
1347 mod->sml_nvalues = ssbva;
1349 modtail = &mod->sml_next;
1351 rc = slap_mods_check_syncrepl( si, op, &modlist,
1352 &text, txtbuf, textlen, NULL );
1354 if ( rc != LDAP_SUCCESS ) {
1356 LDAP_LOG( OPERATION, ERR,
1357 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1359 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1364 op->o_tag = LDAP_REQ_ADD;
1365 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1366 &text,txtbuf, textlen );
1368 if( rc != LDAP_SUCCESS ) {
1370 LDAP_LOG( OPERATION, ERR,
1371 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1373 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1378 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1380 build_new_dn( &sub_bv, pdn, &psubrdn );
1381 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1382 ch_free( sub_bv.bv_val );
1383 ch_free( psubrdn.bv_val );
1387 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1389 if( rc != LDAP_SUCCESS ) {
1391 LDAP_LOG( OPERATION, ERR,
1392 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1394 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1399 cb.sc_response = null_callback;
1402 op->o_callback = &cb;
1403 op->o_req_dn = e->e_name;
1404 op->o_req_ndn = e->e_nname;
1406 /* update persistent cookie */
1407 update_cookie_retry:
1408 op->o_tag = LDAP_REQ_MODIFY;
1409 op->orm_modlist = modlist;
1410 rc = be->be_modify( op, &rs );
1411 if ( rc != LDAP_SUCCESS ) {
1412 if ( rc == LDAP_REFERRAL ||
1413 rc == LDAP_NO_SUCH_OBJECT ) {
1414 op->o_tag = LDAP_REQ_ADD;
1416 rc = be->be_add( op, &rs );
1417 if ( rc != LDAP_SUCCESS ) {
1418 if ( rc == LDAP_ALREADY_EXISTS ) {
1419 goto update_cookie_retry;
1420 } else if ( rc == LDAP_REFERRAL ||
1421 rc == LDAP_NO_SUCH_OBJECT ) {
1423 LDAP_LOG( OPERATION, ERR,
1424 "cookie will be non-persistent\n",
1427 Debug( LDAP_DEBUG_ANY,
1428 "cookie will be non-persistent\n",
1433 LDAP_LOG( OPERATION, ERR,
1434 "be_add failed (%d)\n",
1437 Debug( LDAP_DEBUG_ANY,
1438 "be_add failed (%d)\n",
1447 LDAP_LOG( OPERATION, ERR,
1448 "be_modify failed (%d)\n", rc, 0, 0 );
1450 Debug( LDAP_DEBUG_ANY,
1451 "be_modify failed (%d)\n", rc, 0, 0 );
1461 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1462 mlnext = ml->sml_next;
1471 int slap_mods_check_syncrepl(
1474 Modifications **mlp,
1481 Backend *be = op->o_bd;
1482 AttributeDescription** descs;
1484 Modifications *prevml = NULL;
1485 Modifications *nextml = NULL;
1486 Modifications *ml = *mlp;
1488 while ( ml != NULL ) {
1489 AttributeDescription *ad = NULL;
1491 /* convert to attribute description */
1492 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1494 if( rc != LDAP_SUCCESS ) {
1495 snprintf( textbuf, textlen, "%s: %s",
1496 ml->sml_type.bv_val, *text );
1503 if ( si->lastmod == LASTMOD_REQ ) {
1504 descs = del_descs_lastmod;
1509 for ( i = 0; descs[i] != NULL; i++ ) {
1510 if ( ad == descs[i] ) {
1511 if ( prevml == NULL ) {
1512 mlp = &ml->sml_next;
1515 prevml->sml_next = ml->sml_next;
1517 slap_mod_free( &ml->sml_mod, 0 );
1518 nextml = ml->sml_next;
1525 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1526 && !slap_ad_is_binary( ad )) {
1527 /* attribute requires binary transfer */
1528 snprintf( textbuf, textlen,
1529 "%s: requires ;binary transfer",
1530 ml->sml_type.bv_val );
1532 return LDAP_UNDEFINED_TYPE;
1535 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1536 && slap_ad_is_binary( ad )) {
1537 /* attribute requires binary transfer */
1538 snprintf( textbuf, textlen,
1539 "%s: disallows ;binary transfer",
1540 ml->sml_type.bv_val );
1542 return LDAP_UNDEFINED_TYPE;
1545 if( slap_ad_is_tag_range( ad )) {
1546 /* attribute requires binary transfer */
1547 snprintf( textbuf, textlen,
1548 "%s: inappropriate use of tag range option",
1549 ml->sml_type.bv_val );
1551 return LDAP_UNDEFINED_TYPE;
1554 if ( is_at_obsolete( ad->ad_type ) &&
1555 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1557 * attribute is obsolete,
1558 * only allow replace/delete with no values
1560 snprintf( textbuf, textlen,
1561 "%s: attribute is obsolete",
1562 ml->sml_type.bv_val );
1564 return LDAP_CONSTRAINT_VIOLATION;
1570 if( ml->sml_values != NULL ) {
1572 slap_syntax_validate_func *validate =
1573 ad->ad_type->sat_syntax->ssyn_validate;
1574 slap_syntax_transform_func *pretty =
1575 ad->ad_type->sat_syntax->ssyn_pretty;
1577 if( !pretty && !validate ) {
1578 *text = "no validator for syntax";
1579 snprintf( textbuf, textlen,
1580 "%s: no validator for syntax %s",
1581 ml->sml_type.bv_val,
1582 ad->ad_type->sat_syntax->ssyn_oid );
1584 return LDAP_INVALID_SYNTAX;
1588 * check that each value is valid per syntax
1589 * and pretty if appropriate
1591 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1592 struct berval pval = {0, NULL};
1594 rc = pretty( ad->ad_type->sat_syntax,
1595 &ml->sml_values[nvals], &pval, ctx );
1597 rc = validate( ad->ad_type->sat_syntax,
1598 &ml->sml_values[nvals] );
1602 snprintf( textbuf, textlen,
1603 "%s: value #%ld invalid per syntax",
1604 ml->sml_type.bv_val, (long) nvals );
1606 return LDAP_INVALID_SYNTAX;
1610 ber_memfree( ml->sml_values[nvals].bv_val );
1611 ml->sml_values[nvals] = pval;
1616 * a rough single value check... an additional check is needed
1617 * to catch add of single value to existing single valued attribute
1619 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1620 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1621 snprintf( textbuf, textlen,
1622 "%s: multiple values provided",
1623 ml->sml_type.bv_val );
1625 return LDAP_CONSTRAINT_VIOLATION;
1628 if( nvals && ad->ad_type->sat_equality &&
1629 ad->ad_type->sat_equality->smr_normalize ) {
1630 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1631 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1632 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1633 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1634 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1637 LDAP_LOG( OPERATION, DETAIL1,
1638 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1640 Debug( LDAP_DEBUG_ANY,
1641 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1643 snprintf( textbuf, textlen,
1644 "%s: value #%ld normalization failed",
1645 ml->sml_type.bv_val, (long) nvals );
1650 ml->sml_nvalues[nvals].bv_val = NULL;
1651 ml->sml_nvalues[nvals].bv_len = 0;
1658 return LDAP_SUCCESS;
1662 int slap_mods_opattrs_syncrepl(
1665 Modifications *mods,
1666 Modifications **modtail,
1668 char *textbuf, size_t textlen )
1670 struct berval name = {0, NULL};
1671 struct berval timestamp = {0, NULL};
1672 struct berval csn = {0, NULL};
1673 struct berval nname = {0, NULL};
1674 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1675 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1677 Backend *be = op->o_bd;
1679 int mop = LDAP_MOD_REPLACE;
1681 assert( modtail != NULL );
1682 assert( *modtail == NULL );
1684 if( si->lastmod == LASTMOD_GEN ) {
1686 time_t now = slap_get_time();
1688 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1689 ltm = gmtime( &now );
1690 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1692 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1693 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1694 csn.bv_val = csnbuf;
1696 timestamp.bv_val = timebuf;
1697 timestamp.bv_len = strlen(timebuf);
1699 if( op->o_dn.bv_len == 0 ) {
1700 name.bv_val = SLAPD_ANONYMOUS;
1701 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1709 if( op->o_tag == LDAP_REQ_ADD ) {
1710 struct berval tmpval = {0, NULL};
1712 if( global_schemacheck ) {
1713 int rc = mods_structural_class( mods, &tmpval,
1714 text, textbuf, textlen );
1715 if( rc != LDAP_SUCCESS ) {
1719 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1721 mod->sml_type.bv_val = NULL;
1722 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1723 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1724 ber_dupbv( &mod->sml_values[0], &tmpval );
1725 mod->sml_values[1].bv_len = 0;
1726 mod->sml_values[1].bv_val = NULL;
1727 assert( mod->sml_values[0].bv_val );
1728 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1729 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1730 mod->sml_nvalues[1].bv_len = 0;
1731 mod->sml_nvalues[1].bv_val = NULL;
1732 assert( mod->sml_nvalues[0].bv_val );
1734 modtail = &mod->sml_next;
1737 if( si->lastmod == LASTMOD_GEN ) {
1738 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1740 mod->sml_type.bv_val = NULL;
1741 mod->sml_desc = slap_schema.si_ad_creatorsName;
1742 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1743 ber_dupbv( &mod->sml_values[0], &name );
1744 mod->sml_values[1].bv_len = 0;
1745 mod->sml_values[1].bv_val = NULL;
1746 assert( mod->sml_values[0].bv_val );
1747 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1748 ber_dupbv( &mod->sml_nvalues[0], &nname );
1749 mod->sml_nvalues[1].bv_len = 0;
1750 mod->sml_nvalues[1].bv_val = NULL;
1751 assert( mod->sml_nvalues[0].bv_val );
1753 modtail = &mod->sml_next;
1755 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1757 mod->sml_type.bv_val = NULL;
1758 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1759 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1760 ber_dupbv( &mod->sml_values[0], ×tamp );
1761 mod->sml_values[1].bv_len = 0;
1762 mod->sml_values[1].bv_val = NULL;
1763 assert( mod->sml_values[0].bv_val );
1764 mod->sml_nvalues = NULL;
1766 modtail = &mod->sml_next;
1770 if( si->lastmod == LASTMOD_GEN ) {
1771 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1773 mod->sml_type.bv_val = NULL;
1774 mod->sml_desc = slap_schema.si_ad_entryCSN;
1775 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1776 ber_dupbv( &mod->sml_values[0], &csn );
1777 mod->sml_values[1].bv_len = 0;
1778 mod->sml_values[1].bv_val = NULL;
1779 assert( mod->sml_values[0].bv_val );
1780 mod->sml_nvalues = NULL;
1782 modtail = &mod->sml_next;
1784 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1786 mod->sml_type.bv_val = NULL;
1787 mod->sml_desc = slap_schema.si_ad_modifiersName;
1788 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1789 ber_dupbv( &mod->sml_values[0], &name );
1790 mod->sml_values[1].bv_len = 0;
1791 mod->sml_values[1].bv_val = NULL;
1792 assert( mod->sml_values[0].bv_val );
1793 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1794 ber_dupbv( &mod->sml_nvalues[0], &nname );
1795 mod->sml_nvalues[1].bv_len = 0;
1796 mod->sml_nvalues[1].bv_val = NULL;
1797 assert( mod->sml_nvalues[0].bv_val );
1799 modtail = &mod->sml_next;
1801 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1803 mod->sml_type.bv_val = NULL;
1804 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1805 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1806 ber_dupbv( &mod->sml_values[0], ×tamp );
1807 mod->sml_values[1].bv_len = 0;
1808 mod->sml_values[1].bv_val = NULL;
1809 assert( mod->sml_values[0].bv_val );
1810 mod->sml_nvalues = NULL;
1812 modtail = &mod->sml_next;
1816 return LDAP_SUCCESS;
1821 int slap_mods2entry_syncrepl(
1823 Modifications *mods,
1827 char *textbuf, size_t textlen )
1829 Attribute **tail = &(*e)->e_attrs;
1830 assert( *tail == NULL );
1834 for( ; mods != NULL; mods = mods->sml_next ) {
1837 assert( mods->sml_desc != NULL );
1839 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1841 if( attr != NULL ) {
1842 #define SLURPD_FRIENDLY
1843 #ifdef SLURPD_FRIENDLY
1847 snprintf( textbuf, textlen,
1848 "attribute '%s' provided more than once",
1849 mods->sml_desc->ad_cname.bv_val );
1850 return LDAP_TYPE_OR_VALUE_EXISTS;
1853 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1856 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1861 attr->a_vals = ch_realloc( attr->a_vals,
1862 sizeof( struct berval ) * (i+j) );
1864 /* should check for duplicates */
1866 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1867 sizeof( struct berval ) * j );
1869 if( attr->a_nvals ) {
1870 attr->a_nvals = ch_realloc( attr->a_nvals,
1871 sizeof( struct berval ) * (i+j) );
1873 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1874 sizeof( struct berval ) * j );
1876 /* trim the mods array */
1877 ch_free( mods->sml_nvalues );
1878 mods->sml_nvalues = NULL;
1883 snprintf( textbuf, textlen,
1884 "attribute '%s' provided more than once",
1885 mods->sml_desc->ad_cname.bv_val );
1886 return LDAP_TYPE_OR_VALUE_EXISTS;
1890 if( mods->sml_values[1].bv_val != NULL ) {
1891 /* check for duplicates */
1893 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1895 /* check if the values we're adding already exist */
1896 if( mr == NULL || !mr->smr_match ) {
1897 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1898 /* test asserted values against themselves */
1899 for( j = 0; j < i; j++ ) {
1900 if ( bvmatch( &mods->sml_bvalues[i],
1901 &mods->sml_bvalues[j] ) ) {
1902 /* value exists already */
1903 snprintf( textbuf, textlen,
1904 "%s: value #%d provided more than once",
1905 mods->sml_desc->ad_cname.bv_val, j );
1906 return LDAP_TYPE_OR_VALUE_EXISTS;
1913 const char *text = NULL;
1914 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1916 rc = modify_check_duplicates( mods->sml_desc, mr,
1917 NULL, mods->sml_bvalues, 0,
1918 &text, textbuf, sizeof( textbuf ) );
1920 if ( rc != LDAP_SUCCESS ) {
1926 attr = ch_calloc( 1, sizeof(Attribute) );
1928 /* move ad to attr structure */
1929 attr->a_desc = mods->sml_desc;
1931 /* move values to attr structure */
1932 /* should check for duplicates */
1933 attr->a_vals = mods->sml_values;
1935 attr->a_nvals = mods->sml_nvalues;
1938 tail = &attr->a_next;
1941 return LDAP_SUCCESS;
1945 avl_ber_bvfree( void *bv )
1950 if ( ((struct berval *)bv)->bv_val != NULL ) {
1951 ber_memfree ( ((struct berval *)bv)->bv_val );
1953 ber_memfree ( (char *) bv );
1962 syncinfo_t *si = op->o_callback->sc_private;
1965 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1967 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1970 si->syncCookie = NULL;
1972 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1974 return LDAP_SUCCESS;
1983 syncinfo_t *si = op->o_callback->sc_private;
1985 if ( rs->sr_type == REP_SEARCH ) {
1986 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1989 return LDAP_SUCCESS;
1993 nonpresent_callback(
1998 syncinfo_t *si = op->o_callback->sc_private;
2001 struct berval* present_uuid = NULL;
2003 SlapReply rs_cb = {REP_RESULT};
2004 struct nonpresent_entry *np_entry;
2006 if ( rs->sr_type == REP_RESULT ) {
2007 count = avl_free( si->presentlist, avl_ber_bvfree );
2008 si->presentlist = NULL;
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 );