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 *,
40 Modifications **, 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};
123 LDAPControl **sctrls = NULL;
124 LDAPControl **rctrls = NULL;
125 LDAPControl *rctrlp = NULL;
126 BerElement *sync_ber = NULL;
127 struct berval *sync_bvalp = NULL;
129 BerElement *ctrl_ber = NULL;
130 BerElement *res_ber = NULL;
133 LDAPMessage *res = NULL;
134 LDAPMessage *msg = NULL;
138 int nresponses, nreferences, nextended, npartial;
139 int nresponses_psearch;
141 int cancel_msgid = -1;
143 struct berval *retdata = NULL;
145 int sync_info_arrived = 0;
149 struct berval syncUUID = { 0, NULL };
150 struct berval syncCookie = { 0, NULL };
155 int syncinfo_arrived = 0;
156 int cancel_response = 0;
159 AttributeDescription** descs = NULL;
171 struct berval base_bv = { 0, NULL };
172 struct berval pbase = { 0, NULL };
173 struct berval nbase = { 0, NULL };
174 struct berval sub_bv = { 0, NULL };
175 struct berval psubrdn = { 0, NULL };
176 struct berval nsubrdn = { 0, NULL };
177 struct berval psub = { 0, NULL };
178 struct berval nsub = { 0, NULL };
180 Modifications *modlist = NULL;
181 Modifications *ml, *mlnext;
182 char *def_filter_str = NULL;
185 LDAP_LOG ( OPERATION, DETAIL1, "do_syncrepl\n", 0, 0, 0 );
187 Debug( LDAP_DEBUG_TRACE, "=>do_syncrepl\n", 0, 0, 0 );
193 if ( abs(si->type) != LDAP_SYNC_REFRESH_ONLY &&
194 abs(si->type) != LDAP_SYNC_REFRESH_AND_PERSIST ) {
198 /* Init connection to master */
200 if ( ldap_is_ldap_url( si->masteruri )) {
201 rc = ldap_initialize( &ld, si->masteruri );
202 if ( rc != LDAP_SUCCESS ) {
204 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
205 "ldap_initialize failed (%s)\n",
206 si->masteruri, 0, 0 );
208 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
209 "ldap_initialize failed (%s)\n",
210 si->masteruri, 0, 0 );
214 ld = ldap_init( si->mastername, si->masterport );
217 LDAP_LOG( OPERATION, ERR, "do_syncrepl: "
218 "ldap_init failed (%s:%s)\n",
219 si->mastername, si->masterport, 0 );
221 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
222 "ldap_init failed (%s:%s)\n",
223 si->mastername, si->masterport, 0 );
228 op.o_protocol = LDAP_VERSION3;
229 ldap_set_option( ld, LDAP_OPT_PROTOCOL_VERSION, &op.o_protocol );
234 rc = ldap_start_tls_s( ld, NULL, NULL );
235 if( rc != LDAP_SUCCESS ) {
237 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
238 "%s: ldap_start_tls failed (%d)\n",
239 si->tls == TLS_CRITICAL ? "Error" : "Warning",
242 Debug( LDAP_DEBUG_ANY,
243 "%s: ldap_start_tls failed (%d)\n",
244 si->tls == TLS_CRITICAL ? "Error" : "Warning",
247 if( si->tls == TLS_CRITICAL )
252 if ( si->bindmethod == LDAP_AUTH_SASL ) {
253 #ifdef HAVE_CYRUS_SASL
256 if ( si->secprops != NULL ) {
257 int err = ldap_set_option( ld,
258 LDAP_OPT_X_SASL_SECPROPS, si->secprops);
260 if( err != LDAP_OPT_SUCCESS ) {
262 LDAP_LOG ( OPERATION, ERR, "do_bind: Error: "
263 "ldap_set_option(%s,SECPROPS,\"%s\") failed!\n",
264 si->mastername, si->secprops, 0 );
266 Debug( LDAP_DEBUG_ANY, "Error: ldap_set_option "
267 "(%s,SECPROPS,\"%s\") failed!\n",
268 si->mastername, si->secprops, NULL );
274 defaults = lutil_sasl_defaults( ld,
281 rc = ldap_sasl_interactive_bind_s( ld,
289 if ( rc != LDAP_SUCCESS ) {
291 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
292 "ldap_sasl_interactive_bind_s failed (%d)\n",
295 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
296 "ldap_sasl_interactive_bind_s failed (%d)\n",
301 #else /* HAVE_CYRUS_SASL */
302 fprintf( stderr, "not compiled with SASL support\n" );
306 rc = ldap_bind_s( ld, si->binddn, si->passwd, si->bindmethod );
307 if ( rc != LDAP_SUCCESS ) {
309 LDAP_LOG ( OPERATION, ERR, "do_syncrepl: "
310 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
312 Debug( LDAP_DEBUG_ANY, "do_syncrepl: "
313 "ldap_bind_s failed (%d)\n", rc, 0, 0 );
321 op.o_tmpmemctx = NULL; /* FIXME : to use per-thread mem context */
322 op.o_tag = LDAP_REQ_SEARCH;
323 op.o_dn = si->updatedn;
324 op.o_ndn = si->updatedn;
326 op.o_time = slap_get_time();
327 op.o_managedsait = 1;
328 op.o_threadctx = si->ctx;
331 op.o_connid = op.o_conn->c_connid;
332 op.ors_scope = LDAP_SCOPE_BASE;
333 op.ors_deref = LDAP_DEREF_NEVER;
336 op.ors_attrsonly = 0;
338 op.ors_filter = str2filter( def_filter_str = "(objectClass=*)" );
339 ber_str2bv( def_filter_str, strlen( def_filter_str ), 1,
343 conn.c_send_ldap_result = slap_send_ldap_result;
344 conn.c_send_search_entry = slap_send_search_entry;
345 conn.c_send_search_reference = slap_send_search_reference;
347 /* get syncrepl cookie of shadow replica from subentry */
348 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
349 dnPrettyNormal( 0, &base_bv, &pbase, &nbase, op.o_tmpmemctx );
351 sprintf( substr, "cn=syncrepl%d", si->id );
352 ber_str2bv( substr, strlen(substr), 1, &sub_bv );
353 dnPrettyNormal( 0, &sub_bv, &psubrdn, &nsubrdn, op.o_tmpmemctx );
355 build_new_dn( &op.o_req_dn, &pbase, &psubrdn );
356 build_new_dn( &op.o_req_ndn, &nbase, &nsubrdn );
358 ch_free( base_bv.bv_val );
359 ch_free( pbase.bv_val );
360 ch_free( nbase.bv_val );
361 ch_free( sub_bv.bv_val );
362 ch_free( psubrdn.bv_val );
363 ch_free( nsubrdn.bv_val );
365 /* set callback function */
366 cb.sc_response = cookie_callback;
369 /* search subentry to retrieve cookie */
370 si->syncCookie = NULL;
371 be->be_search( &op, &rs );
373 ch_free( op.o_req_dn.bv_val );
374 ch_free( op.o_req_ndn.bv_val );
375 filter_free( op.ors_filter );
376 ch_free( op.ors_filterstr.bv_val );
378 psub = be->be_nsuffix[0];
380 /* setup LDAP SYNC control */
381 sync_ber = ber_alloc_t( LBER_USE_DER );
382 ber_set_option( sync_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
384 if ( si->syncCookie ) {
385 ber_printf( sync_ber, "{eO}", abs(si->type), si->syncCookie );
387 ber_printf( sync_ber, "{e}", abs(si->type) );
390 if ( ber_flatten( sync_ber, &sync_bvalp ) == LBER_ERROR ) {
391 ber_free( sync_ber, 1 );
394 ber_free( sync_ber, 1 );
396 sctrls = (LDAPControl**) sl_calloc( 3, sizeof(LDAPControl*), op.o_tmpmemctx );
398 c[0].ldctl_oid = LDAP_CONTROL_SYNC;
399 c[0].ldctl_value = (*sync_bvalp);
400 c[0].ldctl_iscritical = si->type < 0;
404 c[1].ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
405 c[1].ldctl_value.bv_val = si->authzId;
406 c[1].ldctl_value.bv_len = strlen( si->authzId );
407 c[1].ldctl_iscritical = 1;
415 err = ldap_set_option( ld, LDAP_OPT_SERVER_CONTROLS, sctrls );
417 ber_bvfree( sync_bvalp );
420 if ( err != LDAP_OPT_SUCCESS )
421 fprintf( stderr, "Could not set controls : %d\n", err );
423 /* Delete Attributes */
424 if ( si->lastmod == LASTMOD_REQ ) {
425 descs = del_descs_lastmod;
430 for ( i = 0; descs[i] != NULL; i++ ) {
431 for ( j = 0; si->attrs[j] != NULL; j++ ) {
432 if ( !strcmp( si->attrs[j], descs[i]->ad_cname.bv_val )) {
433 ch_free( si->attrs[j] );
434 for ( k = j; si->attrs[k] != NULL; k++ ) {
435 si->attrs[k] = si->attrs[k+1];
443 for ( n = 0; si->attrs[ n ] != NULL; n++ ) ;
445 if ( si->lastmod == LASTMOD_REQ ) {
446 descs = add_descs_lastmod;
451 for ( i = 0; descs[i] != NULL; i++ ) {
452 tmp = ( char ** ) ch_realloc( si->attrs,
453 ( n + 2 ) * sizeof( char * ));
456 LDAP_LOG( OPERATION, ERR, "out of memory\n", 0,0,0 );
458 Debug( LDAP_DEBUG_ANY, "out of memory\n", 0,0,0 );
462 si->attrs[ n++ ] = ( char * ) strndup( descs[i]->ad_cname.bv_val,
463 descs[i]->ad_cname.bv_len );
464 si->attrs[ n ] = NULL;
467 /* Send LDAP SYNC search */
469 rc = ldap_search_ext( ld, si->base, si->scope, si->filterstr,
470 si->attrs, si->attrsonly, NULL, NULL,
473 if( rc != LDAP_SUCCESS ) {
474 fprintf( stderr, "syncrepl: ldap_search_ext (%d)\n",
475 ldap_err2string( rc ), rc );
479 while (( rc = ldap_result( ld, LDAP_RES_ANY, LDAP_MSG_ONE, NULL, &res )) > 0 ) {
481 for ( msg = ldap_first_message( ld, res );
483 msg = ldap_next_message( ld, msg ) )
485 switch( ldap_msgtype( msg ) ) {
486 case LDAP_RES_SEARCH_ENTRY:
487 entry = syncrepl_message_to_entry( ld, &op, msg,
488 &modlist, &syncstate, &syncUUID, &syncCookie );
489 rc_efree = syncrepl_entry( ld, &op, entry, modlist,
490 syncstate, &syncUUID, &syncCookie, !syncinfo_arrived );
491 if ( syncCookie.bv_len ) {
492 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
496 for ( ml = modlist; ml != NULL; ml = mlnext ) {
497 mlnext = ml->sml_next;
502 case LDAP_RES_SEARCH_REFERENCE:
504 LDAP_LOG( OPERATION, ERR,
505 "do_syncrepl : reference received\n", 0, 0, 0 );
507 Debug( LDAP_DEBUG_ANY,
508 "do_syncrepl : reference received\n", 0, 0, 0 );
512 case LDAP_RES_SEARCH_RESULT:
513 ldap_parse_result( ld, msg, &err, NULL, NULL, NULL, &rctrls, 0 );
516 ctrl_ber = ber_alloc_t( LBER_USE_DER );
517 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op.o_tmpmemctx );
518 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
519 ber_reset( ctrl_ber, 1 );
521 ber_scanf( ctrl_ber, "{" );
522 if ( ber_peek_tag( ctrl_ber, &len )
523 == LDAP_SYNC_TAG_COOKIE ) {
524 ber_scanf( ctrl_ber, "o", &syncCookie );
527 if (si->type == LDAP_SYNC_REFRESH_AND_PERSIST) {
528 if ( cancel_response ) {
529 if ( syncCookie.bv_len ) {
530 ber_bvfree( si->syncCookie );
531 si->syncCookie = ber_dupbv( NULL, &syncCookie );
534 ber_free( ctrl_ber, 1 );
539 ber_free( ctrl_ber, 1 );
543 if ( syncCookie.bv_len ) {
544 syncrepl_updateCookie( ld, &op, &psub, &syncCookie );
546 syncrepl_del_nonpresent( ld, &op );
548 ber_free( ctrl_ber, 1 );
553 case LDAP_RES_INTERMEDIATE_RESP:
554 ldap_parse_intermediate_resp_result( ld, msg,
555 &retoid, &retdata, 0 );
556 if ( !strcmp( retoid, LDAP_SYNC_INFO ) ) {
557 sync_info_arrived = 1;
558 res_ber = ber_init( retdata );
559 ber_scanf( res_ber, "{e", &syncstate );
561 if ( syncstate == LDAP_SYNC_REFRESH_DONE ) {
562 syncrepl_del_nonpresent( ld, &op );
563 } else if ( syncstate != LDAP_SYNC_NEW_COOKIE ) {
565 LDAP_LOG( OPERATION, ERR,
566 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
568 Debug( LDAP_DEBUG_ANY,
569 "do_syncrepl : unknown sync info\n", 0, 0, 0 );
573 if ( ber_peek_tag( res_ber, &len )
574 == LDAP_SYNC_TAG_COOKIE ) {
575 ber_scanf( res_ber, "o}", &syncCookie );
576 if ( syncCookie.bv_len ) {
577 ber_bvfree( si->syncCookie );
578 si->syncCookie = ber_dupbv( NULL, &syncCookie );
581 if ( syncstate == LDAP_SYNC_NEW_COOKIE ) {
583 LDAP_LOG( OPERATION, ERR,
584 "do_syncrepl : cookie required\n", 0, 0, 0 );
586 Debug( LDAP_DEBUG_ANY,
587 "do_syncrepl : cookie required\n", 0, 0, 0 );
592 ldap_memfree( retoid );
593 ber_bvfree( retdata );
594 ber_free( res_ber, 1 );
598 LDAP_LOG( OPERATION, ERR,"do_syncrepl :"
599 " unknown intermediate "
600 "response\n", 0, 0, 0 );
602 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
603 "unknown intermediate "
604 "response\n", 0, 0, 0 );
606 ldap_memfree( retoid );
607 ber_bvfree( retdata );
613 LDAP_LOG( OPERATION, ERR, "do_syncrepl : "
614 "unknown message\n", 0, 0, 0 );
616 Debug( LDAP_DEBUG_ANY, "do_syncrepl : "
617 "unknown message\n", 0, 0, 0 );
628 LDAP_LOG( OPERATION, ERR,
629 "do_syncrepl : unknown result\n", 0, 0, 0 );
631 Debug( LDAP_DEBUG_ANY,
632 "do_syncrepl : unknown result\n", 0, 0, 0 );
638 if ( syncCookie.bv_val )
639 ch_free( syncCookie.bv_val );
640 if ( syncUUID.bv_val )
641 ch_free( syncUUID.bv_val );
650 syncrepl_message_to_entry(
654 Modifications **modlist,
656 struct berval *syncUUID,
657 struct berval *syncCookie
661 BerElement *ber = NULL;
663 struct berval bv = {0, NULL};
666 Modifications **modtail = modlist;
667 Backend *be = op->o_bd;
670 char txtbuf[SLAP_TEXT_BUFLEN];
671 size_t textlen = sizeof txtbuf;
673 struct berval **bvals = NULL;
675 struct berval bdn = {0, NULL};
677 struct berval empty_bv = { 0, NULL };
681 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
685 LDAPControl** rctrls = NULL;
686 BerElement* ctrl_ber;
690 if ( ldap_msgtype( msg ) != LDAP_RES_SEARCH_ENTRY ) {
692 LDAP_LOG( OPERATION, ERR,
693 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
695 Debug( LDAP_DEBUG_ANY,
696 "Message type should be entry (%d)", ldap_msgtype( msg ), 0, 0 );
701 op->o_tag = LDAP_REQ_ADD;
703 rc = ldap_get_dn_ber( ld, msg, &ber, &bdn );
705 if ( rc != LDAP_SUCCESS ) {
707 LDAP_LOG( OPERATION, ERR,
708 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
710 Debug( LDAP_DEBUG_ANY,
711 "syncrepl_message_to_entry : dn get failed (%d)", rc, 0, 0 );
716 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
717 dnPrettyNormal( NULL, &bdn, &e->e_name, &e->e_nname, op->o_tmpmemctx );
721 for ( rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues);
723 rc = ldap_get_attribute_ber( ld, msg, ber, &tmp.sml_type, &tmp.sml_bvalues))
725 if ( tmp.sml_type.bv_val == NULL ) break;
727 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
729 mod->sml_op = LDAP_MOD_REPLACE;
730 mod->sml_next = NULL;
731 mod->sml_desc = NULL;
732 mod->sml_type = tmp.sml_type;
733 mod->sml_bvalues = tmp.sml_bvalues;
734 mod->sml_nvalues = tmp.sml_bvalues;
737 modtail = &mod->sml_next;
740 if ( ber_scanf( ber, "}") == LBER_ERROR ) {
742 LDAP_LOG( OPERATION, ERR,
743 "syncrepl_message_to_entry: ber_scanf failed\n", 0, 0, 0 );
745 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: ber_scanf failed\n",
752 tmpber = ldap_get_message_ber( msg );
753 ber = ber_dup( tmpber );
755 ber_scanf( ber, "{xx" );
757 rc = ldap_int_get_controls( ber, &rctrls );
759 if ( rc != LDAP_SUCCESS ) {
761 LDAP_LOG( OPERATION, ERR,
762 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
764 Debug( LDAP_DEBUG_ANY,
765 "syncrepl_message_to_entry : control get failed (%d)", rc, 0, 0 );
772 ctrl_ber = ber_alloc_t( LBER_USE_DER );
773 ber_set_option( ctrl_ber, LBER_OPT_BER_MEMCTX, op->o_tmpmemctx );
774 ber_write( ctrl_ber, rctrlp->ldctl_value.bv_val, rctrlp->ldctl_value.bv_len, 0 );
775 ber_reset( ctrl_ber, 1 );
776 ber_scanf( ctrl_ber, "{eo", syncstate, syncUUID );
777 if ( ber_peek_tag( ctrl_ber, &len ) == LDAP_SYNC_TAG_COOKIE ) {
778 ber_scanf( ctrl_ber, "o}", syncCookie );
780 ber_free( ctrl_ber, 1 );
783 LDAP_LOG( OPERATION, ERR,"syncrepl_message_to_entry : "
784 " rctrls absent\n", 0, 0, 0 );
786 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry :"
787 " rctrls absent\n", 0, 0, 0 );
791 if ( *syncstate == LDAP_SYNC_PRESENT ) {
796 if ( *modlist == NULL ) {
798 LDAP_LOG( OPERATION, ERR,
799 "syncrepl_message_to_entry: no attributes\n", 0, 0, 0 );
801 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: no attributes\n",
806 rc = slap_mods_check_syncrepl( op, modlist, &text, txtbuf, textlen, NULL );
808 if ( rc != LDAP_SUCCESS ) {
810 LDAP_LOG( OPERATION, ERR,
811 "syncrepl_message_to_entry: mods check (%s)\n", text, 0, 0 );
813 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods check (%s)\n",
819 rc = slap_mods_opattrs_syncrepl( op, *modlist, modtail,
820 &text,txtbuf, textlen );
822 if( rc != LDAP_SUCCESS ) {
824 LDAP_LOG( OPERATION, ERR,
825 "syncrepl_message_to_entry: mods opattrs (%s)\n", text, 0, 0 );
827 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods opattrs (%s)\n",
833 rc = slap_mods2entry_syncrepl( *modlist, &e, 1, &text, txtbuf, textlen );
834 if( rc != LDAP_SUCCESS ) {
836 LDAP_LOG( OPERATION, ERR,
837 "syncrepl_message_to_entry: mods2entry (%s)\n", text, 0, 0 );
839 Debug( LDAP_DEBUG_ANY, "syncrepl_message_to_entry: mods2entry (%s)\n",
852 syncuuid_cmp( const void* v_uuid1, const void* v_uuid2 )
854 const struct berval *uuid1 = v_uuid1;
855 const struct berval *uuid2 = v_uuid2;
856 int rc = uuid1->bv_len - uuid2->bv_len;
858 return ( strcmp( uuid1->bv_val, uuid2->bv_val ) );
866 Modifications* modlist,
868 struct berval* syncUUID,
869 struct berval* syncCookie,
873 Backend *be = op->o_bd;
874 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
876 struct berval csn_bv = {0, NULL};
877 struct berval *syncuuid_bv = NULL;
878 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
880 SlapReply rs = {REP_RESULT};
883 #if 0 /* FIXME : UUID search required first */
885 struct berval filterstr_bv;
892 ( syncstate == LDAP_SYNC_PRESENT || syncstate == LDAP_SYNC_ADD )) {
893 syncuuid_bv = ber_dupbv( NULL, syncUUID );
894 avl_insert( &si->presentlist, (caddr_t) syncuuid_bv,
895 syncuuid_cmp, avl_dup_error );
898 if ( syncstate == LDAP_SYNC_PRESENT ) {
905 if ( !attr_find( e->e_attrs, slap_schema.si_ad_entryUUID )) {
906 attr_merge_one( e, slap_schema.si_ad_entryUUID, syncUUID, syncUUID );
909 #if 0 /* FIXME : UUID search required first */
910 filterstr = (char *) ch_malloc( strlen("entryUUID=") + syncUUID->bv_len + 1 );
911 strcpy( filterstr, "entryUUID=" );
912 strcat( filterstr, syncUUID->bv_val );
916 si->syncUUID = syncUUID;
918 #if 0 /* FIXME : UUID search required first */
919 filter = str2filter( filterstr );
920 ber_str2bv( filterstr, strlen(filterstr), 1, &filterstr_bv );
921 ch_free( filterstr );
924 op->o_req_dn = e->e_name;
925 op->o_req_ndn = e->e_nname;
927 op->o_callback = &cb;
928 cb.sc_response = null_callback;
931 switch ( syncstate ) {
933 case LDAP_SYNC_MODIFY :
935 op->o_tag = LDAP_REQ_MODIFY;
936 op->orm_modlist = modlist;
937 rc = be->be_modify( op, &rs );
938 if ( rc != LDAP_SUCCESS ) {
939 if ( rc == LDAP_REFERRAL ||
940 rc == LDAP_NO_SUCH_OBJECT ||
941 rc == DB_NOTFOUND ) {
942 op->o_tag = LDAP_REQ_ADD;
944 rc = be->be_add( op, &rs );
945 if ( rc != LDAP_SUCCESS ) {
946 if ( rc == LDAP_ALREADY_EXISTS ) {
948 } else if ( rc == LDAP_REFERRAL ||
949 rc == LDAP_NO_SUCH_OBJECT ||
950 rc == DB_NOTFOUND ) {
951 syncrepl_add_glue(ld, op, e,
953 syncUUID, syncCookie);
956 LDAP_LOG( OPERATION, ERR,
957 "be_add failed (%d)\n",
960 Debug( LDAP_DEBUG_ANY,
961 "be_add failed (%d)\n",
970 LDAP_LOG( OPERATION, ERR,
971 "be_modify failed (%d)\n", rc, 0, 0 );
973 Debug( LDAP_DEBUG_ANY,
974 "be_modify failed (%d)\n", rc, 0, 0 );
981 case LDAP_SYNC_DELETE :
982 op->o_tag = LDAP_REQ_DELETE;
983 be->be_delete( op, &rs );
988 LDAP_LOG( OPERATION, ERR,
989 "unknown syncstate\n", 0, 0, 0 );
991 Debug( LDAP_DEBUG_ANY,
992 "unknown syncstate\n", 0, 0, 0 );
999 syncrepl_del_nonpresent(
1004 Backend* be = op->o_bd;
1005 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1007 struct berval base_bv = {0, NULL};
1009 SlapReply rs = {REP_RESULT};
1010 struct berval filterstr_bv = {0, NULL};
1011 struct nonpresent_entry *np_list, *np_prev;
1013 ber_str2bv( si->base, strlen(si->base), 1, &base_bv );
1014 dnPrettyNormal(0, &base_bv, &op->o_req_dn, &op->o_req_ndn, op->o_tmpmemctx );
1015 ch_free( base_bv.bv_val );
1017 filter = str2filter( si->filterstr );
1019 cb.sc_response = nonpresent_callback;
1022 op->o_callback = &cb;
1023 op->o_tag = LDAP_REQ_SEARCH;
1024 op->ors_scope = si->scope;
1025 op->ors_deref = LDAP_DEREF_NEVER;
1026 op->ors_slimit = -1;
1027 op->ors_tlimit = -1;
1028 op->ors_attrsonly = 0;
1029 op->ors_attrs = NULL;
1030 op->ors_filter = filter;
1031 ber_str2bv( si->filterstr, strlen( si->filterstr ), 1, &op->ors_filterstr );
1033 be->be_search( op, &rs );
1035 if ( !LDAP_LIST_EMPTY( &si->nonpresentlist ) ) {
1036 np_list = LDAP_LIST_FIRST( &si->nonpresentlist );
1037 while ( np_list != NULL ) {
1038 LDAP_LIST_REMOVE( np_list, np_link );
1040 np_list = LDAP_LIST_NEXT( np_list, np_link );
1041 op->o_tag = LDAP_REQ_DELETE;
1042 op->o_callback = &cb;
1043 cb.sc_response = null_callback;
1045 op->o_req_dn = *np_prev->dn;
1046 op->o_req_ndn = *np_prev->ndn;
1047 op->o_bd->be_delete( op, &rs );
1048 ber_bvfree( np_prev->dn );
1049 ber_bvfree( np_prev->ndn );
1050 op->o_req_dn.bv_val = NULL;
1051 op->o_req_ndn.bv_val = NULL;
1056 if ( op->o_req_dn.bv_val )
1057 ch_free( op->o_req_dn.bv_val );
1058 if ( op->o_req_ndn.bv_val )
1059 ch_free( op->o_req_ndn.bv_val );
1060 filter_free( op->ors_filter );
1061 ch_free( op->ors_filterstr.bv_val );
1070 Modifications* modlist,
1072 struct berval* syncUUID,
1073 struct berval* syncCookie
1076 Backend *be = op->o_bd;
1077 syncinfo_t *si = op->o_callback->sc_private;
1078 struct berval uuid_bv = {0, NULL};
1082 char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
1085 struct berval dn = {0, NULL};
1086 struct berval pdn = {0, NULL};
1087 struct berval ndn = {0, NULL};
1088 struct berval rdn = {0, NULL};
1090 SlapReply rs = {REP_RESULT};
1091 Connection *conn = op->o_conn;
1093 op->o_tag = LDAP_REQ_ADD;
1094 op->o_callback = &cb;
1095 cb.sc_response = null_callback;
1098 ber_dupbv( &dn, &e->e_nname );
1099 ber_dupbv( &pdn, &e->e_nname );
1101 while ( !be_issuffix ( be, &pdn )) {
1102 dnParent( &dn, &pdn );
1103 ch_free( dn.bv_val );
1104 ber_dupbv( &dn, &pdn );
1108 for ( i = 0; i <= levels; i++ ) {
1109 glue = (Entry*) ch_calloc( 1, sizeof(Entry) );
1110 ch_free( dn.bv_val );
1111 ch_free( pdn.bv_val );
1112 ber_dupbv( &dn, &e->e_nname );
1113 ber_dupbv( &pdn, &e->e_nname );
1115 for ( k = 0; k < j; k++ ) {
1116 dnParent( &dn, &pdn );
1117 ch_free( dn.bv_val );
1118 ber_dupbv( &dn, &pdn );
1121 dnPrettyNormal( 0, &dn, &pdn, &ndn, op->o_tmpmemctx );
1122 ber_dupbv( &glue->e_name, &pdn );
1123 ber_dupbv( &glue->e_nname, &ndn );
1124 ch_free( dn.bv_val );
1125 ch_free( pdn.bv_val );
1126 ch_free( ndn.bv_val );
1128 a = ch_calloc( 1, sizeof( Attribute ));
1129 a->a_desc = slap_schema.si_ad_objectClass;
1130 a->a_vals = ch_calloc( 3, sizeof( struct berval ));
1131 ber_str2bv( "top", strlen("top"), 1, &a->a_vals[0] );
1132 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[1] );
1133 a->a_vals[2].bv_len = 0;
1134 a->a_vals[2].bv_val = NULL;
1135 a->a_next = glue->e_attrs;
1138 a = ch_calloc( 1, sizeof( Attribute ));
1139 a->a_desc = slap_schema.si_ad_structuralObjectClass;
1140 a->a_vals = ch_calloc( 2, sizeof( struct berval ));
1141 ber_str2bv( "glue", strlen("glue"), 1, &a->a_vals[0] );
1142 a->a_vals[1].bv_len = 0;
1143 a->a_vals[1].bv_val = NULL;
1144 a->a_next = glue->e_attrs;
1147 if ( !strcmp( e->e_nname.bv_val, glue->e_nname.bv_val )) {
1148 op->o_req_dn = e->e_name;
1149 op->o_req_ndn = e->e_nname;
1151 rc = be->be_add ( op, &rs );
1152 if ( rc == LDAP_SUCCESS )
1153 be_entry_release_w( op, e );
1158 op->o_req_dn = glue->e_name;
1159 op->o_req_ndn = glue->e_nname;
1161 rc = be->be_add ( op, &rs );
1162 if ( rc == LDAP_SUCCESS ) {
1163 be_entry_release_w( op, glue );
1165 /* incl. ALREADY EXIST */
1175 syncrepl_updateCookie(
1179 struct berval *syncCookie
1182 Backend *be = op->o_bd;
1183 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1185 Modifications *mlnext;
1187 Modifications *modlist;
1188 Modifications **modtail = &modlist;
1190 struct berval* ocbva = NULL;
1191 struct berval* cnbva = NULL;
1192 struct berval* ssbva = NULL;
1193 struct berval* scbva = NULL;
1198 char txtbuf[SLAP_TEXT_BUFLEN];
1199 size_t textlen = sizeof txtbuf;
1204 struct berval sub_bv = { 0, NULL };
1205 struct berval psubrdn = { 0, NULL };
1208 SlapReply rs = {REP_RESULT};
1210 ocbva = ( struct berval * ) ch_calloc( 4, sizeof( struct berval ));
1211 cnbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1212 ssbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1213 scbva = ( struct berval * ) ch_calloc( 2, sizeof( struct berval ));
1215 /* update in memory cookie */
1216 if ( si->syncCookie != NULL ) {
1217 ber_bvfree( si->syncCookie );
1219 si->syncCookie = ber_dupbv( NULL, syncCookie );
1220 ber_str2bv( "top", strlen("top"), 1, &ocbva[0] );
1221 ber_str2bv( "subentry", strlen("subentry"), 1, &ocbva[1] );
1222 ber_str2bv( "syncConsumerSubentry",
1223 strlen("syncConsumerSubentry"), 1, &ocbva[2] );
1224 ocbva[3].bv_len = 0;
1225 ocbva[3].bv_val = NULL;
1227 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1228 mod->sml_op = LDAP_MOD_REPLACE;
1229 mod->sml_next = NULL;
1230 mod->sml_desc = NULL;
1231 ber_str2bv( "objectClass", strlen("objectClass"), 1, &mod->sml_type );
1232 mod->sml_bvalues = ocbva;
1233 mod->sml_nvalues = ocbva;
1235 modtail = &mod->sml_next;
1237 sprintf( substr, "syncrepl%d", si->id );
1238 sprintf( rdnstr, "cn=%s", substr );
1239 ber_str2bv( substr, strlen( substr ), 1, &cnbva[0] );
1240 ber_str2bv( rdnstr, strlen( rdnstr ), 1, &psubrdn );
1241 cnbva[1].bv_len = 0;
1242 cnbva[1].bv_val = NULL;
1243 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1244 mod->sml_op = LDAP_MOD_REPLACE;
1245 mod->sml_next = NULL;
1246 mod->sml_desc = NULL;
1247 ber_str2bv( "cn", strlen("cn"), 1, &mod->sml_type );
1248 mod->sml_bvalues = cnbva;
1249 mod->sml_nvalues = cnbva;
1251 modtail = &mod->sml_next;
1253 ber_dupbv( &scbva[0], si->syncCookie );
1254 scbva[1].bv_len = 0;
1255 scbva[1].bv_val = NULL;
1256 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1257 mod->sml_op = LDAP_MOD_REPLACE;
1258 mod->sml_next = NULL;
1259 mod->sml_desc = NULL;
1260 ber_str2bv( "syncreplCookie", strlen("syncreplCookie"),
1261 1, &mod->sml_type );
1262 mod->sml_bvalues = scbva;
1263 mod->sml_nvalues = scbva;
1265 modtail = &mod->sml_next;
1267 ber_str2bv( "{}", strlen("{}"), 1, &ssbva[0] );
1268 ssbva[1].bv_len = 0;
1269 ssbva[1].bv_val = NULL;
1270 mod = (Modifications *) ch_malloc( sizeof( Modifications ));
1271 mod->sml_op = LDAP_MOD_REPLACE;
1272 mod->sml_next = NULL;
1273 mod->sml_desc = NULL;
1274 ber_str2bv( "subtreeSpecification",
1275 strlen("subtreeSpecification"), 1, &mod->sml_type );
1276 mod->sml_bvalues = ssbva;
1277 mod->sml_nvalues = ssbva;
1279 modtail = &mod->sml_next;
1281 rc = slap_mods_check_syncrepl( op, &modlist, &text, txtbuf, textlen, NULL );
1283 if ( rc != LDAP_SUCCESS ) {
1285 LDAP_LOG( OPERATION, ERR,
1286 "syncrepl_updateCookie: mods check (%s)\n", text, 0, 0 );
1288 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods check (%s)\n",
1293 op->o_tag = LDAP_REQ_ADD;
1294 rc = slap_mods_opattrs_syncrepl( op, modlist, modtail, &text,txtbuf, textlen );
1296 if( rc != LDAP_SUCCESS ) {
1298 LDAP_LOG( OPERATION, ERR,
1299 "syncrepl_updateCookie: mods opattrs (%s)\n", text, 0, 0 );
1301 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods opattrs (%s)\n",
1306 e = ( Entry * ) ch_calloc( 1, sizeof( Entry ));
1308 build_new_dn( &sub_bv, pdn, &psubrdn );
1309 dnPrettyNormal( NULL, &sub_bv, &e->e_name, &e->e_nname, op->o_tmpmemctx );
1310 ch_free( sub_bv.bv_val );
1311 ch_free( psubrdn.bv_val );
1315 rc = slap_mods2entry_syncrepl( modlist, &e, 1, &text, txtbuf, textlen );
1317 if( rc != LDAP_SUCCESS ) {
1319 LDAP_LOG( OPERATION, ERR,
1320 "syncrepl_updateCookie: mods2entry (%s)\n", text, 0, 0 );
1322 Debug( LDAP_DEBUG_ANY, "syncrepl_updateCookie: mods2entry (%s)\n",
1327 cb.sc_response = null_callback;
1330 op->o_callback = &cb;
1331 op->o_req_dn = e->e_name;
1332 op->o_req_ndn = e->e_nname;
1334 /* update persistent cookie */
1335 update_cookie_retry:
1336 op->o_tag = LDAP_REQ_MODIFY;
1337 op->orm_modlist = modlist;
1338 rc = be->be_modify( op, &rs );
1339 if ( rc != LDAP_SUCCESS ) {
1340 if ( rc == LDAP_REFERRAL ||
1341 rc == LDAP_NO_SUCH_OBJECT ||
1342 rc == DB_NOTFOUND ) {
1343 op->o_tag = LDAP_REQ_ADD;
1345 rc = be->be_add( op, &rs );
1346 if ( rc != LDAP_SUCCESS ) {
1347 if ( rc == LDAP_ALREADY_EXISTS ) {
1348 goto update_cookie_retry;
1349 } else if ( rc == LDAP_REFERRAL ||
1350 rc == LDAP_NO_SUCH_OBJECT ||
1351 rc == DB_NOTFOUND ) {
1353 LDAP_LOG( OPERATION, ERR,
1354 "cookie will be non-persistent\n",
1357 Debug( LDAP_DEBUG_ANY,
1358 "cookie will be non-persistent\n",
1363 LDAP_LOG( OPERATION, ERR,
1364 "be_add failed (%d)\n",
1367 Debug( LDAP_DEBUG_ANY,
1368 "be_add failed (%d)\n",
1377 LDAP_LOG( OPERATION, ERR,
1378 "be_modify failed (%d)\n", rc, 0, 0 );
1380 Debug( LDAP_DEBUG_ANY,
1381 "be_modify failed (%d)\n", rc, 0, 0 );
1391 for ( ml = modlist; ml != NULL; ml = mlnext ) {
1392 mlnext = ml->sml_next;
1401 int slap_mods_check_syncrepl(
1403 Modifications **mlp,
1410 Backend *be = op->o_bd;
1411 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1412 AttributeDescription** descs;
1414 Modifications *prevml = NULL;
1415 Modifications *nextml = NULL;
1416 Modifications *ml = *mlp;
1418 while ( ml != NULL ) {
1419 AttributeDescription *ad = NULL;
1421 /* convert to attribute description */
1422 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
1424 if( rc != LDAP_SUCCESS ) {
1425 snprintf( textbuf, textlen, "%s: %s",
1426 ml->sml_type.bv_val, *text );
1433 if ( si->lastmod == LASTMOD_REQ ) {
1434 descs = del_descs_lastmod;
1439 for ( i = 0; descs[i] != NULL; i++ ) {
1440 if ( ad == descs[i] ) {
1441 if ( prevml == NULL ) {
1442 mlp = &ml->sml_next;
1445 prevml->sml_next = ml->sml_next;
1447 slap_mod_free( &ml->sml_mod, 0 );
1448 nextml = ml->sml_next;
1455 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
1456 && !slap_ad_is_binary( ad )) {
1457 /* attribute requires binary transfer */
1458 snprintf( textbuf, textlen,
1459 "%s: requires ;binary transfer",
1460 ml->sml_type.bv_val );
1462 return LDAP_UNDEFINED_TYPE;
1465 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
1466 && slap_ad_is_binary( ad )) {
1467 /* attribute requires binary transfer */
1468 snprintf( textbuf, textlen,
1469 "%s: disallows ;binary transfer",
1470 ml->sml_type.bv_val );
1472 return LDAP_UNDEFINED_TYPE;
1475 if( slap_ad_is_tag_range( ad )) {
1476 /* attribute requires binary transfer */
1477 snprintf( textbuf, textlen,
1478 "%s: inappropriate use of tag range option",
1479 ml->sml_type.bv_val );
1481 return LDAP_UNDEFINED_TYPE;
1484 if ( is_at_obsolete( ad->ad_type ) &&
1485 ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) ) {
1487 * attribute is obsolete,
1488 * only allow replace/delete with no values
1490 snprintf( textbuf, textlen,
1491 "%s: attribute is obsolete",
1492 ml->sml_type.bv_val );
1494 return LDAP_CONSTRAINT_VIOLATION;
1500 if( ml->sml_values != NULL ) {
1502 slap_syntax_validate_func *validate =
1503 ad->ad_type->sat_syntax->ssyn_validate;
1504 slap_syntax_transform_func *pretty =
1505 ad->ad_type->sat_syntax->ssyn_pretty;
1507 if( !pretty && !validate ) {
1508 *text = "no validator for syntax";
1509 snprintf( textbuf, textlen,
1510 "%s: no validator for syntax %s",
1511 ml->sml_type.bv_val,
1512 ad->ad_type->sat_syntax->ssyn_oid );
1514 return LDAP_INVALID_SYNTAX;
1518 * check that each value is valid per syntax
1519 * and pretty if appropriate
1521 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1522 struct berval pval = {0, NULL};
1524 rc = pretty( ad->ad_type->sat_syntax,
1525 &ml->sml_values[nvals], &pval, ctx );
1527 rc = validate( ad->ad_type->sat_syntax,
1528 &ml->sml_values[nvals] );
1532 snprintf( textbuf, textlen,
1533 "%s: value #%ld invalid per syntax",
1534 ml->sml_type.bv_val, (long) nvals );
1536 return LDAP_INVALID_SYNTAX;
1540 ber_memfree( ml->sml_values[nvals].bv_val );
1541 ml->sml_values[nvals] = pval;
1546 * a rough single value check... an additional check is needed
1547 * to catch add of single value to existing single valued attribute
1549 if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
1550 && nvals > 1 && is_at_single_value( ad->ad_type )) {
1551 snprintf( textbuf, textlen,
1552 "%s: multiple values provided",
1553 ml->sml_type.bv_val );
1555 return LDAP_CONSTRAINT_VIOLATION;
1558 if( nvals && ad->ad_type->sat_equality &&
1559 ad->ad_type->sat_equality->smr_normalize ) {
1560 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
1561 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
1562 rc = ad->ad_type->sat_equality->smr_normalize( 0,
1563 ad->ad_type->sat_syntax, ad->ad_type->sat_equality,
1564 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
1567 LDAP_LOG( OPERATION, DETAIL1,
1568 "str2entry: NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1570 Debug( LDAP_DEBUG_ANY,
1571 "<= str2entry NULL (ssyn_normalize %d)\n", rc, 0, 0 );
1573 snprintf( textbuf, textlen,
1574 "%s: value #%ld normalization failed",
1575 ml->sml_type.bv_val, (long) nvals );
1580 ml->sml_nvalues[nvals].bv_val = NULL;
1581 ml->sml_nvalues[nvals].bv_len = 0;
1588 return LDAP_SUCCESS;
1592 int slap_mods_opattrs_syncrepl(
1594 Modifications *mods,
1595 Modifications **modtail,
1597 char *textbuf, size_t textlen )
1599 struct berval name = {0, NULL};
1600 struct berval timestamp = {0, NULL};
1601 struct berval csn = {0, NULL};
1602 struct berval nname = {0, NULL};
1603 char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
1604 char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
1606 Backend *be = op->o_bd;
1607 syncinfo_t *si = ( syncinfo_t * ) be->syncinfo;
1609 int mop = LDAP_MOD_REPLACE;
1611 assert( modtail != NULL );
1612 assert( *modtail == NULL );
1614 if( si->lastmod == LASTMOD_GEN ) {
1616 time_t now = slap_get_time();
1618 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
1619 ltm = gmtime( &now );
1620 lutil_gentime( timebuf, sizeof(timebuf), ltm );
1622 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
1623 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
1624 csn.bv_val = csnbuf;
1626 timestamp.bv_val = timebuf;
1627 timestamp.bv_len = strlen(timebuf);
1629 if( op->o_dn.bv_len == 0 ) {
1630 name.bv_val = SLAPD_ANONYMOUS;
1631 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
1639 if( op->o_tag == LDAP_REQ_ADD ) {
1640 struct berval tmpval = {0, NULL};
1642 if( global_schemacheck ) {
1643 int rc = mods_structural_class( mods, &tmpval,
1644 text, textbuf, textlen );
1645 if( rc != LDAP_SUCCESS ) {
1649 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1651 mod->sml_type.bv_val = NULL;
1652 mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
1653 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1654 ber_dupbv( &mod->sml_values[0], &tmpval );
1655 mod->sml_values[1].bv_len = 0;
1656 mod->sml_values[1].bv_val = NULL;
1657 assert( mod->sml_values[0].bv_val );
1658 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1659 ber_dupbv( &mod->sml_nvalues[0], &tmpval );
1660 mod->sml_nvalues[1].bv_len = 0;
1661 mod->sml_nvalues[1].bv_val = NULL;
1662 assert( mod->sml_nvalues[0].bv_val );
1664 modtail = &mod->sml_next;
1667 if( si->lastmod == LASTMOD_GEN ) {
1668 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1670 mod->sml_type.bv_val = NULL;
1671 mod->sml_desc = slap_schema.si_ad_creatorsName;
1672 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1673 ber_dupbv( &mod->sml_values[0], &name );
1674 mod->sml_values[1].bv_len = 0;
1675 mod->sml_values[1].bv_val = NULL;
1676 assert( mod->sml_values[0].bv_val );
1677 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1678 ber_dupbv( &mod->sml_nvalues[0], &nname );
1679 mod->sml_nvalues[1].bv_len = 0;
1680 mod->sml_nvalues[1].bv_val = NULL;
1681 assert( mod->sml_nvalues[0].bv_val );
1683 modtail = &mod->sml_next;
1685 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1687 mod->sml_type.bv_val = NULL;
1688 mod->sml_desc = slap_schema.si_ad_createTimestamp;
1689 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1690 ber_dupbv( &mod->sml_values[0], ×tamp );
1691 mod->sml_values[1].bv_len = 0;
1692 mod->sml_values[1].bv_val = NULL;
1693 assert( mod->sml_values[0].bv_val );
1694 mod->sml_nvalues = NULL;
1696 modtail = &mod->sml_next;
1700 if( si->lastmod == LASTMOD_GEN ) {
1701 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1703 mod->sml_type.bv_val = NULL;
1704 mod->sml_desc = slap_schema.si_ad_entryCSN;
1705 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1706 ber_dupbv( &mod->sml_values[0], &csn );
1707 mod->sml_values[1].bv_len = 0;
1708 mod->sml_values[1].bv_val = NULL;
1709 assert( mod->sml_values[0].bv_val );
1710 mod->sml_nvalues = NULL;
1712 modtail = &mod->sml_next;
1714 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1716 mod->sml_type.bv_val = NULL;
1717 mod->sml_desc = slap_schema.si_ad_modifiersName;
1718 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1719 ber_dupbv( &mod->sml_values[0], &name );
1720 mod->sml_values[1].bv_len = 0;
1721 mod->sml_values[1].bv_val = NULL;
1722 assert( mod->sml_values[0].bv_val );
1723 mod->sml_nvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1724 ber_dupbv( &mod->sml_nvalues[0], &nname );
1725 mod->sml_nvalues[1].bv_len = 0;
1726 mod->sml_nvalues[1].bv_val = NULL;
1727 assert( mod->sml_nvalues[0].bv_val );
1729 modtail = &mod->sml_next;
1731 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1733 mod->sml_type.bv_val = NULL;
1734 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1735 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1736 ber_dupbv( &mod->sml_values[0], ×tamp );
1737 mod->sml_values[1].bv_len = 0;
1738 mod->sml_values[1].bv_val = NULL;
1739 assert( mod->sml_values[0].bv_val );
1740 mod->sml_nvalues = NULL;
1742 modtail = &mod->sml_next;
1746 return LDAP_SUCCESS;
1751 int slap_mods2entry_syncrepl(
1752 Modifications *mods,
1756 char *textbuf, size_t textlen )
1758 Attribute **tail = &(*e)->e_attrs;
1759 assert( *tail == NULL );
1763 for( ; mods != NULL; mods = mods->sml_next ) {
1766 assert( mods->sml_desc != NULL );
1768 attr = attr_find( (*e)->e_attrs, mods->sml_desc );
1770 if( attr != NULL ) {
1771 #define SLURPD_FRIENDLY
1772 #ifdef SLURPD_FRIENDLY
1776 snprintf( textbuf, textlen,
1777 "attribute '%s' provided more than once",
1778 mods->sml_desc->ad_cname.bv_val );
1779 return LDAP_TYPE_OR_VALUE_EXISTS;
1782 for( i=0; attr->a_vals[i].bv_val; i++ ) {
1785 for( j=0; mods->sml_values[j].bv_val; j++ ) {
1790 attr->a_vals = ch_realloc( attr->a_vals,
1791 sizeof( struct berval ) * (i+j) );
1793 /* should check for duplicates */
1795 AC_MEMCPY( &attr->a_vals[i], mods->sml_values,
1796 sizeof( struct berval ) * j );
1798 if( attr->a_nvals ) {
1799 attr->a_nvals = ch_realloc( attr->a_nvals,
1800 sizeof( struct berval ) * (i+j) );
1802 AC_MEMCPY( &attr->a_nvals[i], mods->sml_nvalues,
1803 sizeof( struct berval ) * j );
1805 /* trim the mods array */
1806 ch_free( mods->sml_nvalues );
1807 mods->sml_nvalues = NULL;
1812 snprintf( textbuf, textlen,
1813 "attribute '%s' provided more than once",
1814 mods->sml_desc->ad_cname.bv_val );
1815 return LDAP_TYPE_OR_VALUE_EXISTS;
1819 if( mods->sml_values[1].bv_val != NULL ) {
1820 /* check for duplicates */
1822 MatchingRule *mr = mods->sml_desc->ad_type->sat_equality;
1824 /* check if the values we're adding already exist */
1825 if( mr == NULL || !mr->smr_match ) {
1826 for ( i = 0; mods->sml_bvalues[i].bv_val != NULL; i++ ) {
1827 /* test asserted values against themselves */
1828 for( j = 0; j < i; j++ ) {
1829 if ( bvmatch( &mods->sml_bvalues[i],
1830 &mods->sml_bvalues[j] ) ) {
1831 /* value exists already */
1832 snprintf( textbuf, textlen,
1833 "%s: value #%d provided more than once",
1834 mods->sml_desc->ad_cname.bv_val, j );
1835 return LDAP_TYPE_OR_VALUE_EXISTS;
1842 const char *text = NULL;
1843 char textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
1845 rc = modify_check_duplicates( mods->sml_desc, mr,
1846 NULL, mods->sml_bvalues, 0,
1847 &text, textbuf, sizeof( textbuf ) );
1849 if ( rc != LDAP_SUCCESS ) {
1855 attr = ch_calloc( 1, sizeof(Attribute) );
1857 /* move ad to attr structure */
1858 attr->a_desc = mods->sml_desc;
1860 /* move values to attr structure */
1861 /* should check for duplicates */
1862 attr->a_vals = mods->sml_values;
1864 attr->a_nvals = mods->sml_nvalues;
1867 tail = &attr->a_next;
1870 return LDAP_SUCCESS;
1874 avl_ber_bvfree( void *bv )
1879 if ( ((struct berval *)bv)->bv_val != NULL ) {
1880 ber_memfree ( ((struct berval *)bv)->bv_val );
1882 ber_memfree ( (char *) bv );
1891 syncinfo_t *si = op->o_callback->sc_private;
1894 if ( rs->sr_type != REP_SEARCH ) return LDAP_SUCCESS;
1896 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_syncreplCookie );
1899 si->syncCookie = NULL;
1901 si->syncCookie = ber_dupbv( NULL, &a->a_vals[0] );
1903 return LDAP_SUCCESS;
1907 nonpresent_callback(
1912 syncinfo_t *si = op->o_callback->sc_private;
1915 struct berval* present_uuid = NULL;
1917 SlapReply rs_cb = {REP_RESULT};
1918 struct nonpresent_entry *np_entry;
1920 if ( rs->sr_type == REP_RESULT ) {
1921 count = avl_free( si->presentlist, avl_ber_bvfree );
1922 return LDAP_SUCCESS;
1923 } else if ( rs->sr_type == REP_SEARCH ) {
1924 a = attr_find( rs->sr_entry->e_attrs, slap_schema.si_ad_entryUUID );
1929 present_uuid = avl_find( si->presentlist, &a->a_vals[0], syncuuid_cmp );
1931 if ( present_uuid == NULL ) {
1932 np_entry = (struct nonpresent_entry *)
1933 ch_calloc( 1, sizeof( struct nonpresent_entry ));
1934 np_entry->dn = ber_dupbv( NULL, &rs->sr_entry->e_name );
1935 np_entry->ndn = ber_dupbv( NULL, &rs->sr_entry->e_nname );
1936 LDAP_LIST_INSERT_HEAD( &si->nonpresentlist, np_entry, np_link );
1938 avl_delete( &si->presentlist,
1939 &a->a_vals[0], syncuuid_cmp );
1941 return LDAP_SUCCESS;
1943 return LDAP_SUCCESS;
1954 if ( rs->sr_err != LDAP_SUCCESS &&
1955 rs->sr_err != LDAP_REFERRAL &&
1956 rs->sr_err != LDAP_ALREADY_EXISTS &&
1957 rs->sr_err != LDAP_NO_SUCH_OBJECT &&
1958 rs->sr_err != DB_NOTFOUND ) {
1960 LDAP_LOG( OPERATION, ERR,
1961 "null_callback : error code 0x%x\n",
1964 Debug( LDAP_DEBUG_ANY,
1965 "null_callback : error code 0x%x\n",
1969 return LDAP_SUCCESS;