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 slap_mods_check_syncrepl( syncinfo_t *, Operation *, Modifications **,
45 const char **, char *, size_t, void *ctx );
48 slap_mods_opattrs_syncrepl( syncinfo_t *, Operation *, Modifications *,
49 Modifications **, const char **, char *, size_t );
52 slap_mods2entry_syncrepl( syncinfo_t *, Modifications *, Entry **, int,
53 const char **, char *, size_t );
55 /* callback functions */
56 static int cookie_callback( struct slap_op *, struct slap_rep * );
57 static int dn_callback( struct slap_op *, struct slap_rep * );
58 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
59 static int null_callback( struct slap_op *, struct slap_rep * );
61 static AttributeDescription **add_descs;
62 static AttributeDescription **add_descs_lastmod;
63 static AttributeDescription **del_descs;
64 static AttributeDescription **del_descs_lastmod;
66 struct runqueue_s syncrepl_rq;
71 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
72 add_descs[0] = slap_schema.si_ad_objectClass;
75 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
76 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
77 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
78 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
79 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
80 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
81 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
82 add_descs_lastmod[6] = NULL;
84 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
85 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
86 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
87 del_descs[2] = slap_schema.si_ad_hasSubordinates;
88 del_descs[3] = slap_schema.si_ad_creatorsName;
89 del_descs[4] = slap_schema.si_ad_modifiersName;
90 del_descs[5] = slap_schema.si_ad_createTimestamp;
91 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
92 del_descs[7] = slap_schema.si_ad_entryCSN;
95 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
96 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
97 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
98 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
99 del_descs_lastmod[3] = NULL;
106 LDAPControl **sctrls,
107 LDAPControl **cctrls,
115 BerElement *sync_ber = NULL;
116 struct berval *sync_bvalp = NULL;
120 struct timeval timeout;
122 /* setup LDAP SYNC control */
123 sync_ber = ber_alloc_t( LBER_USE_DER );
124 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, NULL );
126 if ( si->syncCookie ) {
127 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
129 ber_printf( sync_ber, "{e}", abs(si->type) );
132 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
133 ber_free( sync_ber, 1 );
136 ber_free( sync_ber, 1 );
138 ctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), NULL );
140 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
141 c[0].ldctl_value = (*sync_bvalp);
142 c[0].ldctl_iscritical = si->type < 0;
146 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
147 c[1].ldctl_value.bv_val = si->authzId;
148 c[1].ldctl_value.bv_len = strlen( si->authzId );
149 c[1].ldctl_iscritical = 1;
157 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, ctrls );
159 ber_bvfree( sync_bvalp );
162 if ( err != LDAP_OPT_SUCCESS )
163 fprintf( stderr, "Could not set controls : %d\n", err );
165 timeout.tv_sec = si->tlimit > 0 ? si->tlimit : 1;
167 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
168 si->attrs, si->attrsonly, sctrls, cctrls,
169 si->tlimit < 0 ? NULL : &timeout,
170 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->provideruri );
265 if ( rc != LDAP_SUCCESS ) {
267 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
268 "ldap_initialize failed (%s)\n",
269 si->provideruri, 0, 0 );
271 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
272 "ldap_initialize failed (%s)\n",
273 si->provideruri, 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->provideruri, si->secprops, 0 );
315 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
316 "(%s,SECPROPS,\"%s\") failed!\n",
317 si->provideruri, 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 */
372 #define SLAB_SIZE 1048576
374 memctx = sl_mem_create( memsiz, ctx );
375 op.o_tmpmemctx = memctx;
376 op.o_tmpmfuncs = &sl_mfuncs;
378 op.o_tag = LDAP_REQ_SEARCH;
379 op.o_dn = si->updatedn;
380 op.o_ndn = si->updatedn;
382 op.o_time = slap_get_time();
383 op.o_managedsait = 1;
384 op.o_threadctx = si->ctx;
387 op.o_connid = op.o_conn->c_connid;
388 op.ors_scope = LDAP_SCOPE_BASE;
389 op.ors_deref = LDAP_DEREF_NEVER;
392 op.ors_attrsonly = 0;
394 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
395 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
399 conn.c_send_ldap_result = slap_send_ldap_result;
400 conn.c_send_search_entry = slap_send_search_entry;
401 conn.c_send_search_reference = slap_send_search_reference;
403 /* get syncrepl cookie of shadow replica from subentry */
404 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
405 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
407 sprintf( substr, "cn=syncrepl%d", si->id );
408 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
409 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
411 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
412 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
414 ch_free( base_bv.bv_val );
415 ch_free( pbase.bv_val );
416 ch_free( nbase.bv_val );
417 ch_free( sub_bv.bv_val );
418 ch_free( psubrdn.bv_val );
419 ch_free( nsubrdn.bv_val );
421 /* set callback function */
422 cb.sc_response = cookie_callback;
425 /* search subentry to retrieve cookie */
426 si->syncCookie = NULL;
427 be->be_search( &op, &rs );
429 ch_free( op.o_req_dn.bv_val );
430 ch_free( op.o_req_ndn.bv_val );
431 filter_free( op.ors_filter );
432 ch_free( op.ors_filterstr.bv_val );
434 psub = be->be_nsuffix[0];
436 /* Delete Attributes */
437 if ( si->lastmod == LASTMOD_REQ ) {
438 descs = del_descs_lastmod;
443 for ( i = 0; descs[i] != NULL; i++ ) {
444 for ( j = 0; si->attrs[j] != NULL; j++ ) {
445 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
446 ch_free( si->attrs[j] );
447 for ( k = j; si->attrs[k] != NULL; k++ ) {
448 si->attrs[k] = si->attrs[k+1];
456 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
458 if ( si->lastmod == LASTMOD_REQ ) {
459 descs = add_descs_lastmod;
464 for ( i = 0; descs[i] != NULL; i++ ) {
465 tmp = ( char ** ) ch_realloc( si->attrs,
466 ( n + 2 ) * sizeof( char * ));
469 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
471 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
475 si->attrs[ n++ ] = ch_strdup ( descs[i]->ad_cname.bv_val );
476 si->attrs[ n ] = NULL;
479 rc = ldap_sync_search( si, ld, NULL, NULL, &msgid );
480 if( rc != LDAP_SUCCESS ) {
481 fprintf( stderr, "syncrepl: ldap_search_ext: %s (%d)\n",
482 ldap_err2string( rc ), rc );
486 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
488 for ( msg = ldap_first_message( ld, res );
490 msg = ldap_next_message( ld, msg ) )
492 switch( ldap_msgtype( msg ) ) {
493 case LDAP_RES_SEARCH_ENTRY:
494 entry = syncrepl_message_to_entry( si, ld, &op, msg,
495 &modlist, &syncstate, &syncUUID, &syncCookie );
496 rc_efree = syncrepl_entry( si, ld, &op, entry, modlist,
497 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
498 if ( syncCookie.bv_len ) {
499 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
503 for ( ml = modlist; ml != NULL; ml = mlnext ) {
504 mlnext = ml->sml_next;
509 case LDAP_RES_SEARCH_REFERENCE:
511 LDAP_LOG( OPERATION, ERR,
512 "do_syncrepl : reference received\n", 0, 0, 0 );
514 Debug( LDAP_DEBUG_ANY,
515 "do_syncrepl : reference received\n", 0, 0, 0 );
519 case LDAP_RES_SEARCH_RESULT:
520 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
523 ctrl_ber = ber_alloc_t( LBER_USE_DER );
524 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op.o_tmpmemctx );
525 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
526 ber_reset( ctrl_ber, 1 );
528 ber_scanf( ctrl_ber, "{" /*"}"*/);
529 if ( ber_peek_tag( ctrl_ber, &len )
530 == LDAP_SYNC_TAG_COOKIE ) {
531 ber_scanf( ctrl_ber, "o", &syncCookie );
534 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
535 if ( cancel_response ) {
536 if ( syncCookie.bv_len ) {
537 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie );
540 ber_free( ctrl_ber, 1 );
545 ber_free( ctrl_ber, 1 );
549 if ( syncCookie.bv_len ) {
550 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
552 syncrepl_del_nonpresent( si, ld, &op );
554 ber_free( ctrl_ber, 1 );
559 case LDAP_RES_INTERMEDIATE:
560 rc = ldap_parse_intermediate( ld, msg,
561 &retoid, &retdata, NULL, 0 );
562 if ( !rc && !strcmp( retoid, LDAP_SYNC_INFO ) ) {
563 sync_info_arrived = 1;
564 res_ber = ber_init( retdata );
565 ber_scanf( res_ber, "{e" /*"}"*/, &syncstate );
567 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
568 syncrepl_del_nonpresent( si, ld, &op );
569 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
571 LDAP_LOG( OPERATION, ERR,
572 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
574 Debug( LDAP_DEBUG_ANY,
575 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
579 if ( ber_peek_tag( res_ber, &len )
580 == LDAP_SYNC_TAG_COOKIE ) {
581 ber_scanf( res_ber, /*"{"*/ "o}", &syncCookie );
582 if ( syncCookie.bv_len ) {
583 syncrepl_updateCookie( si, ld, &op, &psub, &syncCookie);
586 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
588 LDAP_LOG( OPERATION, ERR,
589 "do_syncrepl : cookie required\n", 0, 0, 0 );
591 Debug( LDAP_DEBUG_ANY,
592 "do_syncrepl : cookie required\n", 0, 0, 0 );
597 ldap_memfree( retoid );
598 ber_bvfree( retdata );
599 ber_free( res_ber, 1 );
603 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
604 " unknown intermediate "
605 "response\n", 0, 0, 0 );
607 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
608 "unknown intermediate response (%d)\n",
611 ldap_memfree( retoid );
612 ber_bvfree( retdata );
618 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
619 "unknown message\n", 0, 0, 0 );
621 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
622 "unknown message\n", 0, 0, 0 );
633 LDAP_LOG( OPERATION, ERR,
634 "do_syncrepl : unknown result\n", 0, 0, 0 );
636 Debug( LDAP_DEBUG_ANY,
637 "do_syncrepl : unknown result\n", 0, 0, 0 );
642 if ( syncCookie.bv_val )
643 ch_free( syncCookie.bv_val );
644 if ( syncUUID.bv_val )
645 ch_free( syncUUID.bv_val );
651 ldap_pvt_thread_mutex_lock( &syncrepl_rq.rq_mutex );
652 ldap_pvt_runqueue_stoptask( &syncrepl_rq, rtask );
653 if ( si->type == LDAP_SYNC_REFRESH_ONLY ) {
654 ldap_pvt_runqueue_resched( &syncrepl_rq, rtask );
656 ldap_pvt_runqueue_remove( &syncrepl_rq, rtask );
658 ldap_pvt_thread_mutex_unlock( &syncrepl_rq.rq_mutex );
664 syncrepl_message_to_entry(
669 Modifications **modlist,
671 struct berval *syncUUID,
672 struct berval *syncCookie
676 BerElement *ber = NULL;
678 struct berval bv = {0, NULL};
681 Modifications **modtail = modlist;
682 Backend *be = op->o_bd;
685 char txtbuf[SLAP_TEXT_BUFLEN];
686 size_t textlen = sizeof txtbuf;
688 struct berval **bvals = NULL;
690 struct berval bdn = {0, NULL};
692 struct berval empty_bv = { 0, NULL };
698 LDAPControl** rctrls = NULL;
699 BerElement* ctrl_ber;
705 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
707 LDAP_LOG( OPERATION, ERR,
708 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
710 Debug( LDAP_DEBUG_ANY,
711 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
716 op->o_tag = LDAP_REQ_ADD;
718 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
720 if ( rc != LDAP_SUCCESS ) {
722 LDAP_LOG( OPERATION, ERR,
723 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
725 Debug( LDAP_DEBUG_ANY,
726 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
731 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
732 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, NULL );
736 while ( ber_remaining( ber ) ) {
737 tag = ber_scanf( ber, "{mW}", &tmp.sml_type, &tmp.sml_values );
739 if ( tag == LBER_ERROR ) break;
740 if ( tmp.sml_type.bv_val == NULL ) break;
742 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
744 mod->sml_op = LDAP_MOD_REPLACE;
745 mod->sml_next = NULL;
746 mod->sml_desc = NULL;
747 mod->sml_type = tmp.sml_type;
748 mod->sml_bvalues = tmp.sml_bvalues;
749 mod->sml_nvalues = tmp.sml_bvalues;
752 modtail = &mod->sml_next;
755 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
757 LDAP_LOG( OPERATION, ERR,
758 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
760 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
767 tmpber = ldap_get_message_ber( msg );
768 ber = ber_dup( tmpber );
770 ber_scanf( ber, "{xx" );
772 rc = ldap_int_get_controls( ber, &rctrls );
774 if ( rc != LDAP_SUCCESS ) {
776 LDAP_LOG( OPERATION, ERR,
777 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
779 Debug( LDAP_DEBUG_ANY,
780 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
787 ctrl_ber = ber_alloc_t( LBER_USE_DER );
788 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, &op->o_tmpmemctx );
789 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
790 ber_reset( ctrl_ber, 1 );
791 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
792 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
793 ber_scanf( ctrl_ber, "o}", syncCookie );
795 ber_free( ctrl_ber, 1 );
798 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
799 " rctrls absent\n", 0, 0, 0 );
801 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
802 " rctrls absent\n", 0, 0, 0 );
806 if ( *syncstate == LDAP_SYNC_PRESENT ) {
809 } else if ( *syncstate == LDAP_SYNC_DELETE ) {
813 if ( *modlist == NULL ) {
815 LDAP_LOG( OPERATION, ERR,
816 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
818 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
823 rc = slap_mods_check_syncrepl( si, op, modlist,
824 &text, txtbuf, textlen, NULL );
826 if ( rc != LDAP_SUCCESS ) {
828 LDAP_LOG( OPERATION, ERR,
829 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
831 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
837 rc = slap_mods_opattrs_syncrepl( si, op, *modlist, modtail,
838 &text,txtbuf, textlen );
840 if( rc != LDAP_SUCCESS ) {
842 LDAP_LOG( OPERATION, ERR,
843 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
845 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
851 rc = slap_mods2entry_syncrepl( si, *modlist, &e, 1, &text, txtbuf, textlen);
852 if( rc != LDAP_SUCCESS ) {
854 LDAP_LOG( OPERATION, ERR,
855 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
857 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
870 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
872 const struct berval *uuid1 = v_uuid1;
873 const struct berval *uuid2 = v_uuid2;
874 int rc = uuid1->bv_len - uuid2->bv_len;
876 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
885 Modifications* modlist,
887 struct berval* syncUUID,
888 struct berval* syncCookie,
892 Backend *be = op->o_bd;
894 struct berval csn_bv = {0, NULL};
895 struct berval *syncuuid_bv = NULL;
896 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
898 SlapReply rs = {REP_RESULT};
899 int rc = LDAP_SUCCESS;
901 struct berval base_bv = {0, NULL};
909 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
910 syncuuid_bv = ber_dupbv( NULL, syncUUID );
911 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
912 syncuuid_cmp, avl_dup_error );
915 if ( syncstate == LDAP_SYNC_PRESENT ) {
922 filterstr = (char *) sl_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1,
924 strcpy( filterstr, "entryUUID=" );
925 strcat( filterstr, syncUUID->bv_val );
928 si->syncUUID = syncUUID;
929 si->syncUUID_ndn = NULL;
931 filter = str2filter( filterstr );
932 ber_str2bv( filterstr, strlen(filterstr), 1, &op->ors_filterstr );
933 ch_free( filterstr );
934 op->ors_filter = filter;
935 op->ors_scope = LDAP_SCOPE_SUBTREE;
937 /* get syncrepl cookie of shadow replica from subentry */
938 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
939 dnPrettyNormal( 0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
940 ch_free( base_bv.bv_val );
942 /* set callback function */
943 op->o_callback = &cb;
944 cb.sc_response = dn_callback;
947 be->be_search( op, &rs );
949 ch_free( op->o_req_dn.bv_val );
950 ch_free( op->o_req_ndn.bv_val );
951 filter_free( op->ors_filter );
952 ch_free( op->ors_filterstr.bv_val );
954 cb.sc_response = null_callback;
959 if ( si->syncUUID_ndn ) {
960 op->o_req_dn = *si->syncUUID_ndn;
961 op->o_req_ndn = *si->syncUUID_ndn;
962 op->o_tag = LDAP_REQ_DELETE;
963 rc = be->be_delete( op, &rs );
966 switch ( syncstate ) {
968 case LDAP_SYNC_MODIFY :
970 if ( rc == LDAP_SUCCESS ||
971 rc == LDAP_REFERRAL ||
972 rc == LDAP_NO_SUCH_OBJECT ) {
974 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
975 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
978 op->o_tag = LDAP_REQ_ADD;
980 op->o_req_dn = e->e_name;
981 op->o_req_ndn = e->e_nname;
982 rc = be->be_add( op, &rs );
984 if ( rc != LDAP_SUCCESS ) {
985 if ( rc == LDAP_ALREADY_EXISTS ) {
986 op->o_tag = LDAP_REQ_MODIFY;
987 op->orm_modlist = modlist;
988 op->o_req_dn = e->e_name;
989 op->o_req_ndn = e->e_nname;
990 rc = be->be_modify( op, &rs );
993 } else if ( rc == LDAP_REFERRAL ||
994 rc == LDAP_NO_SUCH_OBJECT ) {
995 syncrepl_add_glue( si, ld, op, e,
997 syncUUID, syncCookie);
1002 LDAP_LOG( OPERATION, ERR,
1003 "be_modify failed (%d)\n",
1006 Debug( LDAP_DEBUG_ANY,
1007 "be_modify failed (%d)\n",
1019 LDAP_LOG( OPERATION, ERR,
1020 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1022 Debug( LDAP_DEBUG_ANY,
1023 "be_modify/be_delete failed (%d)\n", rc, 0, 0 );
1029 case LDAP_SYNC_DELETE :
1030 /* Already deleted */
1035 LDAP_LOG( OPERATION, ERR,
1036 "unknown syncstate\n", 0, 0, 0 );
1038 Debug( LDAP_DEBUG_ANY,
1039 "unknown syncstate\n", 0, 0, 0 );
1046 syncrepl_del_nonpresent(
1052 Backend* be = op->o_bd;
1054 struct berval base_bv = {0, NULL};
1056 SlapReply rs = {REP_RESULT};
1057 struct berval filterstr_bv = {0, NULL};
1058 struct nonpresent_entry *np_list, *np_prev;
1060 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1061 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1062 ch_free( base_bv.bv_val );
1064 filter = str2filter( si->filterstr );
1066 cb.sc_response = nonpresent_callback;
1069 op->o_callback = &cb;
1070 op->o_tag = LDAP_REQ_SEARCH;
1071 op->ors_scope = si->scope;
1072 op->ors_deref = LDAP_DEREF_NEVER;
1075 op->ors_attrsonly = 0;
1076 op->ors_attrs = NULL;
1077 op->ors_filter = filter;
1078 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1080 be->be_search( op, &rs );
1082 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1083 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1084 while ( np_list != NULL ) {
1085 LDAP_LIST_REMOVE( np_list, np_link );
1087 np_list = LDAP_LIST_NEXT( np_list, np_link );
1088 op->o_tag = LDAP_REQ_DELETE;
1089 op->o_callback = &cb;
1090 cb.sc_response = null_callback;
1092 op->o_req_dn = *np_prev->dn;
1093 op->o_req_ndn = *np_prev->ndn;
1094 op->o_bd->be_delete( op, &rs );
1095 ber_bvfree( np_prev->dn );
1096 ber_bvfree( np_prev->ndn );
1097 op->o_req_dn.bv_val = NULL;
1098 op->o_req_ndn.bv_val = NULL;
1103 if ( op->o_req_dn.bv_val )
1104 ch_free( op->o_req_dn.bv_val );
1105 if ( op->o_req_ndn.bv_val )
1106 ch_free( op->o_req_ndn.bv_val );
1107 filter_free( op->ors_filter );
1108 ch_free( op->ors_filterstr.bv_val );
1120 Modifications* modlist,
1122 struct berval* syncUUID,
1123 struct berval* syncCookie
1126 Backend *be = op->o_bd;
1127 struct berval uuid_bv = {0, NULL};
1131 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1134 struct berval dn = {0, NULL};
1135 struct berval pdn = {0, NULL};
1136 struct berval ndn = {0, NULL};
1137 struct berval rdn = {0, NULL};
1139 SlapReply rs = {REP_RESULT};
1140 Connection *conn = op->o_conn;
1143 op->o_tag = LDAP_REQ_ADD;
1144 op->o_callback = &cb;
1145 cb.sc_response = null_callback;
1148 ber_dupbv( &dn, &e->e_nname );
1149 ber_dupbv( &pdn, &e->e_nname );
1152 while ( !be_issuffix ( be, &pdn )) {
1153 dnParent( &dn, &pdn );
1154 dn.bv_val = pdn.bv_val;
1155 dn.bv_len = pdn.bv_len;
1160 for ( i = 0; i <= levels; i++ ) {
1161 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1162 ber_dupbv( &dn, &e->e_nname );
1166 for ( k = 0; k < j; k++ ) {
1167 dnParent( &dn, &pdn );
1168 dn.bv_val = pdn.bv_val;
1169 dn.bv_len = pdn.bv_len;
1172 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1173 ber_dupbv( &glue->e_name, &pdn );
1174 ber_dupbv( &glue->e_nname, &ndn );
1176 ch_free( pdn.bv_val );
1177 ch_free( ndn.bv_val );
1179 a = ch_calloc( 1, sizeof( Attribute ));
1180 a->a_desc = slap_schema.si_ad_objectClass;
1182 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1183 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1184 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1185 a->a_vals[2].bv_len = 0;
1186 a->a_vals[2].bv_val = NULL;
1188 a->a_nvals = ch_calloc( 3, sizeof( struct berval ));
1189 ber_str2bv( "top", strlen("top"), 1, &a->a_nvals[0] );
1190 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[1] );
1191 a->a_nvals[2].bv_len = 0;
1192 a->a_nvals[2].bv_val = NULL;
1194 a->a_next = glue->e_attrs;
1197 a = ch_calloc( 1, sizeof( Attribute ));
1198 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1200 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1201 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1202 a->a_vals[1].bv_len = 0;
1203 a->a_vals[1].bv_val = NULL;
1205 a->a_nvals = ch_calloc( 2, sizeof( struct berval ));
1206 ber_str2bv( "glue", strlen("glue"), 1, &a->a_nvals[0] );
1207 a->a_nvals[1].bv_len = 0;
1208 a->a_nvals[1].bv_val = NULL;
1210 a->a_next = glue->e_attrs;
1213 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1214 op->o_req_dn = e->e_name;
1215 op->o_req_ndn = e->e_nname;
1217 rc = be->be_add ( op, &rs );
1218 if ( rc == LDAP_SUCCESS )
1219 be_entry_release_w( op, e );
1224 op->o_req_dn = glue->e_name;
1225 op->o_req_ndn = glue->e_nname;
1227 rc = be->be_add ( op, &rs );
1228 if ( rc == LDAP_SUCCESS ) {
1229 be_entry_release_w( op, glue );
1231 /* incl. ALREADY EXIST */
1241 syncrepl_updateCookie(
1246 struct berval *syncCookie
1249 Backend *be = op->o_bd;
1251 Modifications *mlnext;
1253 Modifications *modlist;
1254 Modifications **modtail = &modlist;
1256 struct berval* ocbva = NULL;
1257 struct berval* cnbva = NULL;
1258 struct berval* ssbva = NULL;
1259 struct berval* scbva = NULL;
1264 char txtbuf[SLAP_TEXT_BUFLEN];
1265 size_t textlen = sizeof txtbuf;
1270 struct berval sub_bv = { 0, NULL };
1271 struct berval psubrdn = { 0, NULL };
1274 SlapReply rs = {REP_RESULT};
1276 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1277 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1278 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1279 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1281 /* update in memory cookie */
1282 if ( si->syncCookie != NULL ) {
1283 ber_bvfree( si->syncCookie );
1285 si->syncCookie = ber_dupbv( NULL, syncCookie );
1286 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1287 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1288 ber_str2bv( "syncConsumerSubentry",
1289 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1290 ocbva[3].bv_len = 0;
1291 ocbva[3].bv_val = NULL;
1293 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1294 mod->sml_op = LDAP_MOD_REPLACE;
1295 mod->sml_next = NULL;
1296 mod->sml_desc = NULL;
1297 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1298 mod->sml_bvalues = ocbva;
1299 mod->sml_nvalues = ocbva;
1301 modtail = &mod->sml_next;
1303 sprintf( substr, "syncrepl%d", si->id );
1304 sprintf( rdnstr, "cn=%s", substr );
1305 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1306 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1307 cnbva[1].bv_len = 0;
1308 cnbva[1].bv_val = NULL;
1309 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1310 mod->sml_op = LDAP_MOD_REPLACE;
1311 mod->sml_next = NULL;
1312 mod->sml_desc = NULL;
1313 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1314 mod->sml_bvalues = cnbva;
1315 mod->sml_nvalues = cnbva;
1317 modtail = &mod->sml_next;
1319 ber_dupbv( &scbva[0], si->syncCookie );
1320 scbva[1].bv_len = 0;
1321 scbva[1].bv_val = NULL;
1322 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1323 mod->sml_op = LDAP_MOD_REPLACE;
1324 mod->sml_next = NULL;
1325 mod->sml_desc = NULL;
1326 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1327 1, &mod->sml_type );
1328 mod->sml_bvalues = scbva;
1329 mod->sml_nvalues = scbva;
1331 modtail = &mod->sml_next;
1333 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1334 ssbva[1].bv_len = 0;
1335 ssbva[1].bv_val = NULL;
1336 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1337 mod->sml_op = LDAP_MOD_REPLACE;
1338 mod->sml_next = NULL;
1339 mod->sml_desc = NULL;
1340 ber_str2bv( "subtreeSpecification",
1341 strlen("subtreeSpecification"), 1, &mod->sml_type );
1342 mod->sml_bvalues = ssbva;
1343 mod->sml_nvalues = ssbva;
1345 modtail = &mod->sml_next;
1347 rc = slap_mods_check_syncrepl( si, op, &modlist,
1348 &text, txtbuf, textlen, NULL );
1350 if ( rc != LDAP_SUCCESS ) {
1352 LDAP_LOG( OPERATION, ERR,
1353 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1355 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1360 op->o_tag = LDAP_REQ_ADD;
1361 rc = slap_mods_opattrs_syncrepl( si, op, modlist, modtail,
1362 &text,txtbuf, textlen );
1364 if( rc != LDAP_SUCCESS ) {
1366 LDAP_LOG( OPERATION, ERR,
1367 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1369 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1374 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1376 build_new_dn( &sub_bv, pdn, &psubrdn );
1377 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1378 ch_free( sub_bv.bv_val );
1379 ch_free( psubrdn.bv_val );
1383 rc = slap_mods2entry_syncrepl( si, modlist, &e, 1, &text, txtbuf, textlen );
1385 if( rc != LDAP_SUCCESS ) {
1387 LDAP_LOG( OPERATION, ERR,
1388 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1390 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1395 cb.sc_response = null_callback;
1398 op->o_callback = &cb;
1399 op->o_req_dn = e->e_name;
1400 op->o_req_ndn = e->e_nname;
1402 /* update persistent cookie */
1403 update_cookie_retry:
1404 op->o_tag = LDAP_REQ_MODIFY;
1405 op->orm_modlist = modlist;
1406 rc = be->be_modify( op, &rs );
1407 if ( rc != LDAP_SUCCESS ) {
1408 if ( rc == LDAP_REFERRAL ||
1409 rc == LDAP_NO_SUCH_OBJECT ) {
1410 op->o_tag = LDAP_REQ_ADD;
1412 rc = be->be_add( op, &rs );
1413 if ( rc != LDAP_SUCCESS ) {
1414 if ( rc == LDAP_ALREADY_EXISTS ) {
1415 goto update_cookie_retry;
1416 } else if ( rc == LDAP_REFERRAL ||
1417 rc == LDAP_NO_SUCH_OBJECT ) {
1419 LDAP_LOG( OPERATION, ERR,
1420 "cookie will be non-persistent\n",
1423 Debug( LDAP_DEBUG_ANY,
1424 "cookie will be non-persistent\n",
1429 LDAP_LOG( OPERATION, ERR,
1430 "be_add failed (%d)\n",
1433 Debug( LDAP_DEBUG_ANY,
1434 "be_add failed (%d)\n",
1443 LDAP_LOG( OPERATION, ERR,
1444 "be_modify failed (%d)\n", rc, 0, 0 );
1446 Debug( LDAP_DEBUG_ANY,
1447 "be_modify failed (%d)\n", rc, 0, 0 );
1457 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1458 mlnext = ml->sml_next;
1467 int slap_mods_check_syncrepl(
1470 Modifications **mlp,
1477 Backend *be = op->o_bd;
1478 AttributeDescription** descs;
1480 Modifications *prevml = NULL;
1481 Modifications *nextml = NULL;
1482 Modifications *ml = *mlp;
1484 while ( ml != NULL ) {
1485 AttributeDescription *ad = NULL;
1487 /* convert to attribute description */
1488 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1490 if( rc != LDAP_SUCCESS ) {
1491 snprintf( textbuf, textlen, "%s: %s",
1492 ml->sml_type.bv_val, *text );
1499 if ( si->lastmod == LASTMOD_REQ ) {
1500 descs = del_descs_lastmod;
1505 for ( i = 0; descs[i] != NULL; i++ ) {
1506 if ( ad == descs[i] ) {
1507 if ( prevml == NULL ) {
1508 mlp = &ml->sml_next;
1511 prevml->sml_next = ml->sml_next;
1513 slap_mod_free( &ml->sml_mod, 0 );
1514 nextml = ml->sml_next;
1521 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1522 && !slap_ad_is_binary( ad )) {
1523 /* attribute requires binary transfer */
1524 snprintf( textbuf, textlen,
1525 "%s: requires ;binary transfer",
1526 ml->sml_type.bv_val );
1528 return LDAP_UNDEFINED_TYPE;
1531 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1532 && slap_ad_is_binary( ad )) {
1533 /* attribute requires binary transfer */
1534 snprintf( textbuf, textlen,
1535 "%s: disallows ;binary transfer",
1536 ml->sml_type.bv_val );
1538 return LDAP_UNDEFINED_TYPE;
1541 if( slap_ad_is_tag_range( ad )) {
1542 /* attribute requires binary transfer */
1543 snprintf( textbuf, textlen,
1544 "%s: inappropriate use of tag range option",
1545 ml->sml_type.bv_val );
1547 return LDAP_UNDEFINED_TYPE;
1550 if ( is_at_obsolete( ad->ad_type ) &&
1551 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1553 * attribute is obsolete,
1554 * only allow replace/delete with no values
1556 snprintf( textbuf, textlen,
1557 "%s: attribute is obsolete",
1558 ml->sml_type.bv_val );
1560 return LDAP_CONSTRAINT_VIOLATION;
1566 if( ml->sml_values != NULL ) {
1568 slap_syntax_validate_func *validate =
1569 ad->ad_type->sat_syntax->ssyn_validate;
1570 slap_syntax_transform_func *pretty =
1571 ad->ad_type->sat_syntax->ssyn_pretty;
1573 if( !pretty && !validate ) {
1574 *text = "no validator for syntax";
1575 snprintf( textbuf, textlen,
1576 "%s: no validator for syntax %s",
1577 ml->sml_type.bv_val,
1578 ad->ad_type->sat_syntax->ssyn_oid );
1580 return LDAP_INVALID_SYNTAX;
1584 * check that each value is valid per syntax
1585 * and pretty if appropriate
1587 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1588 struct berval pval = {0, NULL};
1590 rc = pretty( ad->ad_type->sat_syntax,
1591 &ml->sml_values[nvals], &pval, ctx );
1593 rc = validate( ad->ad_type->sat_syntax,
1594 &ml->sml_values[nvals] );
1598 snprintf( textbuf, textlen,
1599 "%s: value #%ld invalid per syntax",
1600 ml->sml_type.bv_val, (long) nvals );
1602 return LDAP_INVALID_SYNTAX;
1606 ber_memfree( ml->sml_values[nvals].bv_val );
1607 ml->sml_values[nvals] = pval;
1612 * a rough single value check... an additional check is needed
1613 * to catch add of single value to existing single valued attribute
1615 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1616 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1617 snprintf( textbuf, textlen,
1618 "%s: multiple values provided",
1619 ml->sml_type.bv_val );
1621 return LDAP_CONSTRAINT_VIOLATION;
1624 if( nvals && ad->ad_type->sat_equality &&
1625 ad->ad_type->sat_equality->smr_normalize ) {
1626 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1627 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1628 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1629 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1630 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1633 LDAP_LOG( OPERATION, DETAIL1,
1634 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1636 Debug( LDAP_DEBUG_ANY,
1637 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1639 snprintf( textbuf, textlen,
1640 "%s: value #%ld normalization failed",
1641 ml->sml_type.bv_val, (long) nvals );
1646 ml->sml_nvalues[nvals].bv_val = NULL;
1647 ml->sml_nvalues[nvals].bv_len = 0;
1654 return LDAP_SUCCESS;
1658 int slap_mods_opattrs_syncrepl(
1661 Modifications *mods,
1662 Modifications **modtail,
1664 char *textbuf, size_t textlen )
1666 struct berval name = {0, NULL};
1667 struct berval timestamp = {0, NULL};
1668 struct berval csn = {0, NULL};
1669 struct berval nname = {0, NULL};
1670 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1671 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1673 Backend *be = op->o_bd;
1675 int mop = LDAP_MOD_REPLACE;
1677 assert( modtail != NULL );
1678 assert( *modtail == NULL );
1680 if( si->lastmod == LASTMOD_GEN ) {
1682 time_t now = slap_get_time();
1684 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1685 ltm = gmtime( &now );
1686 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1688 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1689 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1690 csn.bv_val = csnbuf;
1692 timestamp.bv_val = timebuf;
1693 timestamp.bv_len = strlen(timebuf);
1695 if( op->o_dn.bv_len == 0 ) {
1696 name.bv_val = SLAPD_ANONYMOUS;
1697 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1705 if( op->o_tag == LDAP_REQ_ADD ) {
1706 struct berval tmpval = {0, NULL};
1708 if( global_schemacheck ) {
1709 int rc = mods_structural_class( mods, &tmpval,
1710 text, textbuf, textlen );
1711 if( rc != LDAP_SUCCESS ) {
1715 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1717 mod->sml_type.bv_val = NULL;
1718 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1719 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1720 ber_dupbv( &mod->sml_values[0], &tmpval );
1721 mod->sml_values[1].bv_len = 0;
1722 mod->sml_values[1].bv_val = NULL;
1723 assert( mod->sml_values[0].bv_val );
1724 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1725 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1726 mod->sml_nvalues[1].bv_len = 0;
1727 mod->sml_nvalues[1].bv_val = NULL;
1728 assert( mod->sml_nvalues[0].bv_val );
1730 modtail = &mod->sml_next;
1733 if( si->lastmod == LASTMOD_GEN ) {
1734 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1736 mod->sml_type.bv_val = NULL;
1737 mod->sml_desc = slap_schema.si_ad_creatorsName;
1738 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1739 ber_dupbv( &mod->sml_values[0], &name );
1740 mod->sml_values[1].bv_len = 0;
1741 mod->sml_values[1].bv_val = NULL;
1742 assert( mod->sml_values[0].bv_val );
1743 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1744 ber_dupbv( &mod->sml_nvalues[0], &nname );
1745 mod->sml_nvalues[1].bv_len = 0;
1746 mod->sml_nvalues[1].bv_val = NULL;
1747 assert( mod->sml_nvalues[0].bv_val );
1749 modtail = &mod->sml_next;
1751 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1753 mod->sml_type.bv_val = NULL;
1754 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1755 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1756 ber_dupbv( &mod->sml_values[0], ×tamp );
1757 mod->sml_values[1].bv_len = 0;
1758 mod->sml_values[1].bv_val = NULL;
1759 assert( mod->sml_values[0].bv_val );
1760 mod->sml_nvalues = NULL;
1762 modtail = &mod->sml_next;
1766 if( si->lastmod == LASTMOD_GEN ) {
1767 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1769 mod->sml_type.bv_val = NULL;
1770 mod->sml_desc = slap_schema.si_ad_entryCSN;
1771 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1772 ber_dupbv( &mod->sml_values[0], &csn );
1773 mod->sml_values[1].bv_len = 0;
1774 mod->sml_values[1].bv_val = NULL;
1775 assert( mod->sml_values[0].bv_val );
1776 mod->sml_nvalues = NULL;
1778 modtail = &mod->sml_next;
1780 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1782 mod->sml_type.bv_val = NULL;
1783 mod->sml_desc = slap_schema.si_ad_modifiersName;
1784 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1785 ber_dupbv( &mod->sml_values[0], &name );
1786 mod->sml_values[1].bv_len = 0;
1787 mod->sml_values[1].bv_val = NULL;
1788 assert( mod->sml_values[0].bv_val );
1789 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1790 ber_dupbv( &mod->sml_nvalues[0], &nname );
1791 mod->sml_nvalues[1].bv_len = 0;
1792 mod->sml_nvalues[1].bv_val = NULL;
1793 assert( mod->sml_nvalues[0].bv_val );
1795 modtail = &mod->sml_next;
1797 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1799 mod->sml_type.bv_val = NULL;
1800 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1801 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1802 ber_dupbv( &mod->sml_values[0], ×tamp );
1803 mod->sml_values[1].bv_len = 0;
1804 mod->sml_values[1].bv_val = NULL;
1805 assert( mod->sml_values[0].bv_val );
1806 mod->sml_nvalues = NULL;
1808 modtail = &mod->sml_next;
1812 return LDAP_SUCCESS;
1817 int slap_mods2entry_syncrepl(
1819 Modifications *mods,
1823 char *textbuf, size_t textlen )
1825 Attribute **tail = &(*e)->e_attrs;
1826 assert( *tail == NULL );
1830 for( ; mods != NULL; mods = mods->sml_next ) {
1833 assert( mods->sml_desc != NULL );
1835 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1837 if( attr != NULL ) {
1838 #define SLURPD_FRIENDLY
1839 #ifdef SLURPD_FRIENDLY
1843 snprintf( textbuf, textlen,
1844 "attribute '%s' provided more than once",
1845 mods->sml_desc->ad_cname.bv_val );
1846 return LDAP_TYPE_OR_VALUE_EXISTS;
1849 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1852 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1857 attr->a_vals = ch_realloc( attr->a_vals,
1858 sizeof( struct berval ) * (i+j) );
1860 /* should check for duplicates */
1862 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1863 sizeof( struct berval ) * j );
1865 if( attr->a_nvals ) {
1866 attr->a_nvals = ch_realloc( attr->a_nvals,
1867 sizeof( struct berval ) * (i+j) );
1869 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1870 sizeof( struct berval ) * j );
1872 /* trim the mods array */
1873 ch_free( mods->sml_nvalues );
1874 mods->sml_nvalues = NULL;
1879 snprintf( textbuf, textlen,
1880 "attribute '%s' provided more than once",
1881 mods->sml_desc->ad_cname.bv_val );
1882 return LDAP_TYPE_OR_VALUE_EXISTS;
1886 if( mods->sml_values[1].bv_val != NULL ) {
1887 /* check for duplicates */
1889 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1891 /* check if the values we're adding already exist */
1892 if( mr == NULL || !mr->smr_match ) {
1893 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1894 /* test asserted values against themselves */
1895 for( j = 0; j < i; j++ ) {
1896 if ( bvmatch( &mods->sml_bvalues[i],
1897 &mods->sml_bvalues[j] ) ) {
1898 /* value exists already */
1899 snprintf( textbuf, textlen,
1900 "%s: value #%d provided more than once",
1901 mods->sml_desc->ad_cname.bv_val, j );
1902 return LDAP_TYPE_OR_VALUE_EXISTS;
1909 const char *text = NULL;
1910 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1912 rc = modify_check_duplicates( mods->sml_desc, mr,
1913 NULL, mods->sml_bvalues, 0,
1914 &text, textbuf, sizeof( textbuf ) );
1916 if ( rc != LDAP_SUCCESS ) {
1922 attr = ch_calloc( 1, sizeof(Attribute) );
1924 /* move ad to attr structure */
1925 attr->a_desc = mods->sml_desc;
1927 /* move values to attr structure */
1928 /* should check for duplicates */
1929 attr->a_vals = mods->sml_values;
1931 attr->a_nvals = mods->sml_nvalues;
1934 tail = &attr->a_next;
1937 return LDAP_SUCCESS;
1941 avl_ber_bvfree( void *bv )
1946 if ( ((struct berval *)bv)->bv_val != NULL ) {
1947 ber_memfree ( ((struct berval *)bv)->bv_val );
1949 ber_memfree ( (char *) bv );
1958 syncinfo_t *si = op->o_callback->sc_private;
1961 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1963 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1966 si->syncCookie = NULL;
1968 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1970 return LDAP_SUCCESS;
1979 syncinfo_t *si = op->o_callback->sc_private;
1981 if ( rs->sr_type == REP_SEARCH ) {
1982 si->syncUUID_ndn = &rs->sr_entry->e_nname;
1985 return LDAP_SUCCESS;
1989 nonpresent_callback(
1994 syncinfo_t *si = op->o_callback->sc_private;
1997 struct berval* present_uuid = NULL;
1999 SlapReply rs_cb = {REP_RESULT};
2000 struct nonpresent_entry *np_entry;
2002 if ( rs->sr_type == REP_RESULT ) {
2003 count = avl_free( si->presentlist, avl_ber_bvfree );
2004 si->presentlist = NULL;
2005 return LDAP_SUCCESS;
2006 } else if ( rs->sr_type == REP_SEARCH ) {
2007 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
2012 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
2014 if ( present_uuid == NULL ) {
2015 np_entry = (struct nonpresent_entry *)
2016 ch_calloc( 1, sizeof( struct nonpresent_entry ));
2017 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
2018 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
2019 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
2021 avl_delete( &si->presentlist,
2022 &a->a_vals[0], syncuuid_cmp );
2024 return LDAP_SUCCESS;
2026 return LDAP_SUCCESS;
2037 syncinfo_t *si = op->o_callback->sc_private;
2039 if ( rs->sr_err != LDAP_SUCCESS &&
2040 rs->sr_err != LDAP_REFERRAL &&
2041 rs->sr_err != LDAP_ALREADY_EXISTS &&
2042 rs->sr_err != LDAP_NO_SUCH_OBJECT ) {
2044 LDAP_LOG( OPERATION, ERR,
2045 "null_callback : error code 0x%x\n",
2048 Debug( LDAP_DEBUG_ANY,
2049 "null_callback : error code 0x%x\n",
2053 return LDAP_SUCCESS;
2058 str2clist( char ***out, char *in, const char *brkstr )
2067 /* find last element in list */
2068 for (i = 0; *out && *out[i]; i++);
2070 /* protect the input string from strtok */
2071 str = ch_strdup( in );
2073 /* Count words in string */
2075 for ( s = str; *s; s++ ) {
2076 if ( strchr( brkstr, *s ) != NULL ) {
2081 *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2083 for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2085 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2087 *new = ch_strdup( s );