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"
39 syncrepl_message_to_entry ( LDAP *, Operation *, LDAPMessage *, Modifications *,
40 int*, struct berval *, struct berval * );
43 syncrepl_entry( LDAP *, Operation*, Entry*, Modifications*,
44 int, struct berval*, struct berval*, int );
47 syncrepl_del_nonpresent( LDAP *, Operation * );
50 syncrepl_add_glue( LDAP *, Operation*, Entry*, Modifications*, int,
51 struct berval*, struct berval* );
54 syncrepl_updateCookie( LDAP *, Operation *, struct berval *, struct berval * );
57 slap_mods_check_syncrepl( Operation *, Modifications **,
58 const char **, char *, size_t, void *ctx );
61 slap_mods_opattrs_syncrepl( Operation *, Modifications *, Modifications **,
62 const char **, char *, size_t );
65 slap_mods2entry_syncrepl( Modifications *, Entry **, int,
66 const char **, char *, size_t );
68 /* callback functions */
69 static int cookie_callback( struct slap_op *, struct slap_rep * );
70 static int nonpresent_callback( struct slap_op *, struct slap_rep * );
71 static int null_callback( struct slap_op *, struct slap_rep * );
73 static AttributeDescription **add_descs;
74 static AttributeDescription **add_descs_lastmod;
75 static AttributeDescription **del_descs;
76 static AttributeDescription **del_descs_lastmod;
81 add_descs = ch_malloc( 2 * sizeof( AttributeDescription * ));
82 add_descs[0] = slap_schema.si_ad_objectClass;
85 add_descs_lastmod = ch_malloc( 7 * sizeof( AttributeDescription * ));
86 add_descs_lastmod[0] = slap_schema.si_ad_objectClass;
87 add_descs_lastmod[1] = slap_schema.si_ad_creatorsName;
88 add_descs_lastmod[2] = slap_schema.si_ad_modifiersName;
89 add_descs_lastmod[3] = slap_schema.si_ad_createTimestamp;
90 add_descs_lastmod[4] = slap_schema.si_ad_modifyTimestamp;
91 add_descs_lastmod[5] = slap_schema.si_ad_entryCSN;
92 add_descs_lastmod[6] = NULL;
94 del_descs = ch_malloc( 9 * sizeof( AttributeDescription * ));
95 del_descs[0] = slap_schema.si_ad_structuralObjectClass;
96 del_descs[1] = slap_schema.si_ad_subschemaSubentry;
97 del_descs[2] = slap_schema.si_ad_hasSubordinates;
98 del_descs[3] = slap_schema.si_ad_creatorsName;
99 del_descs[4] = slap_schema.si_ad_modifiersName;
100 del_descs[5] = slap_schema.si_ad_createTimestamp;
101 del_descs[6] = slap_schema.si_ad_modifyTimestamp;
102 del_descs[7] = slap_schema.si_ad_entryCSN;
105 del_descs_lastmod = ch_malloc( 4 * sizeof( AttributeDescription * ));
106 del_descs_lastmod[0] = slap_schema.si_ad_structuralObjectClass;
107 del_descs_lastmod[1] = slap_schema.si_ad_subschemaSubentry;
108 del_descs_lastmod[2] = slap_schema.si_ad_hasSubordinates;
109 del_descs_lastmod[3] = NULL;
118 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
120 SlapReply rs = {REP_RESULT};
122 LDAPControl c[2], **sctrls = NULL, **rctrls = NULL, *rctrlp;
123 BerElement *sync_ber;
124 struct berval *sync_bvalp;
126 BerElement *ctrl_ber;
130 LDAPMessage *res = NULL, *msg;
134 int nresponses, nreferences, nextended, npartial;
135 int nresponses_psearch;
137 int cancel_msgid = -1;
139 struct berval *retdata = NULL;
141 int sync_info_arrived = 0;
145 struct berval syncUUID;
146 struct berval syncCookie;
151 int syncinfo_arrived = 0;
152 int cancel_response = 0;
155 AttributeDescription** descs;
166 struct berval base_bv = { 0, NULL };
167 struct berval pbase = { 0, NULL };
168 struct berval nbase = { 0, NULL };
169 struct berval sub_bv = { 0, NULL };
170 struct berval psubrdn = { 0, NULL };
171 struct berval nsubrdn = { 0, NULL };
172 struct berval psub = { 0, NULL };
173 struct berval nsub = { 0, NULL };
175 Modifications *modlist;
177 char *def_filter_str;
178 struct berval def_filter_bv = { 0, NULL };
181 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
183 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
189 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
190 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
194 /* Init connection to master */
196 if ( ldap_is_ldap_url( si->masteruri )) {
197 rc = ldap_initialize( &ld, si->masteruri );
198 if ( rc != LDAP_SUCCESS ) {
200 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
201 "ldap_initialize failed (%s)\n",
202 si->masteruri, 0, 0 );
204 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
205 "ldap_initialize failed (%s)\n",
206 si->masteruri, 0, 0 );
210 ld = ldap_init( si->mastername, si->masterport );
213 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
214 "ldap_init failed (%s:%s)\n",
215 si->mastername, si->masterport, 0 );
217 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
218 "ldap_init failed (%s:%s)\n",
219 si->mastername, si->masterport, 0 );
224 op.o_protocol = LDAP_VERSION3;
225 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
230 rc = ldap_start_tls_s( ld, NULL, NULL );
231 if( rc != LDAP_SUCCESS ) {
233 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
234 "%s: ldap_start_tls failed (%d)\n",
235 si->tls == TLS_CRITICAL ? "Error" : "Warning",
238 Debug( LDAP_DEBUG_ANY,
239 "%s: ldap_start_tls failed (%d)\n",
240 si->tls == TLS_CRITICAL ? "Error" : "Warning",
243 if( si->tls == TLS_CRITICAL )
248 if ( si->bindmethod == LDAP_AUTH_SASL ) {
249 #ifdef HAVE_CYRUS_SASL
252 if ( si->secprops != NULL ) {
253 int err = ldap_set_option( ld,
254 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
256 if( err != LDAP_OPT_SUCCESS ) {
258 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
259 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
260 si->mastername, si->secprops, 0 );
262 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
263 "(%s,SECPROPS,\"%s\") failed!\n",
264 si->mastername, si->secprops, NULL );
270 defaults = lutil_sasl_defaults( ld,
277 rc = ldap_sasl_interactive_bind_s( ld,
285 if ( rc != LDAP_SUCCESS ) {
287 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
288 "ldap_sasl_interactive_bind_s failed (%d)\n",
291 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
292 "ldap_sasl_interactive_bind_s failed (%d)\n",
297 #else /* HAVE_CYRUS_SASL */
298 fprintf( stderr, "not compiled with SASL support\n" );
302 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
303 if ( rc != LDAP_SUCCESS ) {
305 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
306 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
308 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
309 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
318 conn.c_send_ldap_result = slap_send_ldap_result;
319 conn.c_send_search_entry = slap_send_search_entry;
320 conn.c_send_search_reference = slap_send_search_reference;
322 /* get syncrepl cookie of shadow replica from subentry */
323 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
324 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
326 sprintf( substr, "cn=syncrepl%d", si->id );
327 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
328 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
330 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
331 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
333 op.o_tag = LDAP_REQ_SEARCH;
334 op.o_ndn = be->be_rootndn;
335 psub = be->be_nsuffix[0];
337 op.o_time = slap_get_time();
338 op.o_managedsait = 1;
339 op.o_threadctx = si->ctx;
341 /* set callback function */
342 cb.sc_response = cookie_callback;
345 /* search subentry to retrieve cookie */
346 si->syncCookie = NULL;
350 op.o_connid = op.o_conn->c_connid;
351 op.ors_scope = LDAP_SCOPE_BASE;
352 op.ors_deref = LDAP_DEREF_NEVER;
355 op.ors_attrsonly = 0;
357 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
358 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
361 be->be_search( &op, &rs );
363 /* setup LDAP SYNC control */
364 sync_ber = ber_alloc_t( LBER_USE_DER );
366 if ( si->syncCookie ) {
367 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
369 ber_printf( sync_ber, "{e}", abs(si->type) );
372 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
373 ber_free( sync_ber, 1 );
376 ber_free( sync_ber, 1 );
378 sctrls = (LDAPControl**) ch_calloc( 3, sizeof(LDAPControl*) );
380 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
381 c[0].ldctl_value = (*sync_bvalp);
382 c[0].ldctl_iscritical = si->type < 0;
386 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
387 c[1].ldctl_value.bv_val = si->authzId;
388 c[1].ldctl_value.bv_len = strlen( si->authzId );
389 c[1].ldctl_iscritical = 1;
397 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
399 ber_bvfree( sync_bvalp );
402 if ( err != LDAP_OPT_SUCCESS )
403 fprintf( stderr, "Could not set controls : %d\n", err );
405 /* Delete Attributes */
406 if ( si->lastmod == LASTMOD_REQ ) {
407 descs = del_descs_lastmod;
412 for ( i = 0; descs[i] != NULL; i++ ) {
413 for ( j = 0; si->attrs[j] != NULL; j++ ) {
414 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
415 ch_free( si->attrs[j] );
416 for ( k = j; si->attrs[k] != NULL; k++ ) {
417 si->attrs[k] = si->attrs[k+1];
425 for ( n = 0; si->attrs[ n ] != NULL; n++ )
428 if ( si->lastmod == LASTMOD_REQ ) {
429 descs = add_descs_lastmod;
434 for ( i = 0; descs[i] != NULL; i++ ) {
435 tmp = ( char ** ) ch_realloc( si->attrs,
436 ( n + 2 ) * sizeof( char * ));
439 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
441 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
445 si->attrs[ n++ ] = ( char * ) strndup( descs[i]->ad_cname.bv_val,
446 descs[i]->ad_cname.bv_len );
447 si->attrs[ n ] = NULL;
450 /* Send LDAP SYNC search */
452 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
453 si->attrs, si->attrsonly, NULL, NULL,
456 if( rc != LDAP_SUCCESS ) {
457 fprintf( stderr, "syncrepl: ldap_search_ext (%d)\n",
458 ldap_err2string( rc ), rc );
462 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
464 for ( msg = ldap_first_message( ld, res );
466 msg = ldap_next_message( ld, msg ) )
468 switch( ldap_msgtype( msg ) ) {
469 case LDAP_RES_SEARCH_ENTRY:
470 entry = syncrepl_message_to_entry( ld, &op, msg, modlist, &syncstate, &syncUUID, &syncCookie );
471 syncrepl_entry( ld, &op, entry, modlist,
472 syncstate, &syncUUID,
475 if ( syncCookie.bv_len ) {
476 syncrepl_updateCookie( ld, &op, &psub,
481 case LDAP_RES_SEARCH_REFERENCE:
483 LDAP_LOG( OPERATION, ERR,
484 "do_syncrepl : reference received\n", 0, 0, 0 );
486 Debug( LDAP_DEBUG_ANY,
487 "do_syncrepl : reference received\n", 0, 0, 0 );
491 case LDAP_RES_SEARCH_RESULT:
492 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
495 ctrl_ber = ber_init( &rctrlp->ldctl_value );
496 ber_scanf( ctrl_ber, "{" );
497 if ( ber_peek_tag( ctrl_ber, &len )
498 == LDAP_SYNC_TAG_COOKIE ) {
499 ber_scanf( ctrl_ber, "m", &syncCookie );
502 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
503 if ( cancel_response ) {
504 if ( syncCookie.bv_len ) {
505 ber_bvfree( si->syncCookie );
506 si->syncCookie = ber_dupbv( NULL, &syncCookie );
513 if ( syncCookie.bv_len ) {
514 syncrepl_updateCookie( ld,
523 case LDAP_RES_INTERMEDIATE_RESP:
524 ldap_parse_intermediate_resp_result( ld, msg,
525 &retoid, &retdata, 0 );
526 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
527 sync_info_arrived = 1;
528 res_ber = ber_init( retdata );
529 ber_scanf( res_ber, "{e", &syncstate );
531 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
532 syncrepl_del_nonpresent( ld, &op );
533 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
535 LDAP_LOG( OPERATION, ERR,
536 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
538 Debug( LDAP_DEBUG_ANY,
539 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
543 if ( ber_peek_tag( res_ber, &len )
544 == LDAP_SYNC_TAG_COOKIE ) {
545 ber_scanf( res_ber, "m}", &syncCookie );
546 if ( syncCookie.bv_len ) {
547 ber_bvfree( si->syncCookie );
548 si->syncCookie = ber_dupbv( NULL, &syncCookie );
551 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
553 LDAP_LOG( OPERATION, ERR,
554 "do_syncrepl : cookie required\n", 0, 0, 0 );
556 Debug( LDAP_DEBUG_ANY,
557 "do_syncrepl : cookie required\n", 0, 0, 0 );
562 ldap_memfree( retoid );
563 ber_bvfree( retdata );
564 ber_free( res_ber, 1 );
568 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
569 " unknown intermediate "
570 "response\n", 0, 0, 0 );
572 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
573 "unknown intermediate "
574 "response\n", 0, 0, 0 );
576 ldap_memfree( retoid );
577 ber_bvfree( retdata );
583 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
584 "unknown message\n", 0, 0, 0 );
586 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
587 "unknown message\n", 0, 0, 0 );
598 LDAP_LOG( OPERATION, ERR,
599 "do_syncrepl : unknown result\n", 0, 0, 0 );
601 Debug( LDAP_DEBUG_ANY,
602 "do_syncrepl : unknown result\n", 0, 0, 0 );
608 sleep(si->interval * 60);
610 // set alarm clock to send signal to slapd
611 // should set the signal handler beforehand
612 // the signal handler re execute do_syncrepl()
622 syncrepl_message_to_entry(
626 Modifications *modlist,
628 struct berval *syncUUID,
629 struct berval *syncCookie
633 BerElement *ber = NULL;
638 Modifications **modtail = &modlist;
639 Backend *be = op->o_bd;
642 char txtbuf[SLAP_TEXT_BUFLEN];
643 size_t textlen = sizeof txtbuf;
645 struct berval **bvals;
649 struct berval empty_bv = { 0, NULL };
653 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
657 LDAPControl** rctrls = NULL;
658 BerElement* ctrl_ber;
662 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
664 LDAP_LOG( OPERATION, ERR,
665 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
667 Debug( LDAP_DEBUG_ANY,
668 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
673 op->o_tag = LDAP_REQ_ADD;
675 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
677 if ( rc != LDAP_SUCCESS ) {
679 LDAP_LOG( OPERATION, ERR,
680 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
682 Debug( LDAP_DEBUG_ANY,
683 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
688 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
689 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, NULL );
693 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
695 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
697 if ( tmp.sml_type.bv_val == NULL ) break;
699 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
701 mod->sml_op = LDAP_MOD_ADD;
702 mod->sml_next = NULL;
703 mod->sml_desc = NULL;
704 mod->sml_type = tmp.sml_type;
705 mod->sml_bvalues = tmp.sml_bvalues;
706 mod->sml_nvalues = tmp.sml_bvalues;
709 modtail = &mod->sml_next;
712 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
714 LDAP_LOG( OPERATION, ERR,
715 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
717 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
724 tmpber = ldap_get_message_ber( msg );
725 ber = ber_dup( tmpber );
727 ber_scanf( ber, "{xx" );
729 rc = ldap_int_get_controls( ber, &rctrls );
731 if ( rc != LDAP_SUCCESS ) {
733 LDAP_LOG( OPERATION, ERR,
734 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
736 Debug( LDAP_DEBUG_ANY,
737 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
744 ctrl_ber = ber_init( &rctrlp->ldctl_value );
745 ber_scanf( ctrl_ber, "{em", syncstate, syncUUID );
746 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
747 ber_scanf( ctrl_ber, "m}", syncCookie );
751 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
752 " rctrls absent\n", 0, 0, 0 );
754 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
755 " rctrls absent\n", 0, 0, 0 );
759 if ( *syncstate == LDAP_SYNC_PRESENT ) {
764 if ( modlist == NULL ) {
766 LDAP_LOG( OPERATION, ERR,
767 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
769 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
774 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
776 if ( rc != LDAP_SUCCESS ) {
778 LDAP_LOG( OPERATION, ERR,
779 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
781 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
787 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail,
788 &text,txtbuf, textlen );
790 if( rc != LDAP_SUCCESS ) {
792 LDAP_LOG( OPERATION, ERR,
793 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
795 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
801 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
802 if( rc != LDAP_SUCCESS ) {
804 LDAP_LOG( OPERATION, ERR,
805 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
807 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
820 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
822 const struct berval *uuid1 = v_uuid1;
823 const struct berval *uuid2 = v_uuid2;
824 int rc = uuid1->bv_len - uuid2->bv_len;
826 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
834 Modifications* modlist,
836 struct berval* syncUUID,
837 struct berval* syncCookie,
841 Backend *be = op->o_bd;
842 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
844 struct berval normdn = {0, NULL};
845 struct berval prettydn = {0, NULL};
846 struct berval csn_bv = {0, NULL};
847 struct berval *syncuuid_bv = NULL;
848 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
850 SlapReply rs = {REP_RESULT};
854 struct berval filterstr_bv;
859 if ( syncstate == LDAP_SYNC_PRESENT ) {
860 syncuuid_bv = ber_dupbv( NULL, syncUUID );
861 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
862 syncuuid_cmp, avl_dup_error );
866 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
867 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
870 filterstr = (char *) ch_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1 );
871 strcpy( filterstr, "entryUUID=" );
872 strcat( filterstr, syncUUID->bv_val );
875 si->syncUUID = syncUUID;
877 filter = str2filter( filterstr );
878 ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
879 ch_free( filterstr );
881 dnPrettyNormal( 0, &(e->e_name), &prettydn, &normdn, NULL );
883 free(e->e_name.bv_val);
884 free(e->e_nname.bv_val);
885 e->e_name = prettydn;
887 op->o_req_dn = e->e_name;
888 op->o_req_ndn = e->e_nname;
890 op->o_callback = &cb;
891 cb.sc_response = null_callback;
894 switch ( syncstate ) {
896 case LDAP_SYNC_MODIFY :
898 op->o_tag = LDAP_REQ_MODIFY;
899 op->orm_modlist = modlist;
900 rc = be->be_modify( op, &rs );
901 if ( rc != LDAP_SUCCESS ) {
902 if ( rc == LDAP_REFERRAL ||
903 rc == LDAP_NO_SUCH_OBJECT ||
904 rc == DB_NOTFOUND ) {
905 op->o_tag = LDAP_REQ_ADD;
907 rc = be->be_add( op, &rs );
908 if ( rc != LDAP_SUCCESS ) {
909 if ( rc == LDAP_ALREADY_EXISTS ) {
911 } else if ( rc == LDAP_REFERRAL ||
912 rc == LDAP_NO_SUCH_OBJECT ||
913 rc == DB_NOTFOUND ) {
914 syncrepl_add_glue(ld, op, e,
916 syncUUID, syncCookie);
919 LDAP_LOG( OPERATION, ERR,
920 "be_add failed (%d)\n",
923 Debug( LDAP_DEBUG_ANY,
924 "be_add failed (%d)\n",
931 LDAP_LOG( OPERATION, ERR,
932 "be_modify failed (%d)\n", rc, 0, 0 );
934 Debug( LDAP_DEBUG_ANY,
935 "be_modify failed (%d)\n", rc, 0, 0 );
942 case LDAP_SYNC_DELETE :
943 op->o_tag = LDAP_REQ_DELETE;
944 be->be_delete( op, &rs );
950 LDAP_LOG( OPERATION, ERR,
951 "unknown syncstate\n", 0, 0, 0 );
953 Debug( LDAP_DEBUG_ANY,
954 "unknown syncstate\n", 0, 0, 0 );
961 syncrepl_del_nonpresent(
966 Backend* be = op->o_bd;
967 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
969 struct berval base_bv = {0, NULL};
971 SlapReply rs = {REP_RESULT};
972 struct berval filterstr_bv;
974 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
975 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, NULL );
977 filter = str2filter( si->filterstr );
979 cb.sc_response = nonpresent_callback;
982 op->o_callback = &cb;
983 op->o_tag = LDAP_REQ_SEARCH;
984 op->ors_scope = LDAP_SCOPE_BASE;
985 op->ors_deref = LDAP_DEREF_NEVER;
988 op->ors_attrsonly = 0;
989 op->ors_attrs = NULL;
990 op->ors_filter = str2filter( si->filterstr );
991 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
993 be->be_search( op, &rs );
1002 Modifications* modlist,
1004 struct berval* syncUUID,
1005 struct berval* syncCookie
1008 Backend *be = op->o_bd;
1009 syncinfo_t *si = op->o_callback->sc_private;
1010 struct berval uuid_bv = {0, NULL};
1014 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1017 struct berval dn = {0, NULL};
1018 struct berval pdn = {0, NULL};
1019 struct berval ndn = {0, NULL};
1020 struct berval rdn = {0, NULL};
1022 SlapReply rs = {REP_RESULT};
1023 Connection *conn = op->o_conn;
1025 op->o_tag = LDAP_REQ_ADD;
1026 op->o_callback = &cb;
1027 cb.sc_response = null_callback;
1030 ber_dupbv( &dn, &e->e_nname );
1031 ber_dupbv( &pdn, &e->e_nname );
1032 while ( !be_issuffix ( be, &pdn )) {
1033 dnParent( &dn, &pdn );
1034 ber_dupbv( &dn, &pdn );
1038 for ( i = 0; i <= levels; i++ ) {
1039 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1040 ber_dupbv( &dn, &e->e_nname );
1041 ber_dupbv( &pdn, &e->e_nname );
1043 for ( k = 0; k < j; k++ ) {
1044 dnParent( &dn, &pdn );
1045 ber_dupbv( &dn, &pdn );
1048 dnPrettyNormal( 0, &dn, &pdn, &ndn, NULL );
1049 ber_dupbv( &glue->e_name, &pdn );
1050 ber_dupbv( &glue->e_nname, &ndn );
1052 a = ch_calloc( 1, sizeof( Attribute ));
1053 a->a_desc = slap_schema.si_ad_objectClass;
1054 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1055 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1056 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1057 a->a_vals[2].bv_len = 0;
1058 a->a_vals[2].bv_val = NULL;
1059 a->a_next = glue->e_attrs;
1062 a = ch_calloc( 1, sizeof( Attribute ));
1063 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1064 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1065 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1066 a->a_vals[1].bv_len = 0;
1067 a->a_vals[1].bv_val = NULL;
1068 a->a_next = glue->e_attrs;
1071 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1072 op->o_req_dn = e->e_name;
1073 op->o_req_ndn = e->e_nname;
1075 rc = be->be_add ( op, &rs );
1076 if ( rc == LDAP_SUCCESS )
1077 be_entry_release_w( op, e );
1082 op->o_req_dn = glue->e_name;
1083 op->o_req_ndn = glue->e_nname;
1085 rc = be->be_add ( op, &rs );
1086 if ( rc == LDAP_SUCCESS )
1087 be_entry_release_w( op, glue );
1097 syncrepl_updateCookie(
1101 struct berval *syncCookie
1104 Backend *be = op->o_bd;
1105 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1107 Modifications *modlist;
1108 Modifications **modtail = &modlist;
1110 struct berval* ocbva;
1111 struct berval* cnbva;
1112 struct berval* ssbva;
1113 struct berval* scbva;
1118 char txtbuf[SLAP_TEXT_BUFLEN];
1119 size_t textlen = sizeof txtbuf;
1124 struct berval sub_bv = { 0, NULL };
1125 struct berval psubrdn = { 0, NULL };
1128 SlapReply rs = {REP_RESULT};
1130 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1131 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1132 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1133 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1135 /* update in memory cookie */
1136 if ( si->syncCookie != NULL ) {
1137 ber_bvfree( si->syncCookie );
1139 si->syncCookie = ber_dupbv( NULL, syncCookie );
1140 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1141 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1142 ber_str2bv( "syncConsumerSubentry",
1143 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1144 ocbva[3].bv_len = 0;
1145 ocbva[3].bv_val = NULL;
1147 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1148 mod->sml_op = LDAP_MOD_REPLACE;
1149 mod->sml_next = NULL;
1150 mod->sml_desc = NULL;
1151 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1152 mod->sml_bvalues = ocbva;
1153 mod->sml_nvalues = ocbva;
1155 modtail = &mod->sml_next;
1157 sprintf( substr, "syncrepl%d", si->id );
1158 sprintf( rdnstr, "cn=%s", substr );
1159 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1160 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1162 cnbva[1].bv_len = 0;
1163 cnbva[1].bv_val = NULL;
1164 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1165 mod->sml_op = LDAP_MOD_REPLACE;
1166 mod->sml_next = NULL;
1167 mod->sml_desc = NULL;
1168 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1169 mod->sml_bvalues = cnbva;
1170 mod->sml_nvalues = cnbva;
1172 modtail = &mod->sml_next;
1174 ber_dupbv( &scbva[0], si->syncCookie );
1175 scbva[1].bv_len = 0;
1176 scbva[1].bv_val = NULL;
1177 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1178 mod->sml_op = LDAP_MOD_REPLACE;
1179 mod->sml_next = NULL;
1180 mod->sml_desc = NULL;
1181 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1182 1, &mod->sml_type );
1183 mod->sml_bvalues = scbva;
1184 mod->sml_nvalues = scbva;
1186 modtail = &mod->sml_next;
1188 ber_str2bv( " ", 1, 1, &ssbva[0] );
1189 ssbva[1].bv_len = 0;
1190 ssbva[1].bv_val = NULL;
1191 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1192 mod->sml_op = LDAP_MOD_REPLACE;
1193 mod->sml_next = NULL;
1194 mod->sml_desc = NULL;
1195 ber_str2bv( "subtreeSpecification",
1196 strlen("subtreeSpecification"), 1, &mod->sml_type );
1197 mod->sml_bvalues = ssbva;
1198 mod->sml_nvalues = ssbva;
1200 modtail = &mod->sml_next;
1202 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1204 if ( rc != LDAP_SUCCESS ) {
1206 LDAP_LOG( OPERATION, ERR,
1207 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1209 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1214 op->o_tag = LDAP_REQ_ADD;
1215 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1217 if( rc != LDAP_SUCCESS ) {
1219 LDAP_LOG( OPERATION, ERR,
1220 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1222 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1227 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1229 build_new_dn( &sub_bv, pdn, &psubrdn );
1230 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, NULL );
1234 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1236 if( rc != LDAP_SUCCESS ) {
1238 LDAP_LOG( OPERATION, ERR,
1239 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1241 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1246 cb.sc_response = null_callback;
1249 op->o_callback = &cb;
1250 op->o_req_dn = e->e_name;
1251 op->o_req_ndn = e->e_nname;
1254 for( mod = modlist; mod != NULL; mod = mod->sml_next ) {
1255 mod->sml_op = LDAP_MOD_REPLACE;
1259 /* update persistent cookie */
1260 update_cookie_retry:
1261 op->o_tag = LDAP_REQ_MODIFY;
1262 op->orm_modlist = modlist;
1263 rc = be->be_modify( op, &rs );
1264 if ( rc != LDAP_SUCCESS ) {
1265 if ( rc == LDAP_REFERRAL ||
1266 rc == LDAP_NO_SUCH_OBJECT ||
1267 rc == DB_NOTFOUND ) {
1268 op->o_tag = LDAP_REQ_ADD;
1270 rc = be->be_add( op, &rs );
1271 if ( rc != LDAP_SUCCESS ) {
1272 if ( rc == LDAP_ALREADY_EXISTS ) {
1273 goto update_cookie_retry;
1274 } else if ( rc == LDAP_REFERRAL ||
1275 rc == LDAP_NO_SUCH_OBJECT ||
1276 rc == DB_NOTFOUND ) {
1278 LDAP_LOG( OPERATION, ERR,
1279 "cookie will be non-persistent\n",
1282 Debug( LDAP_DEBUG_ANY,
1283 "cookie will be non-persistent\n",
1288 LDAP_LOG( OPERATION, ERR,
1289 "be_add failed (%d)\n",
1292 Debug( LDAP_DEBUG_ANY,
1293 "be_add failed (%d)\n",
1300 LDAP_LOG( OPERATION, ERR,
1301 "be_modify failed (%d)\n", rc, 0, 0 );
1303 Debug( LDAP_DEBUG_ANY,
1304 "be_modify failed (%d)\n", rc, 0, 0 );
1312 int slap_mods_check_syncrepl(
1314 Modifications **mlp,
1321 Backend *be = op->o_bd;
1322 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1323 AttributeDescription** descs;
1325 Modifications *prevml = NULL;
1326 Modifications *nextml = NULL;
1327 Modifications *ml = *mlp;
1329 while ( ml != NULL ) {
1330 AttributeDescription *ad = NULL;
1332 /* convert to attribute description */
1333 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1335 if( rc != LDAP_SUCCESS ) {
1336 snprintf( textbuf, textlen, "%s: %s",
1337 ml->sml_type.bv_val, *text );
1344 if ( si->lastmod == LASTMOD_REQ ) {
1345 descs = del_descs_lastmod;
1350 for ( i = 0; descs[i] != NULL; i++ ) {
1351 if ( ad == descs[i] ) {
1352 if ( prevml == NULL ) {
1353 mlp = &ml->sml_next;
1356 prevml->sml_next = ml->sml_next;
1358 slap_mod_free( &ml->sml_mod, 0 );
1359 nextml = ml->sml_next;
1366 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1367 && !slap_ad_is_binary( ad ))
1369 /* attribute requires binary transfer */
1370 snprintf( textbuf, textlen,
1371 "%s: requires ;binary transfer",
1372 ml->sml_type.bv_val );
1374 return LDAP_UNDEFINED_TYPE;
1377 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1378 && slap_ad_is_binary( ad ))
1380 /* attribute requires binary transfer */
1381 snprintf( textbuf, textlen,
1382 "%s: disallows ;binary transfer",
1383 ml->sml_type.bv_val );
1385 return LDAP_UNDEFINED_TYPE;
1388 if( slap_ad_is_tag_range( ad )) {
1389 /* attribute requires binary transfer */
1390 snprintf( textbuf, textlen,
1391 "%s: inappropriate use of tag range option",
1392 ml->sml_type.bv_val );
1394 return LDAP_UNDEFINED_TYPE;
1397 if ( is_at_obsolete( ad->ad_type ) &&
1398 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
1401 * attribute is obsolete,
1402 * only allow replace/delete with no values
1404 snprintf( textbuf, textlen,
1405 "%s: attribute is obsolete",
1406 ml->sml_type.bv_val );
1408 return LDAP_CONSTRAINT_VIOLATION;
1414 if( ml->sml_values != NULL ) {
1416 slap_syntax_validate_func *validate =
1417 ad->ad_type->sat_syntax->ssyn_validate;
1418 slap_syntax_transform_func *pretty =
1419 ad->ad_type->sat_syntax->ssyn_pretty;
1421 if( !pretty && !validate ) {
1422 *text = "no validator for syntax";
1423 snprintf( textbuf, textlen,
1424 "%s: no validator for syntax %s",
1425 ml->sml_type.bv_val,
1426 ad->ad_type->sat_syntax->ssyn_oid );
1428 return LDAP_INVALID_SYNTAX;
1432 * check that each value is valid per syntax
1433 * and pretty if appropriate
1435 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1438 rc = pretty( ad->ad_type->sat_syntax,
1439 &ml->sml_values[nvals], &pval,
1442 rc = validate( ad->ad_type->sat_syntax,
1443 &ml->sml_values[nvals] );
1447 snprintf( textbuf, textlen,
1448 "%s: value #%ld invalid per syntax",
1449 ml->sml_type.bv_val, (long) nvals );
1451 return LDAP_INVALID_SYNTAX;
1455 ber_memfree( ml->sml_values[nvals].bv_val );
1456 ml->sml_values[nvals] = pval;
1461 * a rough single value check... an additional check is needed
1462 * to catch add of single value to existing single valued attribute
1464 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1465 && nvals > 1 && is_at_single_value( ad->ad_type ))
1467 snprintf( textbuf, textlen,
1468 "%s: multiple values provided",
1469 ml->sml_type.bv_val );
1471 return LDAP_CONSTRAINT_VIOLATION;
1474 if( nvals && ad->ad_type->sat_equality &&
1475 ad->ad_type->sat_equality->smr_normalize )
1477 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1478 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1479 rc = ad->ad_type->sat_equality->smr_normalize(
1481 ad->ad_type->sat_syntax,
1482 ad->ad_type->sat_equality,
1483 &ml->sml_values[nvals], &ml->sml_nvalues[nvals],
1487 LDAP_LOG( OPERATION, DETAIL1,
1488 "str2entry: NULL (ssyn_normalize %d)\n",
1491 Debug( LDAP_DEBUG_ANY,
1492 "<= str2entry NULL (ssyn_normalize %d)\n",
1495 snprintf( textbuf, textlen,
1496 "%s: value #%ld normalization failed",
1497 ml->sml_type.bv_val, (long) nvals );
1502 ml->sml_nvalues[nvals].bv_val = NULL;
1503 ml->sml_nvalues[nvals].bv_len = 0;
1510 return LDAP_SUCCESS;
1514 int slap_mods_opattrs_syncrepl(
1516 Modifications *mods,
1517 Modifications **modtail,
1519 char *textbuf, size_t textlen )
1521 struct berval name, timestamp, csn;
1522 struct berval nname;
1523 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1524 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1526 Backend *be = op->o_bd;
1527 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1529 // int mop = op->o_tag == LDAP_REQ_ADD ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
1530 int mop = LDAP_MOD_REPLACE;
1532 assert( modtail != NULL );
1533 assert( *modtail == NULL );
1535 if( si->lastmod == LASTMOD_GEN ) {
1537 time_t now = slap_get_time();
1539 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1540 ltm = gmtime( &now );
1541 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1543 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1544 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1545 csn.bv_val = csnbuf;
1547 timestamp.bv_val = timebuf;
1548 timestamp.bv_len = strlen(timebuf);
1550 if( op->o_dn.bv_len == 0 ) {
1551 name.bv_val = SLAPD_ANONYMOUS;
1552 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1560 if( op->o_tag == LDAP_REQ_ADD ) {
1561 struct berval tmpval;
1563 if( global_schemacheck ) {
1564 int rc = mods_structural_class( mods, &tmpval,
1565 text, textbuf, textlen );
1566 if( rc != LDAP_SUCCESS ) {
1570 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1572 mod->sml_type.bv_val = NULL;
1573 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1575 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1576 ber_dupbv( &mod->sml_values[0], &tmpval );
1577 mod->sml_values[1].bv_len = 0;
1578 mod->sml_values[1].bv_val = NULL;
1579 assert( mod->sml_values[0].bv_val );
1581 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1582 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1583 mod->sml_nvalues[1].bv_len = 0;
1584 mod->sml_nvalues[1].bv_val = NULL;
1585 assert( mod->sml_nvalues[0].bv_val );
1587 modtail = &mod->sml_next;
1590 if( si->lastmod == LASTMOD_GEN ) {
1591 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1593 mod->sml_type.bv_val = NULL;
1594 mod->sml_desc = slap_schema.si_ad_creatorsName;
1595 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1596 ber_dupbv( &mod->sml_values[0], &name );
1597 mod->sml_values[1].bv_len = 0;
1598 mod->sml_values[1].bv_val = NULL;
1599 assert( mod->sml_values[0].bv_val );
1601 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1602 ber_dupbv( &mod->sml_nvalues[0], &nname );
1603 mod->sml_nvalues[1].bv_len = 0;
1604 mod->sml_nvalues[1].bv_val = NULL;
1605 assert( mod->sml_nvalues[0].bv_val );
1607 modtail = &mod->sml_next;
1609 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1611 mod->sml_type.bv_val = NULL;
1612 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1613 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1614 ber_dupbv( &mod->sml_values[0], ×tamp );
1615 mod->sml_values[1].bv_len = 0;
1616 mod->sml_values[1].bv_val = NULL;
1617 assert( mod->sml_values[0].bv_val );
1618 mod->sml_nvalues = NULL;
1620 modtail = &mod->sml_next;
1624 if( si->lastmod == LASTMOD_GEN ) {
1625 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1627 mod->sml_type.bv_val = NULL;
1628 mod->sml_desc = slap_schema.si_ad_entryCSN;
1629 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1630 ber_dupbv( &mod->sml_values[0], &csn );
1631 mod->sml_values[1].bv_len = 0;
1632 mod->sml_values[1].bv_val = NULL;
1633 assert( mod->sml_values[0].bv_val );
1634 mod->sml_nvalues = NULL;
1636 modtail = &mod->sml_next;
1638 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1640 mod->sml_type.bv_val = NULL;
1641 mod->sml_desc = slap_schema.si_ad_modifiersName;
1642 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1643 ber_dupbv( &mod->sml_values[0], &name );
1644 mod->sml_values[1].bv_len = 0;
1645 mod->sml_values[1].bv_val = NULL;
1646 assert( mod->sml_values[0].bv_val );
1648 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1649 ber_dupbv( &mod->sml_nvalues[0], &nname );
1650 mod->sml_nvalues[1].bv_len = 0;
1651 mod->sml_nvalues[1].bv_val = NULL;
1652 assert( mod->sml_nvalues[0].bv_val );
1654 modtail = &mod->sml_next;
1656 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1658 mod->sml_type.bv_val = NULL;
1659 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1660 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1661 ber_dupbv( &mod->sml_values[0], ×tamp );
1662 mod->sml_values[1].bv_len = 0;
1663 mod->sml_values[1].bv_val = NULL;
1664 assert( mod->sml_values[0].bv_val );
1665 mod->sml_nvalues = NULL;
1667 modtail = &mod->sml_next;
1671 return LDAP_SUCCESS;
1676 int slap_mods2entry_syncrepl(
1677 Modifications *mods,
1681 char *textbuf, size_t textlen )
1683 Attribute **tail = &(*e)->e_attrs;
1684 assert( *tail == NULL );
1688 for( ; mods != NULL; mods = mods->sml_next ) {
1691 assert( mods->sml_desc != NULL );
1693 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1695 if( attr != NULL ) {
1696 #define SLURPD_FRIENDLY
1697 #ifdef SLURPD_FRIENDLY
1701 snprintf( textbuf, textlen,
1702 "attribute '%s' provided more than once",
1703 mods->sml_desc->ad_cname.bv_val );
1704 return LDAP_TYPE_OR_VALUE_EXISTS;
1707 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1710 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1715 attr->a_vals = ch_realloc( attr->a_vals,
1716 sizeof( struct berval ) * (i+j) );
1718 /* should check for duplicates */
1720 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1721 sizeof( struct berval ) * j );
1723 if( attr->a_nvals ) {
1724 attr->a_nvals = ch_realloc( attr->a_nvals,
1725 sizeof( struct berval ) * (i+j) );
1727 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1728 sizeof( struct berval ) * j );
1730 /* trim the mods array */
1731 ch_free( mods->sml_nvalues );
1732 mods->sml_nvalues = NULL;
1737 snprintf( textbuf, textlen,
1738 "attribute '%s' provided more than once",
1739 mods->sml_desc->ad_cname.bv_val );
1740 return LDAP_TYPE_OR_VALUE_EXISTS;
1744 if( mods->sml_values[1].bv_val != NULL ) {
1745 /* check for duplicates */
1747 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1749 /* check if the values we're adding already exist */
1750 if( mr == NULL || !mr->smr_match ) {
1751 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1752 /* test asserted values against themselves */
1753 for( j = 0; j < i; j++ ) {
1754 if ( bvmatch( &mods->sml_bvalues[i],
1755 &mods->sml_bvalues[j] ) ) {
1756 /* value exists already */
1757 snprintf( textbuf, textlen,
1758 "%s: value #%d provided more than once",
1759 mods->sml_desc->ad_cname.bv_val, j );
1760 return LDAP_TYPE_OR_VALUE_EXISTS;
1767 const char *text = NULL;
1768 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1770 rc = modify_check_duplicates( mods->sml_desc, mr,
1771 NULL, mods->sml_bvalues, 0,
1772 &text, textbuf, sizeof( textbuf ) );
1774 if ( rc != LDAP_SUCCESS ) {
1780 attr = ch_calloc( 1, sizeof(Attribute) );
1782 /* move ad to attr structure */
1783 attr->a_desc = mods->sml_desc;
1785 /* move values to attr structure */
1786 /* should check for duplicates */
1787 attr->a_vals = mods->sml_values;
1789 attr->a_nvals = mods->sml_nvalues;
1792 tail = &attr->a_next;
1795 return LDAP_SUCCESS;
1799 avl_ber_bvfree( void *bv )
1804 if ( ((struct berval *)bv)->bv_val != NULL ) {
1805 ber_memfree ( ((struct berval *)bv)->bv_val );
1807 ber_memfree ( (char *) bv );
1816 syncinfo_t *si = op->o_callback->sc_private;
1819 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1821 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1824 si->syncCookie = NULL;
1826 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1828 return LDAP_SUCCESS;
1832 nonpresent_callback(
1837 syncinfo_t *si = op->o_callback->sc_private;
1840 struct berval* present_uuid = NULL;
1842 SlapReply rs_cb = {REP_RESULT};
1844 if ( rs->sr_type == REP_RESULT ) {
1845 count = avl_free( si->presentlist, avl_ber_bvfree );
1846 return LDAP_SUCCESS;
1847 } else if ( rs->sr_type == REP_SEARCH ) {
1848 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1853 present_uuid = avl_find( si->presentlist,
1854 &a->a_vals[0], syncuuid_cmp );
1856 if ( present_uuid == NULL ) {
1857 op->o_tag = LDAP_REQ_DELETE;
1858 op->o_callback = &cb;
1859 cb.sc_response = null_callback;
1861 op->o_req_dn = rs->sr_entry->e_name;
1862 op->o_req_ndn = rs->sr_entry->e_nname;
1863 op->o_bd->be_delete( op, &rs_cb );
1865 avl_delete( &si->presentlist,
1866 &a->a_vals[0], syncuuid_cmp );
1868 return LDAP_SUCCESS;
1870 return LDAP_SUCCESS;
1881 if ( rs->sr_err != LDAP_SUCCESS &&
1882 rs->sr_err != LDAP_REFERRAL &&
1883 rs->sr_err != LDAP_ALREADY_EXISTS &&
1884 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1885 rs->sr_err != DB_NOTFOUND ) {
1887 LDAP_LOG( OPERATION, ERR,
1888 "null_callback : error code 0x%x\n",
1891 Debug( LDAP_DEBUG_ANY,
1892 "null_callback : error code 0x%x\n",
1896 return LDAP_SUCCESS;